function addUnLoadEvent(func) 
{
   /* var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') 
        window.onunload = func;
    else 
    {
        window.onunload = function() 
            {
                if (oldonunload) 
                    oldonunload();
                func();
            }
    }*/
}

//**************************************************************************//
//***                     Slider.js                                      ***//
//**************************************************************************//


//***************** Slide *************************	

/* Slide object
 * 'DivsContainer' - The div that contain all the slides
 * 'SlideArray' -  array of divs to change
 * 'TimeOutInMillSec' - timeout in millseconds
 * 'IntervalInMillSec' - intterval in millseconds
 * 'CurrentIndex' - current index of  dispay div
 * 'ObjectName' - name(id) of slide object
*/


function Slide(DivsContainer, SlideArray, TimeOutInMillSec, IntervalInMillSec, CurrentIndex, ObjectName)
{
	if( document.getElementById(DivsContainer) == null )
		return;
	this.Container = document.getElementById(DivsContainer);
	this.SlideArray=SlideArray;
	this.TimeOut=null;
	this.TimeOutInMillSec=TimeOutInMillSec;
	this.Interval=null;
	this.IntervalInMillSec=IntervalInMillSec;
	this.CurrentIndex=CurrentIndex;
	this.ObjectName = ObjectName;	
	this.StartTimeInMsec  = null;
	this.ContinueTimeInMsec = null;
	this.GapTimeInMsec      = null;
	this.TimeOutContinue   = null;
	
	
	var selfSlideObj = this;
	
	//destractor for current object
	this.cleanUp = function()
    {
        selfSlideObj = null; 
    }	
	addUnLoadEvent(this.cleanUp);
	
	
	//display div with current index and set interval
	
	this.startSlide=function(){

		if(this.SlideArray!=null)
		{
			for(i=0; i<this.SlideArray.length; i++)
			{
				elm=document.getElementById(this.SlideArray[i]);
				if(elm!=null)
				{
					if(i==this.CurrentIndex){
						elm.style.display="block";
						elm.style.visibility = "visible";
						}
					else{
						elm.style.visibility = "hidden";
						elm.style.display="none";
						}
				}
			}
			this.restartInterval();
			
		}
	}


///This method invoke when the time out for the manual slide move ended
    this.restartInterval = function () {
    
	if(this.SlideArray!=null)	{
		
		if(this.SlideArray.length>1)
		{
			//Clear the time out if needed
			if(this.TimeOut!=null) {
				window.clearTimeout(this.TimeOut);
			}			
			
			//Restart the timeout					
			func=this.ObjectName + ".moveMagazineAuto(1)";
			this.Interval = setInterval(func,this.IntervalInMillSec);			
			
		}		
	}
}




///This method move the slide

   this.moveMagazineAuto=function (direction) {
   	if(direction==null)
		direction=1;		 
	///Validate array
	if(this.SlideArray==null)
		return;
	var targetIndex=0;
	if(direction==-1 && this.CurrentIndex==0)
	{
		targetIndex = this.SlideArray.length-1;	
	}
	else 
	{
		if(direction==1 && this.CurrentIndex==this.SlideArray.length-1)
			targetIndex = 0;
		else
			targetIndex = this.CurrentIndex + direction;
	}
		
	
	if(document.getElementById(this.SlideArray[targetIndex])!=null && document.getElementById(this.SlideArray[this.CurrentIndex])!=null) {
		if(detectBrowser()) 
		{			
			Switch(this.SlideArray[targetIndex],this.SlideArray[this.CurrentIndex],this.Container);
		}

		else
		{
			
			var strIdL1=""+this.SlideArray[targetIndex];					
			var strIdL2=""+this.SlideArray[this.CurrentIndex];	
			document.getElementById(strIdL1).style.display="block";				
			document.getElementById(strIdL1).style.visibility = "visible";									
			blendimage(strIdL1,strIdL2,600);	
			document.getElementById(strIdL2).style.display="none";					
		}
		
		this.CurrentIndex=	targetIndex;	
		currentTime = new Date();
		this.StartTimeInMsec = currentTime.getTime();	
		if(this.Interval==null)
			this.restartInterval();
	}
}
	
	///This method invoke when the user move the magazine by manual
	this.moveMagazine= function (direction) {
	
		///Rotate the magzine
		this.moveMagazineAuto(direction);
		///Clear interval if needed
		if(this.Interval!=null)
			window.clearInterval(this.Interval);
		///Set time out for restart interval
		func=this.ObjectName + ".restartInterval()";
		this.TimeOut = window.setTimeout(func, this.TimeOutInMillSec);
		this.restartInterval();
	}
	
	this.stopMagazine = function (){	
	if(this.Interval!=null) {
		currentTime = new Date();
		this.ContinueTimeInMsec = currentTime.getTime();		
		window.clearInterval(this.Interval);
		this.Interval=null;	
				
	}
	if(this.TimeOutContinue!=null)		
		window.clearTimeout(this.TimeOutContinue);
			
	} 
	
	///This method invoke when calculate the time slide move
    this.ContinueInterval = function () 
    {   
		if(this.SlideArray!=null)	
		{

			if(this.SlideArray.length>1)
			{
					if(this.StartTimeInMsec==null)
					{
						if(this.Interval==null)
							this.restartInterval();
					}
					else
					{			
					
						//Calculate the timeout		
						this.GapTimeInMsec= this.ContinueTimeInMsec - this.StartTimeInMsec;		
						if(this.GapTimeInMsec >= this.IntervalInMillSec)	
						{
							this.moveMagazineAuto();
						}		
						else
						{
							//Clear the time out if needed
							if(this.TimeOut!=null) {
								window.clearTimeout(this.TimeOut);
							}
							func=this.ObjectName + ".moveMagazineAuto(1)";				
											
							var contIntervalInMsec=this.IntervalInMillSec-this.GapTimeInMsec;				
							this.TimeOutContinue = setTimeout(func,contIntervalInMsec);
							
						}
					}
			
				
			}		
		}
	}// end of ContinueInterval
	
}//End of Slide




function Switch(ToShow,ToHide, filterContainer) {

	var DivToShow=document.getElementById(ToShow);
	var DivToHide=document.getElementById(ToHide);
    filterContainer.filters[0].Apply();
    DivToHide.style.visibility = "hidden";
	DivToHide.style.display = "none";
    DivToShow.style.display = "block";
    DivToShow.style.visibility = "visible";
	filterContainer.filters[0].Play();	
		
} 	
function blendimage(divid1, divid2, millisec) {
	var speed = Math.round(millisec / 100); 
	var timer = 0; 
	for(i = 0; i <= 100; i++) { 
		setTimeout("changeOpac(" + i + ",'" + divid1 + "','"+ divid2+ "')",(timer * speed)); 
		timer++; 
	} 
}
function detectBrowser() {
	var browser=navigator.appName
	if (browser=="Microsoft Internet Explorer")
		return true;
	else
		return false;
}
function changeOpac(opacity, id1,id2) { 
	var object1 = document.getElementById(id1).style; 
	var object2 = document.getElementById(id2).style;
	object1.opacity = (opacity / 100);
	object2.opacity = 0; 
}

/***************************************************************************/
/*					    LoadSliderObject (for flash)
/***************************************************************************/

function LoadSliderObject(sHeight, sWidth, swfImgQuad )
{
	var ihStr='';
	ihStr+='<OBJECT id="Reshet" codeBase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="'+sHeight+'" width="'+sWidth+'" align="middle" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" VIEWASTEXT>';
	ihStr+='<param name="_cx" VALUE="5292">';
	ihStr+='<param name="_cy" VALUE="1588">';
	ihStr+='<param name="FlashVars" VALUE="">';
	ihStr+='<param name="Movie" VALUE="'+swfImgQuad+'">';
	ihStr+='<param name="Src"  VALUE="'+swfImgQuad+'">';
	ihStr+='<param name="WMode" VALUE="Transparent">';
	ihStr+='<param name="Play" VALUE="0">';
	ihStr+='<param name="Loop" VALUE="-1">';
	ihStr+='<param name="Quality" VALUE="High">';
	ihStr+='<param name="SAlign" VALUE="">';
	ihStr+='<param name="Menu" VALUE="-1">';
	ihStr+='<param name="Base" VALUE="">';
	ihStr+='<param name="AllowScriptAccess" VALUE="sameDomain">';
	ihStr+='<param name="Scale" VALUE="ShowAll">';
	ihStr+='<param name="DeviceFont" VALUE="0">';
	ihStr+='<param name="EmbedMovie" VALUE="0">';
	ihStr+='<param name="BGColor" VALUE="FFFFFF">';
	ihStr+='<param name="SWRemote" VALUE="">';
	ihStr+='<param name="MovieData" VALUE="">';
	ihStr+='<param name="SeamlessTabbing" VALUE="1">';
	ihStr+='<param name="Profile" VALUE="0">';
	ihStr+='<param name="ProfileAddress" VALUE="">';
	ihStr+='<param name="ProfilePort" VALUE="0">';
	ihStr+='<EMBED src="'+swfImgQuad+'" loop="true" menu="false" quality="high" bgcolor="#FFFFFF" WIDTH="'+sWidth+'" HEIGHT="'+sHeight+'" NAME="Reshet" ALIGN="middle" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>';
	ihStr+='</OBJECT>';
	return ihStr;

	
}


