// JavaScript Document
var col1Toggled = false;
var recentToggled = false;
var mainColum = "";
var col1 = "";
var col2 = "";
var col3 = "";
var cuscol1 = "";
var cuscol2 = "";
var cuscol3 = "";
var cuscol4 = "";
var customChannels = false;
var pleaseToggle = false;
//Set to true any time a channel needs to toggle upon loading. Such as searching or under "openChannel"
var initialLoad = true;
var callBack = "";
var searchCleared = false;

function loadDiv(url, containerid, targetobj){
//	alert("Loading container ID " + containerid);
//	document.getElementById(containerid).innerHTML = "Wasabi";
	var page_request = false
	if (window.XMLHttpRequest) { // if Mozilla, Safari etc
		page_request = new XMLHttpRequest();
	}else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}else {
		return false;
	}
	if (targetobj != null) {
		switch (targetobj) {
			case "col1":
			col1 = url;
			step = true;
			break;
			case "col2":
			col2 = url;
			step = true;
			break;
			case "col3":
			col3 = url;
			step = true;
			break;
			case "main":
			step = true;		
			break;
		}
	}else {
		step = false;
	}
	if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
	document.getElementById(containerid).innerHTML=defaultcontentarray[containerid];
	return;
	}
	document.getElementById(containerid).innerHTML=loadstatustext;
	page_request.onreadystatechange=function(){
		loadDivPage(page_request, containerid);
	}
	if (bustcachevar) { //if bust caching of external page
	var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	page_request.open('GET', url+bustcacheparameter, true);
	page_request.send(null);
	}
}

function loadDivPage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
document.getElementById(containerid).innerHTML=page_request.responseText;
rerouteDivs(containerid);
	if (containerid == "largepromo") {
		//A new video has been reqeusted, close the window if open and refresh "Recently Played"
		if (col1Toggled) {
			toggleMain();
		}
		loadDiv('/utilities/roo_library.aspx?which=recent','quicklinks_repeat');
	}else if (containerid == "stationhighlight" && initialLoad) {
	//	var myHeight = getMyStyle(document.getElementById("stationhighlight"),'height');
	getVideo();
//		columnToggle(false);
//col1Toggled = true;
//outToMin();
//toggleMain();
	columnToggle(false);
	initialLoad = false;
	initTooltips();
	}
	if (pleaseToggle && containerid == "stationhighlight") {
		//A search or openChannel function has been called
			toggleMain("out");
			pleaseToggle = false;	
	}

}else if (page_request.readyState == 4 && page_request.status != 200) {
	document.getElementById(containerid).innerHTML = "An Error has occurred. This is a temporary problem. Please try again soon.";
}
}

function columnToggle (whichWay) {
	//Hides or shows "vidcolumn" as needed, depending on Column state.
	var vidColumnCount = 0;
	var getColumns = document.getElementById('stationhighlight').getElementsByTagName("div");
	for (var i=0;i<getColumns.length;i++) {
		if (getColumns[i].getAttribute("class") == "vidcolumn" ||getColumns[i].getAttribute("className") == "vidcolumn") {
			if (vidColumnCount > 0) {
				if (whichWay) {
					getColumns[i].style.visibility = "visible";
				}else {
					getColumns[i].style.visibility = "hidden";
				}
			}
			vidColumnCount++;
		}
	}
}


function openChannel (who) {
	loadDiv (who,'stationhighlight');
	pleaseToggle = true;
}

function toggleRecent () {
	if (who == null) {
		who = "quicklinks_repeat";
	}
	var currentWidth = parseInt(getMyStyle(document.getElementById (who), "height"));
	var toggler = document.getElementById("min_quicklinks");
	if (!recentToggled) {
		recentToggled = true;
		toggler.innerHTML = "Close";
		animateWidth(who,'600');
	}else {
			recentToggled = false;
			toggler.innerHTML = "More";
			animateWidth("stationhighlight",'200');
	}
}


function outToMin () {
	columnToggle(false);
	var theBox = document.getElementById("vidlist").getElementsByTagName("div");
	var theTop = document.getElementById("col1_inner_top");
	var vidlist = document.getElementById("vidlist");
	var bottom = document.getElementById("mainChannels_bottom");
//	var linky = document.getElementById("col1_inner_top");
	for (var i=0;i<theBox.length;i++) {
			if (theBox[i].getAttribute("class") == "vidcolumn" || theBox[i].getAttribute("className") == "vidcolumn") {
				theBox[i].style.width = "220px";
			}
	}
//		theTop.className = "";
		vidlist.className = "";
		replaceCenter();
		bottom.className = "toggleBottom";
		theTop.className = "col1innertop";
		animateWidth("stationhighlight",'245');

}

function clearMe (who) {
	if (!searchCleared) {
		who.value = '';
		searchCleared = true;
	}
}

function outToMax () {
	columnToggle(true);	
	var theBox = document.getElementById("vidlist").getElementsByTagName("div");
	var theTop = document.getElementById("col1_inner_top");
	var vidlist = document.getElementById("vidlist");
	var bottom = document.getElementById("mainChannels_bottom");
//	var linky = document.getElementById("col1_inner_top");
	for (var i=0;i<theBox.length;i++) {
			if (theBox[i].getAttribute("class") == "vidcolumn" || theBox[i].getAttribute("className") == "vidcolumn") {
				theBox[i].style.width = "170px";
				
			}
		}
		unplaceCenter();
		theTop.className = "col1_expand_top";
		vidlist.className = "toggledOut";
		bottom.className = "toggledBottom";
//		linky.className = "col1innertoggled";
		animateWidth("stationhighlight",'980');
}



function pullIn (afterarg) {
	animateWidth ("stationhighlight",'1');
	if (afterarg != null) {
		callBack = afterarg;
	}

}

function toggleMain (where) {
	//Main Begin call for left column expanding.
	if (!col1Toggled || where == "out") {
		col1Toggled = true;
		pullIn('outToMax');
	}else if (col1Toggled || where == "in") {
		col1Toggled = false;
		pullIn('outToMin');
	}
}


function toggle (who,where) {
	if (who == null) {
		who = "stationhighlight";
	}
	var toggler = document.getElementById("coltoggle");
	var vidlist = document.getElementById("vidlist");
var theBox = document.getElementById("vidlist").getElementsByTagName("div");
var channels = document.getElementById("moreChannels");
//var container = document.getElementById(who);

	if (!col1Toggled || where == "out") {
		for (var i=0;i<theBox.length;i++) {
			if (theBox[i].getAttribute("class") == "vidcolumn" || theBox[i].getAttribute("className") == "vidcolumn") {
				theBox[i].style.width = "170px";
			}
		}
		channels.innerHTML = "Close";
		col1Toggled = true;
		vidlist.className = "toggledOut";
		unplaceCenter();
		callback = "alertme";
		animateWidth(who,'980');
	}else {
		for (var i=0;i<theBox.length;i++) {
			if (theBox[i].getAttribute("class") == "vidcolumn" || theBox[i].getAttribute("className") == "vidcolumn") {
				theBox[i].style.width = "220px";
			}
		}
		channels.innerHTML = "More";
			replaceCenter();
			col1Toggled = false;
			vidlist.className = "toggledIn";
			vidlist.style.overflow = "hidden";
			animateWidth(who,'242');
	}
}


function showAll () {
var theBox = document.getElementById("vidlist").getElementsByTagName("div");
for (var i=0;i<theBox.length;i++) {
	if (theBox[i].getAttribute("class") == "smallThumb" || theBox[i].getAttribute("className") == "smallThumb") {
		theBox[i].style.visibility = "visible";
		theBox[i].style.position = "relative";
	}
	if (theBox[i].getAttribute("class") == "vidtitle" ||theBox[i].getAttribute("className") == "vidtitle") {
		theBox[i].style.visibility = "visible";
		theBox[i].style.position = "relative";
	}
	if (theBox[i].getAttribute("class") == "viddesc" || theBox[i].getAttribute("className") == "viddesc") {
		theBox[i].style.visibility = "visible";
		theBox[i].style.position = "relative";
	}
}
var theBoxa = document.getElementById("vidlist").getElementsByTagName("div");
}

function getCustomChannels () {
	customChannels = true;
	var channels = "";
	var roo = document.getElementById("rooContainer");				
//	alert("Got roo container " + roo.innerHTML);
//	roo.innerHTML = "";
	for (var i=1;i<=3;i++) {
		channels = getServerCookie("roocustom", "column" + i);
		if (channels) {
	//		roo.innerHTML += "<div id=\"roocol" + i + "\" class=\"roocol\" > One Moment Please... </div>";
			loadDiv(unescape(channels),"roocol" + i,"cuscol" + i);
		}else {
			
		}
	}

}

function getRelatedChannels () {
	customChannels = false;
	var roo = document.getElementById("rooContainer");
//	roo.innerHTML = "";
	for (var i=1;i<=3;i++) {
//		roo.innerHTML += "<div id=\"roocol" + i + " class=\"roocol\">One Moment Please</div>";
//		roo.innerHMTL +=  "<div id=\"roocol" + i + "\" class=\"roocol\" > One Moment Please... </div>";
//var temp = this["col"+i];
//alert("Temp is " + this["col" + i]);
					loadDiv(this["col" + i],"roocol" + i);
//		alert("Col is " + this["col" + i]);
	}


}



function showThumbs () {
var theBox = document.getElementById("vidlist").getElementsByTagName("div");
for (var i=0;i<theBox.length;i++) {
	if (theBox[i].getAttribute("class") == "smallThumb" || theBox[i].getAttribute("className") == "smallThumb") {
		theBox[i].style.visibility = "visible";
		var tempa = theBox[i].getElementsByTagName("a");
		tempa.style.visibility = "visible";
		var temp = theBox[i].getElementsByTagName("img");
		temp.style.visibility = "visible";
		theBox[i].style.position = "relative";
	}
	if (theBox[i].getAttribute("class") == "vidtitle" || theBox[i].getAttribute("className") == "vidtitle") {
		theBox[i].style.visibility = "visible";
		theBox[i].style.position = "relative";
	}
	if (theBox[i].getAttribute("class") == "viddesc" || theBox[i].getAttribute("className") == "viddesc") {

		theBox[i].style.visibility = "hidden";
		theBox[i].style.position = "absolute";
	}
}
}

function titlesOnly () {
var theBox = document.getElementById("vidlist").getElementsByTagName("div");
for (var i=0;i<theBox.length;i++) {
	if (theBox[i].getAttribute("class") == "smallThumb" || theBox[i].getAttribute("className") == "smallThumb") {
				theBox[i].style.position = "absolute";
		theBox[i].style.visibility = "hidden";
	}
	if (theBox[i].getAttribute("class") == "vidtitle" || theBox[i].getAttribute("className") == "vidtitle") {
		theBox[i].style.visibility = "visible";
				theBox[i].style.position = "relative";
	}
	if (theBox[i].getAttribute("class") == "viddesc" || theBox[i].getAttribute("className") == "viddesc") {
		theBox[i].style.visibility = "hidden";
				theBox[i].style.position = "absolute";
	}
}
}

function reloadCols (howmany) {
	for (var i=1;i<=3;i++) {
	var urlString = "";	
	if (customChannels) {
		if (this["cuscol" + i].length > 1) {
		cusCols = this["cuscol" + i];
		}
	}else{
		urlString= this["col" + i];
	}
	var tempString = urlString.replace(/maxclips=\d+/,"maxclips=" + howmany);
	    loadDiv(tempString,"roocol" + i); 
	    
	}
	
}


function searchRoo () {
//		toggle("stationhighlight","out");
/*var highlight = document.getElementById("stationhighlight");
var vidlist = document.getElementById("vidlist");
var theBox = document.getElementById("vidlist").getElementsByTagName("div");
var channels = document.getElementById("moreChannels");
		for (var i=0;i<theBox.length;i++) {
			if (theBox[i].getAttribute("class") == "vidcolumn" || theBox[i].getAttribute("className") == "vidcolumn") {
				theBox[i].style.width = "220px";
			}
		}
			replaceCenter();
			col1Toggled = false;
			vidlist.style.opacity = ".1";
			vidlist.style.backgroundColor = "";
//			toggler.innerHTML = "Show";
			vidlist.style.overflow = "hidden";
			highlight.style.width = "242px";
//			animateWidth(who,'242');
*/
//		pullIn ();
		var temp = document.getElementById("stationhighlight");
		temp.style.width = "245px";
		temp.style.overflow = "hidden";
		pleaseToggle = true;
		loadDiv (document.rooForm.action + "?keywords=" + document.rooForm.elements["keywords"].value,"stationhighlight");
}

