
//------------------------------------------
// SG BOT - WEB SITE
//------------------------------------------
// MODULE JAVASCRIPT : POPUP
//------------------------------------------
// by SG_Kalish (Denis Bajram) 
//------------------------------------------

var popup_style = "background-color: #FFFFFF; border: 1px #000000 solid;";

var popup_loader = "http://images.snipersgaulois.com/common/popup.loader.gif";
var popup_background = "http://images.snipersgaulois.com/common/popup.background.png";

var maxWidth = 610;
var maxHeight = 610;

var debug = false;

var margin = 10;
var loaderWidth = 38;
var loaderHeight = 38;

var popup_bg_code =  '<div id="popup_bg" style="white-space: pre; display: none; z-index: 1000; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background-image: url('+popup_background+');" onclick="hide_popup()"><\/div><div id="popup_frame" style=" z-index: 1001; display: none; position: fixed; overflow: hidden; text-align: center; '+popup_style+'" onclick="hide_popup()"><a href="javascript:hide_popup()"><img id="loader_img" src="'+popup_loader+'" border="0" style="margin: '+margin+'px;" width="'+loaderWidth+'" height="'+loaderHeight+'" /><img id="popup_img" src="" border="0" style="margin: '+margin+'px;" title="Clic to close" /><\/a><\/div>';


var winWidth = 0;
var winHeight = 0;
var winLeft = 0;
var winTop = 0;
	
var imgWidth = 0;
var imgHeight = 0;
	
var ratio = 1;
var step = 10;
	
var popup_bg = 0;
var popup_frame = 0;
var popup_img = 0;
var loader_img = 0;
var main_body = 0;

var img_src = "";

var new_image = new Image();

var useragent = navigator.userAgent;

var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChStart = strChUserAgent.substring(0,intSplitStart);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
var strChEnd = strChUserAgent.substring(strChEnd);

if(strChMid.indexOf("MSIE 7") != -1)
	useragent = "IE7";
else if(strChMid.indexOf("MSIE 6") != -1)
	useragent = "IE6";
else if(strChMid.indexOf("MSIE 5") != -1)
	useragent = "IE5";
else if(strChEnd.indexOf("Firefox/2") != -1)
	useragent = "firefox 2";
else if(strChEnd.indexOf("Firefox") != -1)
	useragent = "firefox";
else if(strChEnd.indexOf("Netscape/7") != -1)
	useragent = "netscape 7";
else if(strChEnd.indexOf("Netscape") != -1)
	useragent = "netscape";
else if(strChStart.indexOf("Opera/9") != -1)
	useragent = "Opera";
else if(strChStart.indexOf("Opera") != -1)
	useragent = "Opera";
else
	useragent = useragent;

var popup_images = Array();

function GetElement(string)
{
	if(document.getElementById) element = document.getElementById(string);
	else if(document.all) element = document.all[string];
	if(!element) { elements = document.getElementsByTagName(string); element = elements[0]; }
	return element;
}	

function get_window_size()
{
	var state = popup_bg.style.display;
		
	popup_bg.style.display = "block";
		
	winWidth = popup_bg.offsetWidth;
	winHeight = popup_bg.offsetHeight;
		
	popup_bg.style.display = state;
}

function show_popup(url)
{
	img_src = url;
	
	if( useragent=="IE5" || useragent=="IE6")
	{
		window.open(img_src,'image','scrollbars=yes,resizable=yes,dependent=yes,fullscreen=yes');
	}
	else
	{
		try	
		{	
			get_window_size();
			
			winLeft = 0;
			winTop = 0;
			
			step = 5;
			
			popup_frame.style.width = loaderWidth+2*margin+"px";
			popup_frame.style.height = loaderHeight+2*margin+"px";
			
			popup_frame.style.left = Math.floor((winWidth-loaderWidth-2*margin)/2)+"px";
			frameTop = winTop + Math.floor((winHeight-loaderHeight)/2);
			if(frameTop<margin) frameTop = margin;
			popup_frame.style.top = frameTop+"px";
			
			popup_bg.style.display = "block";
			popup_frame.style.display = "block";
			popup_img.style.display = "none";
			loader_img.style.display = "block";
			
			if(useragent=="Opera")
			{
				new_img = new Image();
				new_img.src = img_src; 
				
				popup_img.style.maxWidth = "1";
				popup_img.style.maxHeight = "1";
				popup_img.style.display = "block";
				popup_img.onload = img_loaded;
				popup_img.src = img_src;
			}
			else
			{
				new_img = new Image();
				new_img.onload = img_loaded;
				new_img.src = img_src; 
			}
		}
		catch(err)
		{
			window.open(img_src,'image','scrollbars=yes,resizable=yes,dependent=yes,fullscreen=yes');
		}
	}
}

function img_loaded()
{
	
	if(useragent=="Opera")
	{
		popup_img.onload = "";
		popup_img.style.maxWidth = "";
		popup_img.style.maxHeight = "";
		imgWidth = new_img.width;
		imgHeight = new_img.height;
	}
	else
	{
		popup_img.style.display = "none";
		imgWidth = new_img.width;
		imgHeight = new_img.height;
		popup_img.src = new_img.src;
		delete new_img;
	}
	
	get_window_size();
	
	winLeft = 0;
	winTop = 0;
	
	var wratio = 1;
	var hratio = 1;
	
	ratio = 1;
	
	if(imgWidth>(winWidth-4*margin)) { wratio=(imgWidth+4*margin)/(winWidth-2*margin); } 
	if(imgHeight>(winHeight-4*margin)) { wratio=(imgHeight+4*margin)/(winHeight-2*margin); } 
	
	if(wratio>hratio) ratio=wratio; else ratio=hratio; 
	
	imgWidth = Math.floor(imgWidth/ratio);
	imgHeight = Math.floor(imgHeight/ratio);
	
	popup_img.width = imgWidth;
	popup_img.height = imgHeight;
	
	popup_frame.style.width = imgWidth+2*margin+"px";
	popup_frame.style.left = Math.floor((winWidth-imgWidth-2*margin)/2)+"px";
	
	loader_img.style.display = "none";
	
	popup_img.style.display = "block";
	
	if(debug) GetElement("popup_bg").innerHTML = "url => "+img_src+"\rwin => w "+winWidth+" - h "+winHeight+" \rimg => w "+imgWidth+" - h "+imgHeight+"\rratio = >"+ratio+" / margin = >"+margin+"\rdisplay => "+popup_img.style.display;
	
	resize_popup();
}; 

function resize_popup()
{
	step--;
	
	if(imgHeight>maxHeight*1.5 && imgWidth>maxWidth*1.5) step = 0;
	
	if(step>0)
	{
		popup_frame.style.height = Math.floor(imgHeight/step)+(2*margin)+"px";
		frameTop = winTop + Math.floor((winHeight-(2*margin)-imgHeight/step)/2);
		if(frameTop<margin) frameTop = margin;
		popup_frame.style.top = frameTop+"px";
		setTimeout("resize_popup()",50);
	}
	else
	{
		popup_img.height = imgHeight;
		popup_frame.style.height = imgHeight+(2*margin)+"px";
		frameTop = winTop + Math.floor((winHeight-(2*margin)-imgHeight)/2);
		if(frameTop<margin) frameTop = margin;
		popup_frame.style.top = frameTop+"px";
	}
}

function hide_popup()
{
	popup_bg.style.display = "none";
	popup_frame.style.display = "none";
}

function img_checker(main_img)
{
	imgWidth = main_img.width;
	imgHeight = main_img.height;
	
	var wratio = 1;
	var hratio = 1;
	
	ratio = 1;
	
	if(imgWidth>maxWidth) { wratio=imgWidth/maxWidth; }
	if(imgHeight>maxHeight) { hratio=imgHeight/maxHeight; } 
	
	if(wratio>hratio) ratio=wratio; else ratio=hratio;
	
	if(ratio>1) 
	{ 
	
		imgWidth = Math.floor(imgWidth/ratio);
		imgHeight = Math.floor(imgHeight/ratio);
	
		main_img.width = imgWidth;
		main_img.height = imgHeight;
		
		main_src = main_img.src;
		main_img.style.cursor = "pointer";
		main_img.onclick = new Function('show_popup("'+main_src+'");');
	}
	
}

document.write(popup_bg_code);

popup_bg = GetElement("popup_bg");
popup_frame = GetElement("popup_frame");
popup_img = GetElement("popup_img");
main_body = GetElement("body");
loader_img = GetElement("loader_img");
