﻿    var pImage;
    var zImage;
    var mImage;
	var zContainer;
	var zBox;
	var zImageLoad;

	function onLoad2() {

	    $("div.photos a").lightBox({ containerBorderSize: 0, containerResizeSpeed: 200,heading: $("#name").attr("value") });
	    
		pImage = $('#productImage');
		zImage = $('.zoom-contaioner img');
		mImage = $('#productImage img');
		zContainer = $('.zoom-contaioner');
		zZoom = $('.detail-zoom')
		zBox = $('#zoomBox');
		btnTechDetail = $("li.details a")
		techBox = $(".detail-tech");
		techBoxContent = $(".detail-tech .content")
		//$(".items .colorBox:last").css("padding-right", "0px").css("padding-left", "0px");
        
		
		zBox.hide();
		zImage.hide();
		
		pImage.bind('mousemove',onMouseMove);
		pImage.hover(onMouseOver, onMouseOut);
		btnTechDetail.hover(techDetailOver,techDetailOut);

		$(".other-view").click(function() {
		    swapImage();
		});

    }
    
    function techDetailOver(e){
        techBox.show();
        
        if(techBoxContent.children("ul").length > 0){
            console.debug("hey");}
        else{
            var list = techBoxContent.text().split("_");
            console.debug("listi");
        
            var ul = $("<ul></ul>")
            var li; 
        
            $(list).each(function(i,n){
            
                    li = $("<li></li>").text($.trim(n));
                    ul.append(li);
                });
            
            techBoxContent.text("");
            techBoxContent.append(ul);
        }
    }
    
    function techDetailOut(e){
        techBox.hide();
    }

	
	function onMouseMove(e)
	{
		var my = e.pageY - pImage.offset().top - (zBox.height()/2);
		var mx = e.pageX - pImage.offset().left;
		
		setZboxPosition(mx,my);
		
	}

	function onMouseOver(e) {
	    zImage.hide();
		zImage.fadeIn(200);
		zBox.fadeIn(200);
		zZoom.show();
	}
	
	function onMouseOut(e)
	{	
		zBox.fadeOut(200);
		zImage.fadeOut(200);
		zZoom.hide();
	}
	
	function setZboxPosition(mx,my)
	{
		if(my > (0) && my < pImage.height() - (zBox.height()))
		{
			zBox.css('top',my );
		}			
		else
		{
			if(my < pImage.height()/2)
				zBox.css('top',0)
			else
			    zBox.css('top', pImage.height() - zBox.height());
		}


		if (mx > (0) && mx < pImage.width() - (zBox.width())) {
            zBox.css('left', mx-(zBox.width()/2));
        }
        else {
            if (mx < pImage.width() / 2)
                zBox.css('left', 0)
            else
                zBox.css('left', pImage.width() - zBox.width());
        }

        setZoomImagePosition(parseInt(zBox.css('top')) / pImage.height(), (parseInt(zBox.css('left'))+(zBox.width()/2)) / pImage.width())
	}
	
	function setZoomImagePosition(percentOfSize,percentOfSizeX) {
	    zImage.css('margin-top', -1 * ((percentOfSize * zImage.height())-(zContainer.height()/2)));
	    zImage.css('margin-left', -1 * ((percentOfSizeX * zImage.width())-(zContainer.width()/2)));
    }

    function swapImage() {

        var src = mImage.attr('src');
        if (src.indexOf('_b') > 0) {
            zImage.attr('src', zImage.attr('src').replace(/_b/i, ''));
            mImage.attr('src', mImage.attr('src').replace(/_b/i, ''));
            $(".other-view").html("View back");
        } else {
            var a = src.split(".");
            var newSrc = a[0] + "_b.jpg";
            var newSrcb = a[0] + "_b.jpg";
            mImage.attr('src', newSrc);
            zImage.attr('src', newSrcb.replace(/normal/i, 'large'));
            $(".other-view").html("View front");    
        }
    }


    function showTechInfo() {
        $("#techInfo a").lightBox({ containerBorderSize: 2, containerResizeSpeed: 200 });
        $("#techInfo a:first").click();
    }

    function morePhotos() {

        $("#morePhotos a").lightBox({ containerBorderSize: 2, containerResizeSpeed: 200 });
        $("#morePhotos a:first").click();
    }
    
    function showBuyNow()
    {
        if($(".store") != undefined)
        {
            $.get('../../stock.aspx?code='+$("#productCode").html(), function getlink(data)
            { 
                if(data != "")
                {
                    $("#buy-online-eu").css("display","");
                    $("#buy-online-eu").attr('href',data)
                }
            });        
        }
    }
    
    

    $(function() {
        onLoad2();
        showBuyNow();
    });
    
