//
// The code below, if otherwise stated, is copyright Asad Niazi and BollyEnt!
// May not be copied or re-used without permission. 
//

// tab related
var active_tab = "indiafm";
var active_tab_obj = "";

// tip related
var tipHandle = "";
var curLaunched = "";
var lastX = 0;
var lastY = 0;
var stillHidden = 1;

// data related
var tipData = new Array();
var newsData = new Array();

var printCounter = 0;

function printTabsData(toTab)
{
	var ele = document.getElementById('tab-' + toTab);
	if (typeof newsData[toTab] != 'object') {
		return;
	}
	for (var i = 0; i < newsData[toTab].length; i++) {
		printCounter++;
		ele.innerHTML += "<div id='tip_data_" + printCounter +"' class='news-tipData' style='display: none;'>" + tipData[toTab][i] + "</div>";
		ele.innerHTML += "<div id='tabLink'><a href='#' onmouseover='launchTip("+ printCounter +");' onmouseout='closeTip();' onclick=\"launchBrowser('" + newsData[toTab][i][1] + "');\">" + newsData[toTab][i][0] + "</a></div>";
	}
}

function launchTip(dataLink)
{
	if (curLaunched == dataLink) {
		return;
	}
	
	curLaunched = dataLink;
	
	tipHandle = document.getElementById('tip_data_' + dataLink);
	tipHandle.style.position = 'absolute';
	
	document.onmousemove = mouseCoords;
		
	stillHidden = 1;
	

}

function closeTip()
{
	tipHandle.style.display = 'none';
	tipHandle = "";
	curLaunched = "";
	document.onmousemove = '';
}

function mouseCoords(e) {
	var ns6 = document.getElementById && !document.all
	var X; var Y;
	var offsetTop = 10;
	var offsetLeft = 20;

	if (!e) {
		e = window.event;
	}
	

	if (e.pageX && e.pageY) {
		Y = e.pageY;
		X = e.pageX;
	} else {

		Y = e.clientY + document.documentElement.scrollTop;
		X = e.clientX + document.documentElement.scrollLeft;
	}

	
	if (Y == lastY && X == lastX) {
		return;
	}

	tipHandle.style.top = (Y + offsetTop) + 'px';
	tipHandle.style.left = (X + offsetLeft) + 'px';
		
	lastX = X;
	lastY = Y;

	if (stillHidden == 1) {
		tipHandle.style.display = '';
		stillHidden = 0;
	}
}


function activateTab(tab_name, tab_object)
{
	var ele = document.getElementById('tab-'+tab_name);
	if (typeof ele == 'undefined') {
		return;
	}

	var top_tab = document.getElementById('toptab-'+tab_name);
	top_tab.style.backgroundImage = "url('images/news-tab-2.gif')";
	
	var top_tab_prev = document.getElementById('toptab-'+active_tab);
	top_tab_prev.style.backgroundImage = "url('images/news-tab-1.gif')";

	
	toggleDisplay('tab-' + active_tab, 1);
	toggleDisplay('tab-' + tab_name);

	active_tab = tab_name;

	setCookie('active_news_tab', tab_name, 30);
}

function initTabs(tab_name)
{
	if (readCookie('active_news_tab') != null) {
		var tmp_tab_name = readCookie('active_news_tab');
		if (typeof document.getElementById(tmp_tab_name) != 'undefined') {
			tab_name = tmp_tab_name;
		}
	}

	active_tab = tab_name;
	document.getElementById('toptab-'+tab_name).style.backgroundImage = "url('images/news-tab-2.gif')";
}

function initContainer()
{
	toggleDisplay('tab-' + active_tab);
}

function toggleDisplay(element, forceHide)
{
	var ele = document.getElementById(element);
	if (ele.style.display == 'none' && forceHide != 1) {
		ele.style.display = '';
	} else {
		ele.style.display = 'none';
	}
}

function launchBrowser(link)
{
	var fader = document.createElement('div');
	var pageSize = getPageSize();
	fader.setAttribute('id', 'fader-div');
	fader.style.height = pageSize[1] + 'px';
	
	window.onscroll = moveBrowser;
	
	var overlay = document.createElement('div');
	overlay.setAttribute('id', 'overlay-div');
	
	var browser_div = document.createElement('div');
	browser_div.setAttribute('id', 'browser-div');
	
	var frame_div = document.createElement('div');
	frame_div.setAttribute('id', 'frame-div');
	frame_div.innerHTML = "<iframe src='"+ link +"' frameborder='0' height='"+ (pageSize[3] - 120) +"' width='100%'></iframe>";

	
	var div_close = document.createElement('div');
	div_close.setAttribute('id', 'div-close');
	div_close.innerHTML = "<a href='#' onclick='closeBrowser();'>close it!</a>";
		
	var bodyEle = document.getElementsByTagName('body');
	
	browser_div.appendChild(div_close);
	browser_div.appendChild(frame_div);
	overlay.appendChild(browser_div);
	bodyEle[0].appendChild(fader);
	bodyEle[0].appendChild(overlay);

	
	document.getElementById('fader-div').style.display = '';
	document.getElementById('overlay-div').style.display = '';

}

function moveBrowser()
{
	var scrollData = getPageScroll();
	var ele = document.getElementById('overlay-div');
	ele.style.top = (scrollData + 20) + 'px';
}

function closeBrowser()
{
	var bodyEle = document.getElementsByTagName('body');
	bodyEle[0].removeChild(document.getElementById('fader-div'));
	bodyEle[0].removeChild(document.getElementById('overlay-div'));
	window.onscroll = "";
}


// below code from quirksmode.org
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	return yScroll;
}

// below code from quirksmode.org
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function setCookie(name, value, expire)
{
	var exp_date = new Date();
	exp_date.setDate(exp_date.getDate() + expire);
	document.cookie = name + "=" + escape(value) + "; expires = " + exp_date.toGMTString();
}
