var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

}; BrowserDetect.init();

function expMenu(id) {
	var itm = null;
	if (document.getElementById) {
		itm = document.getElementById(id);
	} else if (document.all){
		itm = document.all[id];
	} else if (document.layers){
		itm = document.layers[id];
	}
	
	if (!itm) {
		// do nothing
	} else if (itm.style) {
		if (itm.style.display == "none") {
			itm.style.display = "";
		} else {
			itm.style.display = "none";
		}
	} else {
		itm.visibility = "show";
	}
}

function ShowHide(id1, id2) {
	if (id1 != "") {
		expMenu(id1);
	}
	if (id2 != "") {
		expMenu(id2);
	}
}

function popUp(URL, winwidth, winheight) {
	var day = new Date();
	var id = day.getTime();
	if (!winwidth) {
		winwidth = 300;
	}
	if (!winheight) {
		winheight = 300;
	}
	window.open(URL, "_blank", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + winwidth + ",height=" + winheight + ",left = 490,top = 362");

}

function downref() {
	window.opener.location.reload();
	top.close();
}

function HideIfHidden(hide, ifhidden) {
	if (document.getElementById(ifhidden).style.display == "none") {
		document.getElementById(hide).style.display = "none";
	}
}
function zeroFill(number, width) {
  width -= number.toString().length;
  if (width > 0) {
    return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
  }
  return number;
}
function PlayPause(mediaId, imageId, playURL, pauseURL) {
	var myMedia = document.getElementById(mediaId);
	var myImage = document.getElementById(imageId);
	if (myMedia.paused) {
		myMedia.play();
		myImage.src = pauseURL;
	} else {
		myMedia.pause();
		myImage.src = playURL;
	}
}
function getElementsByClassName(strClass, strTag, objContElm) {
	strTag = strTag || "*";
	objContElm = objContElm || document;
	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length && strTag == "*" && objContElm.all) objColl = objContElm.all;
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++) {
		var arrObjClass = objColl[i].className.split(' ');
		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++) {
			for (var m = 0, n = arrClass.length; m < n; m++) {
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
					arr.push(objColl[i]);
					break comparisonLoop;
				}
			}
		}
	}
	return arr;
}
function createPlayCursor(Id,evt) {
	var posx = 0;
	var e = evt || window.event;
	if (e.pageX) 	{
		posx = e.pageX;
	}
	else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	var cursorMargin = posx - (findX(document.getElementById(Id)) - 2);
	var cursorDiv = document.createElement('div');
	cursorDiv.id = "cursor";
	cursorDiv.style.margin = "0px 0px 0px " + (posx - (findX(document.getElementById(Id)) + 0)) + "px";
	cursorDiv.style.width = "3px";
	cursorDiv.style.height = "38px";
	cursorDiv.style.background = "red";
	document.getElementById(Id).appendChild(cursorDiv);
}
function movePlayCursor(Id,evt) {

	var posx = 0;
	var e = evt || window.event;
	if (e.pageX) 	{
		posx = e.pageX;
	}
	else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	if (BrowserDetect.browser == "Opera") {
		document.getElementById('cursor').style.margin = "0px 0px 0px " +  ((posx - (findX(document.getElementById(Id)) + 0)) - 85) + "px";
	} else {
		document.getElementById('cursor').style.margin = "0px 0px 0px " +  (posx - (findX(document.getElementById(Id)) + 0)) + "px";
	}
	
}
function mouseX(Id,evt) {
	var posx = 0;
	var e = evt || window.event;
	if (e.pageX) 	{
		posx = e.pageX;
	}
	else if (e.clientX) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
	}
	return posx;
}

function findX(obj) {
	var curleft = 0;
	if (typeof( obj.offsetParent ) != 'undefined' )  {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	else
	{
		curLeft = obj.x;
	}
	return curleft;
}

function seek(mediaId, seekId, seekerWidth, evt) {
	var e = evt || window.event;
	
	var media = document.getElementById(mediaId);
//	if (media.src.indexOf("mp3") != -1 && BrowserDetect.browser == "Chrome") {
//		var mediaDuration = (media.duration / 8);
//	} else {
		var mediaDuration = media.duration;
//	}
	var audio = document.getElementById(mediaId);
	audio.currentTime = Math.round((mouseX(seekId,e) - findX(document.getElementById(seekId))) * (mediaDuration / seekerWidth));	
}

function startCount(mediaId, seekId, seekerWidth, durationId, timeId, imageId) {
	var media = document.getElementById(mediaId);
	var seeker = document.getElementById(seekId);
	var t = window.setInterval(function() {
		var mediaDuration = media.duration;
		document.getElementById(durationId).innerHTML = Math.floor(mediaDuration / 60) + ":" + zeroFill(Math.round(mediaDuration % 60), 2);
		if (!media.ended) {
			seeker.style.width = Math.round(media.currentTime * (seekerWidth / mediaDuration)) + "px";
			document.getElementById(timeId).innerHTML = Math.floor(media.currentTime / 60) + ":" + zeroFill(Math.round(media.currentTime % 60), 2);
		} else {
			seeker.style.width = "0px";
			document.getElementById(timeId).innerHTML = "0:00";
			media.pause();
			document.getElementById(imageId).src = "/images/layout/play.png";
		}
	},42);		
}
	
function loadRSS(elem, page, field) {
	//Load Function
	var load = function(){
		document.getElementById(elem).childNodes[0].replaceData(0, document.getElementById("fg_news_title").childNodes[0].length, "Loading...");
	}
	
	//Response Function
	var response = function(response_text, e) {
		document.getElementById(elem).childNodes[0].replaceData(0, document.getElementById("fg_news_title").childNodes[0].length, response_text);
	}
	
	//Request Function
	var request = function(xmlhttp) {
			xmlhttp.open("GET", page, true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			var params = "length=300";
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.send(params);
	}
	
	//Make AJAX Request
	ajaxRequest(request, response, load);
}

function ajaxRequest(onRequestFunction,onResponseFunction,onWaitFunction){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	//WAIT FUNCTION
	onWaitFunction(); //To show loading, etc
	xmlhttp.onreadystatechange=function(){
		//If the state and status are okay
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			var response_text = xmlhttp.responseText;
			onResponseFunction(response_text); //RESPONSE FUNCTION
		}
	}
	//REQUEST FUNCTION
	onRequestFunction(xmlhttp);
}
