// JavaScript Document

/* jQuery main
	--------------------------------------- */
	$(document).ready(function(){
	
		// 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'
		});
		
		// Billing Details - Is this a gift?
		$('input#id_is_gift').click(function() {
			$('#gift-detail').slideToggle('medium')
		});
		
		$('p.message').fadeIn('slow');
		
		// 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+'?t='+pageTitle;
					$('a.twitter').attr('href',twitterURL);
					$('a.facebook').attr('href',facebookURL);
					//$('input#share-url').attr('value', shareURL);
					$('.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();
						$(this).attr('disabled', 'disabled');
						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);
		}*/
		
		if ($('.protect')) {
			
			$('img.protect').each(function() {
													 
				if ($(this).width() < 1 & $(this).height() < 1) {
													 
					var protectHeight = $(this).siblings('img').height();
					if (protectHeight != null) {
						$(this).height(protectHeight);
					}
					
				}
			});
		}
		
		// protect images
		$('.tinymce img').wrap('<div class="protect"></div>');
		$('.tinymce div.protect').prepend('<img src="/site_media/img/protect.gif" class="protect" />');
		$('.tinymce div.protect').each( function() {
			//var intWidth = $(this).find('img:first').width() + 22;
			var intWidth = $(this).find('img:eq(1)').width();
			var intHeight = $(this).find('img:eq(1)').height();
			$(this).width( intWidth );
			$(this).height( intHeight );
		});		
		
	});

