
// Explorer module 'kie' --------------------------------------------------------------------

function kie_module(){

	/* -------------------------------- variables ----------------------------------- */

	var windowX;
	var windowY;
    var gallerynumber = -1;
	
	/* -------------------------------- getElementById function --------------------- */
	
	function $(id){
		return document.getElementById(id);
	}

	/* -------------------------------- constructor/descturctor --------------------- */

	this.constr = function(params){
        gallerynumber = params[0];    
		var node;
		if(!$("kie_maindiv")){
			node = document.createElement("div");
			node.id = "kie_maindiv";
            node.style.background = "#EEE8AA";
			node.style.position = "fixed";
			node.style.left = "0px";
			node.style.top = "0px";
			node.style.zIndex = 1000;
			node.style.overflowX = "hidden";
			node.style.overflowY = "auto";
			node.style.display = "none";
			document.body.appendChild(node);
		}
		if(!$("kie_tb")){
            			var tbcontent = "<img src='ki_base/ki_nav_close.png' style='cursor:pointer; border:0px; margin:0px 2px 0px 2px; padding-top:2px; display:inline;' onclick=\"kie.closeGallery();\" onmouseover=\"this.style.padding = '0px 0px 2px 0px';\" onmouseout=\"this.style.padding = '2px 0px 0px 0px';\" title='Close' />";
			node = document.createElement("div");
			node.id = "kie_tb";
			node.style.position = "fixed";
			node.style.top = "-20px";
            node.style.left = "50%";
            node.style.marginLeft = "-17px";
            node.style.background = "#ffffff";
                        node.style.border = "2px solid #000000";                        
			node.style.zIndex = 10000;
            node.style.padding = "20px 3px 3px 3px";
            node.style.lineHeight = "12px";
			node.innerHTML = tbcontent;
			document.body.appendChild(node);
		}
        if(!$("kie_wait")){
        node = document.createElement("div");
			node.id = "kie_wait";
            node.style.background = "#ffffff";
			node.style.position = "fixed";
			node.style.left = "50%";
			node.style.top = "50%";
			node.style.zIndex = 10000;
            node.style.width = "200px";
            node.style.height = "40px";
            node.style.marginLeft = "-106px";
            node.style.marginTop = "-26px";
            node.style.border = "2px solid #000000";
            node.style.color = "#000000";
            node.style.font = "14px normal Tahoma, sans-serif";
            node.style.padding = "8px 4px 0px 4px";
            node.style.textAlign = "center";
            node.style.lineHeight = "17px";
            node.innerHTML = "Please wait, while folder being scanned ...";
			document.body.appendChild(node);
        }
		window.addEventListener('resize', viewdim, false);
		viewdim();
		document.addEventListener('keypress', taste, false);
        openGallery();
	}
    
	this.destr = function(){
		window.removeEventListener('resize', viewdim, false);
		document.body.removeChild($("kie_maindiv"));
		document.body.removeChild($("kie_tb"));
        document.body.removeChild($("kie_wait"));
		document.removeEventListener('keypress', taste, false);

	}
	
	/* -------------------------------- methodes ------------------------------------ */

	function viewdim(){
		if(window.innerHeight){
        	windowX = window.innerWidth;
			windowY = window.innerHeight;
        } else if(document.documentElement && document.documentElement.clientHeight){ // Explorer 6 Strict Mode
			windowX = document.documentElement.clientWidth;
			windowY = document.documentElement.clientHeight;
		} else if (document.body){ // other Explorers
			windowX = document.body.clientWidth;
        	windowY = document.body.clientHeight;
		}
        var maindiv = $("kie_maindiv");
        maindiv.style.width = windowX + "px";
        maindiv.style.height = windowY + "px";
       	var container = $("kie_maindiv").childNodes;
        var laenge = container.length - 1;
		if(laenge != -1){
            for(var i = 0; i < laenge; i++){
                var temp = container[i].alt.split("_");
                container[i].alt = temp[0] + "_" + temp[1];
            }
            placeimages();
		}
	}

	function openGallery(){
		fw.shadebody(1, Array( function(){
			$("kie_tb").style.display = "block";
			var params = "?reldir=&gallery=" + kib.dirs[gallerynumber] + "&gallerynumber=" + gallerynumber;
			fw.getHTTP("ki_base/ki_explorer.php" + params, kie.gotGallery);
		} ));
	}
	
	this.gotGallery = function(responseText){
    	$("kie_wait").style.display = "none";
		var maindiv = $("kie_maindiv");
		maindiv.innerHTML = responseText;
		maindiv.style.display = "block";
        placeimages();
	}
    
    function placeimages(){
var th_width = 82;var th_height = 82;    	var prozeile = Math.floor(windowX/(th_width+60));
        var offsetx = (windowX - (prozeile*(th_width+60)))*0.5;
        var spaltenbreite = (windowX - 2*offsetx)/prozeile;
        var zeilenhoehe = th_height+60;
        var offsety = 25;

        var zeile = 1;
        var spalte = 1;
        
        var zaehler = 0;
    	var container = $("kie_maindiv").childNodes;
        var act_width;
        var act_height;
        for(var i = 0; i < container.length - 1; i++){
                        if(th_width < th_height){
				act_width = th_width;
			} else {
				act_width = th_height;
			}
			act_height = act_width;
                        xpos = Math.round(offsetx + spaltenbreite*(spalte-0.5) - 0.5*act_width - 5);
            ypos = Math.round(offsety + zeilenhoehe*(zeile-0.5) - 0.5*act_height - 5);
            
            var bild = container[i];            
            bild.style.left = xpos + "px";
            bild.style.top = ypos + "px";
            bild.alt = bild.alt + "_"+xpos+"_"+ypos;
			zaehler++;
			spalte++;
            if(zaehler >= prozeile){
            	zaehler = 0;
                zeile++;
                spalte = 1;
            }
        }
		$("kie_maindiv").lastChild.style.top = ypos + (0.5*act_height) + "px";
        $("kie_maindiv").lastChild.style.height = 0.5*th_height + 60 + "px";
    }

	this.getImage = function(picstring) {
        $("kie_maindiv").style.zIndex = 99;
        $("kie_tb").style.display = "none";
		kib.getImage(picstring);
	}
	
	this.closeGallery = function(){
        var obj = $("kie_maindiv").childNodes;
        for(var i = 0; i < obj.length - 1; i++){
        	obj[i].src = "";
        }
		fw.removejs("kie");
		fw.shadebody(0);	
	}
	
	function preventDefaultAction(aEvent) {	
		        aEvent.stopPropagation(); 
        				aEvent.preventDefault();
					}
	
	function taste(aEvent) {
		aEvent = aEvent ? aEvent : window.event;
        var keyCode = aEvent.keyCode;
		if(keyCode == 27){
        	if(typeof(kiv_module) == "function")return;
			kie.closeGallery();
			preventDefaultAction(aEvent);
			return false;
		}
	}

}