var lock_auto_timeline = false;
var play_after = 0;
var player = null;

$(function(){
	var div = $('div.sc_menu'),

	ul = $('#sc_menu_div');
	
	div.mouseover(function() {
	  lock_auto_timeline = true;
	});

	div.mouseout(function() {
	  lock_auto_timeline = false;
	});
});

function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}

function move_to(seconds) {
  var start = player.getConfig()["state"];

  if (start == "IDLE") {
    player.sendEvent('PLAY');    
     player.addModelListener("STATE","startAfter");
     play_after = seconds;
  } else {
    player.sendEvent('SEEK', seconds);
  }
}
function startAfter(obj) {
  if (play_after && obj.newstate=="PLAYING") {
    setTimeout("player.sendEvent('SEEK', "+play_after+");",500);
    play_after=0;
  }
}

function time_function(obj) {
  //$("#state_sds").html(obj.position);
  if (lock_auto_timeline) return;

  if (isUnsignedInteger(obj.position) && isUnsignedInteger(((obj.position) - 1)/10)) { // &&

    ul = $('#sc_menu_div');
    // alert(obj.position);
//alert(ul);

    var currentPos = ul.find('a.timeline_'+obj.position);

    if (currentPos) {
      //alert(currentPos[0].offsetLeft);
      //var ulWidth = currentPos[0].offsetLeft + lastLi.outerWidth() + ulPadding;
      //var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      var div = $('div.sc_menu');
      div.scrollLeft(currentPos[0].offsetLeft);
      //$("#state_sds").html(currentPos[0].offsetLeft);
    }
  }
}

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	player.addModelListener("TIME","time_function");
}


$(document).ready(function() {
  // hides the vote button
  $("input#vote_button").hide();

  // hides all radio buttons
  $("form#rate_this input[@type=radio]").hide();

  // format the labels to look clickable
  $("form#rate_this label").addClass("selector");

  // change the formatting of the label when it is hovered over
  // by assigning a different class
  $("label.selector").hover(
  function() {
    $(this).addClass("selector_h");
    var current_val = ($(this).children().attr('value'))
    for (i=1;i<=current_val;i++) {
      $("#label_rating_"+i).addClass("selector_h");
    }
  }, function() {
    $(this).removeClass("selector_h");
    var current_val = ($(this).children().attr('value'))
    for (i=1;i<=current_val;i++) {
      $("#label_rating_"+i).removeClass("selector_h");
    }
  });
  $("form#rate_this label").click(function() {
    var current_val = $(this).children().attr('value');
    //$(this).find("input").attr("checked","checked");
    //$("input#vote_button").trigger("click");
    var nid = $('#nid').attr('value');
    $('#rating_scores').html("Your rating: " + current_val);
    
    
    $.post("/index.php?name=movies&action=voting", { 'vote': current_val, 'nid': nid} );
  });
});//end ready