/*
	Created by Les Argonautes - 23/07/09
	http://www.les-argonautes.com
	Working with Mootools < 1.2.3 (Fx.Scroll bug on Mootools More 1.2.3)
		+ scrollerMoo 1.0
*/
	/* @Active Season Link & call function to active Episodes List */
	function setSeaActive(seaID, epID)
	{
		if($$('#saisons ul li').length != 0)
		{
			if(seaID=='first' || seaID==undefined) {
				seaID = $$('#saisons ul li')[0].id;
				seaID = seaID.substr(4);
			}
			$$('#saisons li a').removeClass('active');
			$('sea_'+seaID).getFirst().addClass('active');

			// +Call function to active Episodes List
			setEpListActive(seaID, epID);
		}
	}

	/* @Display Episodes list & call function to active called (or first) Episode link and display Episode summary */
	function setEpListActive(epListID, epID)
	{
		if(epListID=='first' || epListID==undefined) {
			epListID = $$('.episodes_details')[0].id;
		}
		$$('.episodes_details').setStyle('display','none');
		$('sealist_'+epListID).setStyle('display','block');
		
		// +Call function to active called (or first) Episode link and display Episode summary
		setEpActive(epListID, epID);
	}
	
	/* @Active Episode link and display Episode summary */
	function setEpActive(seaID, epID)
	{
		if(epID=='first' || epID==undefined) {
			epURI = new URI($$('#sealist_'+seaID+' ul li')[0].getFirst().href);
			epData = epURI.getData(null, 'fragment');
			epID = epData.e;
		}
		$$('#sealist_'+seaID+' .scrollerMooWrapper li a').removeClass('active');
		$$('#sealist_'+seaID+' .episodes_resume div').setStyle('display','none');
		$('id_'+epID).getFirst().addClass('active');
		$('art_'+epID).setStyle('display','block');
		myscrollerMoo['sealist_'+seaID].scrollToElement($('id_'+epID).getFirst());
	}
	
	/* @Set hash vars from link to address bar */
	function setHashVar(argLink)
	{
		/* +URI configuration*/
		currentURI = new URI(window.location.href);
		//currentDatas = currentURI.getData(null, 'fragment');
		
		/* +link URI */
		linkURI = new URI(argLink.href);
		linkDatas = linkURI.getData(null, 'fragment');
		
		/* +final datas */
		//finalDatas = $merge(currentDatas, linkDatas);
		finalDatas = linkDatas;
		
		/* +data set */
		currentURI.setData(finalDatas, false, 'fragment');
		
		/* +go URI */
		currentURI.go();
		
		/* +set */
		setSeaActive(finalDatas.s, finalDatas.e);
	}
	
	window.addEvent('domready',function()
	{
		/* @Episodes list scrolling */
		/* !Doesn't work correctly with Mootools 1.2.3 due to Offset bug, patched with offset fx of 1.2 */
		myscrollerMoo = new Array();
		$$('.episodes_details').each(function(episodes_details)
		{
			myscrollerMoo[episodes_details.id] = new scrollerMoo(episodes_details,
				{
					steps:5,
					wrapperClass:'ul.scrollerMooWrapper',
					scrollerItemSelector:'li.scrollerMooItem'
				}
			);
		});
		
		/* @Seasons-Episodes loading */
		var currentURI = new URI(window.location.href);
		currentData = currentURI.getData(null, 'fragment');
			
		if(currentData.s)
		{
			setSeaActive(currentData.s, currentData.e);
		}
		else
		{
			setSeaActive('first', 'first');
		}
		
		/* @Seasons-Episodes click event */
		$$('#saisons li a,.episodes_details li a').each(function(mylink) {
			mylink.addEvent('click', function() {
				setHashVar(mylink);
				return false;
			});
		});
	});