﻿if (window != top) {
  top.location = window.location;
}

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) {

	//alert(filterContainer)
	var DivToShow=document.getElementById(ToShow);
	var DivToHide=document.getElementById(ToHide);
    filterContainer.filters[0].Apply();   
   // filterContainer.filters.item(0).Apply();

    DivToHide.style.visibility = "hidden";
	DivToHide.style.display = "none";
    DivToShow.style.display = "block";
    DivToShow.style.visibility = "visible";
	filterContainer.filters[0].Play();
	// filterContainer.filters.item(0).Play();	
		
} 	
function blendimage(divid1, divid2, millisec) {
//alert(divid1);
//alert(divid2);
	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; 
}

