// scripts for product pages
lbInitHeight = 200;
lbInitWidth = 200;
lbHeight = lbInitHeight
lbWidth = lbInitWidth;
var img, lightBox, lbContents, fadeBox, bod , htm, isIE;
// for Internet Explorer (using conditional comments)
/*@cc_on @*/
/*@if (@_win32)
document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
var script = document.getElementById("__ie_onload");
script.onreadystatechange = function() {
  if (this.readyState == "complete") {
    init(); // call the onload handler
  }
};
/*@end @*/
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      clearInterval(_timer);
      init(); // call the onload handler
    }
  }, 10);
}

function init(){
	if(document.getElementById('lightBox'))
		lightBox = document.getElementById('lightBox');
	
	if(document.getElementById('lbContents'))
		lbContents = document.getElementById('lbContents');
	
	if(document.getElementById('product-image'))
		img = document.getElementById('product-image');
	
	fadeBox = document.getElementById('black_overlay');
	bod = document.getElementsByTagName('body')[0];
	htm = document.getElementsByTagName('html')[0];
	
	isIE = isIE();
}


function isIE()
{
      return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
	  
function swapImage(thumb){
	/*img.src = "/images/loader-bar.gif";
	newImg = new Image();
	newImg.src = thumb.getAttribute("image");
	
	newImg.onload = function(){
		img.src = thumb.getAttribute("image");
		if(thumb.getAttribute("zoom"))
			img.setAttribute("zoom", thumb.getAttribute("zoom"))
		else
				img.setAttribute("zoom", "")
	}*/
	img.src = thumb.getAttribute("image");
	
		
	if(isIE){
		img.src = thumb.getAttribute("image");
	} else {
		img.src = "/images/loader-bar.gif";
		newImg = new Image();
		newImg.src = thumb.getAttribute("image");
		newImg.onload = function(){
			img.src = thumb.getAttribute("image");
			}
	}
	if(thumb.getAttribute("zoom"))
		img.setAttribute("zoom", thumb.getAttribute("zoom"))
	else
		img.setAttribute("zoom", "")
}

function zoomImage(){
	

	
	
	imgSrc = img.src;
	
	if(img.getAttribute("zoom"))
		imgSrc = img.getAttribute("zoom");
	
	zoomImg = new Image();
		zoomImg.src = imgSrc;
		
	if(isIE){
		lbContents.innerHTML ='<img src="'+imgSrc+'" alt="" />';
		lbWidth = zoomImg.width + 12;
		lbHeight = zoomImg.height + 30;
		showLightBox();
	} else {
		
		
		showLightBox();
		
		
		zoomImg.onload = function(){
			lbWidth = zoomImg.width + 12;
			lbHeight = zoomImg.height + 30;
			lbContents.innerHTML ='<img src="'+imgSrc+'" alt="" />';
			resizeLightBox();
		}
	}
}
function resizeLightBox(){

	scrollXY = getScrollXY();
	scrollX = scrollXY[0];
	scrollY = scrollXY[1];
	winHeight = document.documentElement.clientHeight;
	winWidth = document.documentElement.clientWidth;
	
	//Position the lightbox
	lbTop = ((winHeight - lbHeight) /2) + scrollY;
	lbTopLeft = ((winWidth-lbWidth) /2);
	lightBox.style.top = lbTop+"px";
	lightBox.style.left = lbTopLeft+"px";
	lightBox.style.width = lbWidth+"px";
	lightBox.style.height = lbHeight+"px";
}
function showLightBox(){


	
	lbClose = lightBox.getElementsByTagName('span')[0];
	lbClose.onclick = function(){ hideLightBox(); } ;

	//Prevent scrolling while lightbox is showing
	bod.style.overflow = "hidden";
	htm.style.overflow = "hidden"; 
	
	winHeight = document.documentElement.clientHeight;
	winWidth = document.documentElement.clientWidth;
	
	scrollXY = getScrollXY();
	scrollX = scrollXY[0];
	scrollY = scrollXY[1];
	
	//Position the Background fade
	fadeBox.style.top = scrollY+"px";	
	fadeBox.style.height = winHeight+"px";
	fadeBox.style.width = winWidth+"px";
	fadeBox.style.display = "block";
	fadeBox.onclick = function(){ hideLightBox(); } ;
	
	
	lightBox.style.display = "block";
	

	
	//Position the lightbox
	lbTop = ((winHeight - lbHeight) /2) + scrollY;
	lbTopLeft = ((winWidth-lbWidth) /2);
	lightBox.style.top = lbTop+"px";
	lightBox.style.left = lbTopLeft+"px";
	lightBox.style.width = lbWidth+"px";
	lightBox.style.height = lbHeight+"px";
	
}
function hideLightBox(){
	bod = document.getElementsByTagName('body')[0];
	htm = document.getElementsByTagName('html')[0];
	lightBox = document.getElementById('lightBox');
	lbContents = document.getElementById('lbContents');
	fadeBox = document.getElementById('black_overlay');
	
	lbContents.innerHTML = '<img src="/images/ajax-loader.gif" alt="loading" class="lbLoad" />';
	lightBox.style.display = "none";
	fadeBox.style.display = "none";
	bod.style.overflow = "auto";
	htm.style.overflow = "auto"; 
	fadeBox.style.height = lbInitHeight+"px";
	fadeBox.style.width = lbInitWidth+"px";
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



function showFlashPrima(){
	swfFile = 'toyboxv7.swf';
	newHTML = '<object width="550" height="440" data="/swf/'+swfFile+'" type="application/x-shockwave-flash"><param value="/swf/'+swfFile+'" name="movie"/></object>';	
	lbContents.innerHTML = newHTML;
	lbWidth = 562;
	lbHeight = 470;
	showLightBox();
}






	$(function(){
		$("#toyboxSlideShowLnk").click(function(){
			$("#lbContents").html('<object width="580" height="580" align="middle" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"><param value="sameDomain" name="allowScriptAccess"><param value="false" name="allowFullScreen"><param value="/swf/tb_slideshow.swf" name="movie"><param value="high" name="quality"><param value="#fff" name="bgcolor"><embed width="580" height="580" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="false" allowscriptaccess="sameDomain" name="lilly" bgcolor="#fff" quality="high" src="/swf/tb_slideshow.swf"></object>');
			$("#lightBox").show();
			$("#lightBox").css("width","600px");
			$("#lightBox").css("height","600px");
			$("#lightBox").css("border","1px solid #000");
			$(".closeLB").click(function(){$("#lightBox").hide();});
			
			$("#lightBox").center();
		
		});
	});
	
	
	/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

		var options = {

			vertical: true,
			horizontal: true

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();
		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width"));
		var borderBottom = parseInt($self.css("border-bottom-width"));
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if(op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if(op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if(positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);


   });

};