/**
 * Standard functions used all over the website
 * @author Benjamin A. Dezile, shawn 
 */

/**
 * Get horizontal position of a given HTML element
 * @author Benjamin A. Dezile
 * @param oElement	HTML element
 * @return Return the element position on the page
 */
function getXPos(oElement)
{
var iReturnValue = 0;
while (oElement != null) 
	{
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
return iReturnValue;
}	


/**
* Get vertical position of a given HTML element
* @author Benjamin A. Dezile
* @param oElement	HTML element
* @return Return the element position on the page
*/
function getYPos(oElement)
{
var iReturnValue = 0;
while (oElement != null) 
	{
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
return iReturnValue;
}


/**
 * Browser Detection
 * @author shawn
 */
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();

/**
 * Create a new XmlHttp Object
 * @author Benjamin A. Dezile 
 */
function newXmlHttp()
{
try
  { 
  /* Firefox, Opera 8.0+, Safari */
  return new XMLHttpRequest(); 
  }  
catch (e)
  {
  /* Internet Explorer */
  try { 
	  return new ActiveXObject("Msxml2.XMLHTTP"); 
	  }
  catch (e)
    {
    try { 
    	return new ActiveXObject("Microsoft.XMLHTTP"); 
    	}
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return null;
      }
    }
  }
return null;
}


/**
 * Resize the current frame/window to fit the size of the content (height only)<br>
 * This has to be called from inside the frame
 * @author Benjamin A. Dezile
 */
function resizeFrame()
{
var doc = parent.window.document;
if (doc != null)
	{
	var offset = 150;
	var h = window.document.body.scrollHeight;
	//if (BrowserDetect.browser == 'Firefox') h = window.document.body.offsetHeight;
	if (doc.getElementById('centralTab') != null) doc.getElementById('centralTab').setAttribute('height',h+offset);
	} 
return;
}


/**
  * Resize the current frame/window to fit the size of the content (height only)<br>
  * This has to be called from inside the frame
  * @param offset
  * @author shawn
  */
function resizeFrame2(offset)
{
var doc = parent.window.document;
if (doc != null)
 	{
 	var h = window.document.body.scrollHeight;
 	//if (BrowserDetect.browser == 'Firefox') h = window.document.body.offsetHeight;
 	if (doc.getElementById('centralTab') != null) doc.getElementById('centralTab').setAttribute('height',h+offset);
 	} 
return;
}
  
  
/**
 * Resize the current frame/window to fit the size of the content (height only)
 * @author Benjamin A. Dezile 
 */
function resizeFrameFromOutside()
{
if (window.frames['centralTab'] != null)
	{
	var offset = 150;
	var h = window.frames['centralTab'].document.body.scrollHeight;
	if (BrowserDetect.browser == 'Firefox') h = window.frames['centralTab'].document.body.offsetHeight;
	document.getElementById('centralTab').setAttribute('height',h+offset);
	}
return;
}


/**
 * Resize top and section bars to fit the screen width
 * @author Benjamin A. Dezile
 * @param l		Number of section displayed in the section bar
 */
function resizeTop(l)
{
var w = 120;
var sw = window.screen.availWidth;
if ($('#topBar').html() != null) $('#topBar').width(((l*w)<sw)?sw:(l*120));
$('#sectionBar').width(((l*120)<sw)?sw:(l*120));
$('#dragndrop').width(((l*120)<sw)?(sw-100):(l*120-100));
return;
} 

 
/**
 * Escape frames
 * @author Vincent Lum
 */
function escapeFrame()
{
if (top != self) top.location.replace(self.location.href);
return;
}


/**
 * Open the given koobi in new window<br><br>
 * @author Benjamin A. Dezile
 * @param koobiID	ID of the koobi to open
 */
function openKoobi(koobiID)
{
window.open(URLRoot+"koobi?url="+$.URLEncode($('#koobi_'+koobiID+'_url').val()));
return;
}
