var featureList = {
	init:function(){
		featureList.newFeature = bbcjs.dom.first( $('feature-list'), 'li' );
		featureList.loadEMP();
		featureList.tabInit();
	},
	tabInit:function(){
		var tabs = bbcjs.dom.getElementsByClassName('tab');
		for(i=0; i<tabs.length; i++){
			var tablink = bbcjs.dom.first( tabs[i] );
			bbcjs.dom.addEventListener( tablink, 'click', featureList.showTabContent );
		}
	},
	showTabContent:function(e){
		bbcjs.dom.stopEvent(e);
		featureList.newFeature = bbcjs.dom.parent( e.target );
		/* get current feature from its class name - current */
		featureList.currentFeature = bbcjs.dom.getElementsByClassName('current', 'li', $('feature-list'))[0];
		/* remove EMP from current tab */
		featureList.removeEMP();
		/* hide current tab, show new one */
		featureList.swapTabs();
		/* if new feature contains an EMP, load it */
		featureList.loadEMP();
	},
	swapTabs:function(){
		bbcjs.dom.removeClassName( featureList.currentFeature, 'current' );
		bbcjs.dom.addClassName( featureList.newFeature , 'current' );
	},
	removeEMP:function(){
		/* if the current Feature contains an EMP, remove it and replace the text */
		featureList.currentEmp = bbcjs.dom.getElementsByClassName('emp-div', 'div', featureList.currentFeature);
		if(featureList.currentEmp.length > 0){
			featureList.currentEmp = featureList.currentEmp[0];
			empDivText ='<p>The enhanced version of the site requires the Flash 8 plugin (or higher) to be installed and JavaScript to be enabled on your browser. To find out how to install a Flash plugin, go to the <a href="/webwise/askbruce/articles/download/howdoidownloadflashplayer_1.shtml">WebWise Flash install guide</a></p><noscript><p>To find out how to turn on JavaScript, go to the <a href="/webwise/askbruce/articles/browse/java_1.shtml">WebWise JavaScript guide</a></p></noscript>';
			featureList.currentEmp.innerHTML = empDivText;
		}
	},
	loadEMP:function(){
		featureList.newEmp = bbcjs.dom.getElementsByClassName('emp-div', 'div', featureList.newFeature);
		if(featureList.newEmp.length > 0){
			featureList.newEmp = featureList.newEmp[0];
			var emp = new embeddedMedia.Player();  
			emp.setWidth("400");
			emp.setHeight("260");
			emp.setDomId(featureList.newEmp.id);
			emp.setPlaylist(playlist[featureList.newEmp.id]);
			emp.write();
		}
	}
}

if (document.documentElement && document.getElementById){
	bbcjs.addOnLoadItem(featureList.init);
}


/* carousel code - http://ideweb01.national.core.bbc.co.uk:8043/carousel/ */
var bloomCarousel;
var buildBloomCarousel = {
	init : function () {
		glow.dom.get("#actions-carousel").css( "display", "block" );
		glow.dom.get("#load").css( "display", "none" );
		bloomCarousel = new Carousel('#actions-carousel', {loop : true, visibleItems : 7});			
		var controls = glow.dom.create('' +
		  '<div class="controls">' +
		    '<a id="previous" href="javascript:bloomCarousel.move(1)"><span>previous</span></a>' +
		    '<a id="next" href="javascript:bloomCarousel.move(-1)"><span>next</span></a>' +
		  '</div>'
		);
		glow.dom.get('#actions-carousel').after(controls);
		// Catch events from Carousel
		// Can move Next and Previous
		glow.events.addListener(
			bloomCarousel,
			'canDoPrevious',
			function () {glow.dom.get("#previous").removeClass("inactive")}
		);
		glow.events.addListener(
			bloomCarousel,
			'canDoNext',
			function () {glow.dom.get("#next").removeClass("inactive")}
		);							
		// Can't do Next / Previous
		glow.events.addListener(
			bloomCarousel,
			'cantDoPrevious',
			function () {glow.dom.get("#previous").addClass("inactive")}
		);
		glow.events.addListener(
			bloomCarousel,
			'cantDoNext',
			function () {glow.dom.get("#next").addClass("inactive")}
		);		
		// Set up items events mouseout/over
		var items = bloomCarousel.getItems();
		items.each(function(i) {
			// Set all items opacity to 75%
			glow.dom.get(this).css("opacity", "0.75");							
			// Add mouse out event
			glow.events.addListener(
				glow.dom.get(this),
				'mouseout',
				function() {
					glow.anim.css(	glow.dom.get(this),
									0.15,
									{"opacity" : {to:0.75}}
					).start();
				},
				this
			);
			// Add mouseover event
			glow.events.addListener(
				glow.dom.get(this),
				'mouseover',
				function() {
					glow.anim.css(	glow.dom.get(this),
									0.15,
									{"opacity" : {to:1}}
					).start();
				},
				this
			);				
		});
	}
}
glow.onDomReady(buildBloomCarousel.init);