
// Basic module 'kib' --------------------------------------------------------------------

function kib_module(){

	/* -------------------------------- variables ----------------------------------- */

	var galleries;						// gallery objects
	this.dirs; 							// gallery folders
	this.pics;							// pic names and dimensions
    this.customsettings = new Array();	// individual width, heights and backgrounds
    var mouseover;						// Array for mouseover
    this.lastquery;						// needed to prevent ...
    
this.customsettings['default'] = Array(900, 300, "#666666", "#ffffff", "#bbbbbb", 1.5, 1, 1, 2, 1, 0, 1, "#ffffff", "#000000", "right", 1, 1);

	/* -------------------------------- getElementById function --------------------- */
			
	function $(id){
		return document.getElementById(id);
	}

	/* -------------------------------- constructor/descturctor --------------------- */

	this.constr = function() {
		galleries = new Array();
        this.dirs = new Array();
		mouseover = new Array();      
		var temp = document.getElementsByTagName("div");
		var found = 0;
		for(var i = 0; i < temp.length; i++){
			if(temp[i].className.indexOf("koschtitgallery") >= 0){
				temp[i].id = temp[i].title + "_" + found;
                temp[i].style.position = "relative";
                temp[i].style.padding = "0px";
                temp[i].style.overflow = "hidden";
                var obj = kib.customsettings[temp[i].title] ? kib.customsettings[temp[i].title] : kib.customsettings['default'];
                temp[i].style.width = obj[0] + "px";
                temp[i].style.height = obj[1] + "px";
                temp[i].style.minHeight = "0";
                temp[i].style.background = obj[2];
				this.dirs.push(temp[i].title);
				temp[i].title = "";
				galleries.push(temp[i]);
                mouseover.push(0);
				found++;
			}
		}
		if(galleries.length == 0){
						alert("WARNING: KoschtIT Image Gallery didn't find any gallery to display. Set '$ki_show_warnings = 0' if you don't want to see this warning again.");			return;
		} else {
			this.pics = new Array(galleries.length);
			showgallery(0);
		}
	}

	this.destr = function(){
		
	}

	/* -------------------------------- methodes ------------------------------------ */
    
    this.reinit = function(){
    	var count = this.dirs.length;
        for(var i = 0; i < count; i++){
        	$(this.dirs[i]+"_"+i).title = this.dirs[i];
        }
        for(var i = 0; i < count; i++){
        	var temp = galleries[i].cloneNode(false);
            galleries[i].parentNode.insertBefore(temp, galleries[i]);
            galleries[i].parentNode.removeChild(galleries[i]);
        }
    	kib.constr();
    }

	this.reloadcsandreinit = function(){
    	fw.getHTTP("ki_base/ki_getcustomsettings.php", gotcustomsettings);
    }

	function gotcustomsettings(responseText){
    	if(responseText != ""){
        	eval(responseText);
            kib.reinit();
		}
    }

	function showgallery(gallerynumber){
	
		var gallery = galleries[gallerynumber];
		gallery.innerHTML = "<div style='background:#ffffff; position:absolute; left:50%; top:50%; width:200px; height:40px; margin-left:-106px; margin-top:-26px; border:2px solid #000000; color:#000000; font: 14px normal Tahoma, sans-serif; padding:8px 4px 0px; text-align:center; line-height:17px;' id='" + gallery.id + "_wait" + "'>Please wait, while folder being scanned ...</div>";
        
		var node = document.createElement("div");
		node.id = gallery.id + "_see";
		node.style.position = "absolute";
		node.style.height = "100%";
		node.style.width = "100%";
		node.style.overflow = "hidden";
		node.style.display = "none";
		gallery.appendChild(node);
		
		node = document.createElement("div");
		node.id = gallery.id + "_hide";
		node.style.position = "absolute";
		node.style.height = "100%";
		node.style.width = "100%";
		node.style.overflow = "hidden";
		node.style.display = "none";
		gallery.appendChild(node);

       	kib.inc(gallerynumber, 0, 0, 1);
        
        fw.addevent( gallery, "mousemove", function(){ if(mouseover[gallerynumber] == 0)expandfornav(gallerynumber, 1); } );
        fw.addevent( gallery, "mouseout", function(){ expandfornav(gallerynumber, 0); } );
        
		if(gallerynumber+1 < galleries.length){
			showgallery(gallerynumber+1);
		} else {
					}
	}
    
    function expandfornav(gallerynumber, type){
    	var gallery = galleries[gallerynumber];
        var container = $(gallery.id + "_hide");
        
        if(type == 1){
            mouseover[gallerynumber] = 1;
            container.style.overflow = "visible";
            gallery.style.overflow = "visible";
        } else {
            mouseover[gallerynumber] = 0;
            container.style.overflow = "hidden";
            gallery.style.overflow = "hidden";        
        }
        
        var cs = kib.customsettings[kib.dirs[gallerynumber]] ? kib.customsettings[kib.dirs[gallerynumber]] : kib.customsettings['default'];
        if(cs[9] == 1 && cs[10] == 0){
			            var ysize = cs[1];
            if(cs[6] == 1){
                for(var i = container.childNodes.length-1; i >= 0; i--){
                    if(container.childNodes[i]){
                        if(container.childNodes[i].nodeName == "IMG"){
                            var temp2 = container.childNodes[i].alt.split("_");
                            ysize = parseInt(parseInt(temp2[3]) + 0.5*(parseInt(temp2[1]) + cs[8]*2));
                            break;
                        }
                    }
                }
                for(var j = 0; j < 10; j++){
                    if(container.childNodes[j]){
                        if(container.childNodes[j].nodeName == "IMG"){
                            temp2 = container.childNodes[j].alt.split("_");
                            ysize = ysize + parseInt(parseInt(temp2[3]) + 0.5*(parseInt(temp2[1]) + cs[8]*2));
                            if(ysize > cs[1])ysize = cs[1];
                            break;
                        }
                    }
                }
            }
            var tb = $(container.id + "_tb");
            if(tb != null){
                if(type == 1){
                    if(cs[10] == 0)fw.fade(tb, 100);
                    fw.resize(gallery, 4, 0, ysize + 38);
                } else {
                    if(cs[10] == 0)fw.fade(tb, 0);
                    fw.resize(gallery, 4, 0, ysize);
                }
            }
		}
    }

	
	//direction: 0 = static 1 = left, 2 = right, 3 = top, 4 = bottom
	this.inc = function(gallerynumber, direction, startfrom, collectinfo) {
		var params = "?reldir=&gallery=" + this.dirs[gallerynumber] + "&gallerynumber=" + gallerynumber;
		if(startfrom)params += "&startfrom="+startfrom;
		if(collectinfo)
			params += "&collectinfo=1";
		else {
            collectinfo = 0;
		}
		fw.getHTTP("ki_base/ki_koschtit.php" + params, gotinc, new Array(direction, gallerynumber, collectinfo));
	}

	function gotinc(responseText, params) {
        var direction = params[0];
		var gallerynumber = params[1];
		var collectinfo = params[2];
		
		var container = $(kib.dirs[gallerynumber]+"_"+gallerynumber+"_see");
		var switcher = $(kib.dirs[gallerynumber]+"_"+gallerynumber+"_hide");
		container.id = kib.dirs[gallerynumber]+"_"+gallerynumber+"_hide";
		switcher.id = kib.dirs[gallerynumber]+"_"+gallerynumber+"_see";

		if(collectinfo == 1)container.parentNode.removeChild($(kib.dirs[gallerynumber]+"_"+gallerynumber+"_wait"));
        
        container.innerHTML = responseText;
        
		if(collectinfo == 1){
			var jsontxt = $(gallerynumber+"_info").value;
			kib.pics[gallerynumber] = eval("(" + jsontxt + ")");
			            if(gallerynumber == galleries.length-1){
	            if(typeof(kit_opensharedpic) == "function")kit_opensharedpic();
			}
		}
		
		var x = parseInt(container.parentNode.style.width);
		var y = parseInt(container.parentNode.style.height);
		var xto = 0;
		var yto = 0;
		if(direction > 2)
			var onlydir = 3;
		else
			var onlydir = 2;
		if(direction == 1){xto = x;}
		if(direction == 2){xto = -x;}	
		if(direction == 3){yto = y;}
		if(direction == 4){yto = -y;}
		
        container.style.overflow = "hidden";
        container.parentNode.style.overflow = "hidden";
 		container.style.left = -1*xto+"px";
		container.style.top = -1*yto+"px";
        container.style.display = "block";
        
       	fw.move(container, onlydir, 0, 0);
        fw.move(switcher, onlydir, xto, yto, Array( function(){
			var node = document.createElement("div");
			node.id = switcher.id;
			node.style.position = "absolute";
			node.style.height = "100%";
			node.style.width = "100%";
			node.style.overflow = "hidden";
			node.style.lineHeight = "12px";
			node.style.display = "none";
			var parnt = switcher.parentNode;			
			parnt.removeChild(switcher);
			parnt.appendChild(node);
            
            container.style.overflow = "visible";
            container.parentNode.style.overflow = "visible";
            
                        var cs = kib.customsettings[kib.dirs[gallerynumber]] ? kib.customsettings[kib.dirs[gallerynumber]] : kib.customsettings['default'];
   			var ysize = cs[1];
            if(cs[6] == 1){
				for(var i = container.childNodes.length-1; i >= 0; i--){
                    if(container.childNodes[i]){
                        if(container.childNodes[i].nodeName == "IMG"){
                            var temp2 = container.childNodes[i].alt.split("_");
                            ysize = parseInt(parseInt(temp2[3]) + 0.5*(parseInt(temp2[1]) + cs[8]*2));
                            break;
                        }
                    }
				}
                for(var j = 0; j < 10; j++){
	                if(container.childNodes[j]){
                        if(container.childNodes[j].nodeName == "IMG"){
                            temp2 = container.childNodes[j].alt.split("_");
                            ysize = ysize + parseInt(parseInt(temp2[3]) + 0.5*(parseInt(temp2[1]) + cs[8]*2));
							if((cs[9] == 1 && cs[10] == 1) || (mouseover[gallerynumber] == 1 && cs[9] == 1 && cs[10] == 0))ysize += 38;
                            if(ysize <= cs[1]){
                            	fw.resize(parnt, 4, 0, ysize);
							} else {
                            	var temp = fw.getDim(parnt, 1)[1];
                                if(cs[9] == 1)temp += 38;
                            	if(ysize > temp){
                                	fw.resize(parnt, 4, 0, ysize);
								}
                            }
                            if(mouseover[gallerynumber] == 1 && cs[9] == 1 && cs[10] == 0)ysize -= 38;
                            break;
                        }
                    }
                }
            }
            if(cs[9] == 1){
				var tbcontent = "";
                var breite = 0;
                if(cs[15] == 1){
					tbcontent += "<img src='ki_base/ki_nav_full.png' style='cursor:pointer; border:0px; margin:0px 2px 0px 2px; padding-top:2px; display:inline;' onclick=\"kib.startExplorer(" + gallerynumber + ");\" onmouseover=\"this.style.padding = '0px 0px 2px 0px';\" onmouseout=\"this.style.padding = '2px 0px 0px 0px';\" title='Teljes k&eacute;pernyő' />";
					breite += (4 + 20);
                }
				if(cs[11] == 1 && kib.pics[gallerynumber].length > 0){
	               	tbcontent += "<img src='ki_base/ki_nav_play.png' style='cursor:pointer; border:0px; margin:0px 2px 0px 2px; padding-top:2px; display:inline;' onclick=\"kib.slideshow(" + gallerynumber + ");\" onmouseover=\"this.style.padding = '0px 0px 2px 0px';\" onmouseout=\"this.style.padding = '2px 0px 0px 0px';\" title='Automatikus lej&aacute;tsz&aacute;s' />";
                    breite += (4 + 20);
                }
                if($(gallerynumber+"_prev")){
	               	tbcontent += "<img src='ki_base/ki_nav_prev.png' style='cursor:pointer; border:0px; margin:0px 2px 0px 2px; padding-top:2px; display:inline;' onclick=\"kib.inc(" + gallerynumber + ", 1, " + $(gallerynumber+"_prev").innerHTML + ");\" onmouseover=\"this.style.padding = '0px 0px 2px 0px';\" onmouseout=\"this.style.padding = '2px 0px 0px 0px';\" title='Previous page' />";
                    breite += (4 + 20);
				}
				if($(gallerynumber+"_next")){
                	tbcontent += "<img src='ki_base/ki_nav_next.png' style='cursor:pointer; border:0px; margin:0px 2px 0px 2px; padding-top:2px; display:inline;' onclick=\"kib.inc(" + gallerynumber + ", 2, " + $(gallerynumber+"_next").innerHTML + ");\" onmouseover=\"this.style.padding = '0px 0px 2px 0px';\" onmouseout=\"this.style.padding = '2px 0px 0px 0px';\" title='K&ouml;vetkező lap' />";
                    breite += (4 + 20);
				}
                if(breite != 0){
                	breite -= 4;
                } else {
                	return;
                }
                var navpos = 0;
                if(cs[14] == "right"){
                	navpos = -4;
                } else if(cs[14] == "left"){
                	navpos = 4;
                } else {
                	breite += 16;
                	navpos = (cs[0]-breite+2)*0.5;
                }
                if(cs[9] == 1 && cs[10] == 0)ysize += (20 + 18);
                fw.addtoolbar(container, tbcontent, navpos, ysize - 20 - 14, 1);
                var tb = $(container.id + "_tb");
                tb.style.background = cs[12];
                if(cs[7] == 2){
                    tb.style.borderRadius = "20px";
                    tb.style.MozBorderRadius = "20px";
                    tb.style.WebkitBorderRadius = "20px";
                }
                tb.style.border = "2px solid " + cs[13];                        
                tb.style.padding = "1px 3px 3px 3px";
                tb.style.lineHeight = "12px";
                if(collectinfo == 1 && cs[10] == 0)tb.style.display = "none";
			}
		} ));
	}
	
	this.getImage = function(picstring) {
		var temp = picstring.indexOf("_");
        var gallerynumber = Number(picstring.substr(0, temp));
        var picnumber =  Number(picstring.substr(temp+1));
        var params = Array( gallerynumber, picnumber );
        var error = 0;
        if(gallerynumber >= this.pics.length){
			error = 1;
		} else {
        	if(picnumber >= this.pics[gallerynumber].length)error = 1;
        }
        if(error == 1 || isNaN(picnumber)|| isNaN(gallerynumber)){
        	alert("ERROR: KoschtIT Image Gallery didn't find the picture you want to display.");
            return;
        }
				fw.addjs("ki_base/ki_js_view.php?reldir=&gallery="+kib.dirs[gallerynumber], "kiv", params);
			}
	
    this.slideshow = function(gallerynumber) {
    	kib.getImage(gallerynumber + "_0");
        (function(){
			if($("thepicture")){
            	var sspic = $("ssbutton");
      			                sspic.src = "ki_base/ki_nav_stop.png";
                                sspic.title = "Stop slideshow";
                if($("kiv_gps"))$("kiv_gps").value = "0,0";
				if($("kiv_help"))$("kiv_help").style.display = "none";                
				kiv.sshelp(1);
				return;
			}
			setTimeout(arguments.callee, 10);
		})();
    }
    
	this.startExplorer = function(gallerynumber) {
    	var params = Array();
        params.push(gallerynumber);
		fw.addjs("ki_base/ki_js_explorer.php?reldir=&gallery="+kib.dirs[gallerynumber], "kie", params);
	}
    
    this.makebigger = function(obj){
	    var temp = obj.id.split("_");
        var cs = kib.customsettings[kib.dirs[temp[0]]] ? kib.customsettings[kib.dirs[temp[0]]] : kib.customsettings['default'];
		var faktor = cs[5];
       	var temp = obj.alt.split("_");
        var size = Array( temp[0], temp[1] );
		var newsize = Array( Math.round(size[0]*faktor), Math.round(size[1]*faktor) );
 	   	var pos = Array( temp[2], temp[3] );
 		var newpos = Array( pos[0]-0.5*(newsize[0]-size[0]), pos[1]-0.5*(newsize[1]-size[1]) );
        fw.move(obj, 1, newpos[0], newpos[1]);
        fw.resize(obj, 1, newsize[0], newsize[1]);
        obj.style.zIndex = 100;
        obj.style.borderColor = cs[4];
        if(cs[16] == 1)fw.dropshadow(obj, 1);
    }
    
    this.makesmaller = function(obj){
        var temp = obj.alt.split("_");
        var newsize = Array( temp[0], temp[1] );
	   	var newpos = Array( temp[2], temp[3] );
	    fw.move(obj, 1, newpos[0], newpos[1]);
	    fw.resize(obj, 1, newsize[0], newsize[1]);
        obj.style.zIndex = 0;
        temp = obj.id.split("_");
		var cs = kib.customsettings[kib.dirs[temp[0]]] ? kib.customsettings[kib.dirs[temp[0]]] : kib.customsettings['default'];
        obj.style.borderColor = cs[3];
        if(cs[16] == 1)fw.dropshadow(obj, 0);
    }
}

