var url = window.location.toString();
var addthisStatus = false;
var browserDetect = $.browser.msie + $.browser.version;

$(document).ready(function() {
	if (browserDetect != "true6.0") {
		//Make sure initialise only happens when in the drinks and cocktails section or on a product
		if (url.indexOf("drinks-and-cocktails") != -1 || url.indexOf("DrinksRecipe.aspx") != -1) {
			recipes.init();
		}
	}
	//if ie 6 only
	else {
		if (url.indexOf("all-recipes") != -1) {
			//Hide or show the intro bar if in all recipes for IE 6.0
			$("#seasonIntro").hide();
		}
		
		//Hide the share button
		$("#share").hide();
	}

	$('#printThisPage').show();
});

recipes = {
	init: function() {
		$("#recipe").hide();

		recipes.deepLinker();
		recipes.updateMenus();
		recipes.filterRecipes();
		recipes.showRecipes();
		recipes.showRecipeItem();
		recipes.defaultHighlight();
	},
	//Highlight the selected nvigation on load
	defaultHighlight: function() {
		$.each($("div#recipeNav a"), function() {
			var test = $(this).attr("href");
			test = test.substring(1);

			if (url.indexOf(test) != -1) {
				$(this).addClass("active");
			}
		});
	},
	//Forward if a deeplink with a bookmarklet is included
	deepLinker: function() {
		if (url.indexOf("#") != -1) {
			url = url.substring(url.indexOf("#") + 1);

			//Redirect to the correct location
			window.location = url.toString();
		}
	},
	//Change relevant links to bookmarklets (with a '#)
	updateMenus: function() {

		//Change the main nav
		$("div#recipeNav a").each(function() {
			if ($(this).attr("href").indexOf("#") == -1) {
				var href = $(this).attr("href");
				$(this).attr("href", "#" + href);
			}
		});

		//Change the items nav
		$("div#recipeMenu a").each(function() {
			//Check the 
			if ($(this).attr("href").indexOf("#") == -1) {
				var browserDetect = $.browser.msie + $.browser.version;
				//IE7 hack with multiple redirects, only get the page
				if (browserDetect == "true7.0") {
					var href = $(this).attr("href").split('/');
					$(this).attr("href", "#" + "/" + href[3]);
				}
				else {
					var href = $(this).attr("href");
					$(this).attr("href", "#" + href);
				}
			}
		});
	},
	//
	showRecipes: function() {
		//Hide the introduction if on all recipes
		if (url.indexOf("all-recipes") != -1) {
			$("#seasonIntro").hide();
		}

		$("div#recipeNav a").click(function() {
			//Replace and hide the recipe  div so it does not get deleted
			$("#recipe").hide().insertAfter("#recipeNav");

			var navName = $(this).html();
			var href = $(this).attr("href");
			href = href.substring(href.lastIndexOf("#") + 1);

			//Remove active classes from all links
			$("div#recipeNav a").removeClass("active");

			//Highlight the active link
			$(this).addClass("active");

			$("#seasonIntro").fadeOut();

			//Get the whole page as several elements need to be retrieved
			$.get(
				href,
				null,
				function(data, textStatus) {
					if (textStatus == "success") {

						$("#recipeMenu").html($(data).find("#recipeMenu").html());

						//Change the background of the page
						var pageClass = $(data).find("#ctl00_pageID").attr("class");
						$("#ctl00_pageID").removeClass();
						$("#ctl00_pageID").addClass(pageClass);

						//Change the occasion intro copy
						var introHtml = $(data).find("div#seasonIntro").html();
						$("div#seasonIntro").html(introHtml);

						//Remove the intro copy panel for all recipes (no copy there)
						if (navName.toLowerCase() == "all recipes") {
							$("#seasonIntro").hide();
						} else {
							$("#seasonIntro").fadeIn();
						}

						recipes.updateMenus();
						recipes.showRecipeItem();

						//Re-highlight checkboxes
						$("ul#filterList input").attr("checked", true);

						try {
							recipes.tracking(); //Track the event
						}
						catch (err) { }
					}
				},
				"html"
			);
		});
	},
	//
	showRecipeItem: function() {
		var oldHref = "";
		var slideState = ""; //Instead of slideToggle, the keep custom state of the slide

		$("div#recipeMenu a").click(function() {
			//Highlight the item
			$("div#recipeMenu a").removeClass("active");
			$(this).addClass("active");

			//Get the value of the div's position by making to the nearest 5, 10, 15 etc.
			var itemsTotal = $("div#recipeMenu a.recipeIcon").length;
			var itemNo = $("div#recipeMenu a.recipeIcon").index(this) + 1;
			var position = ((Math.ceil(itemNo / 9)) * 9) - 1; //9 is the number of items per row

			//
			position = (itemsTotal < position) ? itemsTotal - 1 : position;
			var href = $(this).attr("href");
			href = href.substring(href.lastIndexOf("#") + 1);

			//Slide up the recipe to change
			$("#recipe").slideUp("slow", function() {
				//If the old href is not the same as the new one, get the new content and slide down
				//Otherwise, the content will just be slid up
				if (oldHref != href || slideState == "up") {
					$("#recipe").load(
						href + " #recipe > *",
						null,
						function(responseText, textStatus, XMLHttpRequest) {
							//Show the share button (hidden by default)
							$(".recipeWrap .share").show();

							//Add the recipe item into the correct place
							$(this).insertAfter("#recipeMenu div.recipeItem:eq(" + position + ")");

							//Add addthis button
							recipes.addThis();

							//Show the item in position
							$(this).slideDown("slow");
							slideState = "down";
							//Set the old href
							oldHref = href;

							recipes.tracking(); //Track the event
						}
					);
				}
				else {
					slideState = "up";
				}
			});
		});
	},
	filterRecipes: function() {
		//Get the values from the hidden field on the page, the values come from the CMS meta data named RecipeType
		var filterItems = $("#ctl00_contentPlaceHolder_filterItems").val();
		filterArray = filterItems.split(";");

		//Filter HTML to add with Javascript (so not shown to those without)
		var filterList = '';
		filterList += '<ul id="filterList">';
		for (i = 0; i < filterArray.length; i++) {
			//List each item
			var filterName = filterArray[i].replace(" ", "");
			filterList += '	<li><input type="checkbox" class="' + filterName.toLowerCase() + '" checked="checked" value="' + filterName + '" /> ' + filterName + '</li>';
		}
		filterList += '</ul>';

		//Add the html to the correct place on the page
		$("div#recipeNav").after(filterList);

		//Filter click actions
		$("ul#filterList :checkbox").change(function() {
			//Get value and state of checked box
			var checked = this.checked;
			var checkValue = $(this).val();

			//Hide and show items associated with filter
			switch (checked) {
				case true:
					$("div.recipeItem[rel='" + checkValue + "']").fadeIn("slow");
					break;
				case false:
					$("div.recipeItem[rel='" + checkValue + "']").fadeOut("slow");
					break;
				default:
					break;
			}

			return false;
		});
	},
	addThis: function() {
		$("#addthis-button").toggle(
			function() {
				addthis_pub = '';
				addthis_options = 'email,print,digg,myspace,facebook,twitter,favorites,delicious,live,blogger,google,wordpress';

				//alert($("img", this).attr("src"));
				$("img", this).attr("src", "/images/content/share_button_close.png");
				return addthis_open(this, '', '[URL]', '[TITLE]');
			},
			function() {
				//alert($("img", this).attr("src"));
				$("img", this).attr("src", "/images/content/share_button_open.png");
				return addthis_close();
			}
		);
		//addthis.button(document.getElementById('addthis-button'));
	},
	tracking: function() {
		try {
			occasionTracking();
			//pageTracker._initData();
			//pageTracker._trackPageview();
		}
		catch (err) { }
	}
}