// JavaScript Document

	var runImgProtection = function() {
		
		if ($('.protect')) {
			
			$('img.protect').each(function() {
													 
				var protectHeight = $(this).siblings('img').height();
				if (protectHeight != null) {
					$(this).height(protectHeight);
				}
			});
		}
	}
	
	var runTinyMCEImgProtection = function() {
		
		var width = 0;
		var height = 0;
		
		$('.tinymce img').each(function() {
										
			if ($(this).hasClass('no-protect')==false) {
												  
				width = $(this).width();
				height = $(this).height();
				
				$(this).wrap('<div class="protect" style="float: right; margin: 10px 0 10px 10px; width: ' + width +'; height: ' + height + '"></div>');
				$(this).before('<img src="/site_media/img/protect.gif" width="' + width + '" height="' + height + '" class="protect">');
			}
		});
		
	}
	
/* jQuery main
	--------------------------------------- */
	$(document).ready(function(){
										
		// Billing Details - Is this a gift?
		$('input#id_is_gift').click(function() {
			$('#gift-detail').slideToggle('medium')
		});										
										
		/* trigger sizing chart overlay */
		$('a#trigger-sizing-chart, li#foot-trigger-sizing-chart a').click(function(e) {
		
			e.preventDefault();
			
			$.get($(this).attr('href'), function(data) {
				data = $(data).find('#body').html();
				data = '<a href="#" class="close">close</a>' + data;
				
				// overlay settings
				Overlay.opacity = 40;
				Overlay.background = "#000000";		
				Overlay.width = 850;
				Overlay.hideSelect = true;
				Overlay.setStyle('overlay');
				
				Overlay.show(data, function() { 
					$('.close').click(Overlay.hide);
				});					
				
			});				
		
		});										
		
		// validation handler for contact form
		$("#contact-form, #donate-form, #feedback-form").validate({		
			errorContainer: "#validation",
			focusCleanup: false
		});
		
		// Rollover effect for main nav
		$("#nav-main li").hover(function() {		
			$(this).find('a').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
				.animate({
					backgroundPosition: '(0 -300px)'
				}, 400); /* this value of "200" is the speed of how fast/slow this hover animates */
		
			} , function() {		
			$(this).find('a').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
				.animate({
					backgroundPosition: '(0 0)'
				}, 300);
		});
		
		// Rollover effect for sub nav, secondary nav and general buttons
		$("a.btn, ul#secondary-nav-main a, ul#nav-hero li a, #sub-nav dd a, #gallery-main-nav ul li a, #tertiary-nav li a").hover(function() {		
			$(this).addClass("hover").stop() 
				.animate({ backgroundPosition: '(0 -200px)'}, 400); 
		
			} , function() {		
			$(this).removeClass("hover").stop()
				.animate({backgroundPosition: '(0 0)'}, 300);
		});
		
		// Display other field for Donate form
		$("#howHear").change ( function() {									
			if (this.value == "Other") {		
				$("#textAreaOther").fadeIn(2000);
				$("#textAreaOther textarea").addClass("required");		
			}
			else {		
				$("#textAreaOther").fadeOut(2000);
				$("#textAreaOther textarea").removeClass("required");		
			};									
										
		});
		
		// Main LHS Nav Slide
		$('dt.submenu').click(function(){  
			$(this).next().slideFadeToggle(1000) 
			return false;
		});
		
		// Checkout Payment CCV Tooltp
		$('#ccv-trigger').tooltip({
			position: "top left",
			effect: "fade",
			tip: '.tooltip'
		});
		
		// form validation
		$('form.validate').validate();
		
		$('a.print').click(function(e) {
			e.preventDefault();			
			window.print();
		});
		
		// trigger Send to Friend overlay
		$('a#send-to-friend, a#send-to-friend-product').click(function(e) {
		
			e.preventDefault();
			
			var shareURL = $(this).attr('rel');
			var pageTitle = $('title').text();
			
			$.get($(this).attr('href'), function(data) {
				
				data = $(data).find('#body').html();
				data = '<a href="#" class="close">close</a>' + data;
				
				// overlay settings
				Overlay.opacity = 40;
				Overlay.background = "#000000";		
				Overlay.width = 650;
				Overlay.hideSelect = true;
				Overlay.setStyle('overlay');
				
				Overlay.show(data, function() { 
					var twitterURL = $('a.twitter').attr('href');
					var facebookURL = $('a.facebook').attr('href');
					twitterURL += shareURL;
					
					facebookURL += '?u='+shareURL;
					
					if ($('meta[name="title"]').length > 0) {
						facebookURL += '?t=' + $('meta[name="title"]').attr('content');
					} else {
						facebookURL += '?t=' + pageTitle;
					}
					
					$('a.twitter').attr('href',twitterURL);
					$('a.facebook').attr('href',facebookURL);
					$('.close').click(Overlay.hide);
					$("form#sendtofriend input, form#sendtofriend textarea").addClass('required');
					$("form#sendtofriend input[id*='email']").addClass('email');
					$("form#sendtofriend input:first").focus();
					$("form#sendtofriend").validate();
					$("form#sendtofriend").submit( function(e) {
						e.preventDefault();
						if ($('form#sendtofriend').valid()) {
							$.post("/send-to-friend/", $("form#sendtofriend").serialize(),function(data) {
								strData = $(data).find('#body').html();
				                strData = '<a href="#" class="close">close</a>' + strData;
								$('#overlay').html(strData);
								$('.close').click(Overlay.hide);
							});
						}
					});
				});
				
			});				
			
		});
	
	});

/* Run on image load
	--------------------------------------- */
	$(window).load(function() { 
		$('#product_category ul > li').equalHeights();		
		
		// image protection
/*		if ($('#product-image')) {
			var protectHeight = $('#product-image img.product').height();
			$('#product-image img.protect').height(protectHeight);
		}*/
		
		runImgProtection();
		runTinyMCEImgProtection();
		
	});
	