//START initialise
function initialise() {
	startFade();
}
//END initialise




//START pulse
timeoutPtr = null;
previousMenu = null;

containerId = "panelStories";
highlightClass = "normal";
currentFade = -1;
opacity = 0;
fadeSpeed = "slow";  // this can be slow or fast
minimumWait = 1000;  // base number of seconds to wait
maximumWait = 1000;  // random number of seconds after
highlightPause = 700;


function startFade() {
	setTimeout(fadeRandomIn, 1000);
}


function fadeRandomIn()
{
	if(document.getElementById(containerId) == null)
	{
		return;
	}
	var child_nodes = document.getElementById(containerId).childNodes;
	elementArray = new Array();

	for(var i = 0; i < child_nodes.length; i++)
	{
		if(child_nodes[i].nodeName == "LI")
		{
			elementArray.push(child_nodes[i]);
		}
	}
	//console.log("got " + elementArray.length);

	// now get a random element
	hasChanged = false;

	do
	{
		do
		{
			var randomNumber = Math.floor(Math.random() * elementArray.length);
		} while(randomNumber == currentFade)
		currentFade = randomNumber;

		/*for(var i = 0; i < elementArray[currentFade].childNodes[1].childNodes[3].childNodes.length; i++)
		{
			var loopItem = elementArray[currentFade].childNodes[1].childNodes[3].childNodes[i];

			if(loopItem.className == highlightClass)
			{
				$(loopItem).fadeTo(fadeSpeed, opacity, pauseFor);
				console.log("executed successfully");
				hasChanged = true;
			}
		}*/

		var loopItemId = $(elementArray[currentFade]).attr("id");
		var loopItem = $("li#" + loopItemId + " div div div.normal");

		if(loopItem.length != 0)
		{
			loopItem.fadeTo(fadeSpeed, opacity, pauseFor);
			hasChanged = true;
		}

	} while (hasChanged == false);
}


function pauseFor()
{
	setTimeout(fadeOut, highlightPause);
}


function fadeOut()
{
	var loopItemId = $(elementArray[currentFade]).attr("id");
	var loopItem = $("li#" + loopItemId + " div div div.normal");

	if(loopItem.length != 0)
		loopItem.fadeTo(fadeSpeed, 1, bideYourTime);
}


function bideYourTime()
{
	var randomTime = Math.floor(Math.random() * maximumWait + minimumWait);
	setTimeout(fadeRandomIn, randomTime);
}
//END pulse



function OpenNewWin(sURL,WinWidth,WinHeight)
{
newwindow=open(sURL,"newwin","scrollbars=no,toolbar=no,directories=no,menubar=no,resizable=yes,status=no,width=" + WinWidth + ",height=" + WinHeight);
}



$(function() {

    /*	http://www.reindel.com/five_javascript_tricks_jquery/
        1.) Form Field Value Swap
    */

    swapValues = [];
    $(".swap_value").each(function(i){
        swapValues[i] = $(this).val();
        $(this).focus(function(){
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
                $(this).val(swapValues[i]);
            }
        });
    });
});