/*======================================================================
	DEEP DISCOUNT: JavaScript Functions
----------------------------------------------------------------------*/
var WEBLINC = {};
$(document).ready(function(){
	
	/*======================================================================
		INPUT TEXT SWITCHING
		--------------------------------------------------------------------
		On focus of these fields, remove text, on blur if empty replace
		with initial field value text.
	----------------------------------------------------------------------*/	
		WEBLINC.focusInputs =
		{
			init: function($obj) {
				var initVal = $obj.val();
				
				$obj
					.focus(function() {
						if($(this).val() == initVal)
							$(this).val('').removeClass('hint');
					})
					.blur(function() {
						if($(this).val() == '')
							$(this).val(initVal).addClass('hint');
					});
			}
		}

	/*======================================================================
		PRIMARY NAVIGATION DROP DOWN MENUS
	----------------------------------------------------------------------*/	
		var navHoverMaxWidth = 632,
			navHoverMinWidth = 250
		;
		
		var primaryNavHoverConfig =
		{
			sensitivity: 10, // sensitivity threshold (must be 1 or higher)
			interval: 150,   // milliseconds for onMouseOver polling interval
			timeout: 100,    // milliseconds delay before onMouseOut
			over: function(){
				var flyoutNavItemsWidth = 0,
					thisWidth = ($(this).outerWidth(true) > navHoverMinWidth) ? $(this).outerWidth(true) : navHoverMinWidth,
				 	$flyoutNav = $('> ul', this)
				;
				
				$(this).addClass('over');
				if ($.browser.msie){
					// kill select bug in IE6
					if ($.browser.version == "6.0")
						$('select').css({ visibility: 'hidden' });
					// account for IE's (any version) inablity to fade alpha channels
					$flyoutNav.show();
				} else {
					$flyoutNav.fadeIn(500);
				}
				
				$('> li', $flyoutNav).each(function()
				{
					flyoutNavItemsWidth += $(this).outerWidth(true);
				});
				
				if(flyoutNavItemsWidth < thisWidth)
					flyoutNavItemsWidth = thisWidth;
				else if (flyoutNavItemsWidth > navHoverMaxWidth)
					flyoutNavItemsWidth = navHoverMaxWidth;
					
				$flyoutNav.css('width', flyoutNavItemsWidth);
			}, 
			out: function(){
				var $flyoutNav = $('> ul', this);
				
				$(this).removeClass('over');
				if ($.browser.msie){
					// restore selects for bug in IE6
					if ($.browser.version == "6.0")
						$('select').css({ visibility: 'visible' });
					// account for IE's (any version) inablity to fade alpha channels
					$flyoutNav.hide();
				} else {
					$flyoutNav.fadeOut(300);
				}
			} 
		};
		$('#primary-nav > ul > li:has(ul)').hoverIntent(primaryNavHoverConfig);
		

	/*======================================================================
		PRODUCT QUICKVIEW
	----------------------------------------------------------------------*/
	
		WEBLINC.productQuickview = 
		{
			init: function() {
				var $productImg = 'div.products-grid ul li div.image a:not(.quick-view), div.products table.lineitems td.column-image a:not(.quick-view)',
					$quickviewBtn = 'div.products-grid ul li div.image a.quick-view, div.products table.lineitems td.column-image a.quick-view';
				
				WEBLINC.productQuickview.bindEvents($productImg,$quickviewBtn);
			},
			initMyBuys: function() {
				var $productImg = '#mybuyspagezone1 div.image a:not(.quick-view)',
					$quickviewBtn = '#mybuyspagezone1 div.image a.quick-view';
					
				WEBLINC.productQuickview.bindEvents($productImg,$quickviewBtn);
			},
			buildQuickView: function(parentElm) {
				// create main quickview container, append to current product container
				var $quickView = $('<div class="quickview-modal" style="display: none;"><a href="#" class="close">Close Quick View</a><div class="inner-content clearfix"><h2 class="loading">Loading...</h2></div></div>').appendTo(parentElm);
				
				// hide selects
				if( $.browser.msie && $.browser.version == 6 )
					$("select").css("visibility","hidden");
				
				$('a.close',$quickView).click(function() {
					$quickView.remove();
					if( $.browser.msie && $.browser.version == 6 )
						$("select").css("visibility","visible");
						
					return false;
				});
				
				$quickView.show();
				
				return $quickView;
			},
			bindEvents: function($productImg, $quickviewBtn) {
				$($productImg).hover(
					function()
					{
						$(this).siblings('a').show();
					},
					function()
					{
						$(this).siblings('a').hide();
					}
				);
				
				$($quickviewBtn).hover(
						function()
						{
							$(this).show();
						},
						function()
						{
							$(this).hide();
						}
					)
					.click(function() {
						$('#content div.products div.quickview-modal').remove();
						var $this = $(this),
							$products = $this.closest('div.products'),
							productID = $this.attr('rel'),
							thisXY = $this.offset(),
							productsXY = $products.offset(),
							qvTop = (thisXY.top - productsXY.top),
							$thisQV = WEBLINC.productQuickview.buildQuickView($products),
							winWidth = ($products.width() / 2),
							qvWidth = ($thisQV.width() / 2),
							leftPos = ((winWidth - qvWidth) - 30),
							leftCoord = thisXY.left + leftPos,
							leftPadding = 10,
							rightCoord = thisXY.left + leftPos + $thisQV.width(),
							windowRightBoundary = $(window).width() + $(window).scrollLeft();
						/*
						console.log('leftPos: ', leftPos);
						console.log('productsXY.left: ', productsXY.left);
						console.log('thisXY.left: ', thisXY.left);
						console.log('$thisQV.width(): ', $thisQV.width());
						console.log('qvWidth: ', qvWidth);
						console.log('rightCoord: ', rightCoord);
						console.log('windowRightBoundary: ', windowRightBoundary);
						*/
						// Keep our quickview within the scrollable window area
						if(rightCoord >= windowRightBoundary && leftPos < 0) {
							leftPos += (windowRightBoundary - rightCoord);
						} else if (leftCoord <= leftPadding && leftPos < 0) {
							leftPos = leftPadding;
						}
						
						$thisQV.css({
							top: qvTop + 'px',
							left: leftPos + 'px'
						});

						$.ajax({
							async: true,
							url: '/index.cfm/fuseaction/product.quickview/productID/'+productID,
							dataType: "html",
							cache: true,
							success: function(data, textStatus)
							{				
								$('div.inner-content',$thisQV).html(data);	
								$('.button, .button-alt',$thisQV).buttonize();
								$('div.product-images a.image-link',$thisQV).removeAttr('title').click(function() { return false; });
							},
							error: function()
							{
								$('div.inner-content',$thisQV).html("<h2>An Error Occurred</h2>");
							}
						});
			
						return false;
				});
			}
		};
				

	/*======================================================================
		INTERFACE INITIALIZATIONS
	----------------------------------------------------------------------*/	
		$('.button, .button-alt').buttonize();
		$('.corners').cornerize();
		
		/*---------------------------------
			GUARANTEES
		---------------------------------*/
		var $guarantees = $('#guarantees'),
			$guaranteeItems = $('> li', $guarantees),
			timeout = 13000,
			speed = 1000,
			currentIndex = 0;
		
		// IE does not support opacity on PNG with alpha (for cross-fade),
		// so hide/show instead of fade
		if ($.browser.msie) {
			window.setInterval(function(){
				$guaranteeItems
					.hide()
					.eq(currentIndex).show();
						
				currentIndex++;
				if (currentIndex >= $guaranteeItems.length) {
					currentIndex = 0;
				}
			}, timeout);
		} else {
			$guarantees.cycle({ timeout: timeout, speed: speed });
		}
		
		/*---------------------------------
			CATEGORY PROMOTIONS
		---------------------------------*/
		$('#content ul.category-promotions').each(function() {
			$('li.category:first', this).addClass('first');
			$('li.category:last', this).addClass('last');
		});
		// WEBLINC.focusInputs.init($('input#vsearch-basicsearchform-searchstring'));
		WEBLINC.focusInputs.init($('input#newsletter-email-address'));
		WEBLINC.productQuickview.init();
});

