
var flyInAds=new Array();
var flyInIndex=new Array();

function registerFlyIn(divId,startPos,stopPos,speed,pauseTime,delayTime,showPeriod,canShow) {
  var newIndex=flyInAds.length;
  flyInIndex[divId]=newIndex;
  flyInAds[newIndex]=new FlyIn(divId);
  flyInAds[newIndex].audioStop();
  flyInAds[newIndex].startPos=startPos;
  flyInAds[newIndex].stopPos=stopPos;
  flyInAds[newIndex].speed=speed;
  flyInAds[newIndex].pauseTime=pauseTime;
  flyInAds[newIndex].delayTime=delayTime;
  flyInAds[newIndex].canShow=canShow;
  flyInAds[newIndex].showPeriod=showPeriod;
}

function flyInOnLoad(){
  var totalFlyIns=flyInAds.length;
  for(i=0; i<totalFlyIns; i++){
    if(flyInAds[i].canShow){
      if(flyInAds[i].showPeriod>0){
        if(checkCookie(flyInAds[i].id,flyInAds[i].showPeriod)){
          flyInAds[i].start();
        }
      } else {
        flyInAds[i].start();
        setCookie(flyInAds[i].id+"showNext",-1);
      }
    }
  }
}

function flyInStart(divId){
  flyInAds[flyInIndex[divId]].start();
}
function flyInStop(divId){
  flyInAds[flyInIndex[divId]].stop();
}
function flyInResume(divId){
  flyInAds[flyInIndex[divId]].resume();
}
function flyInClose(divId){
  flyInAds[flyInIndex[divId]].finished();
}
function FlyIn(divId) {
  this.id=divId;
  this.isPaused=false;
  this.hasPaused=false;
  this.flying=false;
  this.canShow=true;
  this.showPeriod=0; // how often the popup can show per visitor. Days.
  this.speed=1;
  this.startPos=1;
  this.stopPos=1;
  this.pauseTime=0;
  this.onshow=false;
  this.audioPlaying=false;
  this.isIE=false;
  this.startFlyIn=startFlyIn;
  this.startX=startX;
  this.startY=startY;
  this.move=moveFlyIn;
  this.moveRight=moveRight;
  this.moveLeft=moveLeft;
  this.moveUp=moveUp;
  this.moveDown=moveDown;
  this.moveSpiralIn=moveSpiralIn;
  this.moveSpiralOut=moveSpiralOut;
  this.start=startFlyIn;
  this.stop=stopFlyIn;
  this.resume=resumeFlyIn;
  this.pause=pauseFlyIn;
  this.unpause=unpauseFlyIn;
  this.finished=finished;
  this.startMove=startMove;
  this.audioPlay=audioPlay;
  this.audioStop=audioStop;

  if(window.innerWidth){
    this.documentWidth=window.innerWidth;
  } else if((document.body)&&(document.body.clientWidth)){
    this.documentWidth=document.body.clientWidth;
  }
  if(window.innerHeight){
    this.documentHeight=window.innerHeight;
  } else if((document.body)&&(document.body.clientHeight)){
    this.documentHeight=document.body.clientHeight;
  }

  this.isIE=(document.all&&document.getElementById);
  this.element=document.getElementById(this.id);
  this.width=parseInt(this.element.style.width);
  this.height=parseInt(this.element.style.height);
  this.xCentre=(this.documentWidth/2)-(this.width/2);
  this.yCentre=(this.documentHeight/2)-(this.height/2);
  this.aCos=new Array(360);
  this.aSin=new Array(360);
  for(var i=0; i<360; i++) {
    this.aSin[i]=Math.sin(i*Math.PI/180);
    this.aCos[i]=Math.cos(i*Math.PI/180);
  }
}
function startFlyIn() {
  this.isPaused=false;
  this.hasPaused=false;
  this.flying=false;
  this.element.style.left=this.startX();
  this.element.style.top=this.startY();
  this.direction=this.startPos;
  this.radius=this.documentHeight-this.height;
  this.deg=0;
  var thisObj=this;
  var delay=setTimeout(function(){
  thisObj.startMove()},1000*this.delayTime);
}

function startMove(){
  this.element.style.display='block';
  this.onshow=true;
  this.audioPlay();
  this.move();
}

function startX() {
  switch(this.startPos) {
    case 1:return(-this.width);
    break;
    case 2:return this.documentWidth;
    break;
    case 3:return this.xCentre;
    break;
    case 4:return this.xCentre;
    break;
    default:return(-this.width);
    break;
  }
}

function startY(){
  switch(this.startPos){
    case 1:return this.yCentre;
    break;
    case 2:return this.yCentre;
    break;
    case 3:return(-this.height);
    break;
    case 4:return this.documentHeight;
    break;
    default:return this.yCentre;
    break;
  }
}

function moveFlyIn(){
  if((!this.isPaused)){
    switch(this.direction){
      case 1:this.moveRight();
      break;
      case 2:this.moveLeft();
      break;
      case 3:this.moveDown();
      break;
      case 4:this.moveUp();
      break;
      case 5:this.moveSpiralIn();
      break;
      case 6:this.moveSpiralOut();
      break;
      default:this.moveLeft();
      break;
    }
    if(this.onshow){
      var thisObj=this;
      this.flying=setTimeout(function(){thisObj.move()},10);
    }
  }
}

function moveSpiralIn(){
  if(this.radius>0) {
    var y=parseInt(this.radius*this.aSin[this.deg]);
    var x=parseInt(this.radius*this.aCos[this.deg]);
    this.element.style.left=(x+this.xCentre);
    this.element.style.top=(y+this.yCentre);
    this.deg+=this.speed;
    if(this.deg>=360)this.deg=0;
    if(this.deg%10==0)this.radius-=this.speed;
  } else{
    this.pause();
    return;
  }
}

function moveSpiralOut(){
  if(this.radius<this.documentHeight){
    var y=parseInt(this.radius*this.aSin[this.deg]);
    var x=parseInt(this.radius*this.aCos[this.deg]);
    this.element.style.left=(x+this.xCentre);
    this.element.style.top=(y+this.yCentre);
    if(this.deg<=0)this.deg=360;
    if(this.deg%10==0)this.radius+=this.speed;
    this.deg-=this.speed;
  } else {
    this.finished();
  }
}

function moveRight(){
  if(parseInt(this.element.style.left)<=this.documentWidth){
    this.element.style.left=(parseInt(this.element.style.left)+this.speed);
    if((!this.hasPaused)&&((parseInt(this.element.style.left)+(this.width/2))>=(this.documentWidth/2))){
      this.pause();
      return;
    }
  } else{
    this.finished();
  }
}

function moveLeft(){
  if((parseInt(this.element.style.left)+this.width)>=0){
    this.element.style.left=(parseInt(this.element.style.left)-this.speed);
    if((!this.hasPaused)&&((parseInt(this.element.style.left)+(this.width/2))<=(this.documentWidth/2))){
      this.pause();
      return;
    }
  }else{
    this.finished();
  }
}

function moveUp(){
  if((parseInt(this.element.style.top)+this.height)>=0){
    this.element.style.top=parseInt(this.element.style.top)-this.speed;
    if((!this.hasPaused)&&((parseInt(this.element.style.top)+(this.height/2))<=(this.documentHeight/2))){
      this.pause();
      return;
    }
  }else{
    this.finished();
  }
}

function moveDown(){
  if(parseInt(this.element.style.top)<=this.documentHeight){
    this.element.style.top=parseInt(this.element.style.top)+this.speed;
    if((!this.hasPaused)&&((parseInt(this.element.style.top)+(this.height/2))>=(this.documentHeight/2))){
      this.pause();
      return;
    }
  }else{
    this.finished();
  }
}

function stopFlyIn(){
  clearTimeout(this.flying);
  this.flying=false;
}

function pauseFlyIn(){
  this.hasPaused=true;
  this.radius=0;
  this.direction=this.stopPos;
  if(this.pauseTime>0){
    this.stop();
    var thisObj=this;
    this.isPaused=setTimeout(function(){
    thisObj.unpause()},1000*this.pauseTime);
  }
}

function unpauseFlyIn(){
  var thisObj=this.isPaused;
  clearTimeout(thisObj);
  this.isPaused=false;
  this.move();
}

function resumeFlyIn(){
  if(!this.isPaused){
    this.move();
  }
}

function finished(){
  this.onshow=false;
  this.element.style.display='none';
  this.stop();
  this.audioStop();
}

function audioPlay(){
  if((!this.audioPlaying)&&(this.isIE)){
    if(el=document.getElementById(this.id+"_audio")){
      el.play();
      this.audioPlaying=true;
    }
  }
}

function audioStop(){
  if((el=document.getElementById(this.id+"_audio"))&&(this.isIE)){
    el.stop();
    this.audioPlaying=false;
  }
}

function checkCookie(id,period){
  var now=new Date();
  fixDate(now);
  var canNextShow=getCookie(id+"showNext");
  if(canNextShow==null)canNextShow=0;
  if(canNextShow<=now.getTime()){
    canNextShow=now.getTime()+((24*60*60*1000)*period);
    now.setTime(now.getTime()+365*24*60*60*1000);
    setCookie(id+"showNext",canNextShow,now);
    return true;
  }
  return false;
}

function setCookie(name,value,expires,path,domain){
  var curCookie=name+"="+escape(value)+ ((expires)?";expires="+expires.toGMTString():"")+((path)?";path="+path:"")+((domain)?";domain="+domain:"");
  document.cookie=curCookie;
}

function getCookie(name){
  var dc=document.cookie;
  var prefix=name+"=";
  var begin=dc.indexOf(";"+prefix);
  if(begin==-1){
    begin=dc.indexOf(prefix);
    if(begin!=0)return null;
  }else{
    begin+=2;
  }
  var end=document.cookie.indexOf(";",begin);
  if(end==-1)end=dc.length;
  return unescape(dc.substring(begin+prefix.length,end));
}

function fixDate(date){
  var base=new Date(0);
  var skew=base.getTime();
  if(skew>0)
    date.setTime(date.getTime()-skew);
}

function hideIt(objectID) {
	if (document.all) {
	document.all[objectID].style.visibility = 'hidden'
	} else {
		if (document.layers) {
		document.layers[objectID].visibility = 'hidden'
		} else {
			if (document.getElementById) {
			document.getElementById(objectID).style.visibility = 'hidden'
			}
		}
	}
}

function showIt(objectID) {
	if (document.all) {
	document.all[objectID].style.visibility = 'visible'
	} else {
		if (document.layers) {
		document.layers[objectID].visibility = 'visible'
		} else {
			if (document.getElementById) {
			document.getElementById(objectID).style.visibility = 'visible'
			}
		}
	}
}

