﻿var MouseX, MouseY;
var ClickedX, ClickedY;

var scrollpos = function() {
	  return window.pageYOffset
	  || document.documentElement && document.documentElement.scrollTop
	  ||  document.body.scrollTop;
	   }
	   
	   
		jQuery(document).ready(function(){
		   jQuery().mousemove(function(e){
		   		MouseX=e.pageX;
		   		MouseY=e.pageY;
		   }); 
		})
		
		
	function ShowMe(x, tit, w, h) {
		ClickedX=MouseX
		ClickedY=MouseY

        //Default window size
		if (w == null) w=800;
		if (h == null) h=620;
		
		var ww=jQuery(window).width()
		var wh=jQuery(window).height()
		
		//End position left
		var l = (ww  / 2) - (w /2);
		if ((l+w) > ww) l=1;
		
		//End position top
		var t= ((wh / 2) - (h /2)) +scrollpos();
		if ((t+h) > (wh+scrollpos())) t=(scrollpos() + 20);
		//alert("scroll:" + scrollpos() + ", t:" + t + ", h:" + h + ", wh:" + wh);
		
		//Start position
		jQuery('#l360').css({"top":ClickedY,"left":ClickedX});
		
		
		if (x.indexOf('.aspx') > -1) {
		    jQuery("#titlebar360").html("<b>Logga in</b>");
            jQuery("#l360").animate({width:w, height:h, top:t, left:l, opacity: 1}, 800, function(){jQuery('#ifr360').attr("height", jQuery('#l360').height() -20)});
            ShowLogin(x, w, h);
	    }
	    //Load image and set window size
	    else if (x.indexOf('.jpg') > -1) {
	        jQuery(function () {
            var img = new Image();
            img.src=x;
                jQuery(img).load(function () {
                		w=img.width+10;
                    h=img.height+10;
                    jQuery('#ifr360').attr("height", h);
						        jQuery('#ifr360').attr("width", w);
						        jQuery('#ifr360').css("height", h);
						        jQuery('#ifr360').css("width", w);
						        
						        w+=30;
						        h+=30;
                     jQuery("#titlebar360").html("<b>" + tit + "</b>");
                     jQuery("#l360").animate({width:w, height:h, top:t, left:l, opacity: 1}, 800, function(){jQuery('#ifr360').attr("height", jQuery('#l360').height() -20)});
	                ShowImg(x);
                })
            });
	    }
	   //Cad viewer, default size
	    else {
	        jQuery("#titlebar360").html("<b>" + tit + "</b>" + " 360º");
	        jQuery("#l360").animate({width:w, height:h, top:t, left:l, opacity: 1}, 800);
	        Show360(x);
	    }

	}
	
	function ShowImg(file) {
	  jQuery('#loading360').css("visibility", "visible");
		jQuery('#l360').css("visibility", "visible");
		jQuery('#ifr360').attr("src", file);
		jQuery('#ifr360').load(function() 
		{
			jQuery('#loading360').css("visibility", "hidden");
		}
		)
	}
	
	
	
	function Show360(file) {
		jQuery('#loading360').css("visibility", "visible");
		jQuery('#l360').css("visibility", "visible");
		jQuery('#ifr360').attr("height", 600);
        jQuery('#ifr360').attr("width", 800);
        jQuery('#ifr360').css("height", 600);
        jQuery('#ifr360').css("width", 800);
		jQuery('#ifr360').attr("src", "http://freewheel.autodesk.com/dwf.aspx?path=http%3A%2F%2Fwww.ilabcontainer.se%2Fuserfiles%2Ffile%2Fdwf%2F" + file);
		jQuery('#ifr360').load(function() 
		{
			jQuery('#loading360').css("visibility", "hidden");
			//jQuery('#ifr360').css("height", "96%");
		}
		)
	}

	
	function ShowLogin(x, w, h) {
		jQuery('#loading360').css("visibility", "visible");
		jQuery('#l360').css("visibility", "visible");
		jQuery('#ifr360').attr("height", h-25);
    jQuery('#ifr360').attr("width", w);
    jQuery('#ifr360').css("height", h-25);
    jQuery('#ifr360').css("width", w);
		jQuery('#ifr360').attr("src", x);
		jQuery('#ifr360').load(function() 
		{
		    jQuery('#loading360').css("visibility", "hidden");
		}
		)
	}
	

	function Hide360() {
	    jQuery("#l360").animate({width:8, height:8, opacity: 0.0, left:ClickedX, top:ClickedY}, 500, function() {
			jQuery("#l360").css("visibility", "hidden");
			jQuery("#ifr360").attr("src", "about:blank");
			jQuery('#ifr360').attr("height", "0");
      jQuery('#ifr360').attr("width", "0");
      jQuery('#ifr360').css("height", "0");
      jQuery('#ifr360').css("width", "0");
			}	
		)
	}
	
	function InitLogin() {
	    ShowMe("/LoggaIn.aspx", "Logga in", 280, 140);
	}
	
	function LogOut() {
        self.location="/Default.aspx?LogOut=1";
	}
	
	function GetTrolley() {
	    jQuery('#offer_request').load('/trolley.aspx');
	}
	
	function OfferRequest(x) {
	    jQuery('#offer_request').load('/trolley.aspx?ID=' +x, function() {
	        FlashTrolley();
	    });
	    
	}
	
	function FlashTrolley() {
	    scrollTo(1,1);

	    jQuery('#offer_request #second').css('backgroundColor', '#ffdc5a');
	    setTimeout("jQuery('#offer_request #second').css('backgroundColor', '#ffffff')", 200);
	    setTimeout("jQuery('#offer_request #second').css('backgroundColor', '#ffdc5a')", 400);
	    setTimeout("jQuery('#offer_request #second').css('backgroundColor', '#ffffff')", 600);
	}
	
	$(document).ready(function () {
        GetTrolley();
	 });

