var global = {
	xmlHttp: null,
	current: 0,
	x: 0,
	timeout: null,
	progress: 0,
	p: 0,
	open: 0,
	
	clients: function()
	{
		$("div.clients").stop(true);
			if(global.open == 0){
				$("div.clients").animate({
					marginLeft: -11
				}, 300 );
				$(".iphone").animate({
					marginTop: 368
				}, 300 );
				global.open = 1;
			} else {
				$("div.clients").animate({
					marginLeft: -131
				}, 300 );
				$(".iphone").animate({
					marginTop: 300
				}, 300 );
				global.open = 0;
			}
	},
	
	contact: function()
	{
		$("div.contact").stop(true);
			if(global.open == 0){
				$("div.contact").animate({
					marginLeft: -11
				}, 300 );
				$("div.clients").animate({
					marginTop: 330
				}, 300 );
				global.open = 1;
			} else {
				$("div.contact").animate({
					marginLeft: -200
				}, 300 );
				$("div.clients").animate({
					marginTop: 292
				}, 300 );
				global.open = 0;
			}
	},
	
	status: function(show)
	{
		var show;
		if(show==1)
			$(".preloader").animate({ top: -1}, 300 ); 
		else
			$(".preloader").animate({ top: -54}, 300 );
	},
	
	disableSelect: function(e){
		return false;
	},
	
	fetchImage: function(op)
	{
		global.xmlHttp = global.GetXmlHttpObject();
		
		var url="lib/imageFetcher.php";
		var sendValues='op='+op;
		url=url+"?sid="+Math.random();
		global.xmlHttp.onreadystatechange=global.stateChanged;
		global.xmlHttp.open("POST",url,true);
		global.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		global.xmlHttp.send(sendValues);
	},
	
	stateChanged: function()
	{
		if(global.xmlHttp.readyState==4)
		{ 
			global.progress = global.xmlHttp.responseText.split(',');
			global.preload();
		}
		
	},
	
	preload: function()
	{
		global.status(1);
		global.p   = new Array();
		global.x   = 0;
	
	    for(i=0;i<global.progress.length;i++)
	    {
			global.p[i] = new Image();
	   		global.p[i].onabort		=	global.update;
	    	global.p[i].onerror		= 	global.update;
	    	global.p[i].onload 		= 	global.update;
	   		global.p[i].src 		=	global.progress[i];
		}
	},
	
	update: function()
	{
		global.x++;
		pro=Math.floor((global.x/global.progress.length)*100);
		
		//document.getElementById('status').style.width = pro*7.5 +'px';
		
		 if(pro==100)
		 {
		 	global.status();
		 	setTimeout(function()
		 	{	
		 		$("#bigImage").stop(true);
		 		var bi = document.getElementById('imageHolder');
		 		bi.style.opacity = 1;
		 		bi.style.display = 'none';
		 		bi.innerHTML = '<img id="thePicture" src="' + global.p[0].src + '" alt="" />';
		 		
				var h = document.getElementById('thePicture').height;
				if(document.getElementById('thePicture').width < h)
				{
					bi.style.marginLeft = '158px';
					bi.style.marginTop = '0';
					//$("#shadeV").fadeIn(300);
				} else {
					bi.style.marginLeft = '25px';
					bi.style.marginTop = '0';
					//$("#shadeH").fadeIn(300);
				}
		 		$(".imageHolder").fadeIn(300);
		 		//$("#status").fadeOut(300);
		 		setTimeout("global.animate()", 200);
		 	}, 230);
		}
	},
	
	change: function(op)
	{
		clearTimeout(global.timeout);
		if((parseFloat(op) == parseInt(op)))global.current = op;
		if(op=='plus' && global.current<(global.progress.length))global.current++;
		if(op=='minus' && global.current>-1)global.current--;
		
		//alert(global.current)
		
			var bi = document.getElementById('imageHolder');
			//document.getElementById('shadeV').style.display = 'none';
			//document.getElementById('shadeH').style.display = 'none';
			bi.style.opacity = 1;
			if(global.current==-1)global.current = 0; else if(global.current>(global.progress.length)-1)global.current = global.progress.length-1; else bi.style.display = 'none';
			bi.innerHTML = '<img id="thePicture" src="' + global.p[global.current].src + '" alt="" />';
						
			var w = document.getElementById('thePicture').width;
			var h = document.getElementById('thePicture').height;

			
			var margin = (602 / 2) - (global.p[global.current].width / 2);
			
			/*
if(document.getElementById('thePicture').width < document.getElementById('thePicture').height || document.getElementById('thePicture').width == document.getElementById('thePicture').height)
			{
				///console.log('nu');
				bi.style.marginLeft = '158px';
				
				//var margin = (bi.width / 2) - (parseInt(document.getElementById('thePicture').width / 2))
				//console.log(margin);
				
				//bi.style.marginLeft = parseInt(document.getElementById('thePicture').width / 2) + 'px';
				//console.log(parseInt(document.getElementById('thePicture').width / 2) + 'px');
				//bi.style.marginLeft = parseFloat(w / 2) + 'px';
				
				bi.style.marginTop = '0';
				//$("#shadeV").fadeIn(300);
			} else {
				bi.style.marginLeft = '0px';
				bi.style.marginLeft = '25px';
				bi.style.marginTop = '0';
				//$("#shadeH").fadeIn(300);
			}
*/
			
			bi.style.marginLeft = margin + 'px';
		
			
			//bi.style.marginLeft = '133px'; else bi.style.marginLeft = '0px';
			//alert(document.getElementById('thePicture').width +'.'+document.getElementById('thePicture').height)
			//alert(document.getElementById('thePicture').width);bi.style.marginLeft = '133px'; else bi.style.marginLeft = '0px';
			$("div.imageHolder").fadeIn(300);
			document.getElementById('imageHolder').style.display = 'block';
			
		global.timeout = setTimeout("global.animate()", 300);
	},
	
	
	animate: function()
	{
		var margin;
		if(global.current<5)margin = 0; else margin = (global.current*(63+22.9))-(4*(63+22.9));
		
		$("div.inside").animate({
			width: 850 + margin,
			marginLeft: -margin
		}, 300 );
		
	
	
		//$("div.imageHolder").animate({
		//	opacity: 0.3
		//}, 50 );
		$("div.smallImage" + global.current).animate({
			opacity: 1
		}, 200 );
			
		//document.getElementById('footer').innerHTML = (global.current+1) + ' of ' + (global.progress.length);
	
	},
	
	checkKeycode: function(e) {
		if(!e)var e = window.event;
		var keycode;
		if(e.which)keycode = e.which;
		if(e.keyCode)keycode = e.keyCode;
		if(keycode==39){
			global.change('plus');
		} else if(keycode==37){
			global.change('minus');
		}
		if(keycode==27)viewImage();
	},
	
	renderFont: function()
	{
		$('.logo').ttfgen();
		$('.client').ttfgen();
		$('.iphone').ttfgen();
		$('.contact_header').ttfgen();
	},
	
	GetXmlHttpObject: function()
	{
		try { global.xmlHttp=new XMLHttpRequest(); }
		catch(e)
		{
			try { global.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } 
			catch(e) { global.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		}
		return global.xmlHttp;
	}
}
//document.onmousedown	=	global.disableSelect;
document.onkeydown		=	global.checkKeycode
window.onLoad			=	global.fetchImage();
//setTimeout("global.renderFont();", 100);
