/**
 * @author Hans Elgelid
 */
var JayaUtil = {}; // global Object cointainer
JayaUtil.states = false;
JayaUtil.percentToPx = function (percent, fullLength) {
	if(!JayaUtil.isDefined(percent)){
		percent = 0;
	}
	if(!JayaUtil.isDefined(fullLength)) {
		fullLength = JayaUtil.getDocWidth();
	}
	var res = fullLength*percent/100 ;
	//Logger.log("percentToPx " + res + " %=" + percent + " fullLength=" + fullLength);
	return res;
}
JayaUtil.percentToPxY = function (percent, fullHeight) {
	if(!JayaUtil.isDefined(percent)){
		percent = 0;
	}
	if(!JayaUtil.isDefined(fullHeight)) {
		fullHeight = JayaUtil.getBrowserWindowHeight();
	}
	var res = fullHeight*percent/100 ;
	//Logger.log("percentToPx " + res + " %=" + percent + " fullHeight=" + fullHeight);
	return res;
}
JayaUtil.applyCss = function (css_url) {
	Logger.log("applyCss " + css_url + " " + screen.width + "X" + screen.height, "debug");
	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = css_url;
	cssNode.media = 'screen';
	headID.appendChild(cssNode);
	//alert(" applyCss " + css_url  );
	return;
}
JayaUtil.setOpacity = function(elem, opacityAsInt) {
	var opacityAsDecimal = opacityAsInt;
	if (opacityAsInt > 100)
   		opacityAsInt = opacityAsDecimal = 100; 
   	else if (opacityAsInt < 0)
    	opacityAsInt = opacityAsDecimal = 0; 
    opacityAsDecimal /= 100;
    if (opacityAsInt < 1)
        opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0
	elem.style.opacity = (opacityAsDecimal);
	elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
}
JayaUtil.getParentDiv = function(div) {
	if (div) {
		do {
			div = div.parentNode;
			//alert("getParentDiv div=" + div + " " +div.id + "tagName=" +div.tagName);
		} while (div && div.tagName != 'DIV');
	}
	return div;
}
JayaUtil.getWidth = function (divName) {
	return document.getElementById(divName).clientWidth;
}
JayaUtil.getHeight = function (divName) {
	return document.getElementById(divName).clientHeight;
}
JayaUtil.getDocWidth = function () {
	//return document.getElementById(divName).clientWidth;
	return document.body.scrollWidth;
}
JayaUtil.f_scrollLeft = function() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
JayaUtil.getDocHeight = function() {
	return JayaUtil.f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
JayaUtil.f_filterResults = function(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
JayaUtil.getBrowserWindowWidth = function() { 
	var browserWinWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		browserWinWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		browserWinWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		browserWinWidth = document.body.clientWidth;
	}
	//alert( 'Browser Window Width = ' + browserWinWidth);
	return browserWinWidth -18;
}
JayaUtil.getBrowserWindowHeight = function(){
	var browserWinHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		browserWinHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		browserWinHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		browserWinHeight = document.body.clientHeight;
	}
	//alert(' Browser Window Height = '+browserWinHeight);
	return browserWinHeight -6;
}
JayaUtil.getElementsByClassName = function(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;
}
JayaUtil.getElementsByTagName = function(div, tag) {
	
	var r = [];
	for ( var c = div.firstChild; c; c = c.nextSibling ) {
		if ( c.nodeType == 1 && ( c.nodeName == tag || tag == "*" ) )
			r.push( c );
	}
	//alert("getElementsByTagName ");
	return r;
}

JayaUtil.createCSS = function(selector, declaration){
	// test for IE
    var ua = navigator.userAgent.toLowerCase();
    var isIE = (/msie/.test(ua)) && !(/opera/.test(ua)) && (/win/.test(ua));

    // create the style node for all browsers
    var style_node = document.createElement("style");
    style_node.setAttribute("type", "text/css");
    style_node.setAttribute("media", "screen");

    if (!isIE) style_node.appendChild(document.createTextNode(selector + " {" + declaration + "}"));

    document.getElementsByTagName("head")[0].appendChild(style_node);

    // use alternative methods for IE
    if (isIE && document.styleSheets && document.styleSheets.length > 0) {
            var last_style_node = document.styleSheets[document.styleSheets.length - 1];
            if (typeof(last_style_node.addRule) == "object") last_style_node.addRule(selector, declaration);
    }
	return;
}

JayaUtil.isDefined = function(param){
	if(param=="" || param==null || param ==undefined) {
		return false;
	} else {
		return true;
	}
}


