/***************************/
//@Author: resatt
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var imghe;
var imgwi;
var imghe_o;
var imgwi_o;
var imgtop;
var imgleft;
var imgtop_o;
var imgleft_o;
var imgurl;
var k, szov;
var txt_left;
var tmphtml;
var tmplogo="<img src='/data/skins/1/logo_small.gif' width='200' />";
var windowHeight;
var cont_height;
var gallery_w;

function loadData(){
			
	imgurl = k.src;	
	imghe_o = k.height;
	imgwi_o = k.width;

imgurl=imgurl.replace('/small/','/large/');
//var tmp_html = "<img src='"+imgurl+"' width='"+imgwi_o+"' height='"+imghe_o+"' />";
//var imgo = $(".sw_6_1_preview_img_outer").find("img");
//imgwi = Math. round(imgo.width()*4);		
//imghe= Math. round(imgo.height()*4);		
//alert(imgwi+'-kep-'+imghe);

tmp_img = new Image();	
tmp_img.src = imgurl;	
imgwi = tmp_img.width;
imghe = tmp_img.height;
tmp_html=$(szov).html();
	$(szov).html(tmplogo+tmp_html);
}

function textanim(){
	if (tmp_html!=null)
	{
		var txt_curr_top = -434;
		var txt_curr_left = imgleft+10; 
		$($(szov)).css({
			"left":txt_curr_left+"px",	
			"top":txt_curr_top+"px",
			"display": "block",
				"visibility": "visible"
			});
		txt_left = imgleft+imgwi;
		$($(szov)).animate({ 
				left: txt_left+"px"					
			  }, 1200, function(){popupStatus = 1;});
	}
	else{
	popupStatus = 1;
	}
}
//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"display": "block",
			"opacity": "0.8"
		});
		
		$("#backgroundPopup").fadeIn("slow");
		
		$(k).css({
			"width": imgwi_o+"px",
			"height": imghe_o+"px",
			"max-width":"840px",
			"max-height":"840px",
			"border":"solid 2px silver"
		});
		
		k.src = imgurl;	
		
		$(k).animate({			
			"width": imgwi+"px",			
			"height": imghe+"px",			
			"left" : imgleft+"px",
			"top" : "-230px"
		}, 1200, function(){textanim();});	
		
		$("#popupimg").css({
			"display": "block",
			"position": "absolute",
			"width": "100%",
			"height": "100%",			
			"left" : "0px",
			"top" : "0px",
			"z-index":"620",
			"cursor":"url(/data/skins/1/zoomout.cur), pointer"
		});
		
	}
}
function k_css_back(){
		$(k).css({
			"border":"none",
			"max-width":"116px",
			"max-height":"116px",
			"z-index":"110"
		});
		popupStatus = 0;
}
//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		imgurl=imgurl.replace('/large/','/small/');		
		$($(szov)).css({ 
		"position": "relative",
        "left": "0px",	
		"display": "none"	,
		"top" : 0,
			"visibility": "hidden"
		  });
		$(szov).html(tmp_html);
		$("#popupimg").css({
			"display": "none",
			"position": "relative",
			"width": "1px",
			"height": "1px",			
			"left" : "0px",
			"top" : "0px",
			"z-index":"110"
		});
		$(k).animate({			
			"width": imgwi_o+"px",			
			"height": imghe_o+"px",			
			"left" : 0,
			"top" : 0
		}, 1000, function(){k_css_back();});		
		$(k).src = imgurl;
			
				
	}
}

//positioning popup
function centerPopup(){
	//request data for positioning
	windowHeight = document.documentElement.clientHeight;
	windowWidth = document.documentElement.clientWidth;
	//var popupHeight = $("#popupContact").height();
	//var popupWidth = $("#popupContact").width();
	//alert(popupWidth+'-popup-'+popupHeight)
	
		
	$(k).css({
		"position": "relative",
			"z-index":"600",
			"top": "0px"
	});
	
	/*imgleft = -(imgwi/2+310);*/
	/*imgleft = -(windowWidth/2-imgwi/2);*/
	imgleft = -(730-gallery_w+imgwi/2);
	txt_left = -(imgleft);	
	$("#backgroundPopup").css({
		"top":"0px",
		"height": cont_height			
	});	
	$("#popupimg").css({
		"top":"0px",
		"height": cont_height
	});
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	if ( $(".sw_s1_gallery").parent().is("#side_content2")==true )
	{
		
	//get cont height
	cont_height=350+$("#main_content").height()+$("#footer").height();
	gallery_w = $(".sw_s1_gallery").width();
	//preload images

	var primg = $(".sw_6_1_preview_img_outer").find("img");
	var primga = new Array();

	primg.each(function(i){		
		var imgurla = this.src; 
		imgurla = imgurla.replace('/small/','/large/');
		primga[i] = new Image();	
		primga[i].src = imgurla;		
	});
	
	//clear a tag href
	var hiv = $(".sw_s1_doctype_3 > .sw_6_1_preview_img_outer").find("a");
	hiv.each(function(i){		
		this.href = "javascript:void(0)";		
		this.title = "click to enlarge";
		$(this).css({
			"cursor":"url(/data/skins/1/zoomin.cur), pointer",
			"outline": "none"
			})
	});
	$(".sw_s1_doctype_3 > h2 ").css({
	"display": "none",
			"width":"1px",
			"height":"1px",
			"margin":"0x",
			"float": "left"
	});

	$(".sw_6_1_preview").css({
		"display": "none",
			"width":"1px",
			"height":"1px",
			"margin":"0x"
	});
	//rollover

	//LOADING POPUP
	//Click the button event!
	$(".sw_s1_doctype_3").click(function(){
		if( popupStatus==0){
		//load data
		k=$(this).find("img");
		k=k.get(0);			
		szov = $(this).find(".article_text");		
		loadData();
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		}
	});
	/**/
	$(".sw_6_1_gal_item_admin_links").click(function(){
		
		popupStatus = 1;
		
	});

	//CLOSING POPUP
	//Click the x event!
	$("#popupimg").click(function(){
		disablePopup();
	});
	
	/*
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	*/
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

	}
});