$(document).ready(function(){

    
    
        $('.play a').click(function() {
        $('.play').hide();
        $('.pause').show();
        clearTimeout(slideShowTimeout);
        //rmTime();
        changeImage();
        return false;
    });
    $('.pause a').click(function() {
        $('.play').show();
        $('.pause').hide();
        clearTimeout(slideShowTimeout);
        return false;
    });
    
     $('.play').hide();
    
    
});


function startTV() {
	        $("#dragTV").clickNScroll({
           reverse: false,
           acceleration: 0.5,
           rightMouse: false
        });
   

   $('#dragTV').mousedown(function() {
       $('#dragTV').css('cursor', 'url(http://www.premiersports.tv/assets/templates/ps/images/grab_cursor.cur), pointer')
   });
   $('#dragTV').mouseup(function() {
       $('#dragTV').css('cursor', 'url(http://www.premiersports.tv/assets/templates/ps/images/hand_cursor.cur), pointer')
   });
   
   
   $('#dragTV').scroll(function(object) {
       if($('#tv2-moreinfo').css('top') == "347px") {
        $('#tv2-moreinfo').stop().animate({"top": 390});
       }
      //Schedule has been scrolled
      var am = $(this).scrollLeft();
        
      //Total Width 
      var tot = ($('.hour').width()*6);
      
      //Total width of 7 Days
      var wto = $('.week').width();
      var all = (((am/tot)*wto)*-1);
      
      $('.week').stop().animate({'left': all});
      $('.quart').stop().animate({'left': (all+2)});
     
      mvtotimeofday = ((((am/(tot/6))*(wto/7*6))+(all/6))-8);
      
    /**
     * Highlighting quarts
     * Partial: #D3ACAC
     * Full #E56464
     * Off #CCCCCD;
     */
      //Figure out which quart
      var full = Math.floor(((am/$('.hour').width())*28));
      
      /**
	* Highlight Days
	* RED: #D60000
	* Grey: #989695
	*/
      var hDay = Math.floor(((am/$('.hour').width())*7)+0.2);
      $('.week li').stop().animate({'backgroundColor': '#989695'});
      $($('.week li').get(hDay)).stop().animate({'backgroundColor': '#D60000'});
      
      $('.quart li').stop().animate({'backgroundColor': '#CCCCCD'});
      
      $($('.quart li').get(full)).stop().animate({'backgroundColor': '#E56464'});
      $($('.quart li').get((full-1))).stop().animate({'backgroundColor': '#D3ACAC'});
      $($('.quart li').get((full+1))).stop().animate({'backgroundColor': '#D3ACAC'});
      
      //clearTimeout(totimeofdayTimeout);
      //totimeofdayTimeout = setTimeout(function() {
      if(!ttodLock) {
            $('#totimeofday').stop().animate({'left': mvtotimeofday});
        //}, 70);
      }
    });
    
    var d = new Date();
    var dl = new Date(d.getFullYear(),(d.getMonth()),d.getDate(),0,0,0);
    
    //Calculate Start Time
    var startLeft = d.getTime();
    //Zero it against todays stamp
    startLeft = ((startLeft-dl.getTime()));
    //Start time, per unit time, times total width
    startLeft = (((startLeft/(60*60*24*7*1000))*(160*7*24)));
    
    //$('#dragTV').stop().animate({'scrollLeft': startLeft}, 2000);
    $('#dragTV').scrollLeft( startLeft);
    
    $('.week li span').click(function() {
        var rel = $(this).attr('rel');
        $('#dragTV').stop().animate({'scrollLeft': rel}, 2000);
    });
    
   $('.quart li').click(function() {
       var li = ($(this).index()+0.5);
       var rel = (li*($('.hour').width()/28));
       $('#dragTV').stop().animate({'scrollLeft': rel}, 2000);
   });
    
    
    $( "#totimeofday" ).draggable({ 
        axis: "x",
        drag: function() {
            ttodLock = true;
            var weekl = parseFloat($('.week').css('left'));
            
            var l = (parseInt($(this).css('left'))-weekl);
            var tot = ($('.hour').width());
            var wto = (($('.week').width()/7)*7);
            
            var all = (((l/wto)*(tot)));
            $('#dragTV').stop().animate({'scrollLeft': all});
        },
        stop: function() {
            ttodLock = false;
        },
        containment: 'parent'
                    });


    




    //Bottom Button Events
    $.each([6, 2, 3, 1], function(index, a) {
        
        $('#Forward'+a).click(function() {
            var am = $('#dragTV').scrollLeft();
            $('#dragTV').stop().animate({'scrollLeft': (am+(160*a))}, 2000);
        });
        $('#Back'+a).click(function() {
            var am = $('#dragTV').scrollLeft();
            $('#dragTV').stop().animate({'scrollLeft': (am-(160*a))}, 2000);
        });
    });
    
    
    $('#tvschedule ul.sched a.more').click(function(){
            var trel = 'title'+$(this).attr('rel');
            var drel = 'descr'+$(this).attr('rel');
            var title = $('.moreinfo[rel="'+trel+'"]').text();
            var descr = $('.moreinfo[rel="'+drel+'"]').text();

            var txt = ''+title+' - '+''+descr;
            $('#tv2-moreinfo').html(txt);
            $('#tv2-moreinfo').stop().animate({"top": 347});
            return false;
    });
    
}

function loadTVSchedule2() {
	    //Call the page 
	
        $.ajax({
            url: 'sys/ajax/tv-schedule-2.0/',
            type: 'GET',
            success: function(content2) {
		$('#wrapper #tvload').html(content2);    
		
		    //alert($('#tvscheduleload').css('display'));
		if($('#tvscheduleload').css('display') == 'block') { 
			$('#tvscheduleload').fadeOut('fast');
			$('#tvschedule').fadeIn('fast');
			scrollToTime(); 
			
		}
		startTV();
            }
    }); 
	
	
}

function scrollToTime() {
	
	var d = new Date();
	var dl = new Date(d.getFullYear(),(d.getMonth()),d.getDate(),0,0,0);
			    
	
	//Calculate Start Time
	    var startLeft = d.getTime();
	    //Zero it against todays stamp
	    startLeft = ((startLeft-dl.getTime()));
	    //Start time, per unit time, times total width
	    startLeft = (((startLeft/(60*60*24*7*1000))*(160*7*24)));
	    
	    $('#dragTV').stop().animate({'scrollLeft': startLeft}, 2000);
}
