var xmlHttp;

function loadCurrent(){
today=new Date();
thisDay=today.getDay();
showProgram(thisDay);

}

function resetAll(){
	
var menu = document.getElementById('sheudle_menu');
for (var i=0; i<menu.childNodes.length; i++){
	menu.childNodes[i].className ='';
		
	}
}

function setMenuClass(id){
var menu = document.getElementById('sheudle_menu');
for (var i=0; i<menu.childNodes.length; i++){
	if (i+1==id) {menu.childNodes[i].className ='current'}
	}
}

function showProgram(str) {
	resetAll();
	setMenuClass(str);
	if (str.length == 0) {
		document.getElementById("programlist").innerHTML = "";
		return;
	}
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "/showprogram/";
	url = url + str + "/" + Math.random();

	xmlHttp.onreadystatechange = showProgramStateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function showProgramStateChanged() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		document.getElementById("programlist").innerHTML = xmlHttp.responseText;
	}
}

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}




// tv guide box functions
current_list = "tvList";
bgImage = newImage = "url(/img/site/marked_cell.jpg)";

function scrollItUp(adv) {
	var the_thing = document.getElementById(current_list).style;
	var current_top = parseInt(the_thing.top);
	var new_top = current_top +5;
//alert(current_top+'   '+new_top);
	if (current_top<0) {the_thing.top = new_top + "px";}
	if (adv < 34 && current_top < 0) {
		adv += 1;
		fee = adv;
		the_timeout = setTimeout("scrollItUp(fee);",1);
	} else {
		clearTimeout(the_timeout);
		adv=0;
	}
}

function scrollItDown(adv) {
	var the_thing = document.getElementById(current_list).style;
	var divH = document.getElementById(current_list).offsetHeight;
	var current_top = parseInt(the_thing.top);
	var new_top = current_top - 5;
	if (current_top > (-divH+250)) {the_thing.top = new_top + "px";}
	if (adv < 34) {
		adv += 1;
		fee = adv;
		the_timeout = setTimeout("scrollItDown(fee);",1);
	} else {
		clearTimeout(the_timeout);
		adv=0;
	}
}

function tvScroll(direction) {
	var the_thing = document.getElementById(current_list).style;
	var current_top = parseInt(the_thing.top);
	if (direction=='up' && current_top!=-5) {scrollItUp(0);} else {if(direction=='up'){the_thing.top = 0 + "px";}}
	if (direction=='down') {scrollItDown(0);} else {}
}

// match user time with tv guide box item
function matchTime() {
	if (document.getElementById("tvList")) {
		var dateNow = new Date ();
		var hour = dateNow.getHours ();
		var mins = dateNow.getMinutes ();
	
		var hour1 = Math.floor ( hour / 10 );
		var hour2 = hour % 10;
		var mins1 = Math.floor ( mins / 10 );
		var mins2 = mins % 10;
		
		// 24 hour time now
		hourNow = hour1.toString() + hour2.toString();
		hourNow = parseInt(hourNow,10);
		minNow = mins1.toString() + mins2.toString();
		
	
		
		var cellMarked = -1;
		// make array out of all 'time' table cells
		var arrayTimeCells = getElementsByClassName( 'time', document.getElementById("tvList") );
		// loop through array of time cells
		for (i = 0; i < arrayTimeCells.length; i++) {
			var strCell = arrayTimeCells[i].innerHTML;
			foundColon = strCell.indexOf(":");
			//folosim ora in format 24!!! foundPm = strCell.indexOf("pm");
			// create array of each number before colon 
			//var hourInCell = strCell.substring(0,foundColon);
			//var minInCell = strCell.substring(foundColon+1,foundColon+3);
			//hourInCell = parseInt(hourInCell);
			var hourInCell = strCell.substring(0,2);
			var minInCell = strCell.substring(3,5);
			//folosim ora in format 24!!!   if ((foundPm!=-1)&&(hourInCell!=12)) {hourInCell+=12}else{if ((foundPm==-1)&&(hourInCell==12)){hourInCell-=12}}
			// if the hour of program = current hour, flag it (only last loop counted so we get the nearest match)
			if (hourInCell==hourNow) {
				if (minNow>=minInCell) {
					// mark cell closest to current minute
					cellMarked=i;
				}
			} else {
			// if hour of program is less than current hour, mark nearest program
				if (hourInCell<hourNow) {
					cellMarked=i;
				}
			}
		}// end loop


	
		// if a matching program has been found, scroll to that point, don't scroll too far
		if (cellMarked>-1) {
			cdivH = document.getElementById("tvList").offsetHeight;

			ourCell = arrayTimeCells[cellMarked];
			fromBottom = arrayTimeCells.length-cellMarked;
			ourCell.style.backgroundImage = bgImage;
			var element2 = ourCell.nextSibling;
			while (element2.nodeType !=1) {
		          element2 = element2.nextSibling;

			} 
			element2.style.backgroundImage = bgImage;
			amountToScroll = ((cellMarked*-1*25)+25);
			if (cellMarked ==0) {amountToScroll = 0;}

			// if program item is less than 7 rows from bottom, avoid scrolling too far
			if (fromBottom<=7) {amountToScroll=(cdivH-cdivH*2)+250;}

			//if (amountToScroll>-80) {amountToScroll=0;}
			document.getElementById("tvList").style.top=amountToScroll+'px';
		}
	}
}


function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");
    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    return ar;
}

// end TV guide box functions



