var thePlayer;
var thePlayerRoot;
var theList;
var theListRoot;

var introVideoFinished = 0
var currentVideoID = -1
function initialize() {
	thePlayer = document.getElementById("playerhandle");
	theList = document.getElementById("listhandle");
	thePlayerRoot  = document.getElementById("playerroot");
	theListRoot  = document.getElementById("listroot");

}


function createPlayer(theFile, theImg, start, icons) {
	var s1 = new SWFObject("/resources/mediaplayer.swf","dragplayer","400","240","7");
	var findHTTP = /bigscreen/;
	s1.addParam("allowfullscreen", "false");
	s1.addVariable("enablejs","true");
	s1.addVariable('javascriptid','jsvid');
    s1.addParam('abouttext','About Bigscreen')
    s1.addParam('aboutlink','http://entertainment.ie/aboutus/')
	//s1.addParam('flashvars','&file=' + theFile + '&skin=/resources/snel.swf&autostart=true');
    s1.addVariable('type', 'video');
    if (theFile.search(findHTTP) == -1) {
        s1.addVariable('file', encodeURIComponent(theFile));
    }
    else {
        s1.addVariable('file', theFile);
    }


    
	if (start) s1.addVariable("autostart", "true");
	if (! icons) s1.addVariable("showicons", "false");
	if (theImg != "") s1.addVariable("image", theImg);

	s1.write("videoplayer");
}

function getUpdate(typ,pr1,pr2,swf) { 
  var currentVideoCSSID

  if(typ == 'state'){ 
    if (pr1 == 0 && introVideoFinished == 0) {    
      introVideoFinished = 1;
      if (currentVideoID >= 0) {
        $(currentVideoCSSID).removeClass("playlistselect");
        console.log("unhighlight prev movie");
      }
      currentVideoID++;
      currentVideoCSSID = "#videoentry" + String(currentVideoID);
      console.log(currentVideoCSSID);
      console.log("highlight next movie");
      $(currentVideoCSSID).addClass("playlistselect");
      createPlayer($(currentVideoCSSID).attr("name"), "images/trailer-placeholder.jpg", false, true);
    
        }
    }
  };

function playvideo(flvfile) {
	createPlayer(flvfile, "", true, true);
}

//jquery onload
$(document).ready(function() {
    $("#playlistbox").show()
    $(".slider").jCarouselLite({
        btnNext: ".videonext",
        btnPrev: ".videoprev",
        vertical: true,
        circular: true,
        visible: 2,
        scroll: 2,
        speed: 800
    });
    $(".slider li").mouseover(function(){
        $(this).addClass("playlistselect");
    });
    $(".slider li").mouseout(function(){
        $(this).removeClass("playlistselect");
    });
    $(".slider li").click(function(){
        //$("#trailertitle").replaceWith($(this).attr("title"));
    //alert($(this).attr("title"));
        playvideo($(this).attr("name"));
    }); 
    imgRO.rollover.init();
    
    $('#hometop10box > ul').tabs();
    
    //createPlayer("http://bigscreen.ie/resources/trailers/preload.flv", "images/trailer-placeholder.jpg", true, true);
    //mouseover buttons
    //$(".videonext").attr( 'src', imgRO.rollover.newimage($(".videonext").attr('src')) );
    //$(".videoprev").attr( 'src', imgRO.rollover.newimage($(".videoprev").attr('src')) );
    //console.log("src:", $(".videoprev").attr('src'), "!");
    //play intro movie
    createPlayer("http://bigscreen.ie/resources/trailers/preload.flv", "", true, true);
    //mouseout buttons
    //$(".videonext").attr( 'src', imgRO.rollover.oldimage($(".videonext").attr('src')) );
    //$(".videoprev").attr( 'src', imgRO.rollover.oldimage($(".videoprev").attr('src')) );
    //$("#trailertitle").replaceWith($(".slider li:first").attr("title"));  
});

imgRO = {};

imgRO.rollover = 
{
   init: function()
   {
      this.preload();
      
      $(".ro").hover(
         function () { $(this).attr( 'src', imgRO.rollover.newimage($(this).attr('src')) ); }, 
         function () { $(this).attr( 'src', imgRO.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', imgRO.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)/) ) + '-on' + src.match(/(\.[a-z]+)/)[0];
   },
   
   oldimage: function( src )
   {
      return src.replace(/-on/, '');
   }
};
