/* Script from content page*/
//create array of offer image id's
var offerimages = new Array(3);
//add image names
offerimages[0] = "offer1";
offerimages[1] = "offer2";
offerimages[2] = "offer3";

//create array of offer button id's
var offerbuttons = new Array(3);
//add button names
offerbuttons[0] = "but1";
offerbuttons[1] = "but2";
offerbuttons[2] = "but3";

//create array of offer link id's
var offerlnks = new Array(3);
offerlnks[0] = "lnk1";
offerlnks[1] = "lnk2";
offerlnks[2] = "lnk3";

//create offer rotator, 10 second timing
var rotator = new OfferRotator('rotator',10000,offerimages,offerbuttons,offerlnks);

/* Script from arrivals departures page*/
//create array of offer image id's
var bannerimages = new Array(4);
//add image names
bannerimages[0] = "banner1";
bannerimages[1] = "banner2";
bannerimages[2] = "banner3";
bannerimages[3] = "banner4";

//create array of offer button id's
var bannerbuttons = new Array(4);
//add button names
bannerbuttons[0] = "ban1";
bannerbuttons[1] = "ban2";
bannerbuttons[2] = "ban3";
bannerbuttons[3] = "ban4";

//create array of offer link id's
var bannerlnks = new Array(4);
bannerlnks[0] = "banlnk1";
bannerlnks[1] = "banlnk2";
bannerlnks[2] = "banlnk3";
bannerlnks[3] = "banlnk4";

//create offer rotator, 10 second timing
var banRotate = new OfferRotator('banRotate',10000,bannerimages,bannerbuttons,bannerlnks);


/*----Object for managing rotation of offers----*/
function OfferRotator(name, interval, arrayimg, arraybut, arraylink) 
{
	/*----Properties----*/
	
	//set object name for later reference
 	this.name = name;
 	
 	//timeout instance
 	this.timeoutinst = 0;
 	
 	//set rotation interval
 	this.interval = interval;
 	
 	//array of image ids for rotating
 	this.images = arrayimg;
 	
 	//array of 'button' ids for rotating
 	this.buttons = arraybut;
 	
 	//array of link ids for rotating
 	this.links = arraylink
 	
 	//index of current image
 	this.currentimage = 0;
 	
 	//if rotation paused or not (1= paused, 0= not paused)
 	this.ispaused = 1;
 	
 	/*----End Properties----*/
 	
 	/*----Methods----*/
 	
 	//Changes which offer is shown
 	/*-----------------------------------------------*/
 	this.changeoffer = function(number) 
 	{
 		//clear any current timeout instance
  		if (this.timeoutinst != 0) 
  		{
			clearTimeout(this.timeoutinst);
		}
		
		/*----Images---------------------------------*/
 		//get name of new image
    	var imgname = this.images[number];
    	//get name of current image
    	var currentname = this.images[this.currentimage];
    	
    	//get new image
  		var newimg = this.getElementById(imgname);
  		//get current image
  		var oldimg = this.getElementById(currentname);
  		
  		/*----Buttons-------------------------------*/
  		//get name of new button
  		var newbutname = this.buttons[number];
  		//get name of old button
  		var oldbutname = this.buttons[this.currentimage];
  		
  		//get new button
  		var newbut = this.getElementById(newbutname);
  		//get old button
  		var oldbut = this.getElementById(oldbutname);
  		
  		/*----Links---------------------------------*/
  		
  		//get name of new link
  		var newlnkname = this.links[number];
  		//get name of old link
  		var oldlnkname = this.links[this.currentimage];
  		
  		//get new link
  		var newlnk = this.getElementById(newlnkname);
  		//get old link
  		var oldlnk = this.getElementById(oldlnkname);
  		
  		//TODO apply fade out to old image
  		
  		/*----The Switch----------------------------*/
  		
  		//now hide old image
  		this.changeCss(oldimg,'invisible');
  		//hide old link
  		this.changeCss(oldlnk,'invisible');
  		//change CSS of button
  		this.changeCss(oldbut,'inactive');
  		  		
  		//TODO apply fade in to new image
  		
  		//then show new image
  		this.changeCss(newimg,'visible');
  		//then show new link
  		this.changeCss(newlnk,'visibleinline');
  		//then change CSS of new button
  		this.changeCss(newbut,'active');
  		
  		//now update current image index
  		this.currentimage = number;
  		
  		//if not currently paused
  		if(this.ispaused == 0)
  		{
  			//start rotation again
      		this.startrotate(); //resets timer to avoid jerking
  		}
  	}
  	
  	//Starts Offer rotation
  	/*--------------------*/
  	this.startrotate = function()
  	{
  		//clear any current timeout instance
  		if (this.timeoutinst != 0) 
  		{
			clearTimeout(this.timeoutinst);
		}
		//now setup timeout
		this.timeoutinst = setTimeout(this.name + ".dorotation()",this.interval);
  	}
  	
  	//Performs a single rotation of offer (called on timout)
  	/*----------------------------------------------------*/
  	this.dorotation = function()
  	{
  		//check if we need to go back to begining
  		if (this.currentimage < this.images.length - 1) 
  		{
  			//if not rotate next image
  			this.changeoffer(this.currentimage + 1);
      	}
      	else 
      	{
      		//if we do, return to beginning
      		this.changeoffer(0);
      	}
  	}
  	
  	//Toggle paused or not, showing and hiding supplied controls
  	/*--------------------------------------------------------*/
  	this.togglepause = function(playerId, pauserId)
  	{
  		//get player and pauser elements
  		var player = this.getElementById(playerId);
  		var pauser = this.getElementById(pauserId);
  		
  		//if not paused
  		if(this.ispaused == 0)
  		{
  			//clear any timeout instance (if started)
  			if (this.timeoutinst != 0) 
  			{
				clearTimeout(this.timeoutinst);
			}
			
  			//set paused state
  			this.ispaused = 1;
  			
  			//now hide pauser and show player
  			this.changeCss(pauser,'invisible');
  			this.changeCss(player,'visibleinline');
  		}
  		else
  		{
  			//otherwise start rotation
  			
  			//set paused state 
  			this.ispaused = 0;
  			//start rotation
  			this.startrotate();
  			
  			//now hide player and show pauser
  			this.changeCss(player,'invisible');
  			this.changeCss(pauser,'visibleinline');
  		}
  	}
  	
  	//Sets Css class on supplied element
  	/*--------------------------------*/
  	this.changeCss = function(el, cls)
  	{
  		el.setAttribute('class', cls);
  		el.setAttribute('className', cls); //for old IE
  	}
  	
  	//Gets element by id for any browser
  	/*------------------------------*/
  	this.getElementById = function(id) 
  	{

		//for modern browsers
    	if (document.getElementById) 
    	{
    	  return document.getElementById(id);
    	}
    	//for older ie
    	else if (document.all) 
    	{
    	  return document.all[id];
    	}
    	//for legacy
    	else if (document.layers) 
    	{
    	  return document.layers[id];
    	} 
    	//Otherwise, error
    	else 
    	{
          return undefined;
    	}
  	}
}