google.load("swfobject", "2.1");

var next_video = '';
var loader = -1;
var ytplayer = null;
var hovering = false;
var hovertimer;

/*
 * Chromeless player has no controls.
 */

// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
  //document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
  alert("An error occured of type:" + errorCode);
  $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/error.png')"});
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
  //unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).
  if (newState == 1) {
    //playing
    clearTimeout(hovertimer);
    loader = -1;
    
    $('#playdiv').hide();
    $('#play').css({cursor: 'none'});
      
    $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/pause.png')"});
    
    $('#playdiv').hover(
      function () {
        $('#playdiv').css({backgroundPosition: 'bottom'});
      }, 
      function () {
        $('#playdiv').css({backgroundPosition: 'top'});
      }
    );
    
    $('#play').mousemove(function(event) {
      $('#playdiv').show();
      $(this).css({cursor: 'pointer'});
      clearTimeout(hovertimer);
      hovertimer = setTimeout("$('#playdiv').hide();$('#play').css({cursor: 'none'});",3000);
    });
    
    $('#play').hover(
      function () {
        $('#playdiv').show();
        hovering = true;
      }, 
      function () {
        if (loader == -1)
          $('#playdiv').hide();
        hovering = false;
        clearTimeout(hovertimer);
      }
    );
    
    $('#play').click(function () {
      pauseVideo();
      return false;
      
    });
    
  } else if (newState == 2) {
    clearTimeout(hovertimer);
    $('#play').css({cursor: 'pointer'});
    // paused
    loader = -1;
    
    if (!hovering)
      $('#playdiv').hide();
      
    $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/play.png')"});
    
    $('#playdiv').hover(
      function () {
        $('#playdiv').css({backgroundPosition: 'bottom'});
      }, 
      function () {
        $('#playdiv').css({backgroundPosition: 'top'});
      }
    );
    
    $('#play').mousemove(function(event){ clearTimeout(hovertimer); return true; });
    
    $('#play').hover(
      function () {
        $('#playdiv').show();
        hovering = true;
      }, 
      function () {
        if (loader == -1)
          $('#playdiv').show();
        hovering = false;
      }
    );
    
    $('#play').click(function () {
      ytplayer.playVideo();
      return false;
      
    });
    
    $('#playdiv').show();
  } else if (newState == 0) {
    clearTimeout(hovertimer);
    $('#play').css({cursor: 'pointer'});
    // ended
    loader = -1;
      
    $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/replay.png')"});
    
    $('#playdiv').hover(
      function () {
        $('#playdiv').css({backgroundPosition: 'bottom'});
      }, 
      function () {
        $('#playdiv').css({backgroundPosition: 'top'});
      }
    );
    
    $('#playdiv').show();
    
    $('#play').mousemove(function(event){ clearTimeout(hovertimer); return true; });
    
    $('#play').hover(
      function () {
        $('#playdiv').show();
        hovering = true;
      }, 
      function () {
        if (loader == -1)
          $('#playdiv').show();
        hovering = false;
      }
    );
    
    $('#play').click(function () {
      ytplayer.playVideo();
      return false;
      
    });
    
    $('#playdiv').show();
  } else {
    clearTimeout(hovertimer);
    $('#play').css({cursor: 'pointer'});
    if (loader == -1) {
      loader = 0;
      loadingAnimation();
    }
    
    $('#playdiv').hover(
      function () {
        $('#playdiv').css({backgroundPosition: (offset + 'px 0px')});
      }, 
      function () {
        $('#playdiv').css({backgroundPosition: (offset + 'px 0px')});
      }
    );
    
    $('#play').mousemove(function(event){ clearTimeout(hovertimer); return true; });
    
    $('#play').hover(
      function () {
        $('#playdiv').show();
        hovering = true;
      }, 
      function () {
        if (loader == -1)
          $('#playdiv').hide();
        hovering = false;
      }
    );
    
    $('#play').click(function () {
      pauseVideo();
      return false;
      
    });
      
    $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/loading.png')"});
    $('#playdiv').show();
  
  }
  
}

// Display information about the current state of the player
function updatePlayerInfo() {
  // Also check that at least one function exists since when IE unloads the
  // page, it will destroy the SWF before clearing the interval.
  if(ytplayer && ytplayer.getDuration) {
    updateHTML("videoDuration", ytplayer.getDuration());
    updateHTML("videoCurrentTime", ytplayer.getCurrentTime());
    updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
    updateHTML("startBytes", ytplayer.getVideoStartBytes());
    updateHTML("bytesLoaded", ytplayer.getVideoBytesLoaded());
    updateHTML("volume", ytplayer.getVolume());
  }
}

// Allow the user to set the volume from 0-100
function setVideoVolume(volume) {
  if(isNaN(volume) || volume < 0 || volume > 100) {
    alert("Please enter a valid volume between 0 and 100.");
  }
  else if(ytplayer){
    ytplayer.setVolume(volume);
  }
}

function playVideo(object, video, width, height, title) {
  if (ytplayer) {
    object.style.backgroundImage = "url('images/layout/lightbox/play.png')";
    $('#lightbox').click(function () {
      $('#lightbox').fadeOut();
      $('#play').hide();
      $('#lightbox-content-video').fadeOut(400, function() { $('#lightbox-content-video').css({margin: '-1px 0 0 -1px', top: '100%', left: '100%'}); $('#lightbox-content-video').show(); $('#play').show(); });
      pauseVideo();
    
    });
    
    $('#lightbox #close').click(function () {
      $('#lightbox').fadeOut();
      $('#play').hide();
      $('#lightbox-content-video').fadeOut(400, function() { $('#lightbox-content-video').css({margin: '-1px 0 0 -1px', top: '100%', left: '100%'}); $('#lightbox-content-video').show(); $('#play').show(); });
      pauseVideo();
      return false;
    
    });
    
    //setVideoVolume(0);
    if (loader == -1) {
      loader = 0;
      loadingAnimation();
      $('#playdiv').css({backgroundImage: "url('images/layout/lightbox/loading.png')"});
    }
    $('#lightbox').hide();
    $('#lightbox').css({top: '0px', left: '0px'}).fadeIn();
    $('#lightboxtitle').html(title);
    Cufon.refresh(['#lightbox h1']);
    $('#lightboxtitle').css({marginTop: (-1*(height/2 + 32)) + 'px', marginLeft: (-1*(width/2)) + 'px', width: (width-28)});
    $('#lightbox-content-video').fadeTo(0, 0);
    $('#lightbox-content-video').css({top: '50%', left: '50%', marginTop: (-1*(height/2)) + 'px', marginLeft: (-1*(width/2)) + 'px', width: width, height: height});
    $('#lightbox-content-video').fadeTo(400, 1);
    ytplayer.setSize(width, height);
    ytplayer.cueVideoById(video);
    ytplayer.playVideo();
  } else {
    object.style.backgroundImage = "url('images/layout/lightbox/error.png')";
  }
  
  return false;
}

function pauseVideo() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function muteVideo() {
  if(ytplayer) {
    ytplayer.mute();
  }
}

function unMuteVideo() {
  if(ytplayer) {
    ytplayer.unMute();
  }
}


// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  $('lightbox-content-video').css({margin: '0'});
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
  //setInterval(updatePlayerInfo, 250);
  //updatePlayerInfo();
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
}

// The "main method" of this sample. Called when someone clicks "Run".
function loadPlayer() {
  // Lets Flash from another domain call JavaScript
  var params = { allowScriptAccess: "always", wmode: "transparent" };
  // The element id of the Flash embed
  var atts = { id: "ytPlayer" };
  // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1&hd=1",
                     "video", "960", "720", "8", null, null, params, atts);
}

function _run() {
  loadPlayer();
}

google.setOnLoadCallback(_run);

function showPhoto(photo, width, height, title) {
  if (width == 0) {
    width = 3000;
    height = 3000;
  }
  $('#lightbox-content-photo').hide();
  $('#lightbox').click(function () {
    $('#lightbox').fadeOut();
    $('#lightbox-content-photo').fadeOut(400, function() { $('#lightbox-content-photo').css({margin: '0px', top: '100%', left: '100%'}); $('#lightbox-content-photo').show(); });
  
  });
  
  closephoto = function () {
    $('#lightbox').fadeOut();
    $('#lightbox-content-photo').fadeOut(400, function() { $('#lightbox-content-photo').css({margin: '0px', top: '100%', left: '100%'}); $('#lightbox-content-photo').show(); });
    return false;
  
  }
  
  $('#lightbox #close').click(closephoto);
  $('#lightbox-content-photo').click(closephoto);
  
  $('#lightboxtitle').html(title);
  Cufon.refresh(['#lightbox h1']);
  $('#lightboxtitle').css({marginTop: (-1*(height/2 + 32)) + 'px', marginLeft: (-1*(width/2)) + 'px', width: (width-28)});
  $('#lightbox-content-photo').css({top: '50%', 
                                    left: '50%', 
                                    marginTop: (-1*(height/2)) + 'px', 
                                    marginLeft: (-1*(width/2)) + 'px', 
                                    width: width, height: height,
                                    backgroundImage: "url('" + photo + "')"});

  $('#lightbox').css({top: '0px', left: '0px'}).fadeIn();
  $('#lightbox-content-photo').fadeIn();
  
  return false;
    
}

$(document).ready(function() {
  $('#play').click(function () {
    pauseVideo();
    return false;
    
  });
    
  $('#play').hover(
    function () {
      $('#playdiv').show();
      hovering = true;
    }, 
    function () {
      if (loader == -1)
        $('#playdiv').hide();
      hovering = false;
    }
  );

  loadingAnimation = function() {
    if (loader != -1) {
      loader = (loader + 1) % 12;
      offset = (11 - loader) * 140;
      $('#playdiv').css({backgroundPosition: (offset + 'px 0px')});
      setTimeout('loadingAnimation()', 100);
        
    }

  }
  
});
