var DOMAIN = 'http://localhost:8000/tuniscope';
var over_submenu = false;
var over_menu = false;
var do_rnd = true;
var do_webtv_rnd = true;
var pics = 0;
var s = -1;
var HOVER = false;
var currentTimer;
var currentCounter = -1;
var playerND;
var playerHD;

$(document).ready(function () {
	//$("body.main").bind("click", function(e) { window.location = 'http://www.google.com'; });
	if ($('.nomiss').length)
		$('.nomiss').tipsy({fade: true, gravity:'n'});
	
	if ($.support.opacity)  {
		$(".opacity").css('opacity', 1).hover(
						function () {
							$(this).fadeTo('fast', 0.7); //.parent().children("ul").hide().slideDown();
						}, 
						function () {
							$(this).fadeTo('fast', 1.0); //.parent().children("ul").slideUp().hide();
						}
					);
		
		
	
	}
	
	if ( $("#Gallery").length ) {
		
		$("#Gallery").jcarousel(
			{
				visible:3, 
				scroll: 3 /*, 
				initCallback: function(carousel) {
					$('#push #screens a.screen').bind('click', function() {
						carousel.scroll($.jcarousel.intval($(this).attr('rel')));
						return false;
					});
				},
				itemVisibleInCallback:  {
					onBeforeAnimation: function(carousel, item, idx, state) {},
					onAfterAnimation:  function(carousel, item, idx, state) {
						// No animation on first load of the carousel
						if (state == 'init')
							return;
	
						$("#push #screens a").removeClass('selected');
						$("#push #screens a.screen[rel='" + idx + "']").addClass('selected');
					}
				}*/
			}
		);	
	}
	
	
	$("a[rel^='day,']").click (function(e) {
		var arr = $(this).attr('rel').split(',');
		var DAY = arr[1];
		var displayDay = arr[3];
		displayDay = displayDay.replace('###', '</span>').replace('##', '<span>');
		
		$("a[rel^='day,']").removeClass('even');
		$(this).addClass('even');
		$("#program ul li").removeClass('show').fadeOut('slow').hide();	
		$("#program ul li.program-" + DAY).addClass('show').fadeIn('slow').show();	
		$("#calendar td.date h3").html(displayDay);
	});
	
	$("a[rel^='menu_']").bind('mouseenter', function(e) {
		var vID =  '#' + this.rel ;
		$("div[id!=" + vID + "][id^='menu_']").hide();
		
		var p = $(this).offset();
		var h = $(this).height();
		
		over_menu = true; 
		
		var menu_alignment = $(vID).attr('title');
		$(vID).css({textAlign:menu_alignment});
		
		//$(vID).css({left:p.left, top:p.top + h + 10});
		$(vID).show();
			
   	});
   	
   	$("a[rel^='menu_']").bind('mouseleave', function(e) {over_menu = false;});
   	
   	$("#submenu").bind('mouseleave', function(e) {		over_submenu = false;		   	});	  
	
	if ( $("a[id^='tip']").length ) {
		$("a[id^='tip']").bind('mouseenter', function() {
			//if ( $("div[id='spotPlaceholder']").length ) $("div[id='spotPlaceholder']").hide();
			//if ( $("div[id='bannerRight']").length ) $("div[id='bannerRight']").hide();
			$("#bannerRight iframe").hide();
			if ( $("div[id='spotPlaceholder']").length ) $("#spotPlaceholder object").hide();
			//$("object").hide();
		});
		
		
		$("a[id^='tip']").bind('mouseleave', function(e) {
			//if ( $("div[id='spotPlaceholder']").length ) $("div[id='spotPlaceholder']").show();
			$("#bannerRight iframe").show();
			if ( $("div[id='spotPlaceholder']").length ) $("#spotPlaceholder object").show();
			//if ( $("div[id='bannerRight']").length ) $("div[id='bannerRight']").show();
			//$("object").show();
		});
		
	}
	
	
	$("#submenu").bind('mouseenter', function(e) {over_submenu = true;});	   
   	
   	$("div[id^='topnewsImg']").bind('mouseenter', function(e) {do_rnd=false;});
   	$("div[id^='topnewsImg']").bind('mouseleave', function(e) {do_rnd=true;});   	
   	$("h3[id^='topnewsHdr']").bind('mouseenter', function(e) {do_rnd=false;});
	$("h3[id^='topnewsHdr']").bind('mouseleave', function(e) {do_rnd=true;});
	$("a[id^='topnewsHref']").bind('mouseleave', function(e) {
		do_rnd=true;
	});
	
	$("a[id^='topnewsHref']").bind('mouseenter', function(e) {
		do_rnd=false;
		
		var ID = $(this).attr('id');
		var tmp = ID.split('topnewsHref');
		var r = tmp[1];
		
		pics = r;
		
		$("div[id='topnewsImg" + r + "']").show();
		$("div[id^='topnewsImg'][id!='topnewsImg" + r + "']").hide();
		
		$("h3[id='topnewsHdr" + r + "']").show();
		$("h3[id^='topnewsHdr'][id!='topnewsHdr" + r + "']").hide();
		
	});
	
   	hideSubMenu();
   	topnews();
	
	
	if ( $(".ND").length ) {
		$(".ND").bind("click", function() {
			$(".ND .off").hide();
			$(".ND .on").show();
			
			$(".HD .off").show();
			$(".HD .on").hide();
			
			$(".ver_HD").hide();
			$(".ver_ND").show();
			//playerND.sendEvent("PLAY","true");
		});
	}
	if ( $(".HD").length ) {
		$(".HD").bind("click", function() {
			$(".ND .on").hide();
			$(".ND .off").show();
			
			$(".HD .on").show();
			$(".HD .off").hide();
			
			$(".ver_ND").hide();
			$(".ver_HD").show();
			//playerHD.sendEvent("PLAY","true");
		});
	}
	
	$("#previewDetails > li").bind('mouseenter', function(e) {
		do_webtv_rnd=false;
		$("#previewDetails > li").removeClass("visible");
	});
   	$("#previewDetails > li").bind('mouseleave', function(e) {
		do_webtv_rnd=true;
		
		clearTimeout(currentTimer);
		currentCounter = 1;
		WebTV();
	});   
	
	WebTV();
});

function WebTV() {
	if (do_webtv_rnd) {	
		currentCounter++ ;
		if (currentCounter > 4)
			currentCounter = 0;
		
		try {
			$("#previewDetails > li").removeClass("visible");
			$("#previewDetails > li:eq(" + currentCounter + ")").addClass("visible");
		}
		catch(e) {
			alert(e);
		}
	}
	
	currentTimer = setTimeout('WebTV()', 5000);
}

function hideSubMenu() {
	if (!over_submenu && !over_menu) {
		$("div[id^='menu_']").hide();
	}
	
	setTimeout('hideSubMenu()', 5000);
}

function topnews() {
	if (do_rnd) {	
		/*
		var r = Math.random();
		r = r * 5;
		r = Math.ceil(r);
		*/
		
		pics++ ;
		if (pics > 5)
			pics = 1;
		
		if ($("div[id='topnewsImg" + pics + "']").length) {
			$("div[id='topnewsImg" + pics + "']").show();
			$("div[id^=topnewsImg][id!='topnewsImg" + pics + "']").hide();
				
			$("h3[id='topnewsHdr" + pics + "']").show();
			$("h3[id^=topnewsHdr][id!='topnewsHdr" + pics + "']").hide();
			
			$("a[id='topnewsHref" + pics + "']").attr('class', 'selected');
			$("a[id^=topnewsHref][id!='topnewsHref" + pics + "']").attr('class', '');
		}
	}
	
	setTimeout('topnews()', 5000);
}


function playerReadyCallback(obj) {
	playerND = document.getElementById(obj['spotVideoND']);
	playerHD = document.getElementById(obj['spotVideoHD']);
};
