/* ----------------------------------------- */
/*  Netball Taranaki 2011 - In Page JS Code  */
/*  Ver     	 : 1.1                       */
/*  Created 	 : 02 Feb 2011               */
/*  Last Updated : 23 Jan 2011               */
/* ----------------------------------------- */

/*-----------*/		
/* Functions */
/*-----------*/
function animatedFeatured(contentContainer, navContainer)
{
	var $contentContainer = $(contentContainer),
		$navContainer     = $(navContainer),
		$navContainerText = $("ul.news-list div.text"),
		$navItem          = $(navContainer).find("h2 a"),
		$clickedItem      = "",
		currIndex         = "",
		selectedIndex     = "",
		animated          = false,
		selectedURL       = "";

	// Initialization - fix by jay
	if ($("body.home").length) {
		// add the selected state to the first news item
		$navContainer.find("div.text").first().show().parent().addClass("selected");
		$(".panel-item").first().addClass("active");
		
		// set the first item URL
		selectedURL = $navContainer.find("div.text a").first().attr("href");		
		$navContainer.find("li.selected h2 a").attr("href", selectedURL);
	}

	$contentContainer.find("li.active").show();
	
	$navItem.live("click", function () {
		
		$clickedItem  = $(this);
		currIndex     = $navContainer.find("li.selected h2 a").attr("class");		
		currIndex     = currIndex.substring(8, currIndex.length);		
		selectedIndex = $navItem.index(this) + 1;
		
		if ((animated == false) && (!($clickedItem.parent().parent().hasClass("selected"))))
		{								
			animated  = true;
			
			/* ---------------------------- */
			/* Navigation Sliding Animation */
			/* ---------------------------- */			
			$navContainerText.slideUp(400);
			
			$navContainer.find("li.selected").find("h2 a").attr("href", "#")
				.parent().parent().removeClass("selected default-font-size");
				
			$clickedItem.parent().parent().addClass("selected").find("div.text").slideDown(400, function() {
				
				// Get the current selected item URL
				selectedURL = $clickedItem.parent().parent().find("div.text a").attr("href");				
				
				// set the heading link URL
				$clickedItem.attr("href", selectedURL);
				
				// set animated back to false after the sliding animation complete 
				animated = false;
			});
			
			/* ---------------------------- */
			/* Image Transition - Fade ver. */
			/* ---------------------------- */						
			
			$contentContainer.find("li.active").removeClass("active").fadeOut(400, function() 
			{
				$(contentContainer + " li:nth-child(" + selectedIndex + ")").fadeIn(400, function()
				{
					animated = false;
					$(this).addClass("active");
				});
				
				/*
				$(this).parent().find("li.panel-" + selectedIndex).fadeIn(400, function()
				{	
					animated = false;
					$(this).addClass("active");
				});
				*/
			});			
			
			return false;
		}		
	});	
}

/*--------------------------*/		
/* Document Ready Functions */
/*--------------------------*/

$(document).ready(function () {

	/* Activating JS only elements */
	$("body").addClass("js");

	var $newsList = $("ul.news-list"),
		$newsActiveItemText = $("ul.news-list li.selected div.text"),
		$newsItemText = $newsList.find("div.text"),
		$imgList = $("ul.img-list"),
		$imgListActive = $imgList.find("li.active");

	if ($("body.home")) {
		// set up featured news section
		$newsItemText.hide();
		$newsActiveItemText.show();
		$("div.featured-news").addClass("featured-news-add");

		// Featured News
		animatedFeatured("ul.img-list", "ul.news-list");
	}
});

/*-----------------------*/		
/* Window Load Functions */
/*-----------------------*/

$(window).load(function(){

});


/*-----------------------*/
/* NAVIGATION FUNCTIONS  */
/*-----------------------*/
$(document).ready(function () {
	$("ul#top-navigation").children("li").each(function () {
		var menuitemID = parseInt($(this).find("input").val());
		var currentpageID = parseInt($("#CurrentPageID").text());

		/* if menu item id == current page id, make item selected */
		if (menuitemID == currentpageID) {
			//if(console)	console.log(menuitemID + " - " + currentpageID);
			$(this).addClass("selected");
		}

		/* home doesnt have an id, so if currentpage is null and menuitemid is 1 then make this item selected. */
		if (isNaN(currentpageID) && (menuitemID == 1)) { $(this).addClass("selected"); }
	});
});

/*-----------------*/
/* SEARCH TEXT BOX */
/*-----------------*/
$(document).ready(function () {
	var $searchField = $("[id$='SearchBox']");
	var searchText = "search for ...";
	$searchField.val(searchText);

	$searchField.focus(function () {
		if ($(this).val() == searchText) {
			$(this).val("");
		}
	});
	$searchField.blur(function () {
		if ($(this).val() == "") {
			$(this).val(searchText);
		}
	});
});

/*-------------------*/
/* Email Fields      */
/*-------------------*/
$(document).ready(function () {
	var $mailField = $("input.email");
	var mailText = "Enter your email address";
	$mailField.val(mailText);

	$mailField.focus(function () {
		if ($(this).val() == mailText) {
			$(this).val("");
		}
	});
	$mailField.blur(function () {
		if ($(this).val() == "") {
			$(this).val(mailText);
		}
	});
});

/*-------------------*/
/* Password Fields   */
/*-------------------*/
$(document).ready(function () {
	var $passwordField = $("input.password");
	var passwordText = "Enter your password";
	$passwordField.val(passwordText);

	$passwordField.focus(function () {
		if ($(this).val() == passwordText) {
			$(this).val("");
		}
	});
	$passwordField.blur(function () {
		if ($(this).val() == "") {
			$(this).val(passwordText);
		}
    });
});

/*---------------------*/
/* DATE TIME FUNCTIONS */
/*---------------------*/
function fixDate(monthField) {
	var monthIndex = monthField.selectedIndex;
	
	var dayId = monthField.id.replace("dPlayerDobMonth", "dPlayerDobDay");
	var day = document.getElementById(dayId);

	var yearId = monthField.id.replace("dPlayerDobMonth", "dPlayerDobYear");
	var year = document.getElementById(yearId);
	
	// if month is feb
	if (monthIndex == 1) {
		if (isLeapYear(year[year.selectedIndex].value)) {
			// its a leap year - if the selected day is greater then 29 - set it to 29
			if (day.selectedIndex >= 28) day.selectedIndex = 28;
		} else {
			// its not a leap year - set the day to 28 if its greater then 28.
			if (day.selectedIndex >= 27) day.selectedIndex = 27;
		}
	}

	// 30 days hath september, april, june and november.
	if ((monthIndex == 3) || (monthIndex == 5) || (monthIndex == 8) || (monthField == 10)) {
		if (day.selectedIndex >= 29)
			day.selectedIndex = 29;
	}
}

function isLeapYear(year) {
	return ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0));
}

/*--------------*/
/* TEAM TRACKER */
/*--------------*/
$(document).ready(function () {

    var $elem = $("a[rel='results']");
    var $elem2 = $("a[rel='upcoming']");
    
    if ($elem.length > 0) {
        $("a[rel='results']").fancybox({ autoDimensions: false, width: 670, height: 500 });
    }

    if ($elem2.length > 0) {
        $("a[rel='upcoming']").fancybox({ autoDimensions: false, width: 670, height: 500 });
    }
});

/*---------*/
/* RESULTS */
/*---------*/
function filterRound(roundNumber) {
    $('tr').show();
    $(".round-" + roundNumber).parent().children().each(function () { $(this).removeClass("selected"); });
    $(".round-"+roundNumber).addClass("selected");

    if (roundNumber != "all") {
        // we want to iterate over the tableName finding any cell with the class "round"
        $('td.round').each(function () {
            // now, lets test the value of this cell against the value in roundNumber.
            if (($(this).text() * 1) != (roundNumber * 1)) {
                $(this).parent().hide();
            }
        });
    }
}

/*--------------*/
/* TEST METHODS */
/*--------------*/
function fillRegistrationTestData() {
	/* generate a random string for each Name */
	$("[id$='Name']").each(function () {
		$(this).val('test-'+sRand(10));
	});

	/* generate a random string for each Team */
	$("[id$='Team']").each(function () {
		$(this).val('test-'+iRand(999));
	});

	/* generate a random string for each Address */
	$("[id$='Address']").each(function () {
		$(this).val('test-' + sRand(10));
	});

	/* generate a random string for each Address */
	$("[id$='Qualification']").each(function () {
		$(this).val('test-' + sRand(10));
	});

	/* generate a random int for each Phone */
	$("[id$='Phone']").each(function () {
		var phone = "";

		for (i = 0;i<=7;i++)
			phone += iRand(9);

		$(this).val(phone);
	});

    /* generate a random int for each Mobile */
    $("[id$='Mobile']").each(function () {
        var phone = "";

        for (i = 0; i <= 7; i++)
            phone += iRand(9);

        $(this).val(phone);
    });

	/* generate a random int for each Mobile */
	$("[id$='Number']").each(function () {
		var phone = "";

		for (i = 0; i <= 7; i++)
			phone += iRand(9);

		$(this).val(phone);
	});

	/* generate a random int for each Email */
	$("[id$='Email']").each(function () {
		$(this).val('jay@mail.efinity.co.nz');
	});


	/* select a random item for each drop down */
	$("select").each(function () {
		var itemLength = $(this).find("option").length;
		$(this).val( document.getElementById($(this).attr("id"))[iRand(itemLength)].value );
	});

    $("[id$='Password']").each(function () {
        $(this).val("s3cure");
    });
}
function sRand(len)
{
	var text = "";
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

	for( var i=0; i < len; i++ )
		text += possible.charAt(Math.floor(Math.random() * possible.length));

	return text;
}
function iRand(max) {
	return parseInt(Math.floor(Math.random() * parseInt(max)));
}
