$(document).ready(function(){

	$("a.squarebox, a.thickbox").each(function(){
		$(this).click(function(){
      var x = $(this).is(".robot");
      if(x==1) var lien = $(this).attr("myHref");
			else var lien = $(this).attr("href");
			return ShowPopFrame(lien);
		}).attr('onclick','return false;');
	});

});

var HeightIframe,WidthIframe,PopFrame=0,IframeID,OverHtml,OverHtmlX,OverHtmlY;
var IMGWAIT = "shop/images/loadingAnimation.gif";

function getAttribute(url,key){
	var attr, searchVar;
	if(url.indexOf("&"+key+"=")!==-1) searchVar="&"+key+"=";
	else if(url.indexOf("?"+key+"=")!==-1) searchVar="?"+key+"=";
	else return null;
	attr = url.substring( url.indexOf(searchVar)+searchVar.length )
	if(attr.indexOf("&")!==-1) attr = attr.substr(0, attr.indexOf("&"));
	return attr;
}

function ShowPopFrame(url){
  OverHtmlX = $("html").css('overflow-x');
	var numRand=Math.round(Math.random(0,5000000000000)*100000000000000000);
	IframeID="SquareIframe"+numRand;
	HeightIframe = getAttribute(url,"height");
	WidthIframe = getAttribute(url,"width");
	if(HeightIframe!=null && WidthIframe!=null){
		$("body").append('<div id="SquareBackground"></div><iframe id="'+IframeID+'" name="'+IframeID+'" src="'+url+'" style="display: none" onload="ShowIframe()" frameborder="0"></iframe><div id="SquareWait"></div>');
		$("#SquareBackground").css({position:'absolute',top:0,left:0,overflow:'hidden','z-index':1000,background:'#000','-moz-opacity':'0.65','opacity':'0.65','filter':'Alpha(Opacity=65)'}).click(HidePopFrame);
		$("#"+IframeID).css({position:'absolute',display:'none',overflow:'auto','z-index':1001,border:'none',background:'#fff'});
		$("#SquareWait").html('<img src="'+IMGWAIT+'" id="SquareWaitImg" alt="wait" />');
		PopFrame=1;	ResizePopFrame(1); setTimeout( SetScrollFrame, 500 ); var j=jQuery.browser; if(j.safari||(!j.safari&&!j.opera&&!j.msie&&!j.mozilla)) { ShowIframe(); }
		$(document).keyup( function(e){ var key = e.keyCode; if(key == 27) HidePopFrame(); } );
	}
	return false;
}

function ShowIframe(){
	$("#"+IframeID).fadeIn(400);
	$("#SquareWait").remove();
	SetScrollFrame();
}

function HidePopFrame(){
	$("#SquareBackground").remove(); $("#"+IframeID).remove();
	if( $("#SquareWait") ) $("#SquareWait").remove();
	PopFrame=0; $("html").css({'overflow-x':OverHtmlX});
}

function ResizePopFrame(mode){
	if(PopFrame==1){
		$("html").css({'overflow-x':'hidden'});
		var h=$(document).height()+35; var w=$(document).width();
		if(!jQuery.browser.safari) w+=25;
		if(mode==1){
			var w1 = $("#SquareWaitImg").height(); var w2 = $("#SquareWaitImg").width();
			w1 = (Number(h)-Number(w1))/2; w2 = (Number(w)-Number(w2))/2;
			$("#SquareWait").css({position:'absolute',top:w1+'px',left:w2+'px',width:'auto',height:'auto','z-index':1100});
		}
		var Top = (Number(h)-Number(HeightIframe))/2;
		var Left = (Number(w)-Number(WidthIframe))/2;
		$("#SquareBackground").css({width:w+'px',height:h+'px'});
		$("#"+IframeID).css({width:WidthIframe+'px',height:HeightIframe+'px',top:Top+'px',left:Left+'px'});
	}
}

function SetScrollFrame(){
	if(PopFrame==1){
		var pos=ScrollXY(); var h=pos[2]; var w=pos[3];
		setTimeout(function(){
      var Top = (Number(h)-Number(HeightIframe))/2; var Left = (Number(w)-Number(WidthIframe))/2;
  		$("#"+IframeID).css({top: pos[1]+Top+'px', left: pos[0]+Left+'px'});
  		var w1 = $("#SquareWaitImg").height(); var w2 = $("#SquareWaitImg").width();
  		w1 = (Number(h)-Number(w1))/2; w2 = (Number(w)-Number(w2))/2;
  		$("#SquareWait").css({top: pos[1]+w1+'px', left: pos[0]+w2+'px'});
    },50);
	}
}


function ScrollXY(){
	var yScrolltop; var xScrollleft; var yHeight; var xWidth;
	if (self.pageYOffset || self.pageXOffset) { // Firefox & other gecko
	  yScrolltop = self.pageYOffset;
	  xScrollleft = self.pageXOffset;
	  if(yScrolltop==0){ $("html,body").animate({scrollTop: 1},5); }
	  yHeight = window.innerHeight;
	  xWidth = window.innerWidth;
	} else if(document.documentElement&& document.documentElement.scrollTop
	  || document.documentElement.scrollLeft ){ // I Explorer 6 Strict
	  yScrolltop = document.documentElement.scrollTop;
	  xScrollleft = document.documentElement.scrollLeft;
	  if(yScrolltop==0){ $("html,body").animate({scrollTop: 1},5); }
    yHeight = document.documentElement.clientHeight;
    xWidth = document.documentElement.clientWidth;
	} else if (document.body) { // all other I Explorers
	  yScrolltop = document.body.scrollTop;
	  xScrollleft = document.body.scrollLeft;
	  if(yScrolltop==0){ $("html,body").animate({scrollTop: 1},5); }
	  yHeight = document.body.clientHeight;
	  xWidth = document.body.clientWidth;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop,yHeight,xWidth);
	return arrayPageScroll;
}


window.onresize = ResizePopFrame;
window.onscroll = SetScrollFrame;

