/******************************************************************************
* File: md8lib.js                                                             *
*                                                                             *
* Copyright MatchWare A/S                                                     *
* Author: Jens Ø. Nielsen                                                     *
******************************************************************************/


var clickobj  = null;
var insideobj = null;
var actions   = new Array();
var terminating = false;
var initialized = false;
var animatingobjs = new Array();
var nextanimation = 0;
var bIsIE4Final = false;

//-------------------------------------

function IsNetscape() {
 
 if ( navigator.appName != "Netscape") return false;
 if ( parseInt(navigator.appVersion) < 4) return false;
 if ( parseInt(navigator.appVersion) > 4) return false;
 return true;
}

function IsNetscape6() {
 
 if ( navigator.appName != "Netscape") return false;
 if ( parseInt(navigator.appVersion) < 5) return false;
 return true;
}

function IsMac() {
 return navigator.userAgent.indexOf("Mac") > -1;
}

function IsOldBrowser() {

 var agent = navigator.userAgent;
 if ( agent.substring( 0,7) != "Mozilla") return false;
 var verpos = agent.indexOf( "/")+1;
 if ( verpos < 0) return false;
 agent = agent.substring( verpos,verpos+2);
 var m_ver = parseInt( agent);
 return m_ver < 4;
}

function FrameIt( page) {

 terminating = true;
 window.location = "frame.htm?" + page;
}

//------------------------------------

var ANIM_TICK = 50;

var ANIM_STYLE_NORMAL   = 0;
var ANIM_STYLE_FIRSTPOS = 1;
var ANIM_STYLE_FLYTO    = 2;
var ANIM_STYLE_FLYFROM  = 3;

//------------------------------------

function PageAction( pagename) {

 this.m_PageName = pagename;
 this.Start = PageAction_Start;
}

function PageAction_Start() {

 if ( this.m_PageName == "") return;
 if ( this.m_PageName == "@PrePage") {
   window.history.back();
 }
 else {
   if ( parent != self) {
     if ( top.global.advbookmark) {
       var url = top.location.href;
       var len = url.indexOf( "?",0);
       if ( len > 0)
         url = url.substring( 0,len);
       url += "?" + this.m_PageName + PAGE_ACTION_POST_FIX;
       terminating = true;
       top.location = url;
       return;
     }
   }
   terminating = true;
   location = this.m_PageName + PAGE_ACTION_POST_FIX;
 }
}

//------------------------------------

function bsearch(myarray, val) {

  var lo=0;
  var hi=myarray.length;

  while(true) {
    var mid=Math.floor((hi+lo)/2);
    if(mid==0)
      return mid;
    if(mid==myarray.length-1)
      return mid-1;
    else if((myarray[mid]<=val && myarray[mid+1]>val))
      return mid;               
    else if(val<myarray[mid])
      hi=mid;
    else
      lo=mid;
  }
  return 0;
}


/******************************************************************************
* Class AnimationPath                                                         *
******************************************************************************/

function AnimationPath(xpoints_array, ypoints_array) {

  this.m_xpoints_array=xpoints_array;
  this.m_ypoints_array=ypoints_array;
  this.m_sqdists=new Array(xpoints_array.length);
  this.m_sqdistsum=0;

  this.GetPointAtTime=AnimationPath_GetPointAtTime;

  /*********************/ 
  var x=xpoints_array[0];
  var y=ypoints_array[0];

  for(i=0; i<this.m_sqdists.length; i++) {
    var dx = this.m_xpoints_array[i]-x;
    var dy = this.m_ypoints_array[i]-y;
    this.m_sqdistsum += Math.sqrt(dx*dx+dy*dy);
	this.m_sqdists[i] = this.m_sqdistsum;

    x=xpoints_array[i];
    y=ypoints_array[i];
  }

}


function AnimationPath_GetPointAtTime(t) {

  if(t>=1) {
    var endx=this.m_xpoints_array[this.m_xpoints_array.length-1];
    var endy=this.m_ypoints_array[this.m_ypoints_array.length-1];
    return new Array(endx, endy);
  }
  
  var pos = t*this.m_sqdistsum;
  var idx = bsearch(this.m_sqdists, pos);
  
  var dx  = this.m_xpoints_array[idx+1] - this.m_xpoints_array[idx];
  var dy  = this.m_ypoints_array[idx+1] - this.m_ypoints_array[idx];
   

  var dv  = pos - this.m_sqdists[idx];     /* distance we went too far */
  var lenseg = this.m_sqdists[idx+1]-this.m_sqdists[idx];

  dx*=(dv/lenseg);
  dy*=(dv/lenseg);

  xpos=this.m_xpoints_array[idx]+dx;
  ypos=this.m_ypoints_array[idx]+dy;

  return new Array(xpos, ypos);
}


/******************************************************************************
* Class AnimationAction                                                       *
******************************************************************************/

function AnimationAction( myname,obj,path,totaltime,repeat,reverse,autoshow,style) {

  this.m_name=myname;
  this.m_object= FindObject( obj);
  this.m_path=path;
  this.m_totaltime=totaltime;
  this.m_time=0;
  this.m_paused=false;
  this.m_startdate=null;
  this.m_repeat=repeat;
  this.m_reverse=reverse;
  this.m_style=style;
  this.m_autoshow = autoshow;
  this.m_doshow = this.m_autoshow;
  this.m_timerid = 0;
  
  this.Start = AnimationAction_Start;
  this.Stop  = AnimationAction_Stop;
  this.Pause = AnimationAction_Pause;
  this.Tick  = AnimationAction_Tick;
}

function FindAnimation( objname) {

 for ( var i = 0; i < nextanimation;i++) {
   if ( animatingobjs[ i].m_object == objname)
     return i;
 }
 return -1;
}

function RemoveAnimation( objname) {

 var i = FindAnimation( objname);
 if ( i >= 0) {
   animatingobjs[ i] = null;
   nextanimation--;
   for ( ; i < nextanimation; i++)
     animatingobjs[ i] = animatingobjs[ i+1];
 }
}


function AnimationAction_Start() { 

 var i = FindAnimation( this.m_object);
 if ( i >= 0) {
   animatingobjs[ i].Stop();
 }
 else
   animatingobjs[ nextanimation++] = this;

 if ( IsObjVisible( this.m_object))
   this.m_doshow = false;
 else
   this.m_doshow = this.m_autoshow;

 var obj_x = GetObjLeft( this.m_object) + GetObjWidth( this.m_object) / 2;
 var obj_y = GetObjTop( this.m_object) + GetObjHeight( this.m_object) / 2;
 if ( ! this.m_paused) { // !paused => anim from start 
   this.m_time=0;
   this.m_startdate=new Date();
   var endidx=this.m_path.m_xpoints_array.length-1;
   if ( this.m_style == ANIM_STYLE_NORMAL) {
	   this.m_offset_x=0;
	   this.m_offset_y=0;
   }
   else if ( this.m_style == ANIM_STYLE_FLYTO) {
     var endpoint = this.m_reverse ? 0 : endidx;
	   this.m_offset_x = obj_x - this.m_path.m_xpoints_array[endpoint];
     this.m_offset_y = obj_y - this.m_path.m_ypoints_array[endpoint];
   }
   else if ( this.m_style == ANIM_STYLE_FLYFROM) {
     var startpoint = this.m_reverse ? endidx : 0;
	   this.m_offset_x = obj_x - this.m_path.m_xpoints_array[ startpoint];
	   this.m_offset_y = obj_y - this.m_path.m_ypoints_array[ startpoint];
   }
   else if ( this.m_style == ANIM_STYLE_FIRSTPOS) {
	   this.m_seg0_x = obj_x;
	   this.m_seg0_y = obj_y;
     startpoint = this.m_reverse ? endidx : 0;
     this.m_seg0_dx = this.m_path.m_xpoints_array[ startpoint]-this.m_seg0_x;
     this.m_seg0_dy = this.m_path.m_ypoints_array[ startpoint]-this.m_seg0_y;
  
     this.m_seg0_len=Math.sqrt(this.m_seg0_dx*this.m_seg0_dx + this.m_seg0_dy*this.m_seg0_dy);
     this.m_seg0_endtime= this.m_seg0_len / (this.m_seg0_len+this.m_path.m_sqdistsum);
   }
 }
 else {               // paused
   this.m_startdate=new Date();
   paused=false;
 }

 this.m_timerid = window.setTimeout( "actions." + this.m_name + ".Tick()", ANIM_TICK);
}


function AnimationAction_Tick() {

 if ( terminating) return;
 this.m_timerid = 0;
 if(this.m_startdate!=null) {
   var datenow=new Date();
   var msnow=this.m_time + (datenow-this.m_startdate);
   var t=msnow/this.m_totaltime;
   if ( t > 1) t = 1;
   if ( this.m_style != ANIM_STYLE_FIRSTPOS) {
     if(this.m_reverse)
       t=1-t;
     point = this.m_path.GetPointAtTime(t);
     var x = point[0] + this.m_offset_x - GetObjWidth( this.m_object) / 2;
     var y = point[1] + this.m_offset_y - GetObjHeight( this.m_object) / 2;
     SetObjPosition( this.m_object,x,y);
   }
   else { 
     // firstpos hack
     if(t < this.m_seg0_endtime) {
       var tt= t*(1/this.m_seg0_endtime);
       var x = this.m_seg0_x+tt*this.m_seg0_dx - GetObjWidth( this.m_object) / 2;
       var y = this.m_seg0_y+tt*this.m_seg0_dy - GetObjHeight( this.m_object) / 2;
       SetObjPosition( this.m_object,x,y);
     }
     else {
       var tt=(t-this.m_seg0_endtime)*(1/(1-this.m_seg0_endtime));
       if(this.m_reverse)
         tt=1-tt;
       point=this.m_path.GetPointAtTime(tt);
       var x = point[ 0] - GetObjWidth( this.m_object) / 2;
       var y = point[ 1] - GetObjHeight( this.m_object) / 2;
       SetObjPosition( this.m_object,x,y);
     }
   }

   if(msnow<this.m_totaltime)     
     this.m_timerid = window.setTimeout( "actions." + this.m_name+".Tick()", ANIM_TICK);
   else if(this.m_repeat) {
     this.m_startdate=new Date();
     this.m_timerid = window.setTimeout( "actions." + this.m_name + ".Tick()", ANIM_TICK);
   }
   else {
     RemoveAnimation( this.m_object);
   }
 }
 if ( this.m_doshow) {
   ShowObject( this.m_object,true);
   this.m_doshow = false;
 }
}

function AnimationAction_Stop() {

 RemoveAnimation( this.m_object);
 this.m_paused=false; 
 this.m_time=0;
 if ( this.m_timerid) {
   clearTimeout( this.m_timerid);
   this.m_timerid = 0;
 }
}

function AnimationAction_Pause() {

  this.m_paused=true;
  d= new Date();
  this.m_time+=d-this.m_startdate;
  this.m_startdate=null;
}

//------------------------------------

function TimeLineAction( myname, actionarray,delayarray) {

 this.m_Name       = myname;
 this.m_ActionList = actionarray;
 this.m_DelayList  = delayarray;
 this.m_Current    = 0;
 this.m_TimerId    = 0;

 this.Start = TimeLineAction_Start;
 this.Tick  = TimeLineAction_Tick;
}


function TimeLineAction_Start() {

 if ( this.m_ActionList.length == 0) return;
 if ( this.m_TimerId) 
   clearTimeout( this.m_TimerId);
 this.m_Current = 0;
 this.m_TimerId = window.setTimeout( "actions." + this.m_Name+".Tick()", this.m_DelayList[ 0]);
}


function TimeLineAction_Tick() {

 this.m_TimerId = 0;
 if ( terminating) return;
 var index = this.m_Current;
 this.m_ActionList[ index].Start();
 if ( this.m_Current != index) return;
 this.m_Current++;
 if ( this.m_Current >= this.m_ActionList.length) return;
 this.m_TimerId = window.setTimeout( "actions." + this.m_Name+".Tick()", this.m_DelayList[ this.m_Current]);
}


//------------------------------------

function EmailAction( to,subject,text) {
 
 this.m_To      = to;
 this.m_Subject = subject;
 this.m_Text    = text;

 this.Start = EmailAction_Start;
}

function EmailAction_Start() {

 window.location.href = "mailto:" + this.m_To + "?Subject=" + this.m_Subject + "&Body=" + this.m_Text;
}

//------------------------------------

function HttpAction( url,innewwindow) {

 this.m_Url         = url;
 this.m_InNewWindow = innewwindow;

 this.Start = HttpAction_Start;
}

function HttpAction_Start() {

 if ( this.m_InNewWindow) {
   if ( IsNetscape())
     window.open( this.m_Url,"","menubar,toolbar,status,location,scrollbars,resizable=yes",false);
   else
     window.open( this.m_Url,"","",false);
 } 
 else
   window.location = this.m_Url;
}

//------------------------------------

function StartAction( action) {

 this.m_Action = action;

 this.Start = StartAction_Start;
}

function StartAction_Start() {

 eval( this.m_Action);
}



