var maxVideos = 9;
var isIE = (navigator.userAgent.indexOf('MSIE') != -1);

var channels = new Array();
function loadChannels() {
	var channel_thumbs = document.getElementsByName('channel_thumbnail');
	store_channel_data(channel_thumbs[0].getAttribute('url'), 0);
}

function openGlobeLink(country) {
	var lang = "";
	if (country == "us") lang = "english";
	else if (country == "south_america") lang = "spanish";
	else if (country == "italy") lang = "italian";
	else if (country == "france") lang = "french";
	else if (country == "germany") lang = "german";
	else if (country == "bahrain") lang = "arabic";
	location.href = "?page="+lang+"&globe=true";
}

function renderGlobe() {
	var globe = document.getElementById('globe');
	if (globe) {
		globe.innerHTML = '<embed src="swf/globe.swf" width="100%" height="100%" wmode="transparent"></embed>';
		//<div id="close_globe_btn" onClick="closeGlobe()" title="Close">x</div>';
		/*globe.style.opacity = 1;
		globe.style.filter = "alpha(opacity=100)";*/
		setTimeout("highlightGlobe()", 3000);
		
		// show "close" button after two seconds
		// setTimeout("document.getElementById('close_globe_btn').style.display = 'block'", 2000);
	}
}

function closeGlobe() {
	document.getElementById('globe').innerHTML = '';
}

var dimInterval, dimTimeout;
function highlightGlobe(over) {
	var globe = document.getElementById('globe');
	/*if (dimInterval) {
		clearInterval(dimInterval);
		dimInterval = null
	}*/
	if (over) {
		/*if (dimTimeout) {
			clearTimeout(dimTimeout);
			dimTimeout = null;
		} if (!dimInterval) dimInterval = setInterval('animateDim(1)', 25);*/
		globe.className = 'lit';
	} else {
		globe.className = 'dim';
		//if (!dimTimeout) dimTimeout = setTimeout("dimInterval = setInterval('animateDim()', 25)", 500);
	}
}

function animateDim(highlight) {
	var globe = document.getElementById('globe');
	if (highlight) {
		if (isIE) {
			var ie_opacity = globe.style.filter.replace("alpha(opacity=", "");
			ie_opacity = ie_opacity.substr(0, ie_opacity.length-1);
			if (ie_opacity < 100) {
				globe.style.filter = "alpha(opacity=" +(parseFloat(ie_opacity)+10)+ ")";
			} else clearTimeout(dimInterval);
		} else {
			if (globe.style.opacity < 1) {
				globe.style.opacity = parseFloat(globe.style.opacity) + 0.1;
			} else clearTimeout(dimInterval);
		}
	} else {
		if (isIE) {
			var ie_opacity = globe.style.filter.replace("alpha(opacity=", "");
			ie_opacity = ie_opacity.substr(0, ie_opacity.length-1);
			if (ie_opacity > 20) {
				globe.style.filter = "alpha(opacity=" +(parseFloat(ie_opacity)-10)+ ")";
			} else clearTimeout(dimInterval);
		} else {
			if (globe.style.opacity > .2) {
				globe.style.opacity = parseFloat(globe.style.opacity) - 0.1;
			} else clearTimeout(dimInterval);
		}
	}
}

// ajax functions
function store_channel_data(url, index) {
	var cache_param = location.href.indexOf("cache=fal") != -1? "cache=false&" : "";
	var extractor_url = "../code/util/channel_data_extractor.php?"+cache_param+"feed_url=";
	get_file_contents(extractor_url+encodeURIComponent(url), index);
}

var xmlhttp;
function get_file_contents(url, index) {
	xmlhttp = null;
	if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Safari, Opera
		xmlhttp = new XMLHttpRequest();
	} else { // IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} xmlhttp.onreadystatechange = function() {storeChannelDataReady(index)}
	
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function storeChannelDataReady(index) {
	if (xmlhttp.readyState == 4) { // 4 = "loaded"
		if (xmlhttp.status == 200) { // 200 = "OK"
			var js_data = xmlhttp.responseText;
			if (js_data) {
				var channel_info = eval(js_data);
				var channel_thumbs = document.getElementsByName('channel_thumbnail');
				channel_thumbs[index].src = channel_info[0][2];
				channels[index] = channel_info; // store in global channels var
			}
			
			// process next channel
			var channel_thumbs = document.getElementsByName('channel_thumbnail');
			if (!index) { setTimeout("loadChannel(0);",500); } // load the first channel and video
			if (index < channel_thumbs.length-1) {
				store_channel_data(channel_thumbs[index+1].getAttribute('url'), index+1);
			} else {
				// render globe after loading the last channel
				setTimeout("renderGlobe()", 1000);
			}
		} else {
			xmlhttp.statusText;
		}
	}
}

// channel selector handling
function channelMouseOver(title, desc, div) {
	div.className = div.className.replace("translucent", "opaque");
	updateChannelRollover(title, desc);
}

function channelMouseOut(div) {
	div.className = div.className.replace("opaque", "translucent");
	updateChannelRollout();
}

var prev_channel_div, prev_video_div;
function loadChannel(index, div) {
	if (updatePlayer(channel_list[index][3], 1)) {
		loadVideo(index, 0); // start the first video
		var channel = channels[index];
		if (!channel) return false;
		var video_selector = document.getElementById('videos_inner');
		var num_videos = channel.length > maxVideos? maxVideos: channel.length;
		video_selector.innerHTML = "";
		for (var i=0; i<num_videos; i++) {
			var video = channel[i];
			var title = video[0];
			var desc = video[1];
			var thumbnail = video[2];
			var video_url = video[3];
			var date = video[4];
			
			video_selector.innerHTML += '<div class="video_thumb_holder translucent default_video_border" id="video_holder'+(i+1)+'" onMouseover="videoMouseOver(\''+title+'\',\''+desc+'\',\''+date+'\',\''+thumbnail+'\',this)" onMouseout="videoMouseOut(this)" onClick="loadVideo('+index+','+i+',this)"><div class="video_thumb"><img src="'+thumbnail+'" name="video_thumbnail" class="video_thumb_img"></div></div>';
		}
		
		// update current channel info
		updateCurrentChannelData(channel_list[index][0], channel_list[index][1], index);
		
		// if div is not provided, set it to the first
		if (!div) {
			updateChannelRollout();
			div = document.getElementById('first_channel_holder');
		}
		
		div.className = div.className.replace("default_border", "highlighted_border");
		if (prev_channel_div) prev_channel_div.className = prev_channel_div.className.replace("highlighted_border", "default_border");
		prev_channel_div = div;
		
		// highlight first video frame
		highlightFirstVideoFrame();
	}
}

// video selector handling
function videoMouseOver(title, desc, date, thumbnail, div) {
	div.className = div.className.replace("translucent", "opaque");
	updateVideoDetails(date, title, desc, thumbnail);
}

function videoMouseOut(div) {
	div.className = div.className.replace("opaque", "translucent");
}

function highlightFirstVideoFrame() {
	var div = document.getElementById('video_holder1');
	div.className = div.className.replace("default_video_border", "highlighted_video_border");
	if (prev_video_div) prev_video_div.className = prev_video_div.className.replace("highlighted_video_border", "default_video_border");
	prev_video_div = div;
	
	// trigger mouse events on div
	div.onmouseover(); div.onmouseout();
}

function loadVideo(chanIndex, vidIndex, div) {
	var chan = channels[chanIndex];
	if (!chan) return false; // check that channel data exists
	
	var video_url = chan[vidIndex][3];
	if (playVideo(video_url)) {
		if (div) {
			div.className = div.className.replace("default_video_border", "highlighted_video_border");
			if (prev_video_div) prev_video_div.className = prev_video_div.className.replace("highlighted_video_border", "default_video_border");
			prev_video_div = div;
		}
		
		var channel = channels[chanIndex];
		var num_videos = channel.length > maxVideos? maxVideos: channel.length;
		
		// update now playing / up next info
		updateCurrentVideoData(channel[vidIndex][0], channel[vidIndex][1], channel[vidIndex][2]);
		if (vidIndex+1 < num_videos) {
			updateUpNextVideoData(channel[vidIndex+1][0], channel[vidIndex+1][1], channel[vidIndex+1][2], vidIndex+1);
			hideUpNextDisplay = false;
		} else {
			hideUpNextDisplay = true;
			updateUpNextVideoData("","","","");
		} showNowPlaying();
	}
}

// create channel object
window.onload = function() {
	//ChannelObj.init();
	if (isFlashVersionEnough) {
		startPlayer(xml_url);
	} else {
		document.getElementById("player").innerHTML="<span class=\"flashDetectionErrorMsg\">"+flashDetectionErrorMsg.replace("\\n", "<br/>")+"</span>";
	} loadChannels();
}

var ChannelObj;// = new ChannelObj();

function ChannelObj() {
	var me = this;
	
	var channel_title;
	var channel_desc;
	var video_date;
	var video_title;
	var video_desc;
		
	me.init = function() {
		var doc = document;
		
		channel_title = doc.getElementById("channel_title");
		channel_desc = doc.getElementById("channel_desc");
		video_date = doc.getElementById("video_date");
		video_title = doc.getElementById("video_title");
		video_desc = doc.getElementById("video_desc");
	}

	me.setChannelDetails = function(title, desc) {
		setElmInnerHTML(channel_title, title);
		setElmInnerHTML(channel_desc, desc);
	}

	me.setVideoDetails = function(date, title, desc) {
		setElmInnerHTML(video_date, date);
		setElmInnerHTML(video_title, title);
		setElmInnerHTML(video_desc, desc);
	}

	function setElmInnerHTML(elm, str) {
		if(elm)
			elm.innerHTML = str;
	}
};

function chooseLanguage(selObj) {
	var option = selObj.options[selObj.selectedIndex];
	if(option) {
		var value = option.value;
		var country_code = option.getAttribute("code");
		
		var country_settings = eval("SETTINGS."+country_code);
		var to_search = /selected_language=([a-zA-Z0-9\-\_]*)/g;
		var to_replace = "selected_language="+country_code;
		
		var menu_world = document.getElementById("world");
		var menu_world_a = menu_world.getElementsByTagName("a").item(0);
		menu_world_a.innerHTML = country_settings.MENU.WORLD;
		menu_world_a.href = menu_world_a.href.toString().replace(to_search, to_replace);
		
		var menu_local = document.getElementById("local");
		var menu_local_a = menu_local.getElementsByTagName("a").item(0);
		menu_local_a.innerHTML = country_settings.MENU.LOCAL;
		menu_local_a.href = menu_local_a.href.toString().replace(to_search, to_replace);
		
		var menu_extreme = document.getElementById("extreme");
		var menu_extreme_a = menu_extreme.getElementsByTagName("a").item(0);
		menu_extreme_a.innerHTML = country_settings.MENU.EXTREME;
		menu_extreme_a.href = menu_extreme_a.href.toString().replace(to_search, to_replace);
		
		var powered_by = document.getElementById("powered_by");
		powered_by.innerHTML = country_settings.DICT.POWERED_BY;
		
		var channels_label = document.getElementById("channels_label");
		if(channels_label) {
			channels_label.innerHTML = country_settings.DICT.CHANNELS+":";
		}
		
		var choose_different_country = document.getElementById("choose_different_country");
		if(choose_different_country) {
			var choose_different_country_a = choose_different_country.getElementsByTagName("a").item(0);
			choose_different_country_a.innerHTML = country_settings.DICT.CHOOSE_LOCATION;
			choose_different_country_a.href = choose_different_country_a.href.toString().replace(to_search, to_replace);
		}
		
		var videos_label = document.getElementById("videos_label");
		if (videos_label)
		{
			videos_label.innerHTML = country_settings.DICT.VIDEOS+":";
		}
	}
}
