// JavaScript Document
$(document).ready(function(){
	
/* HOMEPAGE SLIDESHOW */	
	$('#slide_inner').cycle({
		timeout:       4000,
		speed:         1000 
	});

/* CREDITS LIGHTBOX */
	$("#credits .credit_popup").colorbox({
		'opacity'	: 0.5,
		'innerWidth'	: 495,
		'initialWidth'	: 545,
		'initialHeight' : 350
	});

/* CUSTOMER FEEDBACK LIGHTBOX */
	$("#contents .feedback_popup").colorbox({
		'opacity'	: 0.5,
		'innerWidth'	: 850,
		'initialWidth'	: 445,
		'initialHeight' : 350
	});

/* EVENTS LIGHTBOX */
	$("#contents .events_popup").colorbox({
		'opacity'	: 0.5,
		'innerWidth'	: 495,
		'initialWidth'	: 545,
		'initialHeight' : 350
	});
	
/* GIFTWRAP LIGHTBOX */
	$("#shop_col .giftwrap_popup").colorbox({
		'opacity'	: 0.5,
		'innerWidth'	: 700,
		'innerHeight'	: 440,
		'initialWidth'	: 445,
		'initialHeight' : 350
	});

/* PRESS LIGHTBOX */
	$("#press_listings .press_popup").colorbox({
		'opacity'	: 0.8,
		//'innerWidth'	: 553,
		//'innerHeight'	: 721,
		'initialWidth'	: 445,
		'initialHeight' : 350,
		'onOpen'	: applyStyle,
		'onClosed'	: removeStyle
	});
	
	function applyStyle(switcher){
		$('#colorbox').addClass('press');
	}
	
	function removeStyle(switcher){
		$('#colorbox').removeClass('press');
	}
	
/* MOTIF PLACEMENT */
	if( $('#related').length > 0 ) {
		$('#content').wrapInner('<div class="related_bg"></div>');
	}

/* HEAR ABOUT US - CUSTOM */
	if( $('#hear_custom_row').length > 0 ) {
		$('#hear_custom_row').hide();
		$('#id_hear').change(function(){
			if($(this).val() == 'hear_custom'){
				$('#hear_custom_row').show();
			} else {
				$('#hear_custom_row').hide();	
			}
		});
	}

	
// open links in new window
	$("a[rel='external']").attr("target","_blank");
	
	
/* ZOOM PRODUCT */
	
	if( $('#item_imagery').length > 0 ){
		
		
		var mainImg = $('#current_img img');
		var mainImgSrc = mainImg.attr('src');		 
		var holdIt;		

		// make sure safari/chrome picks up image dimensions
		mainImg.attr('src', '');
		mainImg.attr('src', mainImgSrc);		
		
		
		$("#current_img .jqzoom").jqzoom({
			zoomWidth: 419,
			zoomHeight: 405,
			xOffset: 70,
			title: false,
			lens:false,
			showEffect: 'fadein',
			hideEffect: 'fadeout',
			fadeinSpeed: 'fast',
			fadeoutSpeed: 'medium'
		});			
		
		
		mainImg.hover(function(){
			clearTimeout(holdIt);
			mainImg.attr('src', mainImgSrc);	   
		});
		
		$('#thumbs li a').hoverIntent(function(){
			clearTimeout(holdIt);
			mainImg.hide();
			mainImg.attr('src', $(this).attr("href"));	
			mainImg.show();
			//mainImg.fadeIn(250);
		},
		function(){			
			holdIt = setTimeout( revertToMain, 400);			
			function revertToMain(){
				mainImg.hide();
				mainImg.attr('src', mainImgSrc);	
				mainImg.show();
				//mainImg.fadeIn(200);
				clearTimeout(holdIt);
			}				
		});
		
		$('#thumbs li a').click(function(){	return false; });
	}

/* THUMBNAIL CAROUSEL */
	jQuery('#thumbs').jcarousel({
			auto:0,
			scroll : 3
	});	

	
/* PRODUCT TABS */

	if( $('#content div.tabs').length > 0 ){	
		var tabContainers = $('#content div.tabs > div > div');
		
		$('#content div.tabs ul.tab_nav li a').click(function () {
			tabContainers.hide().filter(this.hash).show();
			
			$('#content div.tabs ul.tab_nav li').removeClass('selected');
			$(this).parent('li').addClass('selected');
			
			return false;
		}).filter('.selected a').click();
	}

/* REQUIRED TEXT VALIDATION */

	if( $('#item_options_form').length > 0 ){
	
		$('#item_options_form .field .req').hide();		
		$('#item_options_form .btn_addtocart').click(function(){	
			
			var validSelection = true;
			
			$('#item_options_form .field').each(function(){			
				if($(this).children('select').children('option:selected').val() == "") {
					$(this).children('.req').show();	
					validSelection = false;
				}			
			});
			
			if(!validSelection) return false;		
		
		});
		
		$('#item_options_form .field select').change(function(){
			$(this).next('.req').hide();		  
		});
	
	}

});
