// =============================================================================
// timeVerticalUI.js
// -----------------------------------------------------------------------------
// 
// User Interface
// 
// -----------------------------------------------------------------------------
//   Copyright © 2007 Time Vertical Media Productions, Inc. All rights reserved.
// =============================================================================
function openWindow(url, name, width, height)
{
  return window.open(url, name, 'directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,width='+width+',height='+height+',modal=1', false);
}

// Toggle the classes for the id.
// Be carefull there could be more than one class assigned to an element.
function toggleClassesForId(id, classOne, classTwo)
{

  if (arguments.length < 3) { return }

  var ele = document.getElementById(id);
  var eleClassName = ele.className;
  var classes = eleClassName.split(/\s+/);
  var classesLength = classes.length;

  for (var i=0; i<classesLength; i++)
  {
      if (classes[i] == classOne) {
         classes[i] = classTwo;
         ele.className = classes.join(" ");
         return;
      } else if (classes[i] == classTwo) {
         classes[i] = classOne;
         ele.className = classes.join(" ");
         return;
      }
  }
}

// Toggle a Div open or closed
// the second argument should be the anchor object that contains
// the widget icon to be toggled open or closed
function toggleDisplay(toggleDivId, obj)
{

  toggleClassesForId(toggleDivId, 'display_false', 'display_true');
  if (obj != null)
     setImageByParse(obj);

  return false;
}

// Image swapping by element id and string parse.
// The "_o" is for on, add or remove this for on|off states of a graphic.
// This assumes that both images are in the same directory. You can force a state with the state flag.
function setImageByParse(img, state)
{

  if (img != null) {
     var imgSrc = img.src;
     var ext = imgSrc.substring(imgSrc.lastIndexOf('.'));
     var path = imgSrc.substring(0, imgSrc.lastIndexOf('.'))
     var end = path.lastIndexOf('/');
     var file = path.substring(end + 1);
     path = path.slice(0, end + 1);

     // now do the test
     if (file.length > 2) {
        if (file.substring(file.length -2) == "_o") { // it's on turn off
           if (state == "on") { return true; } // it's  already on just return
           img.src = path + (file.slice(0, file.length -2)) + ext;
           return true;
        } else {
           if (state == "off") { return false; } // it's allready off just return
           img.src = path+file+"_o"+ext;
           return false;
        }
     }
  }
}

// get the state of a div - id only - (open or closed)
function getDivState(id)
{
 var div = document.getElementById(id);

if (div.className.indexOf("display_false") != -1)
     return "closed";

  return "open";
}

// video rating
var pics = new Array();
var stat = new Array();
var pic = new Array();

function changepic(i, code)
{
 var loop=0;

  if (stat[code])
  {
     while ((pics[code][i]==0 || i>=16) && loop<100)
     {
       if (i>=16)
          i=0;
       else
          i++;
       loop++;
     }

     if (pic[code][i].complete)
     {
        document.getElementById(code).src=pic[code][i].src;
        setTimeout("changepic("+(i+1)+",'"+code+"')",500);
     }
     else
        setTimeout("changepic("+i+",'"+code+"')",20);
  }
}

function loadpic(url,code,j)
{
  if (stat[code]) { pic[code][j].src=url; }
}

function startm(code, ta, te)
{
 var jj, jjj;
 var first = 1;

  stat[code]=1;
	
  for (var j=0;j<16;j++)
  {
      if (pics[code][j]==1)
      {
         pic[code][j] = new Image();
         jj=j+1;
         if (jj>=100) { jjj=""+jj; }
         if (jj<100 && jj>=10) { jjj="0"+jj; }
         if (jj<10) { jjj="00"+jj; }
         if (first)
         {
            first=0;
            loadpic(ta+jjj+te,code,j);
         }
         else
            setTimeout("loadpic('"+ta+jjj+te+"','"+code+"',"+j+")", j*50);
      }
  }
  changepic(0,code);
}

function endm(code)
{
  stat[code]=0;
}
