/* 
* main.js
* Milcord.com main javascript classes
* Author: Dustin Burke
* Date: January 22, 2008
*/ 

  var $ = function(id) { return document.getElementById(id); }

/* **************** CAROUSEL ******************************************************** */
				 
var lastRan = -1;


/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	load(this, start, last);	
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {	

	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
	var start = args[0];
	var last = args[1]; 
	var alreadyCached = args[2];
	
	if(!alreadyCached) {
		load(this, start, last);
	}
};

var load = function(carousel, start, last) {
	for(var i=start;i<=last;i++) {
		//var randomIndex = getRandom(3, lastRan);
		lastRan = i; //randomIndex;
		//carousel.addItem(i, fmtItem(imageList[randomIndex], urlList[randomIndex], titleList[randomIndex]));
		carousel.addItem(i, fmtItem(imageList[i-1], urlList[i-1], titleList[i-1]));
		/*
		// Example of an alternate way to add an item (passing an element instead of html string)
		var p = document.createElement("P");
		var t = document.createTextNode("Item"+i);
		p.appendChild(t);
		carousel.addItem(i, p );
*/
	}
};

var getRandom = function(max, last) {
	var randomIndex;
	do {
		randomIndex = Math.floor(Math.random()*max);
	} while(randomIndex == last);
	
	return randomIndex;
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {

	var enabling = args[0];
	var leftImage = args[1];
	if(enabling) {
		leftImage.src = "images/left-enabled.gif";	
	} else {
		leftImage.src = "images/left-disabled.gif";
	}
	
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {

	var enabling = args[0];
	var rightImage = args[1];
	if(enabling) {
		rightImage.src = "images/right-enabled.gif";	
	} else {
		rightImage.src = "images/right-disabled.gif";
	}
	
};


/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 **/
var carousel; // for ease of debugging; globals generally not a good idea
var pageLoad = function() 
{
	carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
		{
			numVisible:        1,
			animationSpeed:    0.75,
			autoPlay:		   5000,
			wrap:			   true,
			scrollInc:         1,
			navMargin:         0,
			size:			   3,
			prevElement:       "prev-arrow",
			nextElement:       "next-arrow",
			loadInitHandler:   loadInitialItems,
			loadNextHandler:   loadNextItems,
			loadPrevHandler:   loadPrevItems,
			prevButtonStateHandler:   handlePrevButtonState,
			nextButtonStateHandler:	  handleNextButtonState
		}
	);
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);
  
  
/* *************** Navigation Widget Functionality ************************ */
/*
* Nav elements ids prefixed with "nav_" and corresponding text block ids prefixed with "text_"
*
*/

  var currentBlock;

  function onClickNav(navId) {	
	//showTextBlock(navId);
	fadeTextBlock(navId);
	selectNav(navId);
  }

  function fadeTextBlock(block) {

	block = "text_" + block; // DOM id

	if (currentBlock == undefined) {
			currentBlock = findCurrentBlock();
	} 

	if (block == currentBlock) { return; } // no nav change needed
	
	 var myAnimOff = new YAHOO.util.Anim(currentBlock, { 
		opacity: {  from: 1, 
					to: 0 }  
	}, .2, YAHOO.util.Easing.easeOut); 

	 var myAnimOn = new YAHOO.util.Anim(block, { 
		opacity: {  from: 0,
					to: 1 }
	}, .2, YAHOO.util.Easing.easeIn); 

	myAnimOff.onComplete.subscribe(function () {
		var el = this.getEl();
		el.style.display='none';
		$(block).style.display = 'block';	// make sure it's showing for animation
	});

	myAnimOff.onComplete.subscribe(myAnimOn.animate());
	
	myAnimOn.onComplete.subscribe(function () {
		var el = this.getEl();
	});

	
	myAnimOff.animate();
	currentBlock = block;
	
	
  }

   // iterates over text blocks and returns id of the one with display='block' (assume all others are display='none')
   function findCurrentBlock() {
		var blocks = $('textwidget-content').getElementsByTagName('li');
		for (var b=0; b<blocks.length; b++) {
			if (blocks[b].style.display == 'block') 
			return blocks[b].id;
		}		
   }
  
  function showTextBlock(block) {
	var blocks = $('textwidget-content').getElementsByTagName('li');
	for (var b=0; b<blocks.length; b++) {
		if (blocks[b].id == 'text_' + block) 
			blocks[b].style.display = 'block';
		else 
			blocks[b].style.display = 'none';
	}	
  }

   function selectNav(link) {
		var link_list = $('textwidget-nav').getElementsByTagName('li');
		for (var l=0; l<link_list.length; l++) {
			if (link_list[l].id == 'nav_' + link)
				link_list[l].className = 'selected';
			else
				link_list[l].className = 'unselected';
		}
   }

