/**
*	javascript.js
*	
*   This is the default javascript functions of CoreTreks Site Base for CorePublish frontends
*	
*	Please DO NOT CHANGE this file. 
*	To add your own functions, put them in another file
*	This makes sure you can update this file (javascript.js)
*	from a newer distribution without worrying about keeping your changes.
*
*	@author Arve Skjørestad
*	
*/

var timervar = null; 

function printArticle( artUrl ){
    behind = window.open(artUrl ,'printwin','height=650,width=810,status=yes,toolbar=yes,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes');
}

function getContentAreaHeight() {
    if (document.all) {
        var ret  =  document.all["menuheighholder"].height;
    } else if (document.getElementById) {
		var ret =  document.getElementById("menuheighholder").height;
    }

    return  ret;

}       

function getAbsolutePos(el){
	for (var lx=0,ly=0;el!=null;
		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {x:lx,y:ly}
}


/**
 * Sets the active stylesheet for the page.
 * 
 * 
 */
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
       if(a.getAttribute("rel").indexOf("style") != -1
          && a.getAttribute("title")) {
           a.disabled = true;
           if(a.getAttribute("title") == title) {
               a.disabled = false;
           }
       }
   }
}

function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1
           && a.getAttribute("title")
           && !a.disabled) { 
            return a.getAttribute("title");
        }
    }
    return null;
}

var debugwin;
var debugwinopened = false;
function debug(str) {
    //return;
    // alert(str); return ;
	if (debugwinopened == false || !debugwin) {
        debugwin = window.open('about:blank','debug_window','width=1000,height=700, toolbars=no,menu=no,scrollbars=yes');
        debugwin.document.write("<h3>CorePublish Javascript Debug</h3>");
        debugwinopened = true;
    }
	debugwin.document.write( str );
	debugwin.focus();
    // make the debug win go away after a while..
    setTimeout("self.focus()",2000);
	// alert(str);
}

/**
*   Function displays the metods and properties of an javascript object
*   @param Object inarray - The array to serialize.
*/
function jsdebug(inarray, level) {
    var result = '';
    var sep = '';

    if (level > 5) {
        return;
    }

    if(inarray!=null) {
        for(var key in inarray) {
            
            if (inarray[key] == null) {
                continue;
            }

            result = "<ul>" +  typeof inarray[key];
            result += ' [' + key + '] => ';

            try {
                tmp = inarray[key].toString();
                result += tmp.substring(0,90) + "";
            } catch (e) {
                result += tmp + "";
            } 

            debug(result);
                
            if (typeof inarray[key] == "object" || typeof inarray[key] == "function") {
                jsdebug(inarray[key],level+1);
            }

            debug("</ul>");
            /*
            if(typeof inarray[key] == 'object') {
                result += '[object]';
            } else {
                result += inarray[key].toString();
            }     */



            /*if (result.length > 1000) {
                alert(result + " (continues >>) ");
                result = "";
            } */

        } 
    } else {
        result = "[null]";
    }
    // alert(result);
}

/**
 * Function that move the element based on another element
 */
function moveElement(objID,posObjID,extraTop,extraLeft) {
    elementUtil = new ElementUtil();
    elementUtil.setElementVisible(objID,false);
    elementUtil.moveElementBasedOnPosElement(objID,posObjID,extraTop,extraLeft);
    elementUtil.setElementVisible(objID,true);
}

/**
 * Class ElementUtil
 * 
 * @author Martin Roessland, 2006-11-06
 */
function ElementUtil() {
    //Adding object function
    this.setElementVisible = setElementVisible;
    this.moveElementBasedOnPosElement = moveElementBasedOnPosElement;
    this.changeElementStyleValue = changeElementStyleValue;

    /**
     * Function that sets an element visible
     * 
     * @param boolean bool //true equals display
     */
    function setElementVisible(objectID,bool) {
        try {
            document.getElementById(objectID).style.visibility = (bool?'visible':'hidden');
        } catch (e) {}
    }
    /**
     * Function that moves an element based on another element
     * 
     * @param objectID //object to move
     * @param posObjectID //object to use for positioning
     * @param posTopExtra //Additional top
     * @param posLeftExtra //Additional left
     */
    function moveElementBasedOnPosElement(objectID,posObjectID,posTopExtra,posLeftExtra) {
        try {
            pos = getAbsolutePos(document.getElementById(posObjectID));
            document.getElementById(objectID).style.top = (pos.y + posTopExtra)+'px';
            document.getElementById(objectID).style.left = (pos.x + posLeftExtra)+'px';
        } catch (e) {}
        //Function that returns absolut position of the given element
        function getAbsolutePos(el){
        	for (var lx=0,ly=0;el!=null;
        		lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
        	return {x:lx,y:ly}
        }
    }
    /**
     * Function that changes a css value of an element
     * 
     * @param objectID //id of object to slide
     * @param styleAttribute //style attribute to change (pixel change)
     * @param delay //millisecond between every value change
     * @param valueFrom //negative and positive integer
     * @param valueTo //negative and positive integer
     * @param valueMiddle //middle value in case we are zooming out in back again. If you do not want to use this, set it to boolean false
     * @param valueChangeGap //negative and positive integer of how much the value are going to change at time
     * @param valueStaticStart //static value that comes before the generated value (ex: alpha(opacity=, <blanc>)
     * @param valueStaticEnd //static value that comes after the generated value (ex: px, %, <blanc>)
     */
    function changeElementStyleValue(objectID,styleAttribute,startDelay,loopDelay,valueFrom,valueTo,valueMiddle,valueChangeGap,valueStaticStart,valueStaticEnd) {
        var _ce_objID = objectID;
        var _ce_sa = styleAttribute;
        var _ce_sd = startDelay;
        var _ce_ld = loopDelay;
        var _ce_vf = valueFrom;
        var _ce_vt = valueTo;
        var _ce_vt_tmp = valueTo;
        var _ce_vm = valueMiddle;
        var _ce_vcg = valueChangeGap;
        var _ce_vss = valueStaticStart;
        var _ce_vse = valueStaticEnd;
        /**
         * Recursive function that do the value change
         * 
         * @return Boolean true on successfull change or false if an exception appears
         */
        function doChangeElementStyleValue() {
            try {
                /**
                 * In case middle value is not false we first have
                 * to walk to the middle value and then futher to
                 * the "to"-value.
                 */
                if(_ce_vm !== false) {
                    _ce_vt = _ce_vm;
                }
                /**
                 * In case we are going to handle the opacity
                 * attribute we have to do special handling to support both
                 * firefox and ie
                 */
                if(_ce_sa == 'opacity') {
                    //microsoft browsers
                    if(navigator.appName.indexOf("Microsoft")!=-1) {
                        document.getElementById(_ce_objID).filters.alpha.opacity=((Math.round((_ce_vf*10)))/10);
                    //the other browsers
                    } else {                 
                        document.getElementById(_ce_objID).style.opacity=((Math.round(((_ce_vf/100)*10)))/10);
                        document.getElementById(_ce_objID).style.MozOpacity=((Math.round(((_ce_vf/100)*10)))/10);
                    }
                //else handle it normaly
                } else {
                    document.getElementById(_ce_objID).style[_ce_sa] = _ce_vss+((Math.round((_ce_vf*10)))/10)+_ce_vse;
                }
                //If "from" value is less than "to" value
                if(_ce_vf < _ce_vt) {
                    _ce_vf = _ce_vf + _ce_vcg > _ce_vt ? _ce_vt : _ce_vf + _ce_vcg;                
                //If "from" value is greater than "to" value
                } else if(_ce_vf > _ce_vt) {
                    _ce_vf = _ce_vf - _ce_vcg < _ce_vt ? _ce_vt : _ce_vf - _ce_vcg;
                //If middle value is set and reached we set the final to value
                } else if(_ce_vm !== false) {
                    _ce_vm = false;
                    _ce_vt = _ce_vt_tmp;
                //Else we jump out of this endless recursion
                } else {
                    return true;
                }
                //In case we have come so far, we run this function over again
                setTimeout(doChangeElementStyleValue,_ce_ld);
            } catch (e) {
                alert(e);
                return false;
            }
        }
        //Run the recursive function
        setTimeout(doChangeElementStyleValue,_ce_sd);

    }
}

function toggleServiceNumbers() {
    $('serviceNumbers').toggle();
}
