// HTML 
//
// Scripts in this document are used to add additional HTML tags and classes for styling purposes
// This HTML was not included in the basic page structure since it has no semantic purpose and is purely used as hooks for visual elements

// Loads up the various functions we are going to use
jQuery(function() {
	hasJS();
	fField();
	nav();
	navA();
	mealsMenu();
	highlights();
	brochure();
	quickOrder();
	lists();
	scrollMore();
	helpHeader();
	myAccountHeader();
	mealHeader();
	helpBoxes();
	init_swap_text_boxes();
	boxExpand();
	//interferes with events attached to A(#) tags, so better leave it off for now.
	//smoothAnchorLinks();
	nicebuttons(); // ed's nice buttons
	nameHLs();
	liBkgs();
	ltrClick();
	formHelp();
	formPairWithDetails();
	checkoutShowHide();
	selectAddress();
	basketPositioner();
	formPairRequired();
	collapseBox();
	removeHover();
	softBrochureTitle();
});

// Adds a class to the body so I can style differently depending on whether Javascript is enabled.
function hasJS() {
	jQuery("body").addClass('hasJS');
	jQuery("body").addClass("jsenabled");
}

// make checkout buttons nicer
function nicebuttons() {
	jQuery(".edsNiceButton").append('<div class="cornerTR"></div><div class="cornerBR"></div><div class="cornerBL"></div>');
}

function basketPositioner() {
	// position so the basket is always on-scree (for browsers not supporting position:fixed)
	/*if (jQuery('#toolsOuterContainer').css('position') == 'static') {
		jQuery(window).scroll(function() {
			jQuery('#tools').css({ top: $(this).scrollTop() });
		});
	}*/
}

function fField() {
	jQuery(".field").wrap('<div class="fieldWrap"></div>');
	jQuery("#allProducts .qtyInput").wrap('<div class="fieldWrap"></div>');
	jQuery("#goog-wm-qt").wrap('<div class="textareaWrap"></div>');
	jQuery("textarea").wrap('<div class="textareaWrap"></div>');
}
function nav() {
	jQuery("#navigation").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
}
function navA() {
	jQuery("#navigation ul a").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"><span></span></div></div></div>');
	jQuery("#navigation ul a,#navigation h1").each(
		function() {
			var troop = jQuery(this).text();
			jQuery(this).prepend("<em></em>");
			jQuery(this).find("em").text(troop);
		}
	)
}
function mealsMenu() {
	jQuery(".mealsMenu").append('<div class="corner"></div>');
}
function highlights() {
	jQuery("#highlights").wrapInner('<div class="corner"></div>');
}
function helpHeader() {
	jQuery("#helpHeader").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
}
function myAccountHeader() {
	jQuery("#myAccountHeader").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
}
function mealHeader() {
	jQuery(".mealHeader").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
}
function helpBoxes() {
	jQuery(".helpRow .helpBox").click(function() { window.location = jQuery(this).find("a").attr("href"); return false; });
	jQuery(".helpDetail .helpBox h2 a").parent().parent().parent().hover(
    function() { $(this).addClass('helpBoxActive'); },
    function() { $(this).removeClass('helpBoxActive'); }
  );
}
function boxExpand() {
	jQuery(".boxExpand h3 a").live('click', function() { jQuery('.boxExpand .co_inner').toggle(); return false; });
}
function brochure() {
	jQuery(".brochure").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
	jQuery(".brochure").live('click', function() { window.location = jQuery(this).find("a").attr("href"); return false; });
}
function quickOrder() {
	jQuery(".quickOrder").wrapInner('<div class="inner1"><div class="inner2"><div class="inner3"></div></div></div>');
	jQuery(".quickOrder").live('click', function() { window.location = jQuery(this).find("a").attr("href"); return false; });
}
function lists() {
	jQuery("ul li:first-child").addClass('firstLi');
	jQuery("ul li:last-child").addClass('lastLi');
	jQuery("ol li:first-child").addClass('firstLi');
	jQuery("ol li:last-child").addClass('lastLi');
}

function init_swap_text_boxes() {
	//Store the default value for each box
	jQuery('input[type=text][value].swaptextbox').each(function() {
		jQuery(this).data('swap', jQuery(this).attr('value'));
	}).bind('focus', function() {
		if (jQuery(this).val() == jQuery(this).data('swap')) {
			jQuery(this).val('');
		}
	}).bind('blur', function() {
		if (jQuery(this).val() == '') {
			jQuery(this).val(jQuery(this).data('swap'));
		}
	});
}

function scrollMore() {
	jQuery("body").prepend('<span id="scrollMore">Scroll for More <em>&darr;</em></span>');
	//fix for ie6, which doesn't support position:fixed
	//-- disabeld for ie6, it was too buggy apparently. it is being blamed for unexplained ie6 crashes.
	if (jQuery('#scrollMore').css('position') == 'absolute') {
		jQuery('#scrollMore').hide();
	}
	else {
	//set opacity dependent on scroll position
		jQuery(window).scroll(function() {
			var opacity = 1.3 - (jQuery(window).scrollTop() / 500);
			if (opacity > 1) opacity = 1;
			if (opacity < 0) opacity = 0;
			jQuery('#scrollMore').fadeTo(50, opacity);
		});
		jQuery('#scrollMore').click(function() {
			jQuery('html,body').animate({ scrollTop: jQuery(window).scrollTop() + 400 + 'px' }, 300);
		}).hover(function() {
			jQuery(this).fadeTo(200, 1);
		});
	}
}

function smoothAnchorLinks() {
	jQuery('a[href^=#]').click(function() {
		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
			var jQuerytarget = jQuery(this.hash);
			jQuerytarget = jQuerytarget.length && jQuerytarget || jQuery('[name=' + this.hash.slice(1) + ']');
			if (jQuerytarget.length) {
				var targetOffset = jQuerytarget.offset().top;
				jQuery('html,body').animate({ scrollTop: targetOffset }, 1000);
				return false;
			}
		}
	});
}

// name the five highlights needed for the rotation
function liBkgs() {
	jQuery('#highlights li').prepend('<img class="bkgImg" src="../images/highlightSub.png" />');
}

// name the five highlights needed for the rotation
function nameHLs() {
	jQuery('#highlights li').removeClass();
	jQuery('#highlights li:first-child').addClass('hl01');
	jQuery('#highlights li:first-child + li').addClass('hl02');
	jQuery('#highlights li:first-child + li + li').addClass('hl03');
	jQuery('#highlights li:first-child + li + li + li').addClass('hl04');
	jQuery('#highlights li:last-child').addClass('hl00');
}

// rotate highlights from left to right eg. no1 becomes no2...
function ltrClick() {
	jQuery('#highlights li:first-child').bind('click', function() { ltr(); })
}
function ltr() {
	jQuery('#highlights li:last-child').remove().prependTo('#highlights');
	jQuery('#highlights li:first-child').css({ top: "400px", left: "0" });
	// zero item - moves from left side to 1st
	jQuery('#highlights li:first-child').animate({
		top: "334px",
		left: "55px",
		width: "191px",
		height: "142px",
		opacity: 1
	}, 1000);
	// 1st item - moves to 2nd
	jQuery('#highlights li:first-child + li').animate({
		top: "317px",
		left: "247px",
		width: "225px",
		fontSize: "1.1em",
		borderWidth: "6px"
	}, 1000);
	// 2nd item - moves to 3rd
	jQuery('#highlights li:first-child + li + li').animate({
		top: "334px",
		left: "473px",
		width: "191px",
		fontSize: "13px",
		borderWidth: "5px"
	}, 1000);
	// 3rd item - moves off the right side
	jQuery('#highlights li:first-child + li + li + li').animate({
		top: "400px",
		left: "717px",
		width: "0px",
		height: "0px",
		fontSize: "0px",
		opacity: 0
	}, 1000, function() {
		nameHLs();
		jQuery('#highlights li').attr({ style: "" });
	});
}


function formHelp() {
	jQuery(".formPairHelp").hover(function() {
		$(this).find("em").show();
	}, function() {
		$(this).find("em").hide();
	}
    );
}

function formPairWithDetails() {
	jQuery(".formPairDetails").parent().parent().addClass('formPairWithDetails');
}

function checkoutShowHide() {
	jQuery(".checkoutSectionShowHide").click(function() {
		$(this).parent().next().toggleClass("checkoutSectionShowHideClosed");
	});
}
function selectAddress() {
	jQuery(".selectAddress dd a").hover(
 			function() {
 				$(this).parent().parent().css({ 'background-color': '#E9F4DD', 'border-color': '#e4f1d6' });
 			},
      function() {
      	$(this).parent().parent().css({ 'background-color': '#fff', 'border-color': '#f1f7e9' });
      }
		);
}

function formPairRequired() {
	jQuery(".formPair dd span.required").each(function(){
		jQuery(this).css({'float':'none', 'display':'inline'}).appendTo(jQuery(this).parent());
	});	
}

function collapseBox() {
	jQuery(".collapseBox").addClass('collapseBoxOpen');
	jQuery(".collapseBoxOpen + .collapseBoxOpen").toggleClass("collapseBoxClosed");
	jQuery(".collapseBoxOpen .collapseTitle").nextAll().wrap('<div class="collapseBoxInner"></div>');
	jQuery(".collapseBoxOpen .collapseTitle").click(function () {
		jQuery(this).parent().toggleClass("collapseBoxClosed");
	});	
}

function softBrochureTitle() {
	jQuery(".softBrochure").each(function() {
		var softBrochure = jQuery(this).text();
		jQuery(this).css({ 'margin-top': '-100px' });
		jQuery("h1").css({ 'width': '80%', 'line-height': '0.7em' });
		jQuery("h2.subheading").css({ 'width': '80%', 'margin-top': '-0.5em' });
	});
}

function removeHover(){
	jQuery("input.remove").hover(
		function () {
			$(this).attr("src","/images/removeHover.gif");
		}, 
		function () {
			$(this).attr("src","/images/remove.gif");
		}
	)
}

