/**
*	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 showWindow(artUrl) {
    behind = window.open(artUrl ,'formwin','height=560,width=700,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes');
}

function ajaxThis(formId) {
    new Ajax.Request('/xmlhttprequest.php', {
        parameters: $(formId).serialize(true),
        method:'get',
        onSuccess: function(transport){
            var response = transport.responseText || "No response text";
            $('status').update(response);
        },
        onFailure: function(transport){
            $('status').show();
            $('status').update("Sending av skjema feilet! " + transport.status);
        }
        });
}

/*
responseArray[70] = 'XML_HTTP_NEWSLETTER_INVALID_EMAIL';
responseArray[71] = 'XML_HTTP_NEWSLETTER_PREVIOUSLY_REGISTERED';
responseArray[72] = 'XML_HTTP_NEWSLETTER_CONFIRMATION_SENT';
responseArray[73] = 'XML_HTTP_NEWSLETTER_CONFIRMATION_NOTSENT';
responseArray[74] = 'XML_HTTP_NEWSLETTER_INVALID_SECURITYCODE';
responseArray[75] = 'XML_HTTP_NEWSLETTER_NEWMEMBER_OK';
responseArray[76] = 'XML_HTTP_NEWSLETTER_NEWMEMBER_ERROR';
responseArray[77] = 'XML_HTTP_NEWSLETTER_CANCELCONFIRMATION_SENT';
responseArray[78] = 'XML_HTTP_NEWSLETTER_CANCELCONFIRMATION_NOTSENT';
responseArray[79] = 'XML_HTTP_NEWSLETTER_CANCEL_ERROR';
responseArray[80] = 'XML_HTTP_NEWSLETTER_CANCEL_OK';
*/

function ajaxNewsletter(formId) {
	$('lightbox-content').hide();
	$('lightbox-spinner').show();
    new Ajax.Request('/xmlhttprequest.php', {
        parameters: $(formId).serialize(true),
        method:'get',
        onSuccess: function(transport){
            var response = transport.responseText || "No response text";
			chunks = response.split('###', 2);
            if (chunks[0] == 70 || chunks[0] == 71 || chunks[0] == 79) {
            	$('lightbox-spinner').hide();
            	$('lightbox-content').show();
            	$('newsletterschema').show();
            	$('newslettererror').show();
				$('newsletterstatus').hide();	            	
            	$('newslettererror').update(chunks[1]);	
            } 
            else if (chunks[0] > 71) {
            	$('lightbox-spinner').hide();
            	$('lightbox-content').show();
            	$('newsletterschema').hide();
            	$('newslettererror').hide();
            	$('newsletterstatus').show();
            	$('newsletterstatus').update(chunks[1]);	
            }
        },
        onFailure: function(transport){
            $('newsletterstatus').show();
            $('newsletterstatus').update("Sending av skjema feilet! " + transport.status);
        }
        });
}

function enlargeVideo(height, width) {
	$('embeddedObj0').width = new Number(width)*2;
	$('embeddedObj0').height = new Number(height)*2;
	$('embeddedObj0').setStyle({float:'right',position: 'relative', top: '-130px'});
	$('embeddedObj0').writeAttribute({onBlur: 'javascript:reduceVideo(130,300);'});
}

function reduceVideo(height, width) {
	$('embeddedObj0').width = new Number(width);
	$('embeddedObj0').height = new Number(height);
	$('embeddedObj0').setStyle({float:'left',position: 'relative', top: '0px'});
	
}

document.observe('dom:loaded', function(event) {
	try {
		if(!($('container').hasClassName("frontpage")) ) {
		var contentHeight = new Number($('container-middle').getHeight());
		var rest = contentHeight%270;
		if(contentHeight < 361) {
			$('container-middle').setStyle({height: '361px'});
		} else if(rest != 91) {
			var additionalHeight = contentHeight + 270 - rest + 91;
			$('container-middle').setStyle({height: additionalHeight.toString()+'px'});
		} 
		var newContentHeight = new Number($('container-middle').getHeight());
		if(newContentHeight > 550) {
			document.getElementById('container-middle').style.backgroundPosition = '0px 0px';
		}
		}
	} catch (e) {}
});

	

