var title;
var wind = '#window';

var winWidth = $(window).width();
var winHeight = $(window).height();

var boxSize = Array();
var boxType = '';

var timer;
var locationUrl = document.location.href;
var backUrl = document.location.href;

var windowOpacity	= 0.9;
var logoOpacity		= 0.1;
var fadeTime		= 'slow';

function setWindow(data) {
	if(data.error=='false'){
		page = data.window || '#window';
		var display = data.display || 'none';
		if(display=='fit'){
			var boxLeft 	= 1;
			var boxTop 		= 42;
			var boxHeight 	= (winHeight-125);
			var boxWidth 	= (winWidth-3);
		}else{
			var boxLeft 	= ((winWidth-900)/2);
			var boxTop 		= (((winHeight-(117+375))/2)+42);
			var boxHeight 	= 375;
			var boxWidth 	= 900;
		}
		boxType = display;
		boxSize['height'] = boxHeight;
		boxSize['width'] = boxWidth;
		
		$('#logo').fadeTo(fadeTime, logoOpacity,
			function(){
				$(wind).fadeOut(fadeTime,
					function(){
						$(page+' > div').html(data.html);
						$(page+' > div').prepend('<a href="/#!/home" onclick="$(wind).fadeOut(\'slow\');" class="close">X</a>');
						$(page).css({left: boxLeft, top: boxTop, width: boxWidth, height: boxHeight});
						$(page).fadeTo(fadeTime, windowOpacity,
							function(){
								wind = page;
							}
						);
					}
				);
			}
		);
		
	}else{
		$(wind).fadeOut(fadeTime,function(){
			$('#logo').fadeTo(fadeTime, 1);
		});
	}
	if(typeof(data.debug)!='undefined')
		alert(data.debug);
}

function openWin(page){
	$.post("json.php", { "page": page },
		function(data){
			setWindow(data);
		}, "json");
	return false;
}

function locationChange(){
	var url = document.location.href;
	if(locationUrl != document.location.href && url.indexOf('#!')>0){
		backUrl 	= locationUrl; 
		locationUrl	= document.location.href;
		openWin('#'+url.substr(url.indexOf('#!')+3));
	}
	
	timer = setTimeout("locationChange()",10);
}

$(window).resize(function() {
	winWidth = $(window).width();
	winHeight = $(window).height();
	if(boxType=='fit'){
		$(wind).css({width: winWidth-3});
		boxSize['width'] = (winWidth-3);
	}else{
		if(winWidth<900){
			$(wind).css({width: winWidth-3});
			boxSize['width'] = (winWidth-3);
		}else if(winWidth>900 && boxSize['width']<900){
			$(wind).css({width: 900});
			boxSize['width'] = 900;
		}
	}
	$(wind).css({left: ((winWidth-boxSize['width'])/2)});
});

$(document).ready(function () {
	$('body').ajaxStart(function(){
		title = document.title;
		document.title = 'Loading...';
		$(this).css('cursor', 'progress');
	});
	$('body').ajaxStop(function(){
		document.title = title;
		$(this).css('cursor', 'auto');
	});
	var url = document.location.href;
	if (url.indexOf('#!')>0) {
//		openWin('#'+url.substr(url.indexOf('#!')+3));
		document.location.href = '/'+url.substr(url.indexOf('#!')+3);
	}
	
	locationChange();
});

function isOver(thi){
	var ti = $(thi);
	ti.prepend('<a href="#" id="buyutec"></a>');
	$('.work').lightBox();
	$('#buyutec').click(function(){
		$(ti).find('.work').click();
		return false;
	});
	var buyutec	= $('#buyutec');
	buyutec.fadeIn(150);
//	buyutec.fadeTo(100, 0.5);
	
	return false;
}
function isOut(){
	$('#buyutec').remove();
	return false;
}

function contactForm(){
	$.post("ajax.php", { "do": "contact", "name": $('#contactName').val(), "email": $('#contactEmail').val(), "message": $('#contactMessage').val() },
			function(data){
				if(typeof(data.debug)!='undefined')
					alert(data.debug);
			}, "json");
		return false;
}
