// JavaScript Document
var browser=navigator.userAgent.toLowerCase();
var pad = "1px";
var leftpd = "-1px";
var quickbuttpos = "2px 2px 2px 2px";
if(browser.indexOf('chrome') > -1 || browser.indexOf('safari') > -1)
{
	pad = "8px";
	leftpd = "-8px";
	quickbuttpos = "12px -0px -0px 8px";
}
	
$(window).load(function()  {

		
		var itemPictures = jQuery(".itemPicture");
		for(var i = 0; i < itemPictures.length; i++) {
		    var pic = itemPictures[i];
		    var img = jQuery(pic).find(".itemDetailImage");
		    var asin = jQuery(pic).attr("id");
		    attachButton(pic, asin);
			
		}
		setLarge();
});

function showView(id)
{
	var smimg_height = (jQuery("#img"+id).height());
	var smimg_width = (jQuery("#img"+id).width());
	var smimage_pos = (jQuery("#img"+id).position());
	var leftPad = 0;
	
	
	/// CALCULATE CENTER OF IMAGE FOR PLACEMENT OF QUICKVIEW BUTTON (horizontal)
	leftpad = (smimg_width - 104)/2;	
	
	var browser=navigator.userAgent.toLowerCase();
	if(browser.indexOf('chrome') > -1 || browser.indexOf('safari') > -1)
	{
		jQuery("#spn_"+id).css({top: smimage_pos.top + (smimg_height/2) - 30 , height: 36, left: smimage_pos.left + leftpad - 4});
	}
	else if ($.browser.msie)
	{
		jQuery("#spn_"+id).css({top: smimage_pos.top + (smimg_height/2) - 20 , height: 36, left: smimage_pos.left + leftpad + 1});
	}
	else
	{
		jQuery("#spn_"+id).css({top: smimage_pos.top + (smimg_height/2) - 20 , height: 36, left: smimage_pos.left + leftpad});
	}
}
function setLarge() {
			$('.quickViewButton').fancyZoom({scaleImg: true, closeOnClick: true,directory:'../../images_bw/easyfinder/bw_zoom'});
			
		}

/// This inserts the "Quick View" button over the small product image 
function attachButton(pic, asin) {

	var ele = "#img"+asin;
	var pos = $(ele).height();
	var clickMe = jQuery("<div onmouseover='setCursor(this);'></div>").addClass("quickViewButton").attr("id", "spn_"+asin);
	var buttonUrl = '<img src="../../images_bw/easyfinder/btn_quickView_new.gif" style="padding: '+quickbuttpos+';" />';
	clickMe.append(buttonUrl).click(prevent);
	jQuery(pic).children("a").append(clickMe);
}

/// Prevents default action of href when clicking "Quick View" button and sets restore value
function prevent(event)
{
	event.preventDefault();
	var asin = this.id;
	var theID = document.getElementById('hdn_restore').value;
	
	if(theID != "")
	{
		document.getElementById(theID).style.visibility = '';

		jQuery("#"+theID).addClass("quickViewButton");
	}
	
	document.getElementById('hdn_restore').value = asin;
	theID = document.getElementById('hdn_restore').value;

	document.getElementById(theID).style.visibility = 'hidden';

	//jQuery("#"+theID).removeClass("quickViewButton");
}

function setCursor(obj)
{	
	obj.style.cursor = "url('../../images_bw/popbox/magplus.cur'), pointer";
}

function setMinus(obj)
{
	obj.style.cursor = "url('../../images_bw/popbox/magminus.cur'), pointer";
}

/// Action for detail page
function showDetail(loc)
{
	jQuery("#zoom").remove();
	$('a').click(function(e) { e.preventDefault(); });
	window.location.href=loc;
	return false;
}


function Browser() {
 
  var ua, s, i;
 
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
 
  ua = navigator.userAgent;
 
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
 
  // Treat any other "Gecko" browser as NS 6.1.
 
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
 
var browser = new Browser();
 
// Global object to hold drag information.
 
var dragObj = new Object();
dragObj.zIndex = 0;
 
function dragStart(event, id) {
 
  var el;
  var x, y;
 	
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
  	{
    dragObj.elNode = document.getElementById(id);
	}
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;
 
    // If this is a text node, use its parent element.
 
    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }
 
 	if(id == null)
	{
		
		dragObj.elNode = document.getElementById('zoom');
	}

	
	if((browser.isIE && browser.version > 6) || browser.isNS)
	{
	jQuery("#zoom").css({opacity:1}).stop().animate({opacity: '0.5'},"fast");
	//// FIXES Opacity bug with bold text
	if (!browser.isNS) {
	jQuery("#zoom_footer").css({opacity:1}).stop().animate({opacity: '0.5'},"fast");
	}
	}

  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Save starting positions of cursor and element.
 
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
 
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
 
  // Update element's z-index.  This messes up IE alpha transparency and doesnt seem to be needed for other browsers.
 
  //dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  //dragObj.elNode.style.zIndex = 55;
 
  // Capture mousemove and mouseup events on the page.
 
  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
  
  return false;
}
 
function dragGo(event) {
 
  var x, y;
 
  document.getElementById('zoom').style.cursor = "move";
  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Move drag element by the same amount the cursor has moved.
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
 
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
  	{
    event.preventDefault();
	}
	
   
}
 
 function fixBackgroundsForIE() {
    if ($.browser.msie && parseFloat($.browser.version) >= 7) {
      switchBackgroundImagesTo('gif');
    }
	}

  function unfixBackgroundsForIE() {
    if ($.browser.msie && $.browser.version >= 7) {
      switchBackgroundImagesTo('png');
    }
	}
	
	function switchBackgroundImagesTo(to) {
    $('#zoom_table td').each(function(i) {
      var bg = $(this).css('background-image').replace(/\.(png|gif|none)\"\)$/, '.' + to + '")');
      $(this).css('background-image', bg);
    });
    var close_img = $('#zoom_close').children('img');
	jQuery("#zoom_close").css({display: 'none'});
    //var new_img = close_img.attr('src').replace(/\.(png|gif|none)$/, '.' + to);
    //close_img.attr('src', new_img);
  }
function dragStop(event) {
 
  // Stop capturing mousemove and mouseup events.
 
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
  
  document.getElementById('zoom').style.cursor = "url('../../images_bw/popbox/magminus.cur'), pointer";
  
  if((browser.isIE && browser.version > 6) || browser.isNS)
  {
	  jQuery("#zoom").stop().animate({opacity: '1.0'},"fast");
	  
	  /// Fixes opacity bug with bold text (IE)
	  if (!browser.isNS) {
	  jQuery("#zoom_footer").stop().animate({opacity: '1.0'},"fast");
	  }
  }
  
  /// If the user didnt drag the div, then they just clicked it, so close the div.
  if((dragObj.elNode.style.left.split('.')[0].replace('px', '') == dragObj.elStartLeft && dragObj.elNode.style.top.split('.')[0].replace('px', '') == dragObj.elStartTop) || (dragObj.elNode.style.left == 0 && dragObj.elNode.style.top == 0))
  {
	  
	  var pid = document.getElementById('hdn_restore').value.split("_")[1];
	  var smimage = document.getElementById("img"+pid);
	  var smimage_pos = (jQuery("#img"+pid).position());
	//alert(smimage_pos.top - 10);
	var productTitle = (jQuery("#title_"+pid).html());

	var titleHeight = (productTitle.length / 1.8);
	
	//alert(titleHeight);
	
	if(titleHeight < 25)
	{
		 titleHeight = 15;
	}
	else if(titleHeight < 38.5)
	{
		titleHeight = 29;
	}
	else if(titleHeight < 53)
	{
		titleHeight = 43;
	}
	else if(titleHeight < 66)
	{
		titleHeight = 57;
	}
	else
	{
		titleHeight = 71;
	}

	var image_start_adjust_top = 1;
	var image_start_adjust_left = 5;
	if($.browser.msie)
	{
		image_start_adjust_top = 1;
		image_start_adjust_left = 5;
	}
	
	fixBackgroundsForIE();

	$('#zoom').unbind('click');
		hideTitle();
		$('#zoom').animate({
	  top: smimage_pos.top + ((smimage.height - titleHeight)/8) + image_start_adjust_top,
	  left: smimage_pos.left - (image_start_adjust_left),
      opacity : "hide",
      width   : (smimage.width / 1.5) + 'px',
      height  : smimage.height + 'px'
    }, 500, 'swing', function() {
     
      unfixBackgroundsForIE();
			//zooming = false;
    });
	
	
	var theID = document.getElementById('hdn_restore').value;
	//alert(theID);

	document.getElementById(theID).style.visibility = '';
	jQuery("#"+theID).addClass("quickViewButton");
    return false;
  } 

}

function displayTitle()
{
	//jQuery(".prod_title").css({visibility: 'visible'});
}

function hideTitle()
{
	jQuery("#zoom_footer").html('');
	jQuery(".prod_title").css({visibility: 'hidden'});
	jQuery("#zoom_close").hide();
}

