// Resize Menus
function size_menu() {
	// top menu numbers
	top_num = $$("#nav li").size();
	top_w = Math.floor(1010/top_num)-1;
	top_last = top_w + 1010%top_num;

	// botom menu numbers
	bottom_num = $$("#bottom_nav li").size();
	bottom_w = Math.floor(1010/bottom_num)-1;
	bottom_last = bottom_w + 1010%bottom_num;

	// top menu
	$$("#nav li").each(function(n) {
		n.setStyle({
		  width: top_w+'px',
		  textAlign: 'center'
		});

	});

	// bottom menu
	$$("#bottom_nav li").each(function(n) {
		n.setStyle({
		  width: bottom_w+'px',
		  textAlign: 'center'
		});

	});

	$$("#nav li a, #bottom_nav li a").each(function(n) {
		n.setStyle({
		  width: '100%',
		  padding: '8px 0 7px 0'
		});

	});

	// top
	$$("#nav li").last().setStyle({	  width: top_last+'px', border: 'none'	});
	// bottom
	$$("#bottom_nav li").last().setStyle({	  width: bottom_last+'px', border: 'none'	});

}

// popups

function preparePopUps() {
	if (!document.getElementsByTagName) return false;
	var lnks = document.getElementsByTagName("a");
	for (var i=0; i<lnks.length; i++) {
		if (lnks[i].className.indexOf("popup") == -1) continue;
		lnks[i].onclick = function() {
			window.open(this.href);
			return false;
		}
		lnks[i].onkeypress = lnks[i].onclick;
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function rollout(objMenuitem) {
    if (objMenuitem.nextSibling.style.display == 'block')
        objMenuitem.nextSibling.style.display = 'none';
    else {
		Element.extend(objMenuitem);
		objMenuitem.ancestors().each(function(n) {
			if(n.identify()=='left') {
				side=$$('#left ul.menu ul');
			} else if(n.identify()=='right') {
				side=$$('#right ul.menu ul');
			}

		});
			
		side.each(function(n) {
			n.hide();

		});

        objMenuitem.nextSibling.style.display = 'block';
	}

    // Stop the browser requesting the link
    return false;
}

function rollup() {
    var bRollup, objLinks, objNode, objAnchor;

    // Check we're working with a DOM compliant browser
    if (document.getElementById && document.createElement)
    {
        var strLocation = window.location;



        // Hide each of the nested unordered list
        $$('#left ul.menu ul, #right ul.menu ul').each(function(objNested) {
            // Only hide, if the current location is not found in the list
            bRollup = true;
            objLinks = objNested.getElementsByTagName('a');

            for (var j=0; j<objLinks.length; j++)
            {
                if (objLinks[j].href == strLocation)
                    bRollup = false;
            }

            if (bRollup == true)
                objNested.style.display = 'none';
            else
                objNested.style.display = 'block';

            // Place the top-level text in an anchor tag
            objNode = objNested.parentNode;
            Element.extend(objNode);

            strContent = objNode.firstChild.data;

            objAnchor = document.createElement('a');
            Element.extend(objAnchor);
            objAnchor.href = '#';
            objAnchor.addClassName($w(objNode.className));
            objAnchor.onclick = function(){return rollout(this);}
            objAnchor.appendChild(document.createTextNode(strContent));

            objNode.replaceChild(objAnchor, objNode.firstChild);
        });
    }
}

function tabs() {
	try {
		boxes = $$('div.tabbox_bg');
		boxes.each(function(n) { n.hide(); });
		boxes.first().show();

		$$('div.tabnav a').each(function(n, index) {
			// n.onclick="boxes.each(function(n) { n.hide(); }); boxes.indexOf(index).show; return false; 	";

			Event.observe(n, 'click', function(event) {
				Event.stop(event);

				// change active 
				$$('div.tabnav a').each(function(m) { m.removeClassName('active'); });
				n.toggleClassName('active');

				//show corresponding box
				boxes.each(function(n) { n.hide(); });
				boxes[index].show();
			});


		});
	}
	catch (e) {
		// do nothing
	}
}

function search_init() {
	try	{
		var s=$('search_keyword');
		s.value='Search for a Course';
		Event.observe(s, 'focus', function() {
			if(s.value=='Search for a Course') { 
				s.value=''; 
			}
		});
		Event.observe(s, 'blur', function() {
			if(s.value=='') { 
				s.value='Search for a Course'; 
			}
		});
	}
	catch (e) {
		// do nothing
	}
}

addLoadEvent(size_menu);

addLoadEvent(rollup);

addLoadEvent(preparePopUps);

addLoadEvent(tabs);

addLoadEvent(search_init);


