﻿//Main navigation
$(document).ready(function() {

	var width = 0;

	//add clicky class to main nav item links, set negative lefts for nav sub ul's
	var li = $('#main-navigation > ul > li');

	for (i = 0, len = li.length; i < len; i++) {
		$('#main-navigation ul > li:eq(' + i + ') > a').addClass("clicky");
		$('#main-navigation > ul > li:eq(' + i + ') > ul').css({ "left": -(width) });
		width += parseInt(($('#main-navigation > ul > li:eq(' + i + ')').width()));
	}


	$('.clicky').click(function() {

		//if this nav item has a sub nav item and this doesn't have a class of active, open the subnav
		if (this.parentNode.getElementsByTagName('ul')[0] && !$(this).hasClass("a")) {


			$('#main-navigation ul li a').removeClass("a");
			$('#main-navigation ul li ul').fadeOut();
			$(this).addClass("a");
			$('#main-navigation').animate({ "height": 76 });
			$(this.parentNode.getElementsByTagName('ul')[0]).fadeIn();
			return false;


			//otherwise if it does have a class of active then close the subnav
		} else if ($(this).hasClass("a")) {

			$(this).removeClass("a");
			$('#main-navigation').animate({ "height": 35 });
			$(this.parentNode.getElementsByTagName('ul')[0]).fadeOut();
			return false;

		}


	});

	// Check if IE
	if (navigator.appName != "Microsoft Internet Explorer1") {

		// Do the toolbox onload event
		toolboxOnLoad();

	}

	// Do the Dug Item onload event
	dugItemOnLoad();

	// Do the Image Gallery onload event
	galleryOnLoad("#photo-gallery");

	// Do the Article Item Gallery onload even;
	galleryOnLoad(".item-image");

});

// Do the toolbox onload event
function toolboxOnLoad() {

	// Hide by default
	//$("#share-this").hide();
	//$("#email-friend").hide();

	var shareVisible = 0;

	// Add the Email tool click handler
	$("#lnkToolsShare").click(function() {

		if (shareVisible == 0) {
			$("#share-this").removeClass("hidden");
			shareVisible = 1;
			
			$("#articles-list").hide().show();

		} else {
			$("#share-this").addClass("hidden");
			shareVisible = 0;
			$("#articles-list").hide().show();

		}

		return false;

	});

	var emailVisible = 0;

	// Add the Email tool click handler
	$("#lnkToolsEmail").click(function() {

		if (emailVisible == 0) {

			// Slide down the email tool.
			$("#email-friend").removeClass("hidden");
			emailVisible = 1;
			$("#articles-list").hide().show();

		} else {

			// Slide down the email tool.
			$("#email-friend").addClass("hidden");
			emailVisible = 0;
			$("#articles-list").hide().show();

		}
		
		return false;

	});

	// The cancel email to friend handler
	$("#lnkCancelEmailFriend").click(function() {

		// Slide down the email tool.
		$("#email-friend").hide();

	});

}

/**
* Handles the loading & interaction of the item dug functions
*/
function dugItemOnLoad() {

	// display the graphical checkbox.
	$(".dug .chkDig").css("display", "none");
	$(".dug #lnkCheckReplace").addClass("img-checkbox").html("");

	var itemID = '';

	// Check if the cookie exists for the current item.
	$("#chkAction input").each(function(i, val) {

		if ($(val).attr("id").indexOf("_hdn") != -1) {

			itemID = $(val).val();

		}

	});

	// Get the cookie
	var dugItemCookie = getCookie("HL_DugItem");

	// If the cookie is set, AND the cookie contains our ID, set the checkbox to checked.
	if (dugItemCookie != null && dugItemCookie.indexOf(itemID) != -1) {

		// set to checked.
		$(".dug #lnkCheckReplace").css("background-position", "left top").css("cursor", "default");

	}

	// Handles the click event for the custom checkbox
	$(".dug #lnkCheckReplace").click(function() {
											  
		// Check to see if the button is first unchecked.
		if ($(".dug #lnkCheckReplace").css("background-position") == "0% 100%" || $(".dug #lnkCheckReplace").css("background-position") == null) {			
		
			// set to checked.
			$(".dug #lnkCheckReplace").css("background-position", "left top").css("cursor", "default");

			// Check the .net control
			$(".dug .chkDig input").attr("checked", "checked");

			// Submit the form.
			document.forms[0].submit();

		}

	});

}

/**
 * Handles the loading & interaction of the gallery items
 */
/*function galleryOnLoad() {

	// Set the initial gallery index
	$("#hdnGalleryIndex").val("0");

	var galIndex = parseInt($("#hdnGalleryIndex").val());

	// Hide the backbutton first
	$("#photo-gallery .controls .left").hide();

	// Set the initial image
	setGalleryImage(galIndex);

	// Set the next button event
	$("#photo-gallery .controls .next").click(function() {

		// Get the amount of images in the list
		var imageCount = $("#photo-gallery-images li").length;

		// Get the gallery index
		var galIndex = parseInt($("#hdnGalleryIndex").val());

		// Increment the index
		galIndex = (galIndex + 1);

		// set the gallery image
		setGalleryImage(galIndex);

		// If where at the last image , hide the next button
		if ((galIndex + 1) >= imageCount) {
			$("#photo-gallery .controls .right").fadeOut(200);
		}

		// Set the current gallery index
		$("#hdnGalleryIndex").val(galIndex);

		// Do not execute the link...
		return false;

	});

	// Set the next button event
	$("#photo-gallery .controls .back").click(function() {

		// Get the gallery index
		var galIndex = parseInt($("#hdnGalleryIndex").val());

		// Increment the index
		galIndex = (galIndex - 1);

		// If where at the last image , hide the next button
		if (galIndex <= 0) {
			$("#photo-gallery .controls .left").fadeOut(200);
		}

		// set the gallery image
		setGalleryImage(galIndex);

		// Set the current gallery index
		$("#hdnGalleryIndex").val(galIndex);

		// Do not execute the link...
		return false;

	});

}*/

/**
* Handles the loading & interaction of the gallery items
*/
function galleryOnLoad(initialControl) {

	if ($(initialControl).length == 0) {
		return;
	}

	// Set the initial gallery index
	$("#hdnGalleryIndex").val("0");

	var galIndex = parseInt($("#hdnGalleryIndex").val());

	// Hide the backbutton first
	$(initialControl + " .controls .left").hide();

	//alert(initialControl);
	
	// Set the initial image
	setGalleryImage(galIndex, initialControl, true);

	// Set the next button event
	$(initialControl + " .controls .next").click(function() {

		// Get the amount of images in the list
		var imageCount = $("#photo-gallery-images li").length;

		//alert(initialControl);

		// Get the gallery index
		var galIndex = parseInt($("#hdnGalleryIndex").val());

		// Increment the index
		galIndex = (galIndex + 1);

		// set the gallery image
		setGalleryImage(galIndex, initialControl, false);

		// If where at the last image , hide the next button
		if ((galIndex + 1) >= imageCount) {
			$(initialControl + " .controls .right").fadeOut(200);
		}

		// Set the current gallery index
		$("#hdnGalleryIndex").val(galIndex);

		// Do not execute the link...
		return false;

	});

	// Set the next button event
	$(initialControl + " .controls .back").click(function() {

		// Get the gallery index
		var galIndex = parseInt($("#hdnGalleryIndex").val());

		// Increment the index
		galIndex = (galIndex - 1);

		// If where at the last image , hide the next button
		if (galIndex <= 0) {
			$(initialControl + " .controls .left").fadeOut(200);
		}

		// Get the amount of images in the list
		var imageCount = $("#photo-gallery-images li").length;

		// If were not the last image, show the next button, if it has been hidden before
		if ((galIndex + 1) < imageCount) {
			$(initialControl + " .controls .right").fadeIn(200);
		}
		
		// set the gallery image
		setGalleryImage(galIndex, initialControl, false);

		// Set the current gallery index
		$("#hdnGalleryIndex").val(galIndex);

		// Do not execute the link...
		return false;

	});

}

// Sets the gallery image
function setGalleryImage(galIndex, initialControl, initalImage) {

	// Show the back-next buttons
	if (galIndex > 0) {
		$(initialControl + " .controls .left").fadeIn(200);
	}

	// Get the first image from the image gallery.
	var theImage = $("#photo-gallery-images li:eq(" + galIndex + ")").html();

	// Se the title of the image
	var theTitle = $(theImage).attr("alt");

	if (!initalImage) {

		// If not IE
		if (navigator.appName != "Microsoft Internet Explorer") {

			// Fade Out the image and text
			$("#the-image").fadeOut(300, function() {

				// Loads the image gallery image
				$("#the-image").html(theImage);
				$("#the-title").html(theTitle);

			});
			$("#the-title").fadeOut(300);

			// Fade In the image and text
			$("#the-image").fadeIn(300);
			$("#the-title").fadeIn(300);

		} else {

			// We are ie, so just replace.
			// Loads the image gallery image
			$("#the-image").html(theImage);
			$("#the-title").html(theTitle);

		}

	}

}


/**
 * AJAX Functions
 */

/**
 * Loads articles or sub categories from the site
 */

// Set the return value
var retValue = true;

function loadCatArticles(itemID, site, obj)
{

	// Get the baseUrl regex
	var baseUrlReg = new RegExp("http://[^/]*");
	var baseUrlResult = baseUrlReg.exec(window.location);

	// Set the base url value
	var baseUrl = (baseUrlResult[0] != null ? baseUrlResult[0] : "");

	// Generate the url
	var url = baseUrl + "/hl_data.aspx?a=get_cat_articles&cat_id=" + itemID + "&site=" + site;

	// Get the item contents
	var itemContents = $(obj).parent().html();

	// Get the index of
	var containsUl = itemContents.indexOf("<ul>");

	// If there is no <ul> in there, then append it.
	if (containsUl == -1)
	{

		$.ajax({

			type: "GET",
			url: url,
			dataType: "text",
			cache: false,
			async: false,
			success: function(data)
			{

				// If there is no data, then something went wrong, so just use the link
				if (data.length == 0)
				{

					retValue = true;

				} else
				{

					// Get the contents of the obj
					var contents = $(obj).parent().html();
					$(obj).parent().html(contents + data);

					// Set the return value
					retValue = false;

				}

			}

		});



	}

	return retValue;

}