$(document).ready(function(){
	Ordering.init();
});

Ordering = {};

Ordering.init = function() {

	Ordering.reset();

	$(window).resize(function() {
		Ordering.resizer();
	});

	$('a.toggle').click(function(){
		//alert('click2 canclick '+Ordering.canClose)
		if(prices_sizes && document.getElementById('input_size').value.length == 0){ 
			alert('Būtina pasirinkti dydį'); 
			return false; 
		}
		
		$('#overlay').css({'z-index':25})
		Ordering.toggling();
		return false;
	});
	
	$('a.submit').click(function(){
		//alert('click3');
		if ($("#jqTransform").valid()) {	

			var url = $('#jqTransform').attr('action');
			$.post( url, $("#jqTransform").serialize(), function(msg){
				$('#overlay').css({'z-index':75})
				$('#thankyou').toggle();
				$('#thankyou').css({'z-index':100})
				Ordering.canClose = true;
			});
		} else {
			Ordering.error_reporting();
		}
		return false;
	});

	$('#thankyou').click(function(){
		//alert('click5');
		if (Ordering.canClose) {
			Ordering.toggling();
			$('#thankyou').toggle();
		}
	});

	$('#overlay').click(function(){
		//alert('click1');
		if (Ordering.canClose) {
			Ordering.toggling();
			$('#thankyou').toggle();
		}
	});

}

Ordering.error_reporting = function() {
	$('.trigger').css('border-bottom', '2px solid orange');
	$('.trigger').css('font-weight', 'bold');
}

Ordering.toggling = function () {
	Ordering.toggleWindow();
	Ordering.resizer();
	$('#jqTransform').jqTransform({imgPath:'img/'});
}

Ordering.reset = function() {
	//alert("RESET");
	Ordering.canClose = false;
	$("form input:text").val("");
}

Ordering.resizer = function () {
	var arrPageSizes = Ordering.getPageSize();

	$('#overlay').css({
		width: arrPageSizes[0],
		height: arrPageSizes[1]
	});
}


Ordering.toggleWindow = function() {
	
	$('.flashbanner').toggle();
	$('#overlay').toggle();
	$('#ordering').toggle();
	$('#logo').toggle();

	return false;
}

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */

Ordering.getPageSize = function() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};