var imagePreLoadList = Array();
var curImagePreload  = null;
//==========================================================
function onImagePreLoaded(){
	if( curImagePreload == null ){
		if( imagePreLoadList.length <= 0 ) return;
		curImagePreload = imagePreLoadList.pop();
		$(curImagePreload).bind("load",onImagePreLoaded);
		curImagePreload.src = "/catalog/image.php?"+$(curImagePreload).attr("prm");
	}
	$(curImagePreload).unbind("load");
	curImagePreload = null;
	onImagePreLoaded();
}

//==========================================================
function createGallery(){
	var win = window.open("", "gallery", "width=800,height=600," +
	"scrollbars=yes,toolbar=no,statusbar=no,location=no,directories=no,status=no,menubar=no");
	win.focus();
	return false;
}

//==========================================================
function getImageParam( str ){
	var param = Array();
	var a     = str.split("?"); a = a[1].split("&");
	var b;
	for( i=0; i < a.length; i++){
		b = a[i].split("=");
		param[b[0]] = b[1];
	}
	return param;
}


var curImg = null;
//==========================================================
function selectImage( img ){
	var param = getImageParam( $(img).attr("src") );
	if( curImg ) $(curImg).css({"border": "1px gray solid"});
	curImg = img;
	$(img).css({"border": "3px blue solid"});
	$("#curImage").attr("src","/catalog/image.php?dom="+param["dom"]+"&d="+param["d"]+"&i="+param["i"]+"&h=500");
}




//==========================================================
function catPopup(){
	this.zIndex   = 5000;
	this.overlay  = null;
	this.dBox     = null;
	this.parent   = null;
	this.width    = 0;
	this.height   = 0;
}

//==========================================================
catPopup.prototype.GetWindowInnerSize = function(){
	var width, height;
	var pDoc = document;
	if( self.innerHeight ){ // all except Explorer
		width = self.innerWidth;
		height = self.innerHeight;
	}
	else if( pDoc.documentElement && pDoc.documentElement.clientHeight ){ // Explorer 6 Strict Mode
		width = pDoc.documentElement.clientWidth;
		height = pDoc.documentElement.clientHeight;
	}
	else if( pDoc.body ){ // other Explorers
		width = pDoc.body.clientWidth;
		height = pDoc.body.clientHeight;
	}
	return {width : width, height : height};
};

//==========================================================
catPopup.prototype.GetWindowScrollSize = function(){
	var width, height;
	pDoc = document;

	if( pDoc.compatMode && pDoc.compatMode == "CSS1Compat" ){
		width = pDoc.documentElement.scrollWidth;
		height = pDoc.documentElement.scrollHeight;
	} else {
		if( pDoc.body.scrollHeight > pDoc.body.offsetHeight )
				height = pDoc.body.scrollHeight;
			else
				height = pDoc.body.offsetHeight;

	if( 	pDoc.body.scrollWidth > pDoc.body.offsetWidth ||
			( pDoc.compatMode && pDoc.compatMode == "BackCompat" ) ||
			( pDoc.documentElement && !pDoc.documentElement.clientWidth )
		)
			width = pDoc.body.scrollWidth;
		else
			width = pDoc.body.offsetWidth;
	}
		return {width : width, height : height};
};

//==========================================================
catPopup.prototype.KeyPress = function(e){
	if(!e) e = window.event;

	if( e.keyCode == 27 ){
		CP.CloseDialog();
		if(e.stopPropagation){ e.preventDefault(); e.stopPropagation();	} else { e.cancelBubble = true; e.returnValue = false; }
	}
}

//==========================================================
catPopup.prototype.CloseDialog = function(){
	if( this.overlay ) this.overlay.parentNode.removeChild(this.overlay);
	if( this.dBox    ) this.dBox.parentNode.removeChild(this.dBox);
}

//==========================================================
catPopup.prototype.ShowDialog = function(img){
	this.CreateOverlay();
	this.CreateDialogBox(img);
};

//==========================================================
catPopup.prototype.CreateOverlay = function(){
	var arSize = this.GetWindowScrollSize();
	this.overlay = document.body.appendChild(document.createElement("DIV"));
	
	$(this.overlay).css({position: "absolute", "z-index": this.zIndex});
	$(this.overlay).css({left: 0, top: 0});
	$(this.overlay).css({background: "url('/images/v2/overlay.png') repeat"});
	$(this.overlay).css({width: "100%", height: arSize.height});
	$(document).keypress( this.KeyPress );
};

//==========================================================
catPopup.prototype.CreateDialogBox = function(img){
	if( !this.overlay ) return false;
	this.dBox = document.body.appendChild(document.createElement("DIV"));
	$(this.dBox).css({position: "absolute", "z-index" : (this.zIndex+1) });
	
	var left = 10;
	var top  = $("#body").attr("scrollTop")+10;
		
	$(this.dBox).css({left: left, top: top, width: "50%", height: "50%"});
	$(this.dBox).css({"box-sizing": "border-box !important", "-moz-box-sizing" : "border-box !important"});

	this.getContent(img);
};

//==========================================================
catPopup.prototype.getContent = function(img){
	var param = getImageParam( $(img).attr("src") );
	var req = new JsHttpRequest();
	req.onreadystatechange = function(){
		if( req.readyState == 4 ){ 
			$(CP.dBox).html( req.responseText );
			selectImage($("img#gal-"+param["i"]));
		}
	}
	req.open(null, '/catalog/gallery.php', true);
	req.send({ "dom" : param["dom"], "d" : param["d"], "i" : param["i"] });
}



var CP = new catPopup();
