// JavaScript Document

var lang =  noHighlight=false;
var p=location.pathname, _GET=parseString(location.search.substring(1)), maxLen=58;
	
$(document).ready(function() {
/*
	$.pngfix(MidPath + 'images/pixel.gif'); // this is optional
	 $('img[@src$=.png], #logo, #logotype, #langBar, #leftMenu, #newsHrd, #topNav, .menu, #footer, #loginSercel').pngfix();
*/
	 //Creates page titles
	 var title = document.title.replace("Sercel: ", "");
	 //It's a HACK:
	 	 
	 $("#dataBounds").prepend('<h1>' + title + '</h1>');
});

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=800,height=600,left = 112,top = 84');");
}

//news ticker
var newsInt;
function getNews() {
	var s = stories, out = '';
	/* THIS WORKS
	for (var i=0, l=stories.length; i<l; i++) 
		out += '<img src="./images/news_arrow.png" align="left" /><p class="newsStory" id="story'+i+'" style="margin-bottom: .85em;"><a href="'+s[i][1]+'" onmouseover="stopNews();" onmouseout="startNews();"'
			+' onclick="this.href+=\'?title=\'+escape(this.title);" title="'+s[i][3]+'">'+''+s[i][0]+'</a></p>';
	*/
	for (var i=0, l=stories.length; i<l; i++) 
		out +=	'<p class="newsStory" id="story'+i+'" style="margin-bottom: .85em;">'
			+ 	'<table border="0" cellpadding="0" cellspacing="0" class="newsTickerTable"><tr><td valign="top"><img src="images/news_arrow.png" align="left" /></td>'
			+ 	'<td valign="top"><a href="'+s[i][1]+'" onmouseover="stopNews();" onmouseout="startNews();"'
			+ 	'onclick="this.href+=\'?title=\'+escape(this.title);" title="'+s[i][3]+'">'+''+s[i][0]+'</a>'
			+ 	'</td></tr></table></p>';
	return out;
}
function scrollNews() {
	var el;
	if ((el = document.getElementById('newsStories')) && (stories.length>1)) {
		var top = parseInt(el.style.top);
		el.style.top = (top-1)+'px';
		var theoffset = document.getElementById('story1').offsetTop;

		//alert('newsStories OffsetTop->');
		//alert(el.offsetTop);
		//alert('theoffset->');
		//alert(theoffset);
		
		//alert(el.offsetTop + " = " +  parseInt(document.getElementById('story1').offsetTop)-1);
		if (el.offsetTop == (theoffset * -1)-1) {
			//alert(stories.length);
			stories.push(stories.shift());
			el.innerHTML = getNews();
			el.style.top = '-1px';
		}
		if (el.offsetTop == (theoffset * 
							 -1)) {
			//alert(el.offsetTop);
			//alert(document.getElementById('story1').offsetTop);
			//stopNews();
			//setTimeout("startNews()", 1);
			startNews();
			//setTimeout("scrollNews()", 100000);
		}
	} else stopNews();
}
function startNews() {
	stopNews();
	newsInt = setInterval("scrollNews()", 50);
}
function stopNews() { clearInterval(newsInt); }

function translate(text) { return (breadText[text]) ? breadText[text] : text; }

window.onload = function() {
	if (document.referrer && !noHighlight) {
		var referer = document.referrer;
		referer = referer.substring(referer.indexOf('?')+1);
		//var _GET = parseString(referer);
		if (_GET['q']) highlightSearchTerms(_GET['q']);
	}
	if (stories.length) setTimeout("startNews()", 0);
}

//Search field
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
	if ((!highlightStartTag) || (!highlightEndTag)) {
		highlightStartTag = '<span class="searchHighlight">';
		highlightEndTag = '</span>';
	}
	var newText = "";
	var i = -1;
	var lcSearchTerm = searchTerm.toLowerCase();
	var lcBodyText = bodyText.toLowerCase();
	
	while (bodyText.length > 0) {
		i = lcBodyText.indexOf(lcSearchTerm, i+1);
		if (i < 0) {
			newText += bodyText;
			bodyText = '';
		} else {
			if (bodyText.lastIndexOf('>', i) >= bodyText.lastIndexOf('<', i)) {
				if (lcBodyText.lastIndexOf('/script>', i) >= lcBodyText.lastIndexOf('<script', i)) {
					newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
					bodyText = bodyText.substr(i + searchTerm.length);
					lcBodyText = bodyText.toLowerCase();
					i = -1;
				}
			}
		}
	}
	return newText;
}

function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
	if (window.searchHighlighted) return;
	if (!document.body || typeof(document.body.innerHTML) == 'undefined') return false;
	searchText = unescape(searchText).split('+').join(' ');
	if (treatAsPhrase) searchArray = [searchText];
  		else searchArray = searchText.split(' ');
	var bodyText = document.body.innerHTML;
	for (var i = 0; i < searchArray.length; i++) bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
	document.body.innerHTML = bodyText;
	searchHighlighted = 1;
	return true;
}

function searchHighlight() {
	if (location.pathname.indexOf('/Search/') != -1) {
		var _GET = parseString(location.href.substring(location.href.indexOf('?')+1));
		if (_GET['q']) highlightSearchTerms(_GET['q']);
		return;
	}
	if (document.referrer && !noHighlight) {
		var referer = document.referrer;
		referer = referer.substring(referer.indexOf('?')+1);
		var _GET = parseString(referer);
		if (_GET['q']) highlightSearchTerms(_GET['q']);
	}
}

function parseString(string) {
    var list=string.split('&'), vars=[], info;
    for (var x=0, l=list.length; x<l; x++) {
        info = list[x].split('=');
        vars[info[0]] = (info[1]) ? unescape(info[1]) : '';
    }
    return vars;
}
