// JavaScript Document
var doingit = false;
var move = true;
var hourShown = 0;
$(document).ready (
	function(){
  $("td.activetd").tooltip({ 
    extraClass: "showpreview",
    showUrl: false,
    bodyHandler: function() { 
        return $("#"+$(this).attr ("activediv")).html(); 
    }, 
    showURL: false 
});
	hourShown = $("#fselecttime").attr('h');

	$("#hour_slider").slider({
			value: hourShown,
			min: 0,
			max: 21,
			step: 0.5,
			slide: function(event, ui) {
				//$("#fselecttime").html (ui.value);
				move_guide_to (ui.value);
			}
		});
		
	$("#listing_container").css ("overflow", "hidden");
	$(document).scroll (function () {
    var coordinates = $("#listing_container").offset();
    
    if ($(document).scrollTop () > coordinates.top + 20)
      $("#float_times_container").fadeIn();
    else 
      $("#float_times_container").fadeOut();
  });
  $(window).resize (function (){
    set_float_position ();
  });
	$("#test").click (function () {
		alert ($("#listing_container").scrollLeft());
    move = false;
	});
  $("#fselecttime").change (function () {
    move_guide_to ($("#fselecttime").val ());
  });
  $("#prevh").click (function (){
    if (hourShown > 3)
      move_guide_to (hourShown - 3);
    else 
      move_guide_to (0);
  });
  $("#nexth").click (function (){
    if (hourShown < 18)
      move_guide_to (hourShown + 3);
    else
      move_guide_to (21);
  });
  
  set_float_position ();
  move_guide_to (hourShown);
});
function set_float_position (){
  var coordinates = $("#listing_container").offset();
  $("#float_times_container").css ('left', coordinates.left);
}
function move_guide_to (hour){
	if (! doingit){
		doingit = true;
    for( i=0; i < hour_to_td["h"+hour].length; i++){
      var td_id = hour_to_td["h"+hour][i];
      
      var marginLeft = (hour/1 - $('#' + td_id).attr('starts')) /0.5 * 134;

      $('#' + td_id + " div.show_listing_contents").css ('margin-left',  marginLeft );
    }

    /*
		$("#listing_table td.show").each (function (){
			if ($(this).attr('length')/1 >= 1){
				if (($(this).attr('starts') < hour/1)/1 && ($(this).attr('ends')/1 > hour/1 )) {
					var marginLeft = (hour/1 - $(this).attr('starts')) /0.5 * 134;
          if (move)
            $('#' + $(this).attr('id') + " div.show_listing_contents").css ('margin-left',  marginLeft );
				}
				else {
					$('#' + $(this).attr('id') + " div.show_listing_contents").css ('margin-left',  0 );
				}
			}
		});*/
		count = false;
    hourShown = hour;
		$("#listing_container").scrollLeft (hour/0.5 * 134);
		$("#dmonitor").html (hour);
    $("#float_times_container").scrollLeft (hour/0.5 * 134);
    $("#fselecttime").val (Math.floor(hour));
		doingit = false;
	}
}
