/*												 
-------------------------------------------------------------------------------------------
	Copyright (c) Intersel 2007-2008 - Tous droits r serv s 
-------------------------------------------------------------------------------------------
   	INTERSEL - SARL au capital de 12.000 € - RCS PARIS 488 379 660 - NAF 721Z 
	4 Cit  d'Hauteville
	75010 PARIS  
-------------------------------------------------------------------------------------------
	File : pictos-viewer
	
	Abstract : 	  shows pictos in sequence
			(used to manage the visualisation of the partner and member pictos in the home page)
	Author : Emmanuel PODVIN - emmanuel.podvin@intersel.org - copyright Intersel 2008
	Modifications :
		- 12/01/08 - EPO - Creation
    - 02/04/2011 - EPO - modification pour Incuballiance
   ------------------------------------------------------------------------------------------- 
inspired from "visionneuse d'images"
Script :  http://www.jejavascript.net/visionn.php
//PLF-http://www.jejavascript.net/

Pour mettre un lien hypertexte sur les images, les nommer <nom_lien>.jpg|png|gif
ex : www.intersel.org.jpg

*/

/*--------------------------------------------------------------------
//	Function name : pictos_viewer
//	Description		: 	Class Object - Object managing the display of picto in an IMG object
//	input 			:					   
//  return value 	: an Object 
//--------------------------------------------------------------------	   
*/	 
								  
var AutoDisplay_Interval=4000; //delay to display the next picto 
//var AutoDisplay_StartAutoDisplayDelay=4000; // delay before starting the autodisplay	
var DebugDivId='';//debug'; // div where we can put some debug message

// internal variables
var GlobalTimers  = new Array(); // timers set
var GlobalViewers = new Array(); // picto viewers object

function AppendInnerHtml(anId,aMessage)
{
  if (anId!="") 
  {
    $('#'+anId).html($('#'+anId).html()+aMessage+"<br>");
  }
}

function pictos_viewer(aPictoObjectId,aPictoURLObjectId,aFileDir,aPictoFilenamesArray,StartAutoDisplayDelay) 
{
	this.directory=aFileDir;
	this.PictoId = 0;	
	this.length = aPictoFilenamesArray.length;
	this.PictoFilenames=aPictoFilenamesArray; 
	this.PictoObjectId=aPictoObjectId;
	this.PictoURLObjectId=aPictoURLObjectId;			//@add by MP
	this.PictoObject=$('#'+aPictoObjectId);
	this.PictoURLObject=$('#'+aPictoURLObjectId);
	GlobalViewers[this.PictoObjectId]=this;	

  this.PictoObject.attr("src", aPictoFilenamesArray[this.length-1]);	
	this.InitStartAutoDisplay(StartAutoDisplayDelay);
	
	AppendInnerHtml(DebugDivId,'PictoObjectId+Length:'+this.PictoObjectId+' '+this.length);//debug message
}

// Class Public function definitions
pictos_viewer.prototype.GoNextPicto = function() 
{	 
	this.RestartTimerPicto();
	this.nextPicto(); 	 

	AppendInnerHtml(DebugDivId,'ClickNext');//debug message
	
}	
		
pictos_viewer.prototype.GoPreviousPicto = function() 
{
	this.RestartTimerPicto();
	this.previousPicto(); 	 

	AppendInnerHtml(DebugDivId,'ClickNext');//debug message
	
}
		
// Class private function definitions
pictos_viewer.prototype.nextPicto = function() 
{	 
	this.PictoId += 1;
	if (this.PictoId >= this.length ) this.PictoId = 0;
	this.ShowPicto(); 	 

	AppendInnerHtml(DebugDivId,'PictoId:'+this.PictoId);//debug message
	
}	
		
pictos_viewer.prototype.previousPicto = function() 
{
	this.PictoId -= 1;
	if (this.PictoId < 0) this.PictoId = this.length-1;
	this.ShowPicto(); 			
	
	AppendInnerHtml(DebugDivId,'PictoId:'+this.PictoId);//debug message
}
		
pictos_viewer.prototype.ShowPicto = function() 
{

	var anImgObj = $(this.PictoObject);
	var anURLObj = $(this.PictoURLObject);								//@add by MP
	
	var NextPicto = this.directory+'/'+this.PictoFilenames[this.PictoId];
	var urlRE=/^(http:\/\/)([a-zA-Z0-9]+\.?)+\.([-a-zA-Z0-9]+)/;			//@add by MP
	var posExt = (this.PictoFilenames[this.PictoId]).lastIndexOf(".");		//@add by MP
    var url = (this.PictoFilenames[this.PictoId]).substring(0, posExt);		//@add by MP
	
	if (urlRE.test("http://" + url))
		anURLObj.attr('href','http://'+url);										//@add by MP
	else
		anURLObj.attr('href','#');										//@add by MP
		
	AppendInnerHtml(DebugDivId, 'NextPicto:'+NextPicto);//debug message
  anImgObj.animate(
    							  {opacity: 0},
    							  200,   function() {anImgObj.attr("src", NextPicto);}
                    );
  
  anImgObj.animate(
  						  {opacity: 1},
  						  200, function() {});


	//AppendInnerHtml(DebugDivId,'PictoObject.src:'+this.PictoObject.src);//debug message
}


function testURL(url){
  
  return urlRE.test(url);
  }
		
pictos_viewer.prototype.InitStartAutoDisplay = function(aStartAutoDisplayDelay) 
{
  AppendInnerHtml(DebugDivId,'InitStartAutoDisplay: '+this.PictoObjectId);//debug message

	this.PictoId--;//in order to cancel StartAuto/nextPicto... 
	this.StartAuto(aStartAutoDisplayDelay);
	
}
		
pictos_viewer.prototype.StartAuto = function(TimeOutDelay) 
{
  AppendInnerHtml(DebugDivId,'StartAuto: '+this.PictoObjectId);//debug message
  
  this.TimeOutDelay=TimeOutDelay;

	this.nextPicto();
  this.RestartTimerPicto();
	
}
		
pictos_viewer.prototype.RestartTimerPicto = function ()
{
	if (GlobalTimers[this.PictoObjectId]!=null) window.clearTimeout(GlobalTimers[this.PictoObjectId]);													   
	GlobalTimers[this.PictoObjectId]=setTimeout("StartAutoDisplayPicto('"+this.PictoObjectId+"')", this.TimeOutDelay);
}

function StartAutoDisplayPicto(aPictoId)
{
	AppendInnerHtml(DebugDivId,'aPictoId:'+aPictoId);//debug message
	GlobalViewers[aPictoId].StartAuto(AutoDisplay_Interval);
}

	
//------------- end class pictos_viewer  ------------------

/*
function initPicto()
{
	pictoList = new Array ("logo_caisse_gros.jpg", "logo_total.gif", "orange.gif");	  
	MyViewer = new pictos_viewer('PictoMember','../images/pictos-membres/', pictoList);
	MyViewer2 = new pictos_viewer('PictoMember2','../images/pictos-membres/', pictoList);
}
 */
