var popupStatus = 0;

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
};

$.preloadImages("style/curcontabover.png", "style/etabover.png", "style/pitabover.png", "style/cuecardtabover.png");

function loadPopup()
{  
	if(popupStatus==0)
	{  
		$("#imagePopup").fadeIn("slow");
		popupStatus = 1;  
	}  
};

function disablePopup()
{  
	if(popupStatus==1)
	{  
		$("#imagePopup").fadeOut("slow");  
		popupStatus = 0;  
	}  
};

function centerPopup()
{  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#imagePopup").height();
	var popupWidth = $("#imagePopup").width();
	
	$("#imagePopup").css({"position": "absolute", "top": windowHeight/2-popupHeight/2, "left": windowWidth/2-popupWidth/2});
};

function openImage(file)
{
	$("#imagePopup").css({'height' : 470, 'width' : 325});
	$("#image").attr("src", file);
	centerPopup();
	loadPopup();
};

function openWideImage(file)
{
	$("#imagePopup").css({'height' : 330, 'width' : 485});
	$("#image").attr("src", file);
	centerPopup();
	loadPopup();
};

$(document).ready(function()
{	
	$("#image").click(function()
	{
		disablePopup();
	});
				
	$("#closePopup").click(function()
	{
		disablePopup();
	});

	$(document).keypress(function(event)
	{
		if(event.keyCode==27 && popupStatus==1)
		{
			disablePopup();
		}
	});
	
	$(".tabImage").hover(
		function()
		{
			if($(this).attr("src").indexOf("over") == -1) 
			{
				var newSrc = $(this).attr("src").replace(".png","over.png");
				$(this).attr("src",newSrc);
			}
		},
		function()
		{
			if($(this).attr("src").indexOf("over.png") != -1) 
			{
				var oldSrc = $(this).attr("src").replace("over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
});

function getContent(id)
{ 
	data=GetXmlHttpObject();

	if (data==null)
	{
		alert ("Oh noes! Your browser doesn't support AJAX!");
		return;
	}
	
	var url="browse.php"+"?id="+id+"&sid="+Math.random();
	
	data.onreadystatechange=function() 
	{
		if(data.readyState == 4 && data.status == 200) 
		{
			document.getElementById("content").innerHTML=data.responseText;
		}
	}
	
	data.open("GET",url,true);
	data.send(null);
};

function browse(id)
{
	switch(id)
	{
		case 1:
		{
			$.preloadImages("images/piscreenshot1_thumb.png", "images/piscreenshot2_thumb.png", "images/piscreenshot3_thumb.png", "images/piscreenshot4_thumb.png", "images/piscreenshot5_thumb.png");
			getContent(id);
			$.preloadImages("images/piscreenshot1.png", "images/piscreenshot2.png", "images/piscreenshot3.png", "images/piscreenshot4.png", "images/piscreenshot5.png");
			break;
		}
		case 2:
		{
			$.preloadImages("images/escreenshot1_thumb.png", "images/escreenshot2_thumb.png", "images/escreenshot3_thumb.png", "images/escreenshot4_thumb.png");
			getContent(id);
			$.preloadImages("images/escreenshot1.png", "images/escreenshot2.png", "images/escreenshot3.png", "images/escreenshot4.png");
			break;
		}
		case 3:
		{
			$.preloadImages("images/curconscreenshot1_thumb.png", "images/curconscreenshot2_thumb.png", "images/curconscreenshot3_thumb.png", "images/curconscreenshot4_thumb.png", "images/curconscreenshot5_thumb.png");
			getContent(id);
			$.preloadImages("images/curconscreenshot1.png", "images/curconscreenshot2.png", "images/curconscreenshot3.png", "images/curconscreenshot4.png", "images/curconscreenshot5.png");
			break;
		}
		case 4:
		{
			$.preloadImages("images/cuecardscreenshot1_thumb.png", "images/cuecardscreenshot2_thumb.png", "images/cuecardscreenshot3_thumb.png", "images/cuecardscreenshot4_thumb.png", "images/cuecardscreenshot5_thumb.png");
			getContent(id);
			$.preloadImages("images/cuecardscreenshot1.png", "images/cuecardscreenshot2.png", "images/cuecardscreenshot3.png", "images/cuecardscreenshot4.png", "images/cuecardscreenshot5.png");
			break;
		}
		default:
		{
			getContent(id);
			break;
		}
	}

};

function GetXmlHttpObject()
{
	var xmlHttp=null;
	
	try //Proper browsers
	{
		xmlHttp=new XMLHttpRequest();
	}
	
	catch(e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		
		catch(e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
};
