/* code here is mostly by brian moore */
function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
}

function sendEvent(typ,prm) { 
  thisMovie('Player').sendEvent(typ,prm); 
}

var playstate = 0;
function getUpdate(typ,pr1,pr2,swf) {
   if(typ == "state") { playstate = pr1; }
};

function loadFile(swf,obj) { 
  thisMovie(swf).loadFile(obj); 
}

function startstory(nid) {
   loadFile('Player', { file:'/playlist.php%3Fq%3D' + nid });
   sendEvent('playpause',true);
}

var currentstory = 0;

function showstory(show, nid) {
  if (show == currentstory)
     return;
  currentstory = show;
  var item = 0;
  for (item = 0; item < 5 ; item = item + 1) { 
    id = '#playtitle-' + item;
    if ( item != show ) {
      $(id).addClass("hidden");
    }
    if ( item == show ) {
      $(id).removeClass("hidden");
      if ( ! playstate ) {
        loadFile('Player', { file:'/playlist.php%3Fq%3D' + nid});
      }
    }
  }
}

function showwon(num) {
  var item=0;
  for (item = 0; item < 7 ; item = item + 1) { 
    id = '#whatson-pane-' + item;
    tab = '#whatson-tab-' + item;
    if ( item != num ) {
      // $(id).addClass("hidden");
      $(id).hide();
      $(tab).removeClass("on");
    } else {
       // $(id).removeClass("hidden");
      $(id).fadeIn();
      $(tab).addClass("on");
    }
  }
}

function showweather(num) {
  var item=0;
  for (item = 0; item < 4 ; item = item + 1) { 
    id = '#weather-con-' + item;
    tab = '#weather-' + item;
    if ( item != num ) {
      $(id).addClass("hidden");
      $(tab).removeClass("on");
    } else {
       $(id).removeClass("hidden");
       $(tab).addClass("on");
    }
  }
}

function radartab(num) {
  var item=0;
  for (item = 0; item < 2 ; item = item + 1) { 
    id = '#radar-pane-' + item;
    tab = '#radar-tab-' + item;
    if ( item != num ) {
      $(id).addClass("hidden");
      $(tab).removeClass("on");
    } else {
       $(id).removeClass("hidden");
       $(tab).addClass("on");
    }
  }
}

function forecast(num) {
  var item=0;
  for (item = 0; item < 3 ; item = item + 1) { 
    id = '#forecast-pane-' + item;
    tab = '#forecast-tab-' + item;
    if ( item != num ) {
      $(id).addClass("hidden");
      $(tab).removeClass("on");
    } else {
       $(id).removeClass("hidden");
       $(tab).addClass("on");
    }
  }
}

var breaking=0;
function nextbreak(max) {
  var item;
  breaking = breaking + 1;
  if (breaking > max) {
    breaking = 0;
  }
  for (item = 0; item <= max; item = item + 1) {
    id = '#breaking-' + item;
    if (item != breaking) {
       $(id).hide();
    } else {
       $(id).show();
    }
  }
}

var checkplayerID = 0;

function playstory(url) {
   var nid = url.match(/\d+$/);
   $("#sliders").cycle("pause");
   $("#videoPlayer").height(0);
   $f("bigplayer").show();
   $("#bigplayer").height(336);
   $.getJSON("/newplaylist.php", { q : nid } ,
      function(data) {
       var ar = eval(data);
       $f("bigplayer").play(ar);
       checkplayerID = setInterval( "checkplaying()", 1000);
      }
   );
} 

function singlestory( player, nid) {
   $.getJSON("/newplaylist.php", { q : nid } ,
      function(data) {
       var ar = eval(data);
       $f(player).play(ar);
      }
   );
} 

function checkplaying() {
   if (   !$f("bigplayer").isPlaying()
       && !$f("bigplayer").isPaused() ) {
     clearInterval(checkplayerID);
     endstory();
   }
}

function endstory() {
   $("#videoPlayer").height(336);
   $("#bigplayer").height(0);
   $f("bigplayer").hide();
   $("#sliders").cycle("resume");
}

function lightbox_bye() {
   $('#darkness').hide();
   $('#light').remove();
   $('#photoflow').height(150);
}

function lightbox(nid) {
   var arrPageSizes = getPageSize();
   $('#light').remove();
   $('.container').after("<div id='light'>&nbsp;</div>");
   $('#photoflow').height(0);
   $('#light').load("/showimage.php?q=" + nid, function(){
     $('#darkness').addClass('black_overlay').css({
         width: arrPageSizes[0], height: arrPageSizes[1]}).show();

     var imwidth = - $('#light').width()/2;
     var imheight = - $('#light').height()/2;

     $('#light').css({
         'margin-top' : imheight, 'margin-left' : imwidth}).fadeIn("slow");
 
     $('#light').bind("click", lightbox_bye);
     $('#darkness').bind("click", lightbox_bye);
   });
}


/**
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function getPageSize() {
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = window.innerWidth + window.scrollMaxX;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }
  var windowWidth, windowHeight;
  if (self.innerHeight) { // all except Explorer
    if(document.documentElement.clientWidth){
      windowWidth = document.documentElement.clientWidth; 
    } else {
      windowWidth = self.innerWidth;
    }
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }       
  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else { 
    pageHeight = yScroll;
  }
  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){      
    pageWidth = xScroll;            
  } else {
    pageWidth = windowWidth;
  }
  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
  return arrayPageSize;
};


