var macbug = navigator.userAgent.indexOf("Mac") > -1? true: false;
var winbug = navigator.userAgent.indexOf("Win") > -1 && navigator.userAgent.indexOf("MSIE 5") > -1? true: false;

if (document.getElementById){			
	if (winbug)
	{
		layers = "document.all.";
	}
	else
	{
		layers = "document.getElementsByTagName('*').";
	}
	style = ".style";
	if (navigator.userAgent.indexOf("Mac") > -1 && navigator.userAgent.indexOf("MSIE 5.0") > -1) { 
		document.write("<link rel='STYLESHEET' type='text/css' href='http://www.directv.com/css/DtvMarker_nn.css'>");
	}else document.write("<link rel='STYLESHEET' type='text/css' href='http://www.directv.com/css/DtvMarker_ie.css'>");	
}else{
	layers = "document.layers.";
	style = "";
	document.write("<link rel='STYLESHEET' type='text/css' href='http://www.directv.com/css/DtvMarker_nn.css'>");
}

var hours = new Array("Prime Time", "Now", "12:00 - 2:00", "2:00 - 4:00", "4:00 - 6:00", "6:00 - 8:00", "8:00 - 10:00", "10:00 - 12:00");
var ampm = new Array("PM", "AM");
var timezone = new Array("Eastern", "Central", "Mountain", "Pacific");

var div_array = new Array("timezoneid", "hoursid", "ampmid");

var cur = new Array();
var pmam = "pm" //keeps track of am/pm internally
var nochange_flag = false; //semaphore for prime time and now vs am

cur["hours"] = 0;
cur["ampm"] = 0;
cur["timezone"] = 0;

function change(up, id, which){
	var content;
	if (which == "hours"){
		content = hours;
	}else if (which == "ampm"){
		content = ampm;
		if (nochange_flag) return;
	}else if (which == "timezone"){
		content = timezone;
	}else{
		content = ampm;
	}
	
	if (which == "special_ampm") {
		cur["ampm"] = up;
		which = "ampm";
	} else {
		if (!up) {
			cur[which] == (content.length-1)? cur[which]=0 : cur[which]++;
		} else {
			cur[which] == 0? cur[which] = (content.length-1) : cur[which]--;
		}//else
	}
	
	output = content[cur[which]];

	save_display(which, content[cur[which]]);
	obj = eval(layers + id);
	if (document.getElementById) {
		obj.innerHTML = "&nbsp;" + output;			
	} else {
		obj.document.open();
		obj.document.write("<span class='whitetext'>&nbsp;" + output + "</span>");
		obj.document.close();
	}	
	select_go(which, content[cur[which]]);
}

function save_display(id, val) {
	if (id == "timezone"){
		document.select_box.timezone_display.value = val;
	}else if (id == "hours"){
		document.select_box.time_display.value = val;
	}else if (id == "ampm"){
		document.select_box.ampm_display.value = val;
	}else{
		alert("wrong id");
	}
}

function convert_time(time, apm){
	if (time == "") {
		time = document.select_box.start_time.value.match(/\d{2}/);
		var calc = parseInt(time, 10);
		if (apm == "am" && calc >= 12) calc = calc - 12;
		if (apm == "pm" && calc < 12) calc = calc + 12;		
		time = calc.toString() == "NaN"? "0" : calc.toString();
	} else {
		var calc = parseInt(time, 10);
		if (apm == "pm" && calc < 12) calc = calc + 12;
		if (apm == "am" && calc == 12) calc = 0;
		time = calc.toString();
	}	
	time.length < 2? time = "0" + time + "00" : time = time + "00";
	document.select_box.start_time.value = time;	
}

function select_go(which, val) {
	if (which == "hours"){
		if (val == "Prime Time"){
			nochange_flag = false;
			pmam = "pm";
			convert_time("8", pmam);
			change(0, "ampmid", "special_ampm");
			nochange_flag = true;
		}else if (val == "Now"){
			nochange_flag = false;
			curdate = new Date();
			temp = Math.floor(parseInt(curdate.getHours()/2)) * 2;
			pmam = (temp >= 12? "pm" : "am");
			pmam == "pm"? change(0, "ampmid", "special_ampm") : change(1, "ampmid", "special_ampm");
			convert_time(temp, pmam);
			nochange_flag = true;
		}else{
			convert_time(val.match(/^\d+/), pmam);			
			nochange_flag = false;
		}	
	}else if (which == "ampm"){
		pmam = val.toLowerCase();
		if (!nochange_flag) {
			convert_time("", pmam);
		}
	}else if (which == "timezone"){
		document.select_box.time_zone.value = (val.match(/^\w{1}/) + "st").toLowerCase();
	}else{
		document.select_box.time_zone.value = (timezone[0].match(/^\w{1}/) + "st").toLowerCase();		
		convert_time("8", "pm");
		nochange_flag = true;
	}
}

function get_left(obj) {
	if (!document.getElementById) return obj.pageX;
	xPos = obj.offsetLeft;
	if ((navigator.userAgent.indexOf("MSIE 5") > -1) ||
		(navigator.userAgent.indexOf("MSIE 6") > -1) ||
		(navigator.userAgent.indexOf("MSIE 4") > -1 && winbug) || (navigator.userAgent.indexOf("AppleWebKit") > -1)) {
		tempobj = obj.offsetParent;
		while (tempobj != null) {
			xPos += tempobj.offsetLeft;
			tempobj = tempobj.offsetParent;
		}//while		  
	} else {	
		tempobj = obj.parentElement;
		while (tempobj != null) {
			xPos += tempobj.offsetLeft;
			tempobj = tempobj.parentElement;
		}//while
	}
	if (navigator.userAgent.indexOf("MSIE 4") > -1 && winbug) {
		xPos = xPos + 3;
	}	
	return xPos;
}

function get_top(obj) {
	if (!document.getElementById) return obj.pageY;	
	yPos = obj.offsetTop;
	if ((navigator.userAgent.indexOf("MSIE 5") > -1) ||
		(navigator.userAgent.indexOf("MSIE 6") > -1) ||
		(navigator.userAgent.indexOf("MSIE 4") > -1 && winbug) || (navigator.userAgent.indexOf("AppleWebKit") > -1)) {
		tempobj = obj.offsetParent;
		while (tempobj != null) {
			yPos += tempobj.offsetTop;
			tempobj = tempobj.offsetParent;
		}//while
	} else {
		tempobj = obj.parentElement;
		while (tempobj != null) {
			yPos += tempobj.offsetTop;
			tempobj = tempobj.parentElement;
		}	
	}
	if (navigator.userAgent.indexOf("MSIE 4") > -1 && winbug) {
		yPos = yPos + 2;
	}
	if (winbug) {
		yPos = yPos - 3;
	}
	return yPos;
}

function place() {
	for (i=0; i < div_array.length; i++){
		selection =  eval(layers  + div_array[i] + style);
		var marker = eval(layers + "marker" + (i+1));
		selection.top = get_top(marker);
		selection.left = get_left(marker);

		selection.visibility = "visible";
	}//for
	set_arrow_display();
}

//for initialization of select boxes [so that you can scroll properly with arrows]
//------------------------------------------------------------------------
function set_arrow_display(){
	cur["timezone"] = set_current_array(timezone, document.select_box.timezone_display.value);
	cur["hours"] = set_current_array(hours, document.select_box.time_display.value);
	cur["ampm"] = set_current_array(ampm, document.select_box.ampm_display.value);
}

function set_current_array(main_array, val){
	found = false;
	if (val == "Prime Time" || val == "Now") nochange_flag = true;	
	for (i=0; i < main_array.length; i++){
		if (val == main_array[i]) {
			found = true;
			break;			
		}
	}//for	
	if (!found) return 0;
	else return i;
}
//------------------------------------------------------------------

function pg_submit(){	
	document.select_box.action="/DTVAPP/glb/ProgramGuide.jsp";
	document.select_box.submit();
}

//fixes resize problems for netscape
function MM_reloadPage(init) {
	if (init==true) with (navigator) {
		if ((appName=="Netscape") && (parseInt(appVersion)==4)) {	
			document.MM_pgW=innerWidth; 
			document.MM_pgH=innerHeight; 
			onresize=MM_reloadPage;
		}
	} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

MM_reloadPage(true);
