$(document).ready(function() {
	if ($.browser.msie && navigator.appVersion.match(/ MSIE [345]/)) return;
	
	var updateWidgetContent = function(url, align){
		if($('#programWidget').data('updating')) return;
		$('#programWidget').data('updating', true);
		
		var pixelsInHour  = 240;
		var animateWidget = function(){
			$('#programWidget div.tv-now-row, #programWidget p.tv-nearest-time').animate(
				{left: -$('#programWidget').get(0).$steps * pixelsInHour},
				1000,
				'swing',
				function(){
					$('#programWidget').get(0).$moving = false;
				}
			);
		}
		
		$('#programWidget').fadeOut("normal").queue(function(){
			$(this).html('<div class="tv-nearest-cont"></div><div class="tv-now-in-ether"><img src="/i/programme/ajax-loader.gif" class="tv-loading"></div>').fadeIn("fast").dequeue();
		});
		
		$.get(url, {ajax: 'widget'}, function(data, status) {
			if(status == 'success'){
				$('#programWidget').queue(function(){
					$(this).hide().html(data).prev('h3.tv-current-date').html($('#tv-utime-hidden').html());
					$(this).get(0).$moving = false;
					$(this).get(0).$steps  = align == 'left' ? 0 : 2;
				
					$('#programWidget div.tv-now-row, #programWidget p.tv-nearest-time').css('left', -$('#programWidget').get(0).$steps * pixelsInHour);
				
					$(this).fadeIn("normal");
							
					$('a#tv-next').click(function(e){
						if($('#programWidget').get(0).$moving) return false;
						
						$('#programWidget').get(0).$moving = true
						if(++$('#programWidget').get(0).$steps > 2) {
							updateWidgetContent(this.href, 'left');
						} else {
							if(parseInt($('#programWidget p.tv-nearest-time').css("left")) > -pixelsInHour) {
								$('#programWidget').get(0).$steps = 1;
							} else if (parseInt($('#programWidget p.tv-nearest-time').css("left")) < -pixelsInHour) {
								$('#programWidget').get(0).$steps = 2;
							}
							animateWidget()
						}
						return false;
					});
					
					$('a#tv-prev').click(function(e){
						if($('#programWidget').get(0).$moving) return false;
						
						$('#programWidget').get(0).$moving = true
						if(--$('#programWidget').get(0).$steps < 0) {
							updateWidgetContent(this.href, 'right');
						} else {
							if(parseInt($('#programWidget p.tv-nearest-time').css("left")) > -pixelsInHour) {
								$('#programWidget').get(0).$steps = 0;
							} else if (parseInt($('#programWidget p.tv-nearest-time').css("left")) < -pixelsInHour) {
								$('#programWidget').get(0).$steps = 1;
							}
							animateWidget()
						}
						return false;
					});
					$('#programWidget').data('updating', false);
					$(this).dequeue();
				});
			}
			
		});
	}
	
	updateWidgetContent('/program/time/', 'left');
	$("body.root .tv-nearest-time, body.root .tv-now-row").livequery(function(){
		$(this).draggable({
			axis:"x",
			drag: function(event, object) {
				var current = object.helper;
				var position = object.position.left;
				if(position > -480 && position < 0) {
					$("body.root .tv-nearest-time, body.root .tv-now-row").css("left", position);
				} else if(position < -480) {
					var href = $("#tv-next").attr("href");
					updateWidgetContent(href, 'left');
					return;
				} else if(position > 0) {
					var href = $("#tv-prev").attr("href");
					updateWidgetContent(href, 'right');
					return;
				}
			}
		});
	});
});
