/*
+----------------------------------------------------------------------+
| some useful global functions
| should be included on any page that relies on other javascript libraries
+----------------------------------------------------------------------+
| author: tom biegeleisen <biege@gmail.com>
| last modified: feb 22 2008
+----------------------------------------------------------------------+
*/

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
function getWidth(obj) {
	if ( obj.offsetWidth ) return obj.offsetWidth ;
	if ( obj.style ) {
		if ( obj.style.pixelWidth ) return obj.style.pixelWidth ;
		if ( obj.style.width ) return parseInt(obj.style.width) ;
	}
	return 0;
}
function getHeight(obj) {
	if ( obj.offsetHeight ) return obj.offsetHeight ;
	if ( obj.style ) {
		if ( obj.style.pixelHeight ) return obj.style.pixelHeight ;
		if ( obj.style.height ) return parseInt(obj.style.height) ;
	}
	return 0;
}
function pageWidth() {
        return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;
}
function pageHeight() {
        // moz compliant browsers
	if ( window.innerHeight != null ) return window.innerHeight ;
	// IE 6 sucks, and 5-7 quirksmode seem to use this property as well
        if ( document.documentElement.clientHeight != null ) return document.documentElement.clientHeight ;
        // a failsafe for IE 5-7
        if ( document.body.clientHeight != null ) return document.body.clientHeight ;
        return null ;
}
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}
function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}
function posRight() {
	return posLeft()+pageWidth();
}
function posBottom() {
	return posTop()+pageHeight();
}



/*
+----------------------------------------------------------------------+
| creates XHTML standards-compliant <object> and <embed> code
+----------------------------------------------------------------------+
*/

function AC_FL_RunStandardsContent() {
	var html = AC_FL_CreateStandardsHtml(arguments[0],arguments[1]);
	document.write(html);
}
function AC_FL_CreateStandardsHtml() {
	var html = "";
	var objHtml = "";
	var embedHtml = "";
	for ( i in arguments[0] ) {
		objHtml += " " + i + "=\"" + arguments[0][i] + "\"" ;
	}
	// OK: CSS2 compliant browsers seem to give <object> inline spacing; add style to compensate
	if ( objHtml != "" ) {
		html += "<object" + objHtml + " style=\"display:block;margin:0 auto;\">\n" ;
	}
	for ( i in arguments[1] ) {
		embedHtml += "<param name=\"" + i + "\" value=\"" + arguments[1][i] + "\" \/>\n" ;
	}
	if ( embedHtml != "" ) {
		html += embedHtml ;
	}
	if ( objHtml != "" ) {
		html += "<\/object>\n" ;
	}
	return html;
}

/*
+----------------------------------------------------------------------+
| clears empty input fields when focused, replaces if blur and empty
+----------------------------------------------------------------------+
*/

function clearInput(obj,text) {
	if ( obj.value == undefined ) return;
	var len = parseInt(obj.value.length);
	if ( obj.value == text ) {
		obj.value = "";
	} else if ( obj.value == "" ) {
		obj.value = text;
	} else if ( len == 0 ) {
		obj.value = text;
	}
}

/*
 * open source stuff below
 */

function IFrame(parentElement) {
	// create the iframe which will be returned
	var iframe = document.createElement("iframe");

	// if no parent element is specified then use body as the parent element
	if (parentElement == null) parentElement = document.body;

	// this is necessary in order to initialize the document inside the iframe
	parentElement.appendChild(iframe);

	// initiate the iframe's document to null
	iframe.doc = null;

	// Depending on browser platform get the iframe's document, this is only
	// available if the iframe has already been appended to an element which
	// has been added to the document
	if (iframe.contentDocument) {
		// ff, op
		iframe.doc = iframe.contentDocument;
	} else if ( iframe.contentWindow ) {
		// ie
		iframe.doc = iframe.contentWindow.document;
	} else if ( iframe.document ) {
		// others?
		iframe.doc = iframe.document;
	}
	if ( iframe.doc == null ) {
		throw "Document not found, append the parent element to the DOM before creating the IFrame";
	}
	// create the script inside the iframe's document which will call the
	iframe.doc.open();
	iframe.doc.close();
	// return the iframe, now with an extra property iframe.doc containing the
	// iframe's document
	return iframe;
}

function getStyle(el,cssRule) {
	var strValue = "";
	if (document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(el,"").getPropertyValue(cssRule);
	} else if (el.currentStyle){
		cssRule = cssRule.replace(/\-(\w)/g, function (strMatch, p1) {
			return p1.toUpperCase();
		});
		strValue = el.currentStyle[cssRule];
	}
	return strValue;
}
