var over = false;


var myGrower = "";
var growerSize = 40;
var growery = 550;
var growerx = 0;
var border = 1;
var growerLeft;
var growerTop;
var growcount = 0;
var growcountmax = 10;
var growinterval = 30; //ms

function initExpander()
{
	growerSize = 40;
	border = 1;
	growcount = 0;
	document.getElementById('expandingDiv').style.width = 50;
	document.getElementById('expandingDiv').style.height = 50;
	document.getElementById('expandingDiv').style.border = border + "px solid #C0C0C0";
}

function windowSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}	
	
	return [myWidth,myHeight];
}

//var dragobject={
//z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
//initialize:function(){
//document.onmousedown=this.drag
//document.onmouseup=function(){this.dragapproved=0}
//},
//drag:function(e){
//var evtobj=window.event? window.event : e
//this.targetobj=window.event? event.srcElement : e.target
//if (this.targetobj.className=="productPopUp"){
//this.dragapproved=1
//
//var myWidth = 0, myHeight = 0;
//myWidth = windowSize()[0];
//myHeight = windowSize()[1];
//
//if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left=Math.round(myWidth*0.15)}
//if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top=Math.round(myHeight*0.15)}
//
//this.offsetx=parseInt(this.targetobj.style.left)
//this.offsety=parseInt(this.targetobj.style.top)
//this.x=evtobj.clientX
//this.y=evtobj.clientY
//if (evtobj.preventDefault)
//evtobj.preventDefault()
//document.onmousemove=dragobject.moveit
//}
//},
//moveit:function(e){
//var evtobj=window.event? window.event : e
//if (this.dragapproved==1){
//this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
//this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"
//return false
//}
//}
//}
//
//dragobject.initialize()


function doGrow(){
	var curpos = getPosition();
	if (isNaN(curpos.x)){
		thex = mouseX;
		they = mouseY;
	}
	else{
		thex = curpos.x;
		they = curpos.y;
	}
	
	initExpander();
	growerLeft = thex;
	growerTop = they;
	//document.getElementById('expandingDiv').style.left = growerLeft;
	//document.getElementById('expandingDiv').style.top = growerTop;
	var myScreen = windowSize();
	var myWidth;// = myScreen[0];
	if (document.all)
	{myWidth=document.body.clientWidth;}
	else
	{myWidth=window.innerWidth;}
	//document.getElementById('expandingDiv').style.left = myWidth*0.3;
	//document.getElementById('expandingDiv').style.top = document.body.scrollTop + 100;
	document.getElementById('expandingDiv').style.left = thex + 'px';
	document.getElementById('expandingDiv').style.top = they + 'px';
	document.getElementById('expandingDiv').style.visibility = 'visible';
	growerx = thex;
	growery = they;
	
	newwid = (myWidth / 2) - 200;
	//myGrower = setInterval("grow("+thex+","+they+","+myScreen[0]*0.15+","+(document.documentElement.scrollTop + 100)+")",growinterval);
	//myGrower = setInterval("grow("+thex+","+they+","+newwid+","+(document.documentElement.scrollTop + 100)+")",growinterval);
	var scrollpos;
	if (navigator.appVersion.search(/safari/i) == -1 && document.compatMode == 'CSS1Compat')
		scrollpos = document.documentElement.scrollTop;
	else
		scrollpos = document.body.scrollTop;
	myGrower = setInterval("grow("+thex+","+they+","+newwid+","+(scrollpos + 100)+")",growinterval);
}
function doShrink(){
	
	//document.getElementById('
	myGrower = setInterval("shrink()",growinterval);
}

function grow(x,y,myscreenx,myscreeny){
	growerSize = growerSize + 35;
	//growerPosition = growerPosition - 10;
	//growerSize += 10;
	if (x > myscreenx)
	{
		var xinterval = (x - myscreenx)/growcountmax;
		growerx -= xinterval;
	}
	else
	{
		var xinterval = (myscreenx - x)/growcountmax;
	}
	if (y > myscreeny)
	{
		var yinterval = (y - myscreeny)/growcountmax;
		growery -= yinterval;
	}
	else
	{
		var yinterval = (myscreeny - y)/growcountmax;
		growery -= yinterval;
	}
	border = border + 1.2;
	//display.innerHTML = growerPosition;
	document.getElementById('expandingDiv').style.border = border + "px solid #C0C0C0";
	document.getElementById('expandingDiv').style.top = growery + "px";
	document.getElementById('expandingDiv').style.left = growerx + "px";
	document.getElementById('expandingDiv').style.width = growerSize + "px";
	document.getElementById('expandingDiv').style.height = growerSize + "px";
	var growleft = document.getElementById('expandingDiv').style.left;
	var growtop = document.getElementById('expandingDiv').style.top;
	//if(growerSize > 300){
	//if(growleft > windowSize()[0]*0.15 && growtop > document.body.scrollTop + 100){
	if(growcount == growcountmax){
		clearInterval(myGrower);
	}
	growcount++;
	//document.getElementById('display').innerHTML = growleft + ';' + windowSize()[0]*0.15;
}

function shrink(){
	growerSize = growerSize - 10;
	document.getElementById('expandingDiv').style.width = growerSize + 'px';
	document.getElementById('expandingDiv').style.height = growerSize + 'px';
	if(growerSize < 50){
		clearInterval(myGrower);
		document.getElementById('expandingDiv').style.visibility = 'hidden';
	}
}


function Product_OptionChanged(obj)
{
	if (obj.value == '')
	{
		try
		{
			document.getElementById('addToCartImg').className = 'lessOpacity';
			document.getElementById('addToCartAnchor').setAttribute("href","#");
		}
		catch (ex)
		{
		}
	}
	else
	{
		try
		{
			document.getElementById('addToCartImg').className = '';
			document.getElementById('addToCartAnchor').setAttribute("href","javascript:Prod_MultiAddToBasket()");
		}
		catch (ex)
		{
		}
	}

}


function Prod_MultiAddToBasket(){
	//_DEBUD_MODE = true; 

	initDialogCart();

	if (CheckValues())
	{
		//style="line-height:4px;"
		$('addtocartmsg').innerHTML = '<br style="line-height:5px;"><img src="images/ajax_indicator_sm.gif" />';
		new Ajax.Request('multiAddToBasket.taf', 
		{   
			parameters: $('productForm').serialize(true),
			onSuccess: function(transport) { 
				$('ajaxcartresponse').innerHTML = transport.responseText;
				
				var basketerror = $('basketerror');
				if (basketerror != null)
				{
					$('addtocartmsg').innerHTML = "";
					alert(basketerror.innerHTML);
				}
				else
				{
					var cartresponse = $('addmsg').innerHTML;
					var totalprice = $('totalprice').innerHTML;
					var totalquantity = $('totalquantity').innerHTML;
					//$('addtocartmsg').innerHTML = "<br style='line-height:5px;'>" + cartresponse;
					$('addtocartmsg').innerHTML = "";
					$('dialogCartMsg').innerHTML = cartresponse;
					jQuery('.dialogCart').fadeOut('medium');
					jQuery('#dialogCart').fadeIn('medium');
					$('basketquantity').innerHTML = totalquantity;
					$('basketprice').innerHTML = totalprice;
				}
			}
		});
	}
}

function Prod_SingleAddToBasket(){
	initDialogCart();

	$('addtocartmsg').innerHTML = '<img src="images/ajax_indicator_sm.gif" />';
	new Ajax.Request('multiAddToBasket.taf', 
	{   
		parameters: $('productForm').serialize(true),
		onSuccess: function(transport) { 
			$('ajaxcartresponse').innerHTML = transport.responseText;
			
			var basketerror = $('basketerror');
			if (basketerror != null)
			{
				$('addtocartmsg').innerHTML = "";
				alert(basketerror.innerHTML);
			}
			else
			{
				var cartresponse = $('addmsg').innerHTML;
				var totalprice = $('totalprice').innerHTML;
				var totalquantity = $('totalquantity').innerHTML;
				//$('addtocartmsg').innerHTML = cartresponse;
				$('addtocartmsg').innerHTML = "";
				$('dialogCartMsg').innerHTML = cartresponse;
				jQuery('.dialogCart').fadeOut('medium');
				jQuery('#dialogCart').fadeIn('medium');
				$('basketquantity').innerHTML = totalquantity;
				$('basketprice').innerHTML = totalprice;
			}
		}
	});
}

function closeDialog(dialogId)
{
    jQuery('#'+dialogId).fadeOut('medium');
}

function initDialogCart()
{

	var myWidth = 0, myHeight = 0;
	//myWidth = windowSize()[0];
	if (document.all)
	{myWidth=document.body.clientWidth;}
	else
	{myWidth=window.innerWidth;}
	myHeight = windowSize()[1];
	var scrollpos;

	if (navigator.appVersion.search(/safari/i) == -1 && document.compatMode == 'CSS1Compat')
	{
		scrollpos = document.documentElement.scrollTop;
	}
	else
		scrollpos = document.body.scrollTop;
//alert(scrollpos);
	//document.getElementById('dialogCart').style.top = scrollpos + 200 + 'px';
	//document.getElementById('dialogCart').style.left = (myWidth / 2) + 'px';
//alert(document.getElementById('dialogCart').style.top);
	var curpos = getPosition();
	if (isNaN(curpos.x)){
		thex = mouseX;
		they = mouseY;
	}
	else{
		thex = curpos.x;
		they = curpos.y;
	}
//alert(scrollpos + ' ' + they);
	document.getElementById('dialogCart').style.top = ((scrollpos + 200) + (scrollpos + they)) / 2 + 'px';
	//alert(document.getElementById('dialogCart').style.top);
	var divwidth;
	if (document.all)
	{
		divwidth = $('dialogCart').style.width;
	}
	else
	{
		divwidth = jQuery('#dialogCart').css("width");
	}
	document.getElementById('dialogCart').style.left = (((myWidth / 2) + thex) / 2) - (300 / 2) + 'px';

}


function CheckValues() {
  var subtotal = 0;
  var GL_numRows = parseInt(document.getElementById('numrows').value);
  for (var k = 1; k <= GL_numRows; k++) {
    if(document.getElementById("Quantity" + k) != null && isNaN(document.getElementById("Quantity" + k).value)){
      alert("Invalid quantity");
      document.getElementById("Quantity" + k).select();
      document.getElementById("Quantity" + k).focus();
      return false;
    }
  }
  return true;
}

function Prod_SingleAddToBasketCheck(){
	if (document.getElementById('OP') != null && document.getElementById('OP').value == ""){
		alert('Please choose an option');
	}
	else
		Prod_SingleAddToBasket();
		
	return;
}

function Prod_AddOrOption(pid)
{
	doGrow();
	var t=setTimeout("Prod_AddOrOptionAjaxCall("+pid+")",growcountmax*growinterval*0.25)
}

function Prod_AddOrOptionAjaxCall(pid)
{
	new Ajax.Request('productPopUp.taf?pid='+pid, 
		{   
			onSuccess: function(transport) { 
				//var arr = transport.responseText.split("{!}");
				$('addoroption').innerHTML = transport.responseText;
				//$('addoroption').innerHTML = arr[0];
				//alert(arr[1]);
				var basketerror = $('basketerror');
				if (basketerror != null)
					alert(basketerror.innerHTML);
				else
				{
					var myWidth = 0, myHeight = 0;
					//myWidth = windowSize()[0];
					if (document.all)
					{myWidth=document.body.clientWidth;}
					else
					{myWidth=window.innerWidth;}
					myHeight = windowSize()[1];
					var scrollpos;

					if (navigator.appVersion.search(/safari/i) == -1 && document.compatMode == 'CSS1Compat')
					{
						scrollpos = document.documentElement.scrollTop;
					}
					else
						scrollpos = document.body.scrollTop;
					
					document.getElementById('addoroption').style.top = scrollpos + 100 + 'px';
					//alert (navigator.appVersion); 
					//alert(screen.availwidth);
					

					document.getElementById('addoroption').style.left = (myWidth / 2)-200 + 'px'; //myWidth*0.15 + 'px';
					document.getElementById('addoroption').style.display = 'block';
				}
			}
		});	
	var t = setTimeout("document.getElementById('expandingDiv').style.visibility = 'hidden';",growcountmax*growinterval*0.75);
}

function Prod_AddOrOptionClose()
{
	var ao = document.getElementById('addoroption');
	ao.style.display = 'none';
	ao.style.left = Math.round(windowSize()[0]*0.15);
	ao.style.top = Math.round(windowSize()[1]*0.15);
}

function Prod_QuickViewTab(tab)
{
	switch(tab)
	{
		case "options":
			document.getElementById('tabOverview').style.backgroundColor = '#ddd';
			document.getElementById('tabOptions').style.backgroundColor = 'white';
			document.getElementById('panelOverview').style.display = 'none';
			document.getElementById('panelOptions').style.display = 'block';
			break;
		case "overview":
			document.getElementById('tabOverview').style.backgroundColor = 'white';
			document.getElementById('tabOptions').style.backgroundColor = '#ddd';
			document.getElementById('panelOverview').style.display = 'block';
			document.getElementById('panelOptions').style.display = 'none';
			break;
	}
}

function showQuickView(obj,pid){
	var elementPosition = findPos(obj);
	var picLeft = elementPosition[0];
	var picTop = elementPosition[1];
	//alert(picLeft + ":" + picTop);
	var qv = document.getElementById('QuickViewLink');
	qv.style.left = picLeft+30 + 'px';
	qv.style.top = picTop+100 + 'px';
	qv.innerHTML = '<img style="cursor:pointer" src="images/QuickViewOff.png" onmouseover="javascript:enableQV(this,true);" onmouseout="javascript:enableQV(this,false);" onclick="javascript:Prod_AddOrOption('+pid+');" onMouseDown="mdQuickView(this)" onMouseUp="muQuickView(this)" />';
	qv.style.display = 'block';

}

function enableQV(obj,val)
{
	over = val;
	if (val){
		obj.src="images/QuickViewOn.png";
	}
	else{
		obj.src="images/QuickViewOff.png";
	}
}

function hideQuickView(obj){
	var curpos = getPosition();
	if (isNaN(curpos.x)){
		thex = mouseX + 2;
		they = mouseY + 2;
		thex2 = mouseX - 2;
		they2 = mouseY - 2;
	}
	else{
		thex = curpos.x;
		they = curpos.y;
		thex2 = thex - 2;
		they2 = they - 2;
	}
	var elementPosition = findPos(obj);
	var curleft = elementPosition[0];
	var curtop = elementPosition[1];
	//document.getElementById('nums').innerHTML = thex + ',' + curleft + '-' + they + ',' + curtop;
	
	if (thex > curleft + 160 || they > curtop + 160 || thex2 < curleft || they2 < curtop)
		document.getElementById('QuickViewLink').style.display = 'none';
}

function mdQuickView(obj)
{
	obj.src = "images/QuickViewClick.png";
}

function muQuickView(obj)
{
	obj.src = "images/QuickViewOn.png";
}

function toggleLogoTypePanel(tid)
{
	var panel = document.getElementById('row_'+tid);
	if (panel.style.display == 'none')
	{
		panel.style.display = '';
		document.getElementById('opener_'+tid).src = 'images/minus.gif';
	}
	else
	{
		panel.style.display = 'none';
		document.getElementById('opener_'+tid).src = 'images/plus.gif';
	}
}

function displayLogo(logoid)
{
	document.getElementById('logoimg_'+logoid).style.visibility = 'visible';
}