var xmlHttp;

//Will attempt to crate XMLHttpRequest object if the browser supports it.
function verifyBrowser()
{  
	try
	{ 
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();    
	}
	catch (e)
	{    
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			catch (e)
			{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}     
		}    
	} 
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

function addCloseEvent(func)
{
	var oldonclose = window.onunload;
	if (typeof window.onunload != 'function')
	{
		window.onunload = func;
	}
	else
	{
		window.onunload = function()
		{
			oldonload();
			func();
		}
	}
}

function activeMenu(id)
{
	var activeMemu = document.getElementById(id);
	activeMemu.className = "activeMenuItem";
}
	
/*	Detects the browser */
function detectBrowser()
{
	var browser = navigator.appName;
	return browser;
}

function opacity(id, opacStart, opacEnd, millisec) {
	
	if(detectBrowser() == "Microsoft Internet Explorer") return;
	
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

//Adjusts the column container's height => displays the columns
//separators right, depeniding on the columns heights.
function fixColumnsHeights()
{
	adjustStories();
		
	//The highest column will determine the height of the columns container
	var highest = 0;
	
	var container = document.getElementById("colContainer");
	var left = document.getElementById("leftCol");
	var middle = document.getElementById("middleCol");
	var right = document.getElementById("rightCol");
	
	var nums = new Array()
	
	nums[0] = left.scrollHeight;
	nums[1] = middle.scrollHeight;
	nums[2] = right.scrollHeight;
	
	for(var i=0;i<3;i++)
	//for(var i=0;i<2;i++)
	{
		if(nums[i] > highest) highest = nums[i];
	}
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		container.style.height = highest+"px";
		left.style.height = highest+"px";
		middle.style.height = highest+"px";
		right.style.height = highest+"px";
	}
	else
	{
		container.style.setProperty("height",highest+"px","");
		left.style.setProperty("height",highest+"px","");
		middle.style.setProperty("height",highest+"px","");
		right.style.setProperty("height",highest+"px","");
		
	}
	
}

function adjustStories()
{
	//This works in IE, so no need to do anything.
	//if(detectBrowser() == "Microsoft Internet Explorer") return;
	
	//SPECIFIES ADDITIONAL SPACE FROM PADDINGS AND MARGINS
	var ADDITIONAL = 10;
	
	var elements = document.getElementsByTagName("div");
	for(var i=0; i<elements.length; i++)
	{
		var element = elements[i];

		//Only looking at the divs that are holding news headlines.
		if(element.className != "headlineHolder " && element.className != "headlineHolder specialStory" && element.className != "headlineHolder") continue;
	
		//Get the headline image of the story, there can be only one such.
		//We don't need to do anything if there isn't an image, so continue if
		//that's the case.
		var imageArr = element.getElementsByTagName("img");
		
		if(imageArr.length == 0) continue;
		
		//Since there can only be one image, it will be the first image.
		var image = imageArr[0];
		
		//No need to resize, if there's enough text...
		if(element.scrollHeight > (image.height + ADDITIONAL )) continue;
										   
		if(detectBrowser() == "Microsoft Internet Explorer")
		{
			element.style.height = image.height + ADDITIONAL + "px";
		}
		else
		{
			element.style.setProperty("height",image.height + ADDITIONAL + "px" ,"");
		}
	}
}

function fixSingleColumnHeight()
{
		var container = document.getElementById("colContainer");
		var left = document.getElementById("leftCol");
		var right = document.getElementById("pageCol");
		
		var highest = 0;
		var nums = new Array()
	
		nums[0] = left.scrollHeight;
		nums[1] = right.scrollHeight;
	
		for(var i=0;i<2;i++)
		//for(var i=0;i<2;i++)
		{
			if(nums[i] > highest) highest = nums[i];
		}
	
		if(detectBrowser() == "Microsoft Internet Explorer")
		{
			container.style.height = highest+"px";
		}
		else
		{
			container.style.setProperty("height",highest+"px","");
		}
}

//Opens a new windows with specified parameters.
function openNewWindow(openWindow,height,width,scrollbars, resizable)
{
  return window.open(openWindow, '_blank', "height="+height+" width="+width+" scrollbars="+scrollbars +" resizable="+ resizable);
}

var WINDOWREF = null;
var currentPlayingRadioDiv = null;
var currentRadioPlayer = null;
var currentPlayerOriginalInnerHTML = null;
var currentType = null;
var radioPlayerMenu_sys = "<div style='width:100%; height:15px; margin-top:10px; clear:both; font-size:smaller; font-weight:bold; color:green;'>Playing... ";
radioPlayerMenu_sys += "[<a href='javascript:void(0);' style='color:red; font-weight:bold; font-size:10px;' onClick='resetCurrentRadioPlayer(WINDOWREF);'>click to switch off</a>]</div>";

var interval;

function radioOn(elementID, ref)
{
	
	if(ref != "undefined") 
	{
		WINDOWREF = ref;
		interval = setInterval(checkForClosedWindow, 1000);
	}
	
	//Reset the current player if any.
	if(currentPlayingRadioDiv != null)
	{
		//alert("Please swtich off the currently playing sound and try again.");
		//return;
		resetCurrentRadioPlayer();
	}
	
	var element = document.getElementById(elementID);
	var currentInnerHTML = element.innerHTML;
	currentPlayerOriginalInnerHTML = currentInnerHTML;
	//Put the playing title now.
	element.innerHTML = currentInnerHTML + radioPlayerMenu_sys;
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		//The neew playing will now have a red border.
		if(element != null) element.style.border = "1px solid #3366CC";
	}
	else
	{
		if(element != null) element.style.setProperty("border","1px solid #3366CC","");
	}
	
	currentPlayingRadioDiv = element;
	setPlayingRadio(elementID);
}

function checkForClosedWindow()
{
	if(!WINDOWREF.closed) return;
	clearInterval(interval);
	
	if(currentPlayingRadioDiv != null)
	{
		//alert("Please swtich off the currently playing sound and try again.");
		//return;
		resetCurrentRadioPlayer();
	}

}
function radioOn_old(src,elementID,playerID,type,doPlay,ref)
{
	WINDOWREF = ref;

	//Reset the current player if any.
	if(currentPlayingRadioDiv != null)
	{
		//alert("Please swtich off the currently playing sound and try again.");
		//return;
		resetCurrentRadioPlayer();
	}
	
	currentType = type;
	
	var element = document.getElementById(elementID);
	var currentInnerHTML = element.innerHTML;
	currentPlayerOriginalInnerHTML = currentInnerHTML;
	//Put the playing title now.
	element.innerHTML = currentInnerHTML + radioPlayerMenu_sys;
	
	var radioElement = document.getElementById(playerID);
	currentRadioPlayer = radioElement;

	if(type == "stream" && doPlay == true)
	{
		radioElement.innerHTML = "<embed src='"+ src + "' width='300' height='300' style='position:absolute; top:0px; left:0px;' autostart='true' repeat='true'></embed>";
	}
	else if(doPlay == true)
	{
		radioElement.src = src;
	}
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		//The neew playing will now have a red border.
		if(element != null) element.style.border = "1px solid #3366CC";
	}
	else
	{
		if(element != null) element.style.setProperty("border","1px solid #3366CC","");
	}
	
	currentPlayingRadioDiv = element;
	currentRadioPlayer = radioElement;
	currentType = type;
	
	setPlayingRadio(elementID);
}

function resetCurrentRadioPlayer(windowref)
{
	if(windowref != null)
	{
		windowref.close();
	}
	
	//else
	//{
	//	windowref = window.open('radios.php?id=$id','mywin','left=100,top=100,width=800,height=600,toolbar=0,resizable=0,scrollbars=1');
	//	windowref.close();
	//}
	
	currentPlayingRadioDiv.innerHTML = currentPlayerOriginalInnerHTML;
	
	/* if(currentType == "stream")
	{
		currentRadioPlayer.innerHTML = "";
	}
	else
	{
		currentRadioPlayer.src = "";
	} */
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		//Reset the red border
		if(currentPlayingRadioDiv != null) currentPlayingRadioDiv.style.border = "0px";	
	}
	else
	{
		if(currentPlayingRadioDiv != null) currentPlayingRadioDiv.style.setProperty("border","0px","");
	}
	
	currentPlayingRadioDiv = null;
	currentRadioPlayer = null;
	currentPlayerOriginalInnerHTML = null;
	currentType = null;
	
	unsetPlayingRadio();
}
//Changes the form action to the appropriate file when the user clicks on preview a story.
function previewStory(root,theForm)
{
	theForm.action = root + "/page.php?preview=1";
	theForm.target = "_blank";
	theForm.submit();
}

//Changes the form action to the appropriate file when the user clicks on preview a story.
function previewHomePage(root,theForm)
{
	var section = document.getElementById("section_").value;
	
	
	theForm.action = root + "/index.php?section="+section+"&preview=1";
	theForm.target = "_blank";
	
	theForm.submit();
}

var STORIESLOADED = false;

function setPlayingRadio(elementID)
{
	verifyBrowser();
	var scriptPath = "setPlayingRadio.php?elementID="+elementID;
	xmlHttp.open("GET",scriptPath,true);
    xmlHttp.send(null);
}

function unsetPlayingRadio()
{
	verifyBrowser();
	var scriptPath = "unsetPlayingRadio.php";
	xmlHttp.open("GET",scriptPath,true);
    xmlHttp.send(null);
}

function showPosition(doShow)
{
	var positionElement = document.getElementById("position");
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		if(doShow)
		{
			positionElement.style.display = "block";
		}
		else
		{
			positionElement.style.display = "none";
		}
	}
	else
	{
		if(doShow)
		{
			positionElement.style.setProperty("display","block","");
		}
		else
		{
			positionElement.style.setProperty("display","none","");
		}
	}
}
	
	
function showTitleImageField(doShow)
{
	var titleImageElement = document.getElementById("titleImage");
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		if(doShow)
		{
			titleImageElement.style.display = "block";
		}
		else
		{
			titleImageElement.style.display = "none";
		}
	}
	else
	{
		if(doShow)
		{
			titleImageElement.style.setProperty("display","block","");
		}
		else
		{
			titleImageElement.style.setProperty("display","none","");
		}
	}
}
	
//Switches the page to the desired page selected in the page input field.s
function switchPage(goPage,args)
{
	window.location = "?page=" + goPage + "&" + args;
}

//Will hide the element corresponding to the passed id
function togglePreviewInfo(show)
{
	var element = document.getElementById("previewInfo");
	var linkElement = document.getElementById("theLink");
	
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		if(show) element.style.display = "block";
		else element.style.display = "none";
	}
	else
	{
		if(show) element.style.setProperty("display","block","");
		else element.style.setProperty("display","none","");
	}
	
	if(show)
	{
		linkElement.innerHTML = "Shrink this area.";
		linkElement.href="javascript:togglePreviewInfo(false);"
	}
	else
	{
		linkElement.innerHTML = "Expand this area.";
		linkElement.href="javascript:togglePreviewInfo(true);"
	}
}

function adjustIframe()
{
	var contentE = document.getElementById("contentHolder");
	
	window.resizeTo(contentE.scrollWidth+23,contentE.scrollHeight);

	window.blur();

	var holder = document.getElementById("subPageContent");
	var iframe = document.getElementById("iframe");
	
	doHeight(holder,holder.scrollHeight);
	doHeight(iframe,iframe.scrollHeight);
}

function adjustIframeLink()
{
	var linkElement = document.getElementById("iframe");
	linkElement.height = linkElement.document.body.scrollHeight;
}

function doHeight(element,heightValue)
{
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		element.style.height = heightValue+"px";
	}
	else
	{
		element.style.setProperty("height",heightValue+"px","");
	}
}
	
function showHeadlineHiddenInfo(elementID,doShow)
{
	var element = document.getElementById(elementID);
	
	//If it's a story with no pop up info.
	if(element == null) return;
	
	if(detectBrowser() == "Microsoft Internet Explorer")
	{
		if(doShow) element.style.display = "block";
		else element.style.display = "none";
	}
	else
	{
		if(doShow) element.style.setProperty("display","block","");
		else element.style.setProperty("display","none","");
	}
}

function off()
{
	showHeadlineHiddenInfo("s1",false);
	showHeadlineHiddenInfo("s2",false);
}

var ELEMENT_1;
var ELEMENT_2;
var SCROLLSPEED = 1;
var doScroll_1 = true;
var doScroll_2 = true;

function scrollText()
{
	ELEMENT_1 = document.getElementById("scroll_1");
	ELEMENT_2 = document.getElementById("scroll_2");
	setInterval("tick_1()",30);
	//doScroll_1 = false;
	//setInterval("toggleScrolls()",10000);
	setInterval("tick_2()",30);
}

/* function toggleScrolls()
{
	if(doScroll_1)
	{
		doScroll_1 = false;
		doScroll_2 = true;
	}
	else
	{
		doScroll_1 = true;
		doScroll_2 = false;
	}
} */

function tick_1()
{
	if(doScroll_1) ELEMENT_1.scrollLeft += SCROLLSPEED;
	if(ELEMENT_1.scrollLeft >= ELEMENT_1.scrollWidth - ELEMENT_1.offsetWidth) ELEMENT_1.scrollLeft = 0;
}

function tick_2()
{
	if(doScroll_2) ELEMENT_2.scrollLeft += SCROLLSPEED;
	if(ELEMENT_2.scrollLeft  >= ELEMENT_2.scrollWidth - ELEMENT_2.offsetWidth) ELEMENT_2.scrollLeft = 0;
}


function Feed(src_a,elementID_a)
{
	this.src = src_a;
	this.elementID = elementID_a;
}

var pendingXml = new Array();

function createFeed(src,elementID)
{
	pendingXml.push(new Feed(src,elementID));
}

function loadFeeds()
{
	if(pendingXml.length == 0) return;
	verifyBrowser();
	
	var feed = pendingXml.pop();
	var xmlsrc = feed.src;
	var elementID = feed.elementID;
	
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
        {
			var response = xmlHttp.responseText;
			document.getElementById(elementID).innerHTML = response;
        	loadFeeds();
		}
    }
	
	var scriptPath = "loadfeed.php?src="+xmlsrc;
	xmlHttp.open("GET",scriptPath,true);
	xmlHttp.send(null);
}





function reloadScrollingNews(language)
{
	verifyBrowser();
	
	if(!language)
	{
		var languageMenu = document.getElementById("scrollLanguage");
		//This will be the language to switch to
		var language = languageMenu.value;
	}
	
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
        {
				loadPRScrollStories();
				//loadBusinesswireScrollStories();
		}
    }
	
	var scriptPath = "changeScrollLanguage.php?l="+language;
	xmlHttp.open("GET",scriptPath,true);
	xmlHttp.send(null);
}

function loadPRScrollStories()
{
	verifyBrowser();
	
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
        {
			document.getElementById("scroll_1").innerHTML = xmlHttp.responseText;
			
			loadBusinesswireScrollStories();
		}
    }
	
	var scriptPath = "prScrollStories.php";
	xmlHttp.open("GET",scriptPath,true);
	xmlHttp.send(null);
}

function loadBusinesswireScrollStories()
{
	verifyBrowser();
	
	xmlHttp.onreadystatechange=function()
    {
    	if(xmlHttp.readyState==4)
        {
			document.getElementById("scroll_2").innerHTML =  xmlHttp.responseText;
		}
    }
	
	var scriptPath = "businessScrollStories.php";
	xmlHttp.open("GET",scriptPath,true);
	xmlHttp.send(null);
}
		
		
function saveScrollTop()
{
	var ScrollTop = document.body.scrollTop;
 	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	
	verifyBrowser();
	var scriptPath = "setScrollTop.php?value=" + ScrollTop;
	xmlHttp.open("GET",scriptPath,true);
	xmlHttp.send(null);
}

function setScrollTop(scrollTopValue)
{
	window.scrollTo(0,scrollTopValue);
}

function doSearch(event)
{
	//13 is the keycode for enter key
	if(event.keyCode == 13)
	{
		document.getElementById("searchButton").click();
	}
}
	