 /* Greybox Redux
 * Required: http://jquery.com/
 * Written by: John Resig
 * Based on code by: 4mir Salihefendic (http://amix.dk)
 * License: LGPL (read more in LGPL.txt)
 */

var GB_DONE = false;
var GB_DONE_ZOOM = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_LOOKBOOK_HEIGHT = 0;

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
  	
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function GB_show(caption, url, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img class='closewin' src='../pix/quickorder/close.gif' alt='Close window'/></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_show_ATB(caption, url, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "</div>");
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_show_zoom(caption, url, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE_ZOOM) {
    $(document.body)
      .append("<div id='GB_overlay_zoom'></div><div id='GB_window_zoom'><div id='GB_caption_zoom'></div>"
        + "</div>");
    $(window).resize(GB_position_zoom);
    GB_DONE_ZOOM = true;
  }

  $("#GB_frame_zoom").remove();
  $("#GB_window_zoom").append("<iframe id='GB_frame_zoom' src='"+url+"'></iframe>");

  $("#GB_caption_zoom").html(caption);
  $("#GB_overlay_zoom").show();
  GB_position_zoom();

  if(GB_ANIMATION)
    $("#GB_window_zoom").slideDown("slow");
  else
    $("#GB_window_zoom").show();
}

function GB_show_in_parent(caption, url, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE) {
    $(parent.document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img class='closewin' src='../pix/quickorder/close.gif' alt='Close window'/></div>");
    $("#GB_window img").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append("<iframe id='GB_frame' src='"+url+"'></iframe>");

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}

function GB_showCustom(caption, html, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'></div>"
        + "<img class='closewin' src='../pix/productdisplay/close.gif' alt='Close window'/></div>");
    $("#GB_window img.closewin").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $("#GB_window img.closewin").click(pdoptions_show);
    $("#GB_overlay").click(pdoptions_show);
    $(window).resize(GB_position);
    GB_DONE = true;
  }

  $("#GB_frame").remove();
  $("#GB_window").append(html);

  $("#GB_caption").html(caption);
  $("#GB_overlay").show();
  GB_position();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}


function GB_showCustomHotlist(caption, html, height, width) {
  GB_HEIGHT = height  
  GB_WIDTH = width 
  if(!GB_DONE) {
    $(document.body)
      .append("<div id='GB_overlay'></div><div id='GB_window'><div id='GB_caption'>test</div>"
        + "</div>");
        
    $("#GB_window img.closewin").click(GB_hide);
    $("#GB_overlay").click(GB_hide);
    $(window).resize(GB_position);
    GB_DONE = true;
    $("#GB_overlay").click(uncheckForAdd);
    $("#addPopUpCloseWin a").click(uncheckForAdd);
  }

  $("#GB_frame").remove();
  
  $("#addedToBagBox").remove();
  $("#addToBagBox").remove();
  $("#GB_caption").html(caption); //Header
  $("#GB_window").append(html);
  $("#GB_overlay").show();
  GB_position();
  
  $("#GB_window").show();

  if(GB_ANIMATION)
    $("#GB_window").slideDown("slow");
  else
    $("#GB_window").show();
}



function GB_hide() {
  $("#GB_window,#GB_overlay").hide();
}

function pdoptions_show() {
  $("#productdisplayoptions").show();
}

function GB_hide_zoom() {
  $("#GB_window_zoom,#GB_overlay_zoom").hide();
}

function GB_position() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  var viewportOffset;
  var overlayExpand;
  
  //Ignore the centering of the popup if the client viewport is to small.
  //This prevents the bottom of the popup from beeing outside the screen.
  if (h < 700) 
    h = GB_HEIGHT + GB_LOOKBOOK_HEIGHT;
  
   if (typeof window.pageYOffset == "number") {
     viewportOffset = window.pageYOffset;
   } else {
    if (typeof de.scrollTop == "number")
      viewportOffset = de.scrollTop;
   }

   overlayExpand = getPageSizeWithScroll()[1];


  //Make sure the overlay covers the whole page including the scroll area!
  
    if (window.scrollMaxY == 0)
    {
        $("#GB_overlay").css({top: "0px"});
    }
    else
    {
        $("#GB_overlay").css({top: "0px", height: overlayExpand });
    }

  
  $("#GB_window").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px", top: ((h - GB_HEIGHT - GB_LOOKBOOK_HEIGHT)/2) + (viewportOffset + 5) + "px"});
  $("#GB_frame").css("height",GB_HEIGHT - 3 +"px");
}

function GB_position_zoom() {
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  var viewportOffset;
  var overlayExpand;
  
  //Ignore the centering of the popup if the client viewport is to small.
  //This prevents the bottom of the popup from beeing outside the screen.
  if (h < 700) 
    h = GB_HEIGHT + GB_LOOKBOOK_HEIGHT;
  
   if (typeof window.pageYOffset == "number") {
     viewportOffset = window.pageYOffset;
   } else {
    if (typeof de.scrollTop == "number")
      viewportOffset = de.scrollTop;
   }

   overlayExpand = getPageSizeWithScroll()[1];

  //Make sure the overlay covers the whole page including the scroll area!
  $("#GB_overlay_zoom").css({top: "0px", height: overlayExpand });
  
  $("#GB_window_zoom").css({width:GB_WIDTH+"px",height:GB_HEIGHT+"px",
    left: ((w - GB_WIDTH)/2)+"px", top: ((h - GB_HEIGHT - GB_LOOKBOOK_HEIGHT)/2) + (viewportOffset + 5) + "px"});
  $("#GB_frame_zoom").css("height",GB_HEIGHT - 3 +"px");
}
