//Change the height of the site
jQuery(document).ready(function() {
    changeHeight();
	
	// Open top-nav Pop-up
	$("ul.top-cnt-menu li").mouseover(function(){
		$("a",this).addClass("hover");
		$("div.item-info",this).css('display','block');
	 });
	$("ul.top-cnt-menu").mouseout(function(){
		$("a",this).removeClass("hover");
		$("div.item-info",this).css('display','none');
	 });
	
	// Shopping Card
	$("#footer").css("margin-bottom","24px");
	
		
	// log in form
	$('#dialog').jqm({modal: true, trigger: 'a.jqModal'});
	
	// terms and conditions
	$('#tac-dialog').jqm({modal: true, trigger: 'a#jqModalTac'});
	
	// message window
	$('#msg-dialog').jqm({modal: true, trigger: 'a#jqModalMsg'});
	
	// invoice window
	$('#i-dialog').jqm({modal: true, trigger: 'a.jqModalI'});

	// videos
	$('#video1-dialog').jqm({modal: true, trigger: 'a#jqModalVideo1'});
	$('#video2-dialog').jqm({modal: true, trigger: 'a#jqModalVideo2'});
});

function closeJqmWindowMsg()
{
	var redirect = $('#redirect').val();
	$('#msg-dialog').jqmHide();
	if(redirect=='logout') {
		logOut();
	} else if(redirect=='login') {
		logIn();
	}
}

function addItemToCart(from)
{
	var baseUrl = $('#baseUrl').val();
	
	var productid = $('#product_id').val();
	if(from==1) {
		var colourid = $('#filter1').val();
		var amount = $('#filter3').val();
	} else if(from==2) {
		var colourid = $('#filter2').val();
		var amount = $('#filter4').val();
	}
	
	
	$.post(
		baseUrl + '/products/additem',
		{
			productid: productid,
			colourid: colourid,
			amount: amount
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
				$('#message-window').text(data['message']);
				$('#msg-dialog').jqmShow();
			} else {
				$('#message-window').html(data['message']);
				$('#msg-dialog').jqmShow();
			}	
		},
		'json'
	);

}





function orderItem(from)
{
	var baseUrl = $('#baseUrl').val();
	
	var productid = $('#product_id').val();
	if(from==1) {
		var colourid = $('#filter1').val();
		var amount = $('#filter3').val();
	} else if(from==2) {
		var colourid = $('#filter2').val();
		var amount = $('#filter4').val();
	}
	
	$.post(
		baseUrl + '/products/additem',
		{
			productid: productid,
			colourid: colourid,
			amount: amount
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			} else {
				$('#message-window').html(data['message']);
				$('#msg-dialog').jqmShow();
			}
			
			
		},
		'json'
	);	
}

function addGItemToCart(from)
{
	var baseUrl = $('#baseUrl').val();
	
	var productid = $('#product_id').val();
	
	var panel_number = $('#filter1').val();
	var panels = '';
	for(var i=1;i<=panel_number;i++) {
		
		var width = parseFloat($('#width_'+i).val());
		var height = parseFloat($('#height_'+i).val());
		var colourid = parseInt($('#filter'+i+'1').val());
		var amount = parseInt($('#filter'+i+'2').val());
		var serviceid = $('#filter'+i+'3').val();
		var servicesamount = $('#filter'+i+'4').val();
		
		if(isNaN(width)){
			width = 0;   
		}
		
		if(isNaN(height)){
			height = 0;   
		}
		
		if(isNaN(amount)){
			amount = 0;   
		}
		
		if(isNaN(servicesamount)){
			servicesamount = 0;   
		}
		panels += width + ";";
		panels += height + ";";
		panels += colourid + ";";
		panels += amount + ";";
		panels += serviceid + ";";
		panels += servicesamount + "[]";

	}
	
	$.post(
		baseUrl + '/glass/products/additem',
		{
			productid: productid,
			panels: panels
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
				$('#message-window').text(data['message']);
				$('#msg-dialog').jqmShow();
			} else {
				$('#message-window').html(data['message']);
				$('#msg-dialog').jqmShow();
			}	
		},
		'json'
	);

}

function orderGItem()
{
	var baseUrl = $('#baseUrl').val();
	
	var productid = $('#product_id').val();
	
	var panel_number = $('#filter1').val();
	var panels = '';
	for(var i=1;i<=panel_number;i++) {
		
		var width = parseFloat($('#width_'+i).val());
		var height = parseFloat($('#height_'+i).val());
		var colourid = parseInt($('#filter'+i+'1').val());
		var amount = parseInt($('#filter'+i+'2').val());
		var serviceid = $('#filter'+i+'3').val();
		var servicesamount = $('#filter'+i+'4').val();
		
		if(isNaN(width)){
			width = 0;   
		}
		
		if(isNaN(height)){
			height = 0;   
		}
		
		if(isNaN(amount)){
			amount = 0;   
		}
		
		if(isNaN(servicesamount)){
			servicesamount = 0;   
		}
		panels += width + ";";
		panels += height + ";";
		panels += colourid + ";";
		panels += amount + ";";
		panels += serviceid + ";";
		panels += servicesamount + "[]";

	}
	//alert(panels);
	
	$.post(
		baseUrl + '/glass/products/additem',
		{
			productid: productid,
			panels: panels
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			} 
			$('#message-window').html(data['message']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);	
}


function addGItemToWishlist(from)
{
	var baseUrl = $('#baseUrl').val();
	var productid = $('#product_id').val();
	
	var panel_number = $('#filter1').val();
	var panels = '';
	for(var i=1;i<=panel_number;i++) {
		
		var width = parseFloat($('#width_'+i).val());
		var height = parseFloat($('#height_'+i).val());
		var colourid = parseInt($('#filter'+i+'1').val());
		var amount = parseInt($('#filter'+i+'2').val());
		var serviceid = $('#filter'+i+'3').val();
		var servicesamount = $('#filter'+i+'4').val();
		
		if(isNaN(width)){
			width = 0;   
		}
		
		if(isNaN(height)){
			height = 0;   
		}
		
		if(isNaN(amount)){
			amount = 0;   
		}
		
		if(isNaN(servicesamount)){
			servicesamount = 0;   
		}
		panels += width + ";";
		panels += height + ";";
		panels += colourid + ";";
		panels += amount + ";";
		panels += serviceid + ";";
		panels += servicesamount + "[]";

	}

	$.post(
		baseUrl + '/glass/products/additemwishlist',
		{
			productid: productid,
			panels: panels
		},
		function(data){
			if(data['result']) {
				$('#wishlist_items').html(data['items_number']);
				$('#message-window').text(data['message']);
				$('#msg-dialog').jqmShow();
			} else {
				$('#message-window').html(data['message']);
				$('#msg-dialog').jqmShow();
			}
			
		},
		'json'
	);
	
}

function addGSampleToCart(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/glass/products/addsample',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
			
}

function orderGSample(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/glass/products/addsample',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
		
	
	
}

function addSampleToCart(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/products/addsample',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
			
}


function orderSample(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/products/addsample',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
		
	
	
}


function addItemToWishlist(from)
{
	var baseUrl = $('#baseUrl').val();
	
	var productid = $('#product_id').val();
	if(from==1) {
		var colourid = $('#filter1').val();
		var amount = $('#filter3').val();
	} else if(from==2) {
		var colourid = $('#filter2').val();
		var amount = $('#filter4').val();
	}

	$.post(
		baseUrl + '/products/additemwishlist',
		{
			productid: productid,
			colourid: colourid,
			amount: amount
		},
		function(data){
			if(data['result']) {
				$('#wishlist_items').html(data['items_number']);
				$('#message-window').text(data['message']);
				$('#msg-dialog').jqmShow();
			} else {
				$('#message-window').html(data['message']);
				$('#msg-dialog').jqmShow();
			}
			
		},
		'json'
	);
	
}


function addRefillToCart(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/refill/addrefill',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
			
}

function orderRefill(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/refill/addrefill',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
		
	
	
}



function addSampleProductToCart(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/special-products/addproduct',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				$('#cart-number').html(data['items_number']);
				$('#cart-total').html(data['basket_total']);
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
			
}

function orderSampleProduct(productid)
{
	
	var baseUrl = $('#baseUrl').val();

	$.post(
		baseUrl + '/special-products/addproduct',
		{
			productid: productid
		},
		function(data){
			if(data['result']) {
				window.location.href = baseUrl + '/myaccount/shopping-cart';
			}
			$('#message-window').text(data['result-msg']);
			$('#msg-dialog').jqmShow();
			
		},
		'json'
	);
		
	
	
}

function changeHeight() {
    
    var pagesize = getPageSize();

	var sitetabs_height = $('#sitetabs').height();
    var header_height = $('#header').height();
    var site_height = $('#site').height();
    var footer_height = $('#footer').height();
    var total_height = site_height + footer_height + header_height + sitetabs_height;
    
	var addheight = 24;
	if(sitetabs_height>0) {
		addheight = 25;
	}
	
    if(total_height<pagesize[3]) {
        var diff = pagesize[3] - footer_height - header_height - sitetabs_height - addheight;
        $('#site').css('height',diff+'px');
    }
}


function getPageSize()
{
	
	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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//sign-up form
function toggleclienttype()
{
	var form_height_before = $("form[name='signup']").height();
	var ct = $("input[name='client_type']:checked").val();
	if(ct==1) {
		$('.person').css('display','');
		$('.company').css('display','none');
		var m = 80;
	} else if(ct==2) {
		$('.person').css('display','none');
		$('.company').css('display','');
		var m = 0;
	}
	var form_height_after = $("form[name='signup']").height();
	var diff = form_height_after - form_height_before;
	//var ch = $('#content').height();
	var sh = $('#site').height();
	//$('#content').css('height',(parseInt(ch)+diff)+'px');
	$('#site').css('height',(parseInt(sh)+diff+m)+'px');
	changeHeight();
	
}



function setCookie(name, value)
{
	document.cookie=name+"="+value;
}

function deleteCookie(sName)
{
	document.cookie = sName + "=" + GetCookie(sName) + "; expires=" + (new Date()).toGMTString() + ";";
}

function getCookie(sName)
{

  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {

    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }


  return null;
}