var btnAddToCart;
var btnAddToCartDisabled;
var additionalArray;
var onZoomButton = -1;
var reqZoom;
var zooming = -1;
var documentLoaded = false;

//////////////////////////////////////////// Additional Photos ////////////////////////////////////////////

function ShowLargePhotoZoom(imgID, imagePath, filename)
{

	cmbColour = document.getElementById("ctl00_cphMain_ctl00_ucQuickZoom_ctl00_cphMain_ctl00_cmbColours");
	cmbSize = document.getElementById("ctl00_cphMain_ctl00_ucQuickZoom_ctl00_cphMain_ctl00_cmbSizes");

	// Loop ALL products so as to change the colour drop down
	for ( i = 0; i < additionalArray.length; i++)
	{

		// Get Product
		product = additionalArray[i];

		// If product's large image is the large image I want to show, set the drop down selectedIndex		
		if ( product.largeImage == filename )
		{
		
			selectedColour = product.colour;
		
			// Loop ALL colours to select the one
			for ( i = 0; i < cmbColour.options.length; i++)
			{	
				if ( cmbColour.options[i].value == product.colour )
				{
					cmbColour.selectedIndex = i;
					break;
				}
			}				
		
			break;
		}
		
	}

	$("#" + imgID).ImageSwitch({Type:"FlyZoom", Speed:1500, StartLeft: 150, NewImage:imagePath}); 
	SizeChanged(cmbSize.options[0].value, selectedColour);

}


function ShowAdditionalPhotosZoom(addArr)
{

	websiteLanguage = "ru-RU";

	try
	{
		if ( addArr == null )
			addArr = additionalArray;
		
		if ( addArr == null )
		    return;
	}

	catch(err)
	{    
		alert(err);    
		return;
	}
	
	additionalArray = addArr;
    
	divMainPhotoClientID = "ctl00_cphMain_ctl00_ucQuickZoom_ctl00_cphMain_ctl00_imgMainPhoto";

	$("#imgAdditional1").attr("src", additionalArray[additionalIndex]["smallImage"]);
	
	$("#imgAdditional1").click
	(
		function()
		{
			ShowLargePhotoZoom(divMainPhotoClientID, additionalArray[additionalIndex]["mediaPath"] + additionalArray[additionalIndex]["largeImage"], additionalArray[additionalIndex]["largeImage"]); return false;
		}
	)
	
	if ( additionalLength > 0 )
	{
	
		$("#imgAdditional2").attr("src", additionalArray[parseInt(additionalIndex)+1]["smallImage"]);

		$("#imgAdditional2").click		
		(
			function()
			{
				ShowLargePhotoZoom(divMainPhotoClientID, additionalArray[parseInt(additionalIndex)+1]["mediaPath"] + additionalArray[parseInt(additionalIndex)+1]["largeImage"], additionalArray[parseInt(additionalIndex)+1]["largeImage"]); return false;
			}
		)
		
	}
	else
		$("#imgAdditional2").attr("src", "/products/images/emptySmall_" + websiteLanguage + ".gif");
	
	if ( additionalLength > 1 )
	{

		$("#imgAdditional3").attr("src", additionalArray[parseInt(additionalIndex)+2]["smallImage"]);

		$("#imgAdditional3").click
		(
			function()
			{
				ShowLargePhotoZoom(divMainPhotoClientID, additionalArray[parseInt(additionalIndex)+2]["mediaPath"] + additionalArray[parseInt(additionalIndex)+2]["largeImage"], additionalArray[parseInt(additionalIndex)+2]["largeImage"]); return false;
			}
		)	
	}
	else
		$("#imgAdditional3").attr("src", "/products/images/emptySmall_" + websiteLanguage + ".gif");

}


function ShowAdditionalPreviousZoom()
{
	if ( additionalIndex != 0 )
	{
		additionalIndex--;
		ShowAdditionalPhotosZoom();
	}
}


function ShowAdditionalNextZoom()
{
    
	if ( (additionalLength-additionalIndex) > 2 )
		additionalIndex++;
	else
		additionalIndex = 0;
		
	ShowAdditionalPhotosZoom();
}


////////////////////////////////////////////////// Cart //////////////////////////////////////////////////

function UpdateCartTotal()
{

	if ( jQuery.browser.mozilla )
		var currentTotal = document.getElementById("spanItemsInCart").textContent;
	else
		var currentTotal = document.getElementById("spanItemsInCart").innerText;
	
	currentTotal++;
	document.getElementById("spanItemsInCart").innerText = currentTotal;
	document.getElementById("spanItemsInCart").textContent = currentTotal;
	document.getElementById("ctl00_cphMain_ctl00_ucQuickZoom_btnCloseWindow").style.display = "block";
	document.getElementById("addToCartLoader").style.display="none";

}


////////////////////////////////////////////////// Zoom //////////////////////////////////////////////////

function getMouseXY(evt) 
{
	var tooltip = document.getElementById('iframeZoomBox');
	standardbody=document.body;
	mouseX = (ns5)? evt.pageX: window.event.clientX + standardbody.scrollLeft;
	mouseY = (ns5)? evt.pageY: window.event.clientY + standardbody.scrollTop;    
	winWd = (ns5)? window.innerWidth-20+window.pageXOffset: standardbody.clientWidth+standardbody.scrollLeft;
	winWd = 905+(winWd - 905)/2;
	winHt = (ns5)? window.innerHeight-20+window.pageYOffset: document.documentElement.clientHeight+document.documentElement.scrollTop;
}


function PositionIFrame(iframeCtrl)
{
	iframeCtrl.style.top = "130px";
	iframeCtrl.style.left = "387px";
}

var hideZoomTimer;
var hideZoomProductId;

var hideZoomTimer;
var hideZoomProductId;

function InitializeZoomButton(productId,documentId,price)
{
    if(!documentLoaded)
	{
		setTimeout("InitializeZoomButton('"+productId+"','"+documentId+"','"+price+"')",100)
	}
	else
	{
		var zoomBtnWrapper = document.getElementById('Zoom_'+productId);
		var zoomBtn=null;
		var i=0;
		
		for(i=0;i<zoomBtnWrapper.childNodes.length;i++)
		{
			var child = zoomBtnWrapper.childNodes[i];
			if(child.tagName!=null && child.tagName.toLowerCase()=="input")
			{
				zoomBtn=child;
				break;
			}
		}
		
		zoomBtnWrapper.style.display='';
		
		zoomBtn.onmouseover= function()
		{
			this.className='ZoomButtonOn';
			onZoomButton=productId;
		}

		zoomBtn.onmouseout= function()
		{
			this.className='ZoomButton';		
			onZoomButton=-1;
		}
	}
		
    
}


function HideZoomButton(productId)
{	
    setTimeout("if(onZoomButton!="+productId+"){document.getElementById('Zoom_"+productId+"').style.display='none';}",100);
}


function HideUpdatePanel()
{
	document.getElementById('divQuickZoomContainer').style.display='none';
	document.getElementById("ctl00_cphMain_ctl00_upnlQuickZoom").style.display="none";
	document.getElementById("divQuickZoomLoadingContainer").style.display="none";
	window.onresize = function(event) {return;}
	window.onscroll = function(event) {return;}
	zooming=-1;
}


function RepositionUpdatePanel(productId)
{    
    
    if(zooming!=productId)
        HideUpdatePanel();
    
    PositionUpdatePanel(productId);
    window.onresize = function(event) 
    {    
        RepositionUpdatePanel(productId);
    }
    window.onscroll = function(event) 
    {    
        RepositionUpdatePanel(productId);
    }
        
}


function PositionUpdatePanel(productId)
{
    
	var upnlQuickZoom = document.getElementById('ctl00_cphMain_ctl00_upnlQuickZoom');
	var loading = false;
	
	if(upnlQuickZoom.style.display=="none")
	{
	    upnlQuickZoom = document.getElementById('divQuickZoomLoadingContainer');
	    upnlQuickZoom.style.zIndex="1000";
	    loading = true;
	}
	
	var ratio = 0.380;
	var windowWidth = 0; 
	var windowHeight = 0;

	var scrollTop = document.documentElement.scrollTop;

	if(scrollTop==0)
		scrollTop = document.body.scrollTop; //-- Chrome/Safari --//

	if(IE)
	{
		ratio=0.375;        
		windowWidth = document.documentElement.offsetWidth;
		windowHeight = document.documentElement.offsetHeight;
	}
	else
	{
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}

	if (windowWidth>=1200)
		ratio+=0.02;

	upnlQuickZoom.style.position="absolute";    
	upnlQuickZoom.style.top=((windowHeight-535)/2+scrollTop)+'px';;
	upnlQuickZoom.style.left=(ratio*windowWidth)+'px';       

	if ( zooming != productId )
	{
		if (!loading)
		{
			$("#divQuickZoomContainer").fadeIn('fast');
		}
		else
		{
			$("#divQuickZoomLoadingContainer").fadeIn('fast');
		}
	}

	zooming=productId;
    
}


function ShowLoadingZoom(productId)
{
    RepositionUpdatePanel(productId);
    document.getElementById("zoomLoading").style.display="";
    
    $("#divQuickZoomLoadingContainer").fadeIn('fast');    
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endZoomReq);    
}


function endZoomReq(sender, args) 
{
    if (args.get_error() != undefined)
    {
        args.set_errorHandled(true);
        return;        
    }
    RepositionUpdatePanel(zooming);
}


function AddToCartClick(ctrl)
{
    ctrl.style.display="none";
    document.getElementById("addToCartLoader").style.display="";
}


function ToggleAddToCart(isEnabled)
{        
    if(isEnabled)
    {        
        btnAddToCart.style.display="";
        btnAddToCartDisabled.style.display="none";        
    }
    else
    {
        btnAddToCart.style.display="none";
        btnAddToCartDisabled.style.display="";
    }
}