// JavaScript Document
// MainMenu Static API
//<![CDATA[	   
SlidingPanels = function(panelID, container, totalItem, itemDisplayed, dossierId, IDLan, pageId)
{
	this.mActiveElement = null;
	this.mSp = null;
	
	this.mPanelId = panelID;
	this.mTotalItem = totalItem+itemDisplayed-1;
	this.mItemDisplayed = itemDisplayed;
	this.mContainerStr = container;
	this.mContainer = document.getElementById(container);
	//this.mLoadedIndex = 0;
	this.mPageArray = new Array();

	//Page Array initiation
	for(var x = 0; x < Math.ceil(totalItem / itemDisplayed); x++)
	{
		this.mPageArray[x] = false;
	}
	
	this.mActivateListener = null;
	
	this.IDLan = IDLan;
	this.dossierId=dossierId;
	this.pageId=pageId-1;
	
	this.mItemsLoadedInPanel = 0;
	
	this.mCurrentIndex = this.pageId * this.mItemDisplayed;
	this.mCurrentPanelIndex = 0;
	this.loadPage(this.pageId,dossierId,itemDisplayed,IDLan,{firstTime:true, i:this.pageId});
	this.activateArrows();
	
}

SlidingPanels.prototype.next = function()
{

	var nextPanel = this.mCurrentIndex + this.mItemDisplayed;
	if (nextPanel > this.mTotalItem - this.mItemDisplayed) {
		nextPanel = this.mTotalItem - this.mItemDisplayed;

	}
	
	this.setCurrentPanelByIndex(nextPanel);
}

SlidingPanels.prototype.previous = function()
{

	
	
	if (this.mCurrentIndex > this.mTotalItem - this.mItemDisplayed)
	{
		
		var delta = this.mItemDisplayed - (this.mTotalItem - (this.mCurrentIndex + 1));
		var previousPanel = this.mCurrentIndex - delta - this.mItemDisplayed + 1;
	}
	else
	{
		var previousPanel = this.mCurrentIndex - this.mItemDisplayed;
	}
	if (previousPanel < 0) {
		previousPanel = 0;	
	}

	this.setCurrentPanelByIndex(previousPanel);
}

SlidingPanels.prototype.activateElement = function( element, id, clip, idClip, ImageHR)
{
	if(element)
	{
		if(document.getElementById('item1_content'))
			document.getElementById('item1_content').className=document.getElementById('item1_content').className.replace(" over","");
		
		if( element != this.mActiveElement )
		{
			
			if (this.mActivateListener) {
				typeMedia='video';
				if(clip.indexOf('wma')!=-1) typeMedia='audio';
				/* Cette fonction appelée ci-bas est dans updateClip.js */
				this.mActivateListener(clip,typeMedia,id,idClip,ImageHR);
			}
			
			element.className=element.className.replace("whs_content","whs_content_active");	
			
			if(this.mActiveElement != null)
			{				
				this.mActiveElement.className = this.mActiveElement.className.replace("whs_content_active","whs_content");		
				this.itemRollout(this.mActiveElement);
			}

			this.mActiveElement = element;
			
			this.setCurrentPanelById(id);
		}
	}
}

/**
 * @param index Index de l'item en tete de liste
 */
SlidingPanels.prototype.setCurrentPanelByIndex = function(index)
{

	var destinationPage = Math.floor(index / this.mItemDisplayed);

	// Est-ce qu'on charge la page suivante?
	var nextDestinationPage = destinationPage + 1;

	if((index % this.mItemDisplayed != 0 )
	   && (nextDestinationPage <  Math.ceil(this.mTotalItem / this.mItemDisplayed))
	   && this.mPageArray[nextDestinationPage] == false)
	{
		this.loadPage( nextDestinationPage, this.dossierId, this.itemDisplayed, this.IDLan,  {callback:"this.setCurrentPanelByIndex(" + index + ")", i:nextDestinationPage, loadPrevious:false});
	}
	else
	{

	if( this.mPageArray[destinationPage] == false)
	{
		var loadPreviousPage = false;
		if(this.mCurrentIndex > index )
			loadPreviousPage = true;
			
		this.loadPage( destinationPage, this.dossierId, this.itemDisplayed, this.IDLan,  {callback:"this.setCurrentPanelByIndex(" + index + ")", i:destinationPage, loadPrevious:loadPreviousPage});
	}
	else
	{

		var deltaMove = index - this.mCurrentIndex;

		this.mCurrentPanelIndex = this.mCurrentPanelIndex + deltaMove;

		if(index > (this.mTotalItem - this.mItemDisplayed))
		{

			this.mSp.showPanel(this.mItemsLoadedInPanel - this.mItemDisplayed);
		}
		else
		{
			this.mSp.showPanel(this.mCurrentPanelIndex);
		}
		
		this.mCurrentIndex = index;
		

		this.activateArrows();
	}
	}

}

SlidingPanels.prototype.setCurrentPanelById = function(id)
{
	var index = this.mSp.getContentPanelIndex(id);

	var firstPageLoaded = 0;
	while(this.mPageArray[firstPageLoaded] == false)
	{
		firstPageLoaded++;
	}
	index = firstPageLoaded * this.mItemDisplayed + index;
	this.setCurrentPanelByIndex(index);
}

SlidingPanels.prototype.itemRollover = function( element ) 
{
	if(document.all && element)
		element.className+=" over";
}

SlidingPanels.prototype.itemRollout = function( element )
{
	if(document.all && element)
		element.className=element.className.replace(" over","");
}

SlidingPanels.prototype.isPageLoaded = function ( index )
{
	return this.mPageArray[index];
}

SlidingPanels.prototype.setPageLoaded = function ( index )
{
	this.mPageArray[index] = true;
}

SlidingPanels.prototype.loadPage = function( pageId, dossierId, itemDisplayed, IDLan, arguments )
{
	var callback =
	{
	  success: wsh_handleSuccess,
	  failure: wsh_handleFailure,
	  argument: {obj:this, args:arguments}
	};
	
	// ** URL TEST originale -- appelant une page NON dynamique *****
	//var sUrl = "clip3.php?page=" + (pageId+1) + "&IDLan=0&IDClip=1781&IDCat=&IDCatPa=";
	// ** URL appelée pour la page DOSSIER *****
	var sUrl = "/dossier_widget.asp?page=" + (pageId+1) + "&IDDossier=" + dossierId + "&itemDisplayed=" + this.mItemDisplayed + "&IDLan=" + IDLan; 
	// ** Condition pour appeler l'URL pour la page C'EST ARRIVE LE - DATE *****
	if(this.mContainerStr=="whs_container_c_a_l_date") {
		//var sUrl = "/date_widget.asp?page=" + pageId + "&IDDossier=" + dossierId + "&itemDisplayed=" + this.mItemDisplayed + "&IDLan=" + IDLan; 
	}
	//alert("inside JS file.... Page:"+this.pageId)
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}

SlidingPanels.prototype.onHandleSuccess = function(args, response)
{
	var startIndex = 0;
	var tempElement = null;
	if(response !== undefined)
	{
		if (args.firstTime) {
			this.mContainer.innerHTML = response;	
			this.mItemsLoadedInPanel += this.mItemDisplayed;

		} else if (args.loadPrevious == true) {			
			this.mContainer.innerHTML = response + this.mContainer.innerHTML;
			this.mItemsLoadedInPanel += this.mItemDisplayed;

		// Décale l'index de l'item courrant pour donner l'illusion qu'on
			// reste sur le même item dans le slider
			var nbAdded = (this.mCurrentIndex - this.mItemDisplayed >= 0 ? this.mItemDisplayed : this.mCurrentIndex);
			this.mCurrentPanelIndex += nbAdded;
		} else {
			this.mContainer.innerHTML += response;	
			// derniere page?
			if (args.i == Math.floor(this.mTotalItem / this.mItemDisplayed))
			{
				
				this.mItemsLoadedInPanel += this.mTotalItem % this.mItemDisplayed;

			}
			else
			{
				this.mItemsLoadedInPanel += this.mItemDisplayed;

			}
			

		}

		this.mPageArray[args.i] = true;

		/* condition pour highlighter le premier clip qui vient d'arriver sur la page */
		if(this.mActiveElement != null) {
			this.mActiveElement = document.getElementById(this.mActiveElement.id);
		} else {
			if(document.getElementById('item1_content')) document.getElementById('item1_content').className+=" over";
		}

		if(args.firstTime)
		{
			this.mSp = new Spry.Widget.SlidingPanels(this.mPanelId);
		}
		else
		{
			if(args.callback)
			{

				this.mSp = new Spry.Widget.SlidingPanels(this.mPanelId, {defaultPanel:this.mCurrentPanelIndex});				

				eval(args.callback);
			}
		}		
	}
}

SlidingPanels.prototype.onHandleFailure = function()
{
	this.mContainer.innerHTML = "<div class=\"whs_loading\">Erreur de chargement - la communication n'a pu être complétée</div>";
}

SlidingPanels.prototype.addActivateListener = function(fct)
{
	this.mActivateListener = fct;	
}

function wsh_handleSuccess(o)
{
	if (o.argument.obj) {
		o.argument.obj.onHandleSuccess(o.argument.args, o.responseText);
	}
}

function wsh_handleFailure(o)
{
	if (o.argument.obj) {
		o.argument.obj.onHandleFailure();
	}	
}


/* sert à déterminer si les flèches sont actives (droite et gauche) */
SlidingPanels.prototype.activateArrows = function() {
	
	if(document.getElementById('whs_id_btn_right')) {
		document.getElementById('whs_id_btn_right').src="/images/btn_right.jpg";
		document.getElementById('whs_id_btn_left').src="/images/btn_left.jpg";
		if(this.mCurrentIndex==0) {
			document.getElementById('whs_id_btn_left').src="/images/btn_left_off.jpg";
		}
		if(this.mCurrentIndex>=(this.mTotalItem-this.mItemDisplayed)) {
			document.getElementById('whs_id_btn_right').src="/images/btn_right_off.jpg";
		}

	}
}

//]]>
