var curHL = 0;
var curNewslistInd = 0;
var curBoxRecoL = 0; // left side = program
var curBoxRecoR = 0; // right side = recommended
var clockID = 0;

$(document).ready(function(){

	// *** Box nav
	if ($('.box_nav_button_prev').length > 0){
		setupBoxNavigation();
	}
	
	// *** Home elements	
	if ($('div#newsblock0').length > 0){
		// *** Home newslist Box nav
		setupHomeNewslistNavigation();
		openNewslist(0);
		// *** Home Highlight
		setupHLNavigation();
		curHL = Math.ceil(Math.random()*(hlIMG.length)) - 1;
		openHL(curHL);
	}
	// *** Box Bottom Reco
	if ($('img#recommend_prev').length > 0){
		setupBoxRecoNavigation();
		curBoxRecoL = Math.ceil(Math.random()*(boxBottomProgramList.length)) - 1;
		curBoxRecoR = Math.ceil(Math.random()*(boxBottomRecoList.length )) - 1;
		
		openBoxReco();
	}

	// *** Schedule Day's navigation 
	if ($('img.nav_big_next').length > 0){
		setupDaysNavigation();
	}
	// *** Schedule Hours onMouseOver 
	if ($('.schedule_list_item').length > 0){
		setupHoursNavigation();
	}
	
	startClock();

});

// *** Clock
function startClock() {
   clockID = setTimeout("updateClock()", 500);
}
function updateClock(){
	if(clockID) {
    	clearTimeout(clockID);
    	clockID  = 0;
	}	
	var tDate = new Date();
	var currentHours = tDate.getHours ( );
	var currentMinutes = tDate.getMinutes ( );
	//var currentSeconds = tDate.getSeconds ( );	
	currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
	//currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
	var timeStr = "" + currentHours + ":" + currentMinutes;
	
	$('div#top_time').html(timeStr);
	clockID = setTimeout("updateClock()", 1000);
}



// *** Schedule Days navigation
function setupDaysNavigation(){
	$('img.nav_big_prev').hover(function(){
		$(this).attr("src", "/local/img/nav_big_prev_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/nav_big_prev_out.gif");
    });
	
	$('img.nav_big_next').hover(function(){
		$(this).attr("src", "/local/img/nav_big_next_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/nav_big_next_out.gif");
    });

}
// *** Schedule Days navigation
function setupHoursNavigation(){
	$('.schedule_list_item').hover(function(){
		$(this).css({background: "#212121" });
	}, function() {  
        $(this).css({background: null });
    });
}


// *** Box Bottom Reco (program / recom) 
function setupBoxRecoNavigation(){
	$('img#recommend_prev').click(function(event){
		curBoxRecoL ++;
		curBoxRecoR ++;
		openBoxReco();
	});    
	$('img#recommend_next').click(function(event){
		
		curBoxRecoL --;
		curBoxRecoR --;
		openBoxReco();
	});    
}

function openBoxReco(){
	if (curBoxRecoL > boxBottomProgramList.length - 1)
		curBoxRecoL = 0;
	if (curBoxRecoL < 0)
		curBoxRecoL = boxBottomProgramList.length - 1;
	if (curBoxRecoR > boxBottomRecoList.length - 1)
		curBoxRecoR = 0;
	if (curBoxRecoR < 0)
		curBoxRecoR = boxBottomRecoList.length - 1;
	hideBoxReco();
}
function hideBoxReco(){
 $('div#box_recommend_left').fadeOut("fast");
 $('div#box_recommend_right').fadeOut("fast", setupBoxReco);
}
function setupBoxReco(){
	var titleL = boxBottomProgramList[curBoxRecoL].title;
	var infoL = boxBottomProgramList[curBoxRecoL].info;
	var urlL = boxBottomProgramList[curBoxRecoL].url;
	var imgsrcL = boxBottomProgramList[curBoxRecoL].image;
	var cStyle = boxBottomProgramList[curBoxRecoL].cStyle;

	var titleR = boxBottomRecoList[curBoxRecoR].title;
	var infoR = boxBottomRecoList[curBoxRecoR].info;
	var urlR = boxBottomRecoList[curBoxRecoR].url;
	var imgsrcR = boxBottomRecoList[curBoxRecoR].image;

	$('div#box_recommend_left_bg').removeClass();
	$('div#box_recommend_left_bg').addClass(cStyle);
	$('div#box_recommend_left>div>div.box_recommend_holder').html('<div class="boxRecoImg fl"><a href="' + urlL + '" class="nocolor nostyle"><img src="' + imgsrcL + '" border= "0" alt="" /></a></div><div class="boxRecoDescr fl"><div class="boxRecoTitle"><a href="' + urlL + '" class="nocolor nostyle">' + titleL + '</a></div><div class="boxRecoInfo"><a href="' + urlL + '" class="nocolor nostyle">' + infoL + '</a></div></div><div class="clearfloat"><!-- --></div>');
	$('div#box_recommend_right>div>div.box_recommend_holder').html('<div class="boxRecoImg fl"><a href="' + urlR + '" class="nocolor nostyle"><img src="' + imgsrcR + '" border= "0" alt="" /></a></div><div class="boxRecoDescr fl"><div class="boxRecoTitle"><a href="' + urlR + '" class="nocolor nostyle">' + titleR + '</a></div><div class="boxRecoInfo"><a href="' + urlR + '" class="nocolor nostyle">' + infoR + '</a></div></div><div class="clearfloat"><!-- --></div>');
	 
	
	$('div#box_recommend_left').fadeIn("fast");
	$('div#box_recommend_right').fadeIn("normal");
	
}




// *** Home Newslist 
function setupHomeNewslistNavigation(){
	$('img#home_news_prev').click(function(event){
		openNewslist(curNewslistInd - 1);
	});    
	$('img#home_news_next').click(function(event){
		openNewslist(curNewslistInd + 1);
	});    
}
function openNewslist(n){
	curNewslistInd = n;
	if (curNewslistInd > newsIMG.length - 3)
		curNewslistInd = 0;
	if (curNewslistInd < 0)
		curNewslistInd = newsIMG.length - 3;
	
	hideNewslist();
}
function hideNewslist(){
 $('div#newsblock0').fadeOut("fast");
 $('div#newsblock1').fadeOut("fast");
 $('div#newsblock2').fadeOut("fast", setupNewslist);
}


function setupNewslist(){
	var i=curNewslistInd;
	
	for (i=curNewslistInd; i<(curNewslistInd + 3); i++){
		
		var imgsrc = " ";
		var url = " ";
		var title = " ";
		var info = " ";
		var myDate = " ";
		
		if (newsIMG[i]){
			imgsrc = newsIMG[i];
		}
		if (newsURL[i]){
			url = newsURL[i];
		}
		if (newsTitle[i]){
			title = newsTitle[i];
		}
		if (newsInfo[i]){
			info = newsInfo[i];
		}
		if (newsDate[i]){
			myDate = newsDate[i];
		}
		
		$(('div#newsblock_img' + (i - curNewslistInd))).html('<a href="' + url +'" class="nocolor nostyle" target="_self"><img src="' + imgsrc + '" border="0" alt=""/></a>');
		$(('div#newsblock_title' + (i - curNewslistInd))).html('<a href="' + url +'" class="nocolor nostyle" target="_self">' + title + '</a>');
		
		$(('div#newsblock_date' + (i - curNewslistInd))).html(myDate);
		$(('div#newsblock_info' + (i - curNewslistInd))).html('<a href="' + url +'" class="nocolor" target="_self">' + info + '</a>');
		
	}
	
	$('div#newsblock0').fadeIn("fast");
	$('div#newsblock1').fadeIn("normal");
	$('div#newsblock2').fadeIn("slow");
	
}





// *** Box Navigation
function setupBoxNavigation(){
	$('.box_nav_button_prev').hover(function(){
		$(this).attr("src", "/local/img/nav_prev_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/nav_prev_out.gif");
    });
	
	$('.box_nav_button_next').hover(function(){
		$(this).attr("src", "/local/img/nav_next_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/nav_next_out.gif");
    });
}

// *** HighLight Navigation
function setupHLNavigation(){
	// *** Next 
	$('.hl_button_next').hover(function(){
		$(this).attr("src", "/local/img/hl_nav_next_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/hl_nav_next_out.gif");
    });
	$('.hl_button_next').click(function(event){
		openHL(curHL + 1);
	});    
	// *** Prev
	$('.hl_button_prev').hover(function(){
		$(this).attr("src", "/local/img/hl_nav_prev_over.gif");
	}, function() {  
        $(this).attr("src","/local/img/hl_nav_prev_out.gif");
    });
	$('.hl_button_prev').click(function(event){
		openHL(curHL - 1);
	});  	
}

// *** Open HighLight
function openHL(n){
	curHL = n;
	if (curHL > hlIMG.length - 1)
		curHL = 0;
	if (curHL < 0)
		curHL = hlIMG.length - 1;
		
	hideHL();
}

function hideHL(){
 $('div#hl_holder').fadeOut("fast", setupHL);
}

function setupHL(){
	var imgsrc = hlIMG[curHL];
	var url = hlURL[curHL];
	var title = hlTitle[curHL];
	var info = hlInfo[curHL];
	var recomm = hlRecommend[curHL];
	$('div#hlTitle').html('<h1 class="hl"><a href="' + url +'" class="nocolor nostyle" target="_self">' + title + '</a></h1>');
	$('div#hlInfo').html('<a href="' + url +'" class="nocolor nostyle" target="_self">' + info + '</a>');
	$('div#hlImg').html('<a href="' + url +'" class="nocolor nostyle" target="_self"><img src="' + imgsrc + '" border="0" alt=""/></a>');
	$('div#hl_recommended').removeClass();
	$('div#hl_recommended').addClass('r' +recomm);
	$('div#hl_holder').fadeIn("fast");
	
}



