// ** Stop IE 6 Background Flicker **
// **                              **
if ($.browser.msie && (parseInt($.browser.version)==6)) {
  try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
};

// ** Add search functionality **
// **                          **
$(function(){ // (executes when the dom is ready)
	
	if ($.browser.safari) {
		// add search field for webkit/safari
		// replace existing search box
		$("#search .search-box").replaceWith('<input class="search-box search-type-box" type="search" name="q" value="" placeholder="Search" results="5" autosave="com.marylousweigh.search"/>');
		// hide button but keep spacing
		$("#search .search-button").css({visibility:'hidden',left:'0'});
	} else {
		// For other browsers add active/inactive states
		$("#search .search-box").focus(function() {
			// clear value if the default value exists
			if ($(this).val() == this.defaultValue) { $(this).val(""); }
			$(this).addClass('active'); 
		}).blur(function() {
			// restore default value if value does not exist
			if ($(this).val() == "") { $(this).val(this.defaultValue); }
			$(this).removeClass('active');
		});
	};
	
});

// embed our flash with UFO
var homeFlashObject = { 
	movie:"/flash/home/MLW_Flash_1.3.swf", 
	width:"867", 
	height:"420", 
	majorversion:"8", 
	build:"0",
	bgcolor:"#ffffff", 
	wmode:"window", 
	menu:"false", 
	name:"flashome", 
	flashvars:"globalAssetPath=/flash/home/"
};
UFO.create(homeFlashObject, "home-flash");

var philosophyFlashObject = { 
	movie:"/flash/video/mlw_video_philo.swf", 
	width:"320", 
	height:"280", 
	majorversion:"8", 
	build:"0", 
	xi:"true", // xi:"true" will do an express install if they don't have the right version
	ximovie: "/flash/ufo.swf", // needed to do express install
	bgcolor:"#ffffff", 
	wmode:"transparent", 
	menu:"false", 
	name:"flashphilosophy", 
	flashvars:"globalAssetPath=/flash/video/&videoToLoad=philosophy.flv"
};
UFO.create(philosophyFlashObject, "flash-video-philosophy");

var productFlashObject = { 
	movie:"/flash/video/mlw_video.swf", 
	width:"320", 
	height:"280", 
	majorversion:"8", 
	build:"0", 
	xi:"true", // xi:"true" will do an express install if they don't have the right version
	ximovie: "/flash/ufo.swf", // needed to do express install
	bgcolor:"#ffffff", 
	wmode:"transparent", 
	menu:"false", 
	name:"flashvideo", 
	flashvars:"globalAssetPath=/flash/video/&videoToLoad=about-the-platform.flv"
};
UFO.create(productFlashObject, "flash-video");

var faqFlashObject = { 
	movie:"/flash/video/mlw_video_faq.swf", 
	width:"320", 
	height:"280", 
	majorversion:"8", 
	build:"0", 
	xi:"true", // xi:"true" will do an express install if they don't have the right version
	ximovie: "/flash/ufo.swf", // needed to do express install
	bgcolor:"#ffffff", 
	wmode:"transparent", 
	menu:"false", 
	name:"flashvideofaq", 
	flashvars:"globalAssetPath=/flash/video/&videoToLoad=faq-6.flv"
};
UFO.create(faqFlashObject, "flash-video-faq");

// ** Add click for video FAQs **
// **                          **
$(function(){ // (executes when the dom is ready)
	
	$("#video-faq-list a").click(function(){
		swfToLoad = $(this).attr("rel") + ".flv";
		//alert(swfToLoad);
		$("#video-faq-list a").removeClass("active");
		$(this).addClass("active");
		$("#flash-video-faq").fadeOut("fast");
		faqFlashObject.flashvars = "globalAssetPath=/flash/video/&videoToLoad="+swfToLoad;
		UFO.create(faqFlashObject, "flash-video-faq");
		$("#flash-video-faq").fadeIn("normal");
		return false
	});
	
});

// ** Add click to show for acheivements **
// **                                    **
$(function(){ // (executes when the dom is ready)
	
	$("#acheivements-nav").find("a").click(function(){
		// hide others
		$(".achievements").addClass("hide");
		// display related info for this image
		$("."+$(this).attr("rel")).removeClass("hide");
		// remove active state for nav
		$("#acheivements-nav a").removeClass("active");
		$(this).addClass("active");
		return false;
	});
	
	$(".show-achievement").click(function(){
		// hide others
		$(".achievements").addClass("hide");
		// display related info for this image
		$("."+$(this).attr("rel")).removeClass("hide");
		// remove active state for nav
		$("#acheivements-nav a").removeClass("active");
		// highlight the related nav for this link
		$("#acheivements-nav a[@rel$='"+$(this).attr("rel")+"']").addClass("active");
		return false;
	});
	
	$(".view-year-list a").click(function(){
		$(".year-list").removeClass("hide").css("display","none");
		$(".year-list").slideDown("slow");
		$(".view-year-list").addClass("hide");
		$(".hide-year-list").removeClass("hide");
		return false;
	});
	
	$(".hide-year-list a").click(function(){
		$(".year-list").slideUp("slow");
		$(".hide-year-list").addClass("hide");
		$(".view-year-list").removeClass("hide");
		return false;
	});
	
});

// ** Add click to show FAQ **
// **                       **
$(function(){ // (executes when the dom is ready)
	
	$(".faq .question").click(function(){
		if ($(this).parent().is(".opened")) {
			$(this).parent().find(".answer").css("display","block").slideUp("normal");
			$(this).parent().addClass("closed").removeClass("opened");
		} else {
			$(".opened .answer").css("display","block").slideUp("normal");
			$(".opened").addClass("closed").removeClass("opened");			
			$(this).parent().find(".answer").slideDown("fast");
			$(this).parent().addClass("opened").removeClass("closed");
		}
	});
	
});

// ** Add click to read more **
// **                        **
$(function(){ // (executes when the dom is ready)
	
	$(".read-more").click(function(){
		if ($("."+$(this).attr("rel")).length > 0) {
			$("."+$(this).attr("rel")+"-ellipse").css("display","none");
			$("."+$(this).attr("rel")).css("display","none").removeClass("hide");
			$("div."+$(this).attr("rel")).slideDown("normal");
			$("span."+$(this).attr("rel")).fadeIn("slow");
			$("."+$(this).attr("rel")+" .hide-read-more")
			$("a.hide-read-more[@rel$='"+$(this).attr("rel")+"']").parent().removeClass("hide");
			$(this).addClass("hide");
			return false;
		};		
	});
	
	$(".hide-read-more").click(function(){
		if ($("."+$(this).attr("rel")).length > 0) {
			$("."+$(this).attr("rel")).slideUp("slow");
			$(this).parent().addClass("hide");
			$("a[@rel$='"+$(this).attr("rel")+"']").removeClass("hide").css("display","none").fadeIn("normal");
			return false;
		};		
	});
	
});

// ** Recipe related **
// **                **
$(function(){ // (executes when the dom is ready)	
	
	// search for recipe-nav-something or recipe-snav-something in the #third-nav tag
	// this tells us if a link should be active by default
	// (this is because each of our nav links uses a class like recipe-nav-breakfast
	//  or recipe-snav-1 etc)
	$("#third-nav").each(function(i) {
		var thirdNavClasses = $(this).attr("class").split(" ");
		var regex1 = /\brecipe-nav-\b/;
		var regex2 = /\brecipe-snav-\b/;
		$.each(thirdNavClasses, function(i, n) {
			if (regex1.test(n) || regex2.test(n)) {
				$('a.'+n).addClass("active");
				// also remove .closed from the parent if it's a main nav
				if (regex1.test(n)) {
					$('a.'+n).parent().removeClass("closed");
				};
			};
		});
	});
	
});


// ** Sends form via ajax/aha                **
$(function(){ // (executes when the dom is ready)

	// setup success story form to send via ajax
	$("#success-story-form").submit(function(){
		// update submit button
		//$("#success-story-form input[@name='submit']").attr({disabled:'disabled',value:'Sending Your Story...'}).addClass("disabled");
		// update submit button
		$("#success-story-form .alt-submit").removeClass("hidden-submit");
		$("#success-story-form input[@name='submit']").attr({disabled:'disabled',value:'Sending Your Story...'}).css("visibility","hidden");
		
		// send via ajax
		$.ajax({
			type: "POST",
			cache: false,
		  url: $("#success-story-form").attr("action"),
			data: { story: $("#success-story-form textarea[@name='story']").val(),
							firstname: $("#success-story-form input[@name='firstname']").val(),
							lastname: $("#success-story-form input[@name='lastname']").val(),
							birthday: $("#success-story-form input[@name='birthday']").val(),
							poundslost: $("#success-story-form input[@name='poundslost']").val(),
							emailaddress: $("#success-story-form input[@name='emailaddress']").val(),
							submit: 'submit'
			},
			dataType: "html",
			success: finishedSendingFormData
		});
		// function to execute on successful load of new content
		function finishedSendingFormData(data) {
			// add new content
			$("#success-story-message").html(data);
			// update submit button
			//$("#success-story-form input[@name='submit']").removeAttr('disabled').attr({value:'Send Your Story'}).removeClass("disabled");
			// update submit button (when submit button is an image)
			$("#success-story-form .alt-submit").addClass("hidden-submit");
			$("#success-story-form input[@name='submit']").removeAttr('disabled').attr({value:'Send Your Story'}).css("visibility","visible");
		};
		return false;
	});
	
	// setup recipe form to send via ajax
	$("#recipe-form").submit(function(){
		// update submit button
		//$("#recipe-form input[@name='submit']").attr({disabled:'disabled',value:'Sending Your Recipe...'}).addClass("disabled");
		// update submit button (when submit button is an image)
		$("#recipe-form .alt-submit").removeClass("hidden-submit");
		$("#recipe-form input[@name='submit']").attr({disabled:'disabled',value:'Sending Your Recipe...'}).css("visibility","hidden");
		
		// send via ajax
		$.ajax({
			type: "POST",
			cache: false,
		  url: $("#recipe-form").attr("action"),
			data: { recipe: $("#recipe-form textarea[@name='recipe']").val(),
							emailaddress: $("#recipe-form input[@name='emailaddress']").val(),
							submit: 'submit'
			},
			dataType: "html",
			success: finishedSendingFormData
		});
		// function to execute on successful load of new content
		function finishedSendingFormData(data) {
			// add new content
			$("#recipe-message").html(data);
			// update submit button
			//$("#recipe-form input[@name='submit']").removeAttr('disabled').attr({value:'Send Your Recipe'}).removeClass("disabled");
			// update submit button (when submit button is an image)
			$("#recipe-form .alt-submit").addClass("hidden-submit");
			$("#recipe-form input[@name='submit']").removeAttr('disabled').attr({value:'Send Your Recipe'}).css("visibility","visible");
		};
		return false;
	});
	
});



// ** Misc **
// **      **
$(function(){ // (executes when the dom is ready)
	
	// make logo a link to go back to home page
	$("#header h1").click(function(){
		document.location = "/";
	}).css("cursor","pointer");
	
	// send "noflash" to adobe's site to download
	$(".get-flash").click(function(){
		document.location = "http://www.adobe.com/go/flashplayer";
	}).css("cursor","pointer");
	
	// fix navigation rollovers (adds hover class to do bug fix)
	$("#navigation a").hover(function(){
		$(this).addClass("hover");
		//$(this).find("a").css('color','red');
		$(this).find("span").css('top','1px');
	},function(){
		$(this).removeClass("hover");
		//$(this).find("a").css('color','blue');
		$(this).find("span").css('top','0');
	});

	
	// add alternate submit underline for windows OS
	if ((navigator.appVersion.indexOf("Win")!=-1)) {
		$("input.plain-submit").addClass("plain-submit-win");
	};
	
	// add form classes for ie (to mimic our css selectors)
	if ($.browser.msie && (parseInt($.browser.version)<=6)) {
		$("input:checkbox").addClass("type-checkbox");
		$("input:radio]").addClass("type-radio");
		$("input:text]").addClass("type-text");
		$("input[@type=password]").addClass("type-password");
	};
	
	// convert hr's to div.hr instead
	if ($.browser.msie && (parseInt($.browser.version)<=7)) {
		$("hr").wrap("<div class='hr'></div>");
	};
	
	// make any links with rel="external" to open in new windows
	$("a[@rel$='external']").click(function(){
		this.target = "_blank";
	});
	
});

