/*************************************************************
 *  CTLN MISCELANEOUS LIBRARY
 *************************************************************/


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  Global Variables
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

// debug
var DEBUGFLAG = false;

// browser
var IS_NS=false, IS_IE=false;
if (navigator.appName=="Netscape") {IS_NS=true;} else {IS_IE=true;}

// platform
var IS_PC=false, IS_MAC=false;
if (navigator.platform=="MacPPC") {IS_MAC==true;} else {IS_PC=true;}

// character string lists
lcase = "abcdefghijklmnopqrstuvwxyz";
ucase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
digit = "0123456789";

// dhtml variables
var docObj, styleObj, VISIBLE, INVISIBLE;
if (IS_IE) {
  docObj   = 'document.all.';
  styleObj = '.style';
  VISIBLE = 'visible';
  INVISIBLE = 'hidden';
} else {
  docObj   = 'document.';
  styleObj = '';
  VISIBLE = 'show';
  INVISIBLE = 'hide';
}function element(_id) {
  return eval(docObj + _id + styleObj);
}

// Interactive Image Variables
var NORM = 0;
var OVER = 1;
var DOWN = 2;
var CURR = 3;
var interactiveImageList;            // image list


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  Debug utilities
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function DEBUG (bugstr) {if(DEBUGFLAG)alert(bugstr);}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  Miscelaneous utilities
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function IS_DEFINED (_x) {if(IS_IE){return(typeof(_x)!="undefined");}else{return(_x!=eval("undefined"));}}
function IS_ARRAY   (_x) {return(_x.constructor==Array);}
function IS_BOOL    (_x) {return(_x.constructor==Boolean);}
function IS_NUMBER  (_x) {return(_x.constructor==Number);}
function IS_STRING  (_x) {return(_x.constructor==String);}
function NOTHING    ()   {return;}

function REMOVE_SPACES_FROM_STRING (_str) {
  var new_str="";
  for (n=0; n<_str.length; n++) {
    t_char = _str.charAt(n);
    if (t_char!=' ') {new_str+=t_char;}}
  return (new_str);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  List Object
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function List_getOne (_index) {
  if (_index.constructor!=String)  DEBUG("List_getOne(): wrong argument type: properties are matched w/ a string");
  else if (!IS_DEFINED(this[_index])) DEBUG("List_getOne(): no properties match argument: property is undefined");
  else                             return this[_index];
}
function List_getAt (_index) {
  if (isNaN(_index))                  DEBUG("List_getAt(): wrong argument type: properties are indexed w/ integers");
  else if (_index>=this.index.length) DEBUG("List_getAt(): index out of range: ...");
  else                                return this[this.index[_index]];
}
function List_add () { var _property, _value, _index;
  _index=this.length;
  switch (arguments.length) {
    case  1: _value=arguments[0]; _property=('generic_'+_index); break;
    case  2: _value=arguments[1]; _property=arguments[0];        break;
    default: return; break;
  }
  this[_property]    = _value;
  this.index[_index] = _property;
  this.length++;
}
function List () {
    // data members
  this.index  = new Array();
  this.length = 0;
    // methods
  this.add    = List_add;
  this.getOne = List_getOne;
  this.getAt  = List_getAt;
  return this;  
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  InteractiveImage Object
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function InteractiveImage (_id, _urlbase, _fileext, _group) {
  if (_fileext[0]!='.') _fileext='.'+_fileext;
  this.norm = new Image();  this.norm.src = _urlbase + _id + _fileext;
  this.over = new Image();  this.over.src = _urlbase + _id + '_over' + _fileext;
  this.down = new Image();  this.down.src = _urlbase + _id + '_down' + _fileext;
  this.curr = new Image();  this.curr.src = _urlbase + _id + '_curr' + _fileext;
  this.urlbase = _urlbase;
  this.fileext = _fileext;
  this.group = _group;
  return this;
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  images_load Function  (Array, String, String, String)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function images_load (_array, _urlbase, _fileext, _group) {
  interactiveImageList = new List();
  for (n=0;n<_array.length;n++) {
    imgname = _array[n];
    if (!IS_DEFINED(interactiveImageList[imgname])) {
      interactiveImageList.add(imgname, new InteractiveImage(imgname, _urlbase, _fileext, _group));
    } else {
      DEBUG("images_load(): image name already defined: "+ imgname);
    }
  }
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 *  images_chg Function
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function images_chg (_id, _mode) {
  mydocobj = eval('document.'+_id);
  mysrcobj = eval('interactiveImageList.'+_id);

  switch (_mode) {
    case /*NORM*/ 0: mydocobj.src=mysrcobj.norm.src; break;
    case /*OVER*/ 1: mydocobj.src=mysrcobj.over.src; break;
    case /*DOWN*/ 2: mydocobj.src=mysrcobj.down.src; break;
 // case /*CURR*/ 3: mydocobj.src=mysrcobj.curr.src; break;
  }
}









/*************************************************************
 *  CTLN REAL PLAYER LIBRARY
 *************************************************************/


/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function real_write_ImageWindow
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function real_write_ImageWindow (_player, _src, _width, _height) {

  /* local variables */
  var html_str="";

  /* create html string (browser dependant) */
  if (document.all) /* internet explorer */ {
    html_str += '<OBJECT ID=\"' +_player+ '\"\n';
    html_str += ' CLASSID="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\"\n';
    html_str += ' WIDTH=\"' +_width+ '\"\n';
    html_str += ' HEIGHT=\"' +_height+ '\">\n';
    html_str += '  <PARAM NAME=\"SRC\" VALUE=\"' +_src+ '\">\n';
    html_str += '  <PARAM NAME=\"CONTROLS\" VALUE=\"ImageWindow\">\n';
    html_str += '  <PARAM NAME=\"CONSOLE\" VALUE=\"cons\">\n';
    html_str += '  <PARAM NAME=\"autostart\" VALUE=\"true\">\n';
    html_str += '</OBJECT>';}
  else /* netscape navigator */ {
    html_str += '<EMBED NAME=\"' +_player+ '\"\n';
    html_str += ' TYPE="audio/x-pn-realaudio-plugin\"\n';
    html_str += ' SRC=\"' +_src+ '\"\n';
    html_str += ' WIDTH=\"' +_width+ '\"\n';
    html_str += ' HEIGHT=\"' +_height+ '\"\n';
    html_str += ' CONTROLS=\"ImageWindow\"\n';
    html_str += ' CONSOLE=\"cons\"\n';
    html_str += ' AUTOSTART=\"true\"\n';
    html_str += '></EMBED>';}

  /* write string to document */
  document.write(html_str);
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function real_write_Control
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function real_write_Control (_player, _controltype, _width, _height) {

  /* local variables */
  var html_str="";

  /* create html string (browser dependant) */
  if (document.all) /* internet explorer */ {
    html_str += '<OBJECT ID=\"' +_player+ '_' +_controltype+ '\"\n';
    html_str += ' CLASSID="CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\"\n';
    html_str += ' WIDTH=\"' +_width+ '\"\n';
    html_str += ' HEIGHT=\"' +_height+ '\">\n';
    html_str += '  <PARAM NAME=\"CONTROLS\" VALUE=\"' +_controltype+ '\">\n';
    html_str += '  <PARAM NAME=\"CONSOLE\" VALUE=\"cons\">\n';
    html_str += '</OBJECT>';}
  else /* netscape navigator */ {
    html_str += '<EMBED NAME=\"' +_player+ '_' +_controltype+ '\"\n';
    html_str += ' TYPE="audio/x-pn-realaudio-plugin\"\n';
    html_str += ' WIDTH=\"' +_width+ '\"\n';
    html_str += ' HEIGHT=\"' +_height+ '\"\n';
    html_str += ' CONTROLS=\"' +_controltype+ '\"\n';
    html_str += ' CONSOLE=\"cons\"\n';
    html_str += '></EMBED>';}

  /* write string to document */
  document.write(html_str);
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function real_write_FullPlayer
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function real_write_FullPlayer (_player, _src, _width, _height) {

  var numOfCols=4
  ,   usePostionField=true
  ,   positionFieldWidth=(_width-144)
  ,   minWidthForPositionField=240
  ;


  if (_width<minWidthForPositionField) {numOfCols=3; usePostionField=false; positionFieldWidth=(_width-44);}


  document.write('<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"' +_width+ '\">');

  document.write('<tr>');
    document.write('<td valign=\"top\" colspan=\"' +numOfCols+ '\">');
    real_write_ImageWindow(_player, _src, _width, _height);
    document.write('</td>');
  document.write('</tr>');

  document.write('<tr>');
    document.write('<td valign=\"top\">');
    real_write_Control(_player, "PlayOnlyButton", 22, 22);
    document.write('</td>');

    document.write('<td valign=\"top\">');
    real_write_Control(_player, "PauseButton", 22, 22);
    document.write('</td>');

    document.write('<td valign=\"top\">');
    real_write_Control(_player, "PositionSlider", positionFieldWidth, 22);
    document.write('</td>');

    if (usePostionField) {
    document.write('<td valign=\"top\">');
    real_write_Control(_player, "PositionField", 100, 22);
    document.write('</td>');
    }
  document.write('</tr>');

  document.write('</table>');
}




/*************************************************************
 *  CTLN TALKINGHEAD LIBRARY
 *************************************************************/


/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Global Variables
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

var  gContentFrame
,    gContentObject_str
,    gContentObject
,    gMovieControl_str
,    gMovieControl
,    gEndMovieObject_str
,    gEndMovieObject
,    gEndMovieAction
,    gEndMovieTime
,    gEndMovieURL
,    gTimecodes_code = new Array()
,    gTimecodes_url = new Array()
,    gCurrentURL = ""
,    gPlayerName
/* default switches */
,    switch_endmovie = false
,    switch_eval     = false
,    switch_controls = 'full'
;



function real_seek (_timecode) {
  t_time = TIMECODE_TO_MILLISECONDS(_timecode);
  mycode = "document." + gPlayerName + ".SetPosition(" + t_time + ");";
  eval(mycode);
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function TIMECODE_TO_MILLISECONDS
 *
 *    takes a string time code and returns the number of
 *    milliseconds it totals. (requires format "HH:MM:SS").
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function TIMECODE_TO_MILLISECONDS (_timecode) {

  /* local variables */
  var SECOND=1000, MINUTE=60*SECOND, HOUR=60*MINUTE,
      seconds_str="", minutes_str="", hours_str="",
      milliseconds=0, sign=1;

  /* extract hours, minutes, seconds from the timecode string*/
  tc_array = _timecode.split(":");
  if (eval(tc_array[0])<0) {sign=(-1); tc_array[0]=tc_array[0].substring(1,tc_array[0].length-1);}
  hours_str   = tc_array[0];
  minutes_str = tc_array[1];
  seconds_str = tc_array[2];

  /* calculate the milliseconds and return them */
  milliseconds+=eval(seconds_str)*SECOND;
  milliseconds+=eval(minutes_str)*MINUTE;
  milliseconds+=eval(hours_str  )*HOUR;

  return(milliseconds*sign);
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function MILLISECONDS_TO_TIMECODE
 *
 *    takes an integer time code in milliseconds and returns 
 *    the number as a string. (returns format "HH:MM:SS").
 *    rounded down to the nearest :SS
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function MILLISECONDS_TO_TIMECODE (_milliseconds) {

  /* local variables */
  var SECOND=1000, MINUTE=60*SECOND, HOUR=60*MINUTE,
      seconds=0, minutes=0, hours=0,
      seconds_str="", minutes_str="", hours_str="",

  /* calculate each field from the total milliseconds */
  hours   = Math.floor(_milliseconds/HOUR);
  minutes = Math.floor((_milliseconds-(hours*HOUR))/MINUTE);
  seconds = Math.floor((_milliseconds-(hours*HOUR)-(minutes*MINUTE))/SECOND);

  /* convert to string and add 0's if necessary */
  hours_str = ""+hours; if(hours_str.length==1){hours_str="0"+hours_str;}
  minutes_str = ""+minutes; if(minutes_str.length==1){minutes_str="0"+minutes_str;}
  seconds_str = ""+seconds; if(seconds_str.length==1){seconds_str="0"+seconds_str;}

  return( hours_str+":"+minutes_str+":"+seconds_str );
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function check_movietime
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function check_movietime (_directcommand) {

  /* sometimes netscape is slow to create the player.
     this makes sure it is created before making calls to it. */
  var success = true;
  if (!IS_DEFINED(gMovieControl)) {gMovieControl=eval(gMovieControl_str); success=false;}
  //if (!IS_DEFINED(gContentObject)) {gContentObject=eval(gContentObject_str); success=false;}
  if (success) {

  /* local variables */
  var n, t_position, t_check;

  /* save current movie position */
  t_position = gMovieControl.GetPosition();

  /* compare with each timecode in the array */
  for (n=0; n<gTimecodes_code.length; n++) {
    if (n==gTimecodes_code.length-1) {
      t_check=gMovieControl.GetLength();
      if (gEndMovieAction && t_position>gEndMovieTime) {mycode=gEndMovieObject_str+'=gEndMovieURL'; eval( mycode ); break;}}
    else {t_check=gTimecodes_code[n+1];}
    if (t_position>=gTimecodes_code[n] && t_position<t_check) {
      if (gCurrentURL!=gTimecodes_url[n]) {
        if (_directcommand) {eval(gTimecodes_url[n]);}
        else {mycode=gContentObject_str+'=(gCurrentURL=gTimecodes_url[n])'; eval( mycode );}}
      break;}}

  } /* end definition check * / else {alert('<gMovieControl> is not defined!');} */

  /* set the time for the next callback */
  setTimeout('check_movietime('+_directcommand+')', 1*500);  // call every 1/2 second
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 *  Function set_timecodes
 *
 *    recieves the name of the content object (frame or image).
 *      example (frame): 
 *    
 *    recieves the url, width and height of the source movie. 
 *    
 *    recieves a boolean value to determine if new page 
 *    should load at movie's end
 *    
 *    recieves each timecode and corresponding url in an 
 *    alternating pairs form.
 *    (timecode[0], url[0], timecode[1], url[1], ...)
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * */


function set_timecodes (_obj, _src, _width, _height /*, [_timecode, _url]... , manual switches */) {

  /* local variables */
  var n, timecode_args=new Array(), t_player="";

  /* manual switches */
  switch_str = REMOVE_SPACES_FROM_STRING(arguments[arguments.length-1]);
  if (switch_str.charAt(switch_str.length-1)==';') {switch_str=switch_str.substring(0,switch_str.length-1);}
  switch_set = switch_str.split(';');
  for (n=0; n<switch_set.length; n++) {
    switch_set[n] = switch_set[n].split(':',2);
    mycode = 'switch_' + switch_set[n][0] + '=' + switch_set[n][1];
    eval(mycode);}

  /* copy timecode and url pair arguments to new array */
  for (n=4; n<(arguments.length-1); n++) {
    timecode_args[timecode_args.length] = arguments[n];}

  /* assign each timecode and url pair to arrays */
  for (n=0; n<timecode_args.length/2; n++) {
    gTimecodes_code[n] = TIMECODE_TO_MILLISECONDS(timecode_args[(n*2)]);
    gTimecodes_url[n]  = timecode_args[(n*2)+1];}

  /* generate random player name */
  for (n=0; n<10; n++) {
    whichChar = Math.floor(Math.random() * lcase.length);
    t_player += lcase.charAt(whichChar);}
  gPlayerName = t_player;

  /* write movie control to document*/
  switch (switch_controls) {
  case 'full': real_write_FullPlayer(t_player, _src, _width, _height); break;
  case 'none': real_write_ImageWindow(t_player, _src, _width, _height); break;
  }

  /* set global variables */
  //gContentFrame     = (_frame==false) ? eval("window") : eval("parent."+_frame);
  gContentObject_str  = _obj;
  gContentObject      = eval(gContentObject_str);
  gMovieControl_str   = 'document.'+t_player; /* store the string for later use */
  gMovieControl       = eval(gMovieControl_str);
  gEndMovieAction     = (switch_endmovie!=false) ? true : false ;
  gEndMovieObject_str = (switch_endmovie!=false) ? switch_endmovie : "document.location" ;
  gEndMovieObject     = eval(gEndMovieObject_str);
  gEndMovieTime       = gTimecodes_code[gTimecodes_code.length-1];
  gEndMovieURL        = gTimecodes_url[gTimecodes_url.length-1];

}



/* pass 'true' in the argument to send manual javascript from the timecodes_x.js */
function init_player () {check_movietime(switch_eval);}


document.Content_tag = new Image();

/* initialize timeout call */
//window.onload = init_player;