$(document).ready(function(){
	
	/* PNGFIX start *******************************************************************/
	//	$(document).pngFix();
	/* PNGFIX end *********************************************************************/
	
	/* EXTERNAL LINKS start ***********************************************************/
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});	
	/* EXTERNAL LINKS end *************************************************************/

	/*SEARCH*/
	$('#search input.text').focus(function(){	if(this.value=='Quick Search') {	$(this).val('');} });
	$('#search input.text').blur(function(){ if(this.value =='') {	$(this).val('Quick Search');}	});
	/*SEARCH*/
	
	/* NAVIGATION start **************************************************************/
	$('#nav ul > li.level1 > ul').prepend('<li class="top first"></li>');
	$('#nav ul ul ul').prepend('<li class="top"></li>');
	$('#nav ul ul').append('<li class="bottom"></li>');
	$('#nav ul ul').hide();
	//hover show next level
	$('#nav ul > li').hover(function() { $('ul:first', this).show(); $('a:first',this).addClass('hover'); },function() {$('ul:first', this).hide(); $('a:first',this).removeClass('hover'); });
	//add class for flyouts
	if ($('#nav ul ul ul').length > 0) { $(this).prev().addClass('flyout'); }
	
	$('#nav li.level1:first,#ad-squares li:first').addClass('first');
	$('#nav li.level1:last,#ad-squares li:last').addClass('last');
	/* NAVIGATION end ****************************************************************/
	
	/* SIFR***************************************************************************/
//	sifr('.font-01','font-gill-sans-mt',3.2);
	/* SIFR**************************************************************************/
	Cufon.replace('.font-01');

	/* COMMERCE:img click replace****************************************************/	
	$('.prod-sub-img').click(function() { $('.prod-image').html($(this).html()); return false });
	/*COMMERCE:img click replace****************************************************/
	
	/* HOMEPAGE SLIDER ****************************************************/	
	var selector = '#slideshow .photo > div';
	var fade_speed = 1000;
	var slideshow = setInterval( "slideSwitch('"+selector+"',"+fade_speed+",'Next')", 5000 );
	var selector_thumb = '#slideshow .thumbs > a';
	var fade_speed_thumb = 0;
	var slideshow_thumb = setInterval( "slideSwitch('"+selector_thumb+"',"+fade_speed_thumb+",'Next')", 5000 );
	var selector_num = '#slideshow .nav .slide';
	var fade_speed_num = 0;
	var slideshow_num = setInterval( "slideSwitch('"+selector_num+"',"+fade_speed_num+",'Next')", 5000 );
	
	$('#slideshow .thumbs a,#slideshow .nav .slide').click(
		function(){
			clearInterval(slideshow);
			clearInterval(slideshow_thumb);
			clearInterval(slideshow_num);
			
			var id = $(this).attr('rel').replace(/[^\d]+/,'');
			
			$(selector).removeClass('active');
			$(selector_thumb).removeClass('active');
			$(selector_num).removeClass('active');
			$('#'+$(this).attr('rel')).addClass('active');
			$('#num'+id).addClass('active');
			$('#thumb'+id).addClass('active');
			$(this).addClass('active');
		}
	);
	
	$('#slideshow .nav .action').click(
		function(){
			var type = $(this).attr('rel');
			if(type == 'pause'){
				clearInterval(slideshow);
				clearInterval(slideshow_thumb);
				clearInterval(slideshow_num);
				$('#slideshow .nav .action.play').addClass('active');
			}else if(type == 'play'){
				slideshow_num = setInterval( "slideSwitch('"+selector_num+"',"+fade_speed_num+",'Next')", 5000 );
				slideshow_thumb = setInterval( "slideSwitch('"+selector_thumb+"',"+fade_speed_thumb+",'Next')", 5000 );
				slideshow = setInterval( "slideSwitch('"+selector+"',"+fade_speed+",'Next')", 5000 );
				$('#slideshow .nav .action.pause').addClass('active');
			}
			$(this).removeClass('active');
		}
	);
	
	
	if($("#myController").length >0){
		$("#myController").jFlow({
			slides: "#mySlides",
			width: "638px",
			height: "327px",
			duration: 400
		});
	}
	
	/* HOMEPAGE SLIDER ****************************************************/
	
	
	
	/* Tooltips ****************************************************/
	//if($('.bottleimg').length >0){
		/*$('.bottleimg').tooltip({ 
			delay: 0, 
			showURL: false, 
			bodyHandler: function() {
				//return $(this).prepend('<span src="'+$(this).attr("src")+'">ssfd</span>');
				// return $("<img/>").attr("src", this.src).css({ "position":"absolute"}).appendTo(""); 
				return $("<img/>").attr("src", this.src); 
			} 
		});*/
	//}
	/* Tooltips ****************************************************/
	
	/* BROWSE WINES start
	
	$('#browse-wines').css({ 'overflow':'visible'});	
	$('#browse-wines-dropdown').jScrollPane({showArrows:true,arrowSize:15,scrollbarWidth:15}).css({ 'overflow':'visible'});
	
	$('#browse-wines .jScrollPaneContainer').hide().addClass('active');
	$('body > div').prepend('<div id="browse-wines-bg"></div>').prepend($('#browse-wines .jScrollPaneContainer'));
	$('#browse-wines-bg').hide();
	
	$('#browse-wines .handle').click(function(){
		var $dropdown = $('.jScrollPaneContainer');
		var $dropdown_bg = $('#browse-wines-bg');
			$dropdown.show();
			$dropdown_bg.show();
			$dropdown_bg.click(function(){
				$(this).hide();
				$('.jScrollPaneContainer').hide();
		});
	});
	/* BROWSE WINES end */
	
	/* We Recommend end*/
	if($('.link-may-recommend')){
		$('.link-may-recommend a').click(function(){
			createOverlay();
			var href = $(this).attr('href');
			createPopup(href);
			return false;
		});	
	}
	/* We Recommend start*/	
			
}); //close document.ready

function createOverlay(){
	$('body').append('<div id="overlay"></div>');
	$('#overlay').css({ opacity: 0.7 })
	.click(function(){
		$(this).next().fadeOut().remove();
		$(this).fadeOut().remove();
	})
}
function createPopup(href){
	$('body').append('<div id="overlay-popup"><span class="close"></span></div>');
	var offset = dimensions();
	var left = (offset.width - $('#overlay-popup').width())/2;
	$('#overlay-popup').css({ 'left':left,'background-image':'none'}).load(href)
	$('#recommended').jScrollPane({showArrows:true,arrowSize:15,scrollbarWidth:15});
}

function closePopup() {
	$('#overlay-popup').remove();
	$('#overlay').remove();
}


/* AJAX SEARCH function start **************************************************************/
function ajaxSearch(qryString) {
	$("#ajax-search").load('/store/ajaxsearch/ajax.cfm?' + qryString,'','');
}
/* AJAX SEARCH function end **************************************************************/

function dimensions() {
	var dimensions = {width: 0, height: 0};
	if (document.documentElement) {
		dimensions.width = document.documentElement.offsetWidth;
	  	dimensions.height = document.documentElement.offsetHeight;
  	} else if (window.innerWidth && window.innerHeight) {
	  dimensions.width = window.innerWidth;
	  dimensions.height = window.innerHeight;
  	}
	return dimensions;
}

function getYX(coord){
	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;
  }
	if(coord=="X"){
		return scrOfX;
	}
	else if(coord=="Y"){
		return scrOfY;
	}
}

function labelHover(){
	//if($('.recommended') ==0){
	$('dt span.image').hover(function(){
		span = $(this);
		img = $('img',span);

		var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
		var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
		$('#internal').prepend('<span id="wine-label"><img src="'+ img.attr('src')+'"></span>'); //display full image
		$('#wine-label').css({ "top": getYX("Y")+30});
	
	},function(){ $('#wine-label').remove(); }); //put errthing back to normal
	//}
}
				
/* SLIDESHOW function start *********************************************************/
function slideSwitch(s,o,d){var $a=$(s+'.active');if($a.length==0)$a=$(s+':first');if(d=='Next'||d==null){var $n=$a.next().length?$a.next():$(s+':first');}else if(d=='Previous'){var $n=$a.prev().length?$a.prev():$(s+':last');}$a.addClass('last-active');$n.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0},o,function(){$a.removeClass('active last-active');});}
/* SLIDESHOW function end ***********************************************************/
