/**
 * IE needs a separate value so declare as global and adjust in meta.php
 **/
var mPowerThemeMenuHeight = 29;

/**
 * Override menu positioning
 *
 * @param array pos X Y coordinates
 * return array
 **/
function pageThemeAdjustMenuHeight(pos) {
    pos[1] = pos[1] + mPowerThemeMenuHeight; // Bring it down some

    return pos;
}

/*
addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
*/
function mPowerAddEvent( obj, type, fn )
{
    if (obj.addEventListener)
        obj.addEventListener( type, fn, false );
    else if (obj.attachEvent)
    {
        obj["e"+type+fn] = fn;
        obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
        obj.attachEvent( "on"+type, obj[type+fn] );
    }
}

/*
createElement function found at http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
*/
function mPowerCreateElement(element) {
    if (typeof document.createElementNS != 'undefined') {
        return document.createElementNS('http://www.w3.org/1999/xhtml', element);
    }
    if (typeof document.createElement != 'undefined') {
        return document.createElement(element);
    }
    return false;
}

function mPowerInsertTop(obj) {
    // Create the two div elements needed for the top of the box
    d=mPowerCreateElement("div");
    d.className="bt"; // The outer div needs a class name
    d2=mPowerCreateElement("div");
    d.appendChild(d2);
    obj.insertBefore(d,obj.firstChild);
}

function mPowerInsertBottom(obj) {
    // Create the two div elements needed for the bottom of the box
    d=mPowerCreateElement("div");
    d.className="bb"; // The outer div needs a class name
    d2=mPowerCreateElement("div");
    d.appendChild(d2);
    obj.appendChild(d);
}

function mPowerInitCB()
{
    // Find all div elements
    var divs = document.getElementsByTagName('div');
    var cbDivs = [];
    for (var i = 0; i < divs.length; i++) {
    // Find all div elements with cbb in their class attribute while allowing for multiple class names
        if (/\bsideblock\b/.test(divs[i].className))
            cbDivs[cbDivs.length] = divs[i];
    }
    // Loop through the found div elements
    var thediv, outer, i1, i2;
    for (var i = 0; i < cbDivs.length; i++) {
    // Save the original outer div for later
        thediv = cbDivs[i];
    //  Create a new div, give it the original div's class attribute, and replace 'cbb' with 'cb'
        outer = mPowerCreateElement('div');
        outer.className = thediv.className;
        outer.className = thediv.className.replace('sideblock', 'sideblock cb');
    // Change the original div's class name and replace it with the new div
        thediv.className = 'i3';
        thediv.parentNode.replaceChild(outer, thediv);
    // Create two new div elements and insert them into the outermost div
        i1 = mPowerCreateElement('div');
        i1.className = 'i1';
        outer.appendChild(i1);
        i2 = mPowerCreateElement('div');
        i2.className = 'i2';
        i1.appendChild(i2);
    // Insert the original div
        i2.appendChild(thediv);
    // Insert the top and bottom divs
        mPowerInsertTop(outer);
        mPowerInsertBottom(outer);
    }
}

/***
   This Javascript is used because moodle doesn't allow a theme developer 
   to control the header of a block.  As such in order to add onclick 
   actions to an atrribute in the block header we have to do it after 
   the page has been loaded.  IE has an issue wiht adding JAvascript after 
   the browser has loaded the page unless it is done through the create 
   element function.  In order to get this to work then we have to 
   completely remove the original h2 header and add a new one with 
   the onclick funcitonality we need 
***/
function mPowerShowHide(){
    // Find all div elements
    var divs = document.getElementsByTagName('div');
    var cbDivs = [];
    for (var i = 0; i < divs.length; i++) {
    // Find all div elements with cbb in their class attribute while allowing for multiple class names
        if (/\bsideblock\b/.test(divs[i].className))
            cbDivs[cbDivs.length] = divs[i];
    }
    // Loop through the found div elements
    var thediv, hidden;
    hidden = false;
    for (var i = 0; i < cbDivs.length; i++) {
    // Save the original outer div for later
        thediv = cbDivs[i];

        if(/\bhidden\b/.test(thediv.className)){
		  hidden = true;
		}
		
	    var children = thediv.childNodes;

	    for(var j = 0; j < children.length; j++){
			if(/\bheader\b/.test(children[j].className)){
				var headerchildren = children[j].childNodes;
				for(var k = 0; k < headerchildren.length; k++){
		  			if(/\btitle\b/.test(headerchildren[k].className)){
			            var titlechildren = headerchildren[k].childNodes;
						for(var l = 0; l < titlechildren.length; l++){
							if((/\bH2\b/.test(titlechildren[l].tagName))){
								if(hidden){
									try {
										
										//IE										
										var hideDiv = document.createElement('<h2 onclick="elementToggleHide(this, true, function(el) {return findParentNode(el, \'DIV\', \'sideblock\'); }, \'Show block\', \'Hide block\'); return false;"></h2>');
										hideDiv.innerText = titlechildren[l].innerText;
										headerchildren[k].insertBefore(hideDiv,titlechildren[l]);
										//Increment l by one because we have added a child to headerchildren
										headerchildren[k].removeChild(titlechildren[l+1]);
									} 
									catch (ex){
										//Everything else (FF, Opera, etc)
										titlechildren[l].setAttribute('onclick', "elementToggleHide(this, true, function(el) {return findParentNode(el, 'DIV', 'sideblock'); }, 'Show block', 'Hide block'); return false;");
									}	

		    					} else {
									try {
										//IE										
										var hideDiv = document.createElement('<h2 onclick="elementToggleHide(this, true, function(el) {return findParentNode(el, \'DIV\', \'sideblock\'); }, \'Hide block\', \'Show block\'); return true;"></h2>');
										hideDiv.innerText = titlechildren[l].innerText;
										headerchildren[k].insertBefore(hideDiv,titlechildren[l]);
										//Increment l by one because we have added a child to headerchildren
										headerchildren[k].removeChild(titlechildren[l+1]);
									} 
									catch (ex){
										//Everything else (FF, Opera, etc)
			  							titlechildren[l].setAttribute('onclick', "elementToggleHide(this, true, function(el) {return findParentNode(el, 'DIV', 'sideblock'); }, 'Hide block', 'Show block'); return true;");
									}
		    					}
							}
						}
					}
				}
		    	hidden = false;
		  	} 
		}
    }
}