/* 20090402ED Javascript for the In Depth section only */

/* Editor's Choice iCarousel  */
window.addEvent(itEventType, function() {	
	if (! $('edchoice') ) { return };

	var itemwidth = 105;
	var scrollCarousel =  new iCarousel("edchoice_content", {
		idPrevious: "edchoice_previous",
		idNext: "edchoice_next",
		idToggle: "undefined",	
		item: {
			klass: "edchoice_item",
			size: 105
		},
		animation: {
			duration: 500,
			amount: 3,
			rotate: {
				type: "auto",
				interval: 7000,
				onMouseOver: "stop"
			}
		}
	});
	$$( 'ul#edchoice_content' )[0].style.width = (scrollCarousel.aItems.length *itemwidth)+"px";
});

/* 20090624 ED  Carousel on LHS e.g. Festivals and Events, Swine Flu  */
window.addEvent(itEventType, function() {
	if (! $('crsl_lhs') ) { return };
	
	var itemnav = $$( 'div#crsl_lhs_frame li' );
	
	var itemwidth = 320;
	var scrollCarousel =  new iCarousel("crsl_lhs_content", {
		idPrevious: "crsl_lhs_previous",
		idNext: "crsl_lhs_next",
		idToggle: "undefined",
		item: {
			klass: "crsl_lhs_item",
			size: 320
		},
		animation: {
			duration: 500,
			amount: 1
		},
		shuffle: false, //turned off for now while developing number list
		randomstart:true, //new function to start at a different panel each time
		
		//set nav-on indicator calls
		onPrevious: function(el){navOn( $$('ul#crsl_lhs_content li' )[ el.atScreen ], itemnav);},
		onNext: function(el){navOn( $$( 'ul#crsl_lhs_content li' )[ el.atScreen ], itemnav);} ,
		onGoTo: function(el){navOn( $$( 'ul#crsl_lhs_content li' )[ el.atScreen ], itemnav);}		
	});
	
	//set the click events on the nav bar
	itemnav.each( function( item, index ) {
		if (item.id.indexOf('crsl_lhs_pn') == 0) {
			item.addEvent("click", function(event){new Event(event).stop(); scrollCarousel.goTo( index - 1  )  }); 
		}
	});
	$$( 'ul#crsl_lhs_content' )[0].style.width = (scrollCarousel.aItems.length *itemwidth)+"px";	
});	

/* extend iCarousel Class to allow for li's to be shuffled - GW 06/04/2009 */
var iCarousel = iCarousel.extend({
  initialize: function(container, options) {
		if ( options.shuffle ) { this._shuffle(container); }
		this.parent(container, options);
	},
	_shuffle: function (container) {

		var ul = $( container );
		var list = ul.getElements( 'li' );
		if (list.length === 0) { return; }

		for (var i=0; i < list.length; i++) {
			var tmp = list[i];
			var randomNum = $random( 0, list.length-1 );
			list[i] = list[randomNum];
			list[randomNum] = tmp;
		}

		list.each( function(li) {
			li.injectInside( ul );
		});	
	}
});

/* extend iCarousel Class randomise start position - eoneill 28/04/2009 */
var iCarousel = iCarousel.extend({
  initialize: function(container, options) {
		this.parent(container, options);
		if ( options.randomstart ) { this._randomstart(container); }
	},
	_randomstart: function (container) {
		var list = $( container ).getElements( 'li' );
		if (list.length === 0) { return; }
		var randomNum = $random( 0, list.length-1 );

		this.goTo( randomNum );
	}
});

//eoneill - new code
//determine which item is on and set indicator
function navOn(currentItem, itemNumList){
	var item_index = parseInt( currentItem.id.split("_")[1] ); 
	// console.log("currentItem.id=",currentItem.id, " itemNumList.length=",itemNumList.length,  " item_index=",item_index);
	
	for(i=0; i<itemNumList.length; i++){
		if(i != item_index){
			itemNumList[i].style.background = "none";
		}else{
			itemNumList[i].style.background = "url(/images/v3/homepage/nav-on.png) center center no-repeat";
		}
	};
	
};
