$(document).ready(function(){
	// form validation
	$(".validate").validationEngine();
	
	// image tooltip
	imagePreview();
	
	// fancybox
	$("a.zoom").fancybox({
		'padding'        : 0,
		'width'          : 640,
		'height'         : 495,
		'autoScale'      : false,
		'overlayOpacity' : .5,
		'overlayColor'   : '#000',
		'transitionIn'   : 'fade',
		'transitionOut'  : 'fade'
	});
	
	// photo gallery
	$('#gallery').galleria({
		width: 640,
		height: 400
	});
	
	// dynamic minimums
	$('#patchType').change(function(){
		$('#qty').next().remove();
		$('option[value="Embroidered"]:selected').each(function(){$('#qty').after(' <span>(minimum 12)</span>');});
		$('option[value="Dye Sublimation"]:selected').each(function(){$('#qty').after(' <span>(minimum 100)</span>');});
		$('option[value="Woven"]:selected').each(function(){$('#qty').after(' <span>(minimum 300)</span>');});
		$('option[value="Bullion"]:selected').each(function(){$('#qty').after(' <span>(minimum 10)</span>');});
		$('option[value="Blank"]:selected').each(function(){$('#qty').after(' <span>(minimum 100)</span>');});
	});
	// delivery expectations
	$('#patchType').change(function(){
		$('#patchDate').next().remove();
		$('option[value="Embroidered"]:selected').each(function(){$('#patchDate').after(' <!--<span>(rush 10 day - reg 3 to 4 weeks)</span>-->');});
	});
	
	/* nav ------------------------------------------------------------------------------------------------ */
	/* ---------------------------------------------------------------------------------------------------- */
	// dropdown animation
	$("#nav ul li").hover(function(){
		$(this).find('ul:first').css({visibility:"visible",display:"none"}).show(400);
	},function(){
		$(this).find('ul:first').css({visibility:"hidden"});
	});

	// maintain hover
	$("#nav ul ul").hover(function(){
		$(this).parent().find("a:first").addClass("hover");
	},function(){
		$(this).parent().find("a:first").removeClass("hover");
	});

	// add more arrow
	$("#nav li li:has(ul)").addClass("more");
	/* ---------------------------------------------------------------------------------------------------- */
	
	// accordion
	$("#subnav h3").click(function(){
		$(this).parents("#subnav").find("ul ul ul:visible").slideUp("slow");
		$(this).next("ul:hidden").slideDown("slow");
	});
	
	// anythingslider
	$('#slider').anythingSlider({
		// appearance
		width               : null,      // Override the default CSS width
		height              : null,      // Override the default CSS height
		resizeContents      : true,      // If true, solitary images/objects in the panel will expand to fit the viewport
		tooltipClass        : 'tooltip', // Class added to navigation & start/stop button (text copied to title if it is hidden by a negative text indent)
		theme               : 'default', // Theme name
		themeDirectory      : 'css/theme-{themeName}.css', // Theme directory & filename {themeName} is replaced by the theme value above
	  
		// navigation
		startPanel          : 1,         // This sets the initial panel
		hashTags            : false,      // Should links change the hashtag in the URL?
		enableKeyboard      : true,      // if false, keyboard arrow keys will not work for the current panel.
		buildArrows         : true,      // If true, builds the forwards and backwards buttons
		toggleArrows        : false,     // if true, side navigation arrows will slide out on hovering & hide @ other times
		buildNavigation     : false,      // If true, builds a list of anchor links to link to each panel
		enableNavigation    : true,      // if false, navigation links will still be visible, but not clickable.
		toggleControls      : false,     // if true, slide in controls (navigation + play/stop button) on hover and slide change, hide @ other times
		appendControlsTo    : null,      // A HTML element (jQuery Object, selector or HTMLNode) to which the controls will be appended if not null
		navigationFormatter : null,      // Details at the top of the file on this use (advanced use)
		forwardText         : "&raquo;", // Link text used to move the slider forward (hidden by CSS, replaced with arrow image)
		backText            : "&laquo;", // Link text used to move the slider back (hidden by CSS, replace with arrow image)
	  
		// slideshow options
		enablePlay          : true,      // if false, the play/stop button will still be visible, but not clickable.
		autoPlay            : false,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
		autoPlayLocked      : true,      // If true, user changing slides will not stop the slideshow
		startStopped        : false,     // If autoPlay is on, this can force it to start stopped
		pauseOnHover        : true,      // If true & the slideshow is active, the slideshow will pause on hover
		resumeOnVideoEnd    : true,      // If true & the slideshow is active & a youtube video is playing, the autoplay will pause until the video completes
		stopAtEnd           : false,     // If true & the slideshow is active, the slideshow will stop on the last page
		playRtl             : false,     // If true, the slideshow will move right-to-left
		startText           : "Start",   // Start button text
		stopText            : "Stop",    // Stop button text
		delay               : 6000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
		resumeDelay         : 6000,     // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
		animationTime       : 800,       // How long the slideshow transition takes (in milliseconds)
		easing              : "easeInOutExpo",   // Anything other than "linear" or "swing" requires the easing plugin
	  
		// callbacks
		onBeforeInitialize  : null,      // Callback before the plugin initializes
		onInitialized       : null,      // Callback when the plugin finished initializing
		onShowStart         : null,      // Callback on slideshow start
		onShowStop          : null,      // Callback after slideshow stops
		onShowPause         : null,      // Callback when slideshow pauses
		onShowUnpause       : null,      // Callback when slideshow unpauses - may not trigger properly if user clicks on any controls
		onSlideInit         : null,      // Callback when slide initiates, before control animation
		onSlideBegin        : null,      // Callback before slide animates
		onSlideComplete     : null,      // Callback when slide completes
	  
		// interactivity
		clickArrows         : "click",         // Event used to activate arrow functionality (e.g. "click" or "mouseenter")
		clickControls       : "click focusin", // Events used to activate navigation control functionality
		clickSlideshow      : "click",         // Event used to activate slideshow play/stop button
	  
		// misc options
		addWmodeToObject    : "opaque", // If there is an embedded object & swfobject.js is active, the script will automatically add this wmode parameter
		maxOverallWidth     : 32766     // Max width (in pixels) of combined sliders (side-to-side); set to 32766 to prevent problems with Opera
	});
	$('#slider').data('AnythingSlider').startStop(true);
});

// image tooltip
this.imagePreview = function(){	
		xOffset = 10;
		yOffset = 30;
	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
