
// epg vars
var closeit; // timer
var currentActive = "n";

// current tab - tv guide box
var current_list = "listAbc";

// generic onload
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

// generic get by class name
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;
}

// popup info on epg listing rollover
function showMore(prgCat,prgRat,prgTitle,imgLoc,prgText,bitId) {
	popInfoN = "nfo"+bitId;
	if (currentActive!="n") {closePopNow(currentActive);clearTimeout(closeit);}
	document.getElementById(popInfoN).innerHTML = "&nbsp;"; // wipe div contents
	if(imgLoc!="na"||prgText!="na") {
	popStatus = 1;
	document.getElementById(popInfoN).innerHTML = "<div id=\"popContent\"><h2 id=\"pn\">Program Name</h2><em id=\"showCat\"></em>, <em id=\"showRate\"></em><img src=\"http://www.abc.net.au/tv/img/1p.gif\" id=\"showMoreImg\" name=\"showMoreImg\" border=\"0\"><p id=\"morePara\"></p></div>";
	document.getElementById(popInfoN).style.display='block';
	} else {popStatus = 0;}
	if (popStatus == 1) {
		if(imgLoc!="na") {
			document.getElementById("showMoreImg").src = imgLoc;
			document.getElementById("showMoreImg").style.display='block';
		} else {document.getElementById("showMoreImg").style.display='none';}
		if(prgText!="na") {
			document.getElementById("morePara").innerHTML = prgText;
		} else {document.getElementById("morePara").innerHTML = "&nbsp;";}
		document.getElementById("pn").innerHTML = prgTitle;
		document.getElementById("showCat").innerHTML = prgCat;
		if(prgRat!="Not Specified") {document.getElementById("showRate").innerHTML = prgRat;}
	}
	currentActive = popInfoN;
}


function closePop(pba) {
	tmppba = pba;
	closeit = setTimeout("closePopNow(tmppba)",1000)
}
function closePopNow(pb) {
	document.getElementById(pb).style.display='none';
	document.getElementById(pb).innerHTML = "&nbsp;";
}


// tv guide box functions
function tvTab(tabname) {
	if (tabname=="abc3") {
		document.getElementById("tvGuideBox-Abc3").style.display='block';
		document.getElementById("tvGuideBox-Abc2").style.display='none';
		document.getElementById("tvGuideBox-Abc").style.display='none';
		current_list = "listAbc3";
		matchTime();
	}
	if (tabname=="abc2") {
		document.getElementById("tvGuideBox-Abc2").style.display='block';
		document.getElementById("tvGuideBox-Abc").style.display='none';
		document.getElementById("tvGuideBox-Abc3").style.display='none';
		current_list = "listAbc2";
		matchTime();
	}
	if (tabname=="abc") {
		document.getElementById("tvGuideBox-Abc").style.display='block';
		document.getElementById("tvGuideBox-Abc2").style.display='none';
		document.getElementById("tvGuideBox-Abc3").style.display='none';
		current_list = "listAbc";
		matchTime();
	}
}

function scrollItUp(adv) {
	var the_thing = document.getElementById(current_list).style;
	var current_top = parseInt(the_thing.top);
	var new_top = current_top + 5;
	if (current_top<-6) {the_thing.top = new_top + "px";}
	if (adv < 35 && 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+202)) {the_thing.top = new_top + "px";}
	if (adv < 35) {
		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("tvGuideBox")) {
		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(current_list) );
		// loop through array of time cells
		for (i = 0; i < arrayTimeCells.length; i++) {
			var strCell = arrayTimeCells[i].innerHTML;
			foundColon = strCell.indexOf(":");
			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);
			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(current_list).offsetHeight;
			ourCell = arrayTimeCells[cellMarked];
			fromBottom = arrayTimeCells.length-cellMarked;
			ourCell.style.background='#d0e9ff';
			var element2 = ourCell.nextSibling;
			while (element2.nodeType !=1) {
		          element2 = element2.nextSibling;
			} 
			element2.style.background='#d0e9ff';
			amountToScroll = ((cellMarked*-1*22)+20);
			// if program item is less than 7 rows from bottom, avoid scrolling too far
			if (fromBottom<=7) {amountToScroll=(cdivH-cdivH*2)+202;}
			if (amountToScroll>-80) {amountToScroll=0;}
			document.getElementById(current_list).style.top=amountToScroll+'px';
		}
	}
}
// end TV guide box functions


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

//addLoadEvent(findMRSSLinks)

function findMRSSLinks() {
	var links = document.getElementsByTagName('A');
	for (var i=0; i<links.length; i++) {
		var link = links[i];
		if ((link.href.indexOf(".xml") > -1) && (link.href.indexOf("tv_epg.xml") == -1)) {
			link.href = "/tv/video/program.htm?feed=" + link.href + "&story=1"
		} 
	}
}




function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

// onload tv guide box
addEvent(window, 'load', matchTime);



