/** 
 * @projectDescription	Brooke Jump
 * @author 	Matt Hobbs matt.hobbs@harvestdigital.com
 * @version 	0.1 
 */

/** @id first function
* @classDescription the description
*/
$(function(){
	//Detect if JS is turned on.
	$('body').addClass('hasJS');
	//If JS on, change the donate href for the modal box
	$('#makeDonation a.thickbox, #whatWillYour a.thickbox, #storyBox a.thickbox').attr('href','/donate/index.html?KeepThis=true&TB_iframe=true&height=255&width=470');
	//Open terms in new window
	$("li a.terms").attr("target", "_blank");
	
	//Show the youtube video after map closed
	$("#makeDonation a.thickbox, #whatWillYour a.thickbox, #storyBox a.thickbox").click(function(){
		$("div.flashContainer").hide();
	});
	
	//Look for #donate in the url string, if its there fire the donate button
	var url = window.location;
	var value = url.toString();
	var donateResult = value.indexOf("#donate");
	if(donateResult !== -1){
		$("#makeDonation a").trigger("click");
	}
	
	//Show the owners page if they click on owners.
	var ownerStory = value.indexOf("#owner");
	if(ownerStory !== -1){
		$("#donkeyStoryPage").hide();
		$("#ownerStoryPage").show();
	}
	
	//Show the owners page if they click on owners.
	var donkeyStory = value.indexOf("#donkey");
	if(donkeyStory !== -1){
		$("#ownerStoryPage").hide();
		$("#donkeyStoryPage").show();
	}
	
	$("#donate10 a, #donate23 a, #donate27 a, #inNews a").each(function(){
		var or = $(this).attr("title");
		or += " - Opens in a new window";
		$(this).attr("title", or).attr("target", "_blank");
	});
	
	if ($('body').is('#home')) {
		$("b#donkey").hover(
			function(){
				$("#offState h1, #offState p").hide();
				$("h1.donkey, p.donkey").show();
				$("#allOff").removeClass().addClass('boyBlur');
			},
			function(){
				return;
			}
		);
		$("b#child").hover(
			function(){
				$("#offState h1, #offState p").hide();
				$("h1.child, p.child").show();
				$("#allOff").removeClass().addClass('donkeyBlur');
			},
			function(){
				return;
			}
		);
		$("a#donkeyStory, a#ownerStory").hover(
			function(){
				$("#offState h1, #offState p").hide();
				$("h1.off, p.off").show();
				$("#allOff").removeClass();
			},
			function(){
				return;
			}
		);
		$('#youBox a').flash(
	        { height: 166, width: 208 },
	        { version: 8 },
	        function(htmlOptions) {
	            $this = $(this);
	            htmlOptions.src = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));						
	        }
	    );
		$("#youBox a").hide();
		$("#whereWorks a").attr("target", "_blank");
	}
	if ($('body').is('#stories')) {
		$("#dStoryButton").click(function(){
			$("#ownerStoryPage").hide();
			$("#donkeyStoryPage").show();
		});
		$("#oStoryButton").click(function(){
			$("#donkeyStoryPage").hide();
			$("#ownerStoryPage").show();
		});
		
		$('#youBoxBig a').flash(
	        { height: 188, width: 236 },
	        { version: 8 },
	        function(htmlOptions) {
	            $this = $(this);
	            htmlOptions.src = $this.attr('href');
	            $this.before($.fn.flash.transform(htmlOptions));						
	        }
	    );
		$("#youBoxBig a").hide();

		
		$("#flashButton").flash({
		    src: '../swf/donkeyBtn.swf',
		    width: 237,
		    height: 206,
			flashvars: {clickTag: "https://www.thebrooke.org/form.asp?id=6802"}
		});
		$("#flashButton div.alt").hide();
	}
	if ($('body').is('#donation')) {
		$("#other").click(function(){
			$("#otherAmount").focus();
		});
		var urlString = "https://www.thebrooke.org/form.asp?id=6802&currency=GBP";
		var amount = 5;
		var date = new Date();
		var day = "&day=" + date.getDay();
		var month = date.getMonth();
		month += 1;
		var fullMonth = "&month=" +  month;
		var year = "&year=" + date.getFullYear();
		
		$("#formWrapper a").click(function(){
			if($("#other").is(":checked")){
				amount = $("#otherAmount").val();
				if(amount === ""){
					amount = 5;
				}
				amount = "&amount=" + amount;
			} else {
				amount = "&amount=" + $("#amount input[name='amount']:checked").val();
			}
			

			if($("#oneoff").is(":checked")){
				urlString = "https://www.thebrooke.org/form.asp?id=6802&currency=GBP";
				urlString += amount;
				$("#donateForm").attr("action", urlString);
			} else if($("#monthly").is(":checked")) {
				urlString = "https://www.thebrooke.org/form.asp?id=6695";
				urlString = urlString + day + fullMonth + year + amount;
				$("#donateForm").attr("action", urlString);
			}
			var parentURL = "javascript:parent.changeParentURL('" + urlString + "');";
			$(this).attr("href", parentURL);
		});
	}
});
