/*********************************/
/* COOKIE FUNCTIONS              */
/*********************************/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*********************************/
/* STARTLIST                     */
/*********************************/

// <!-- fix lack of :hover psuedo class for IE/PC -->
startList = function() {
	if (document.all && document.getElementById) {
		navRoot = document.getElementById("main_nav");
		if (navRoot == null)
		    return;
		
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

/*********************************/
/* STYLESHEET SWITCHER           */
/*********************************/

var currTextSize = 0;

function setStylesheet(val) {
    if (isNaN(val))
        return;
    val = Math.floor(val);
    
    // Set active/inactive button states
    var button = document.getElementById("decrease_button");
    if (button != null) {
        if (val <= 0)
            button.className = "decrease-disabled";
        else
            button.className = "decrease";
    }
    
    button = document.getElementById("increase_button");
    if (button != null) {
        if (val >= 3)
            button.className = "increase-disabled";
        else
            button.className = "increase";
    }
        
    // Check limits
    if (val < 0 || val > 3)
        return;
    currTextSize = val;
    createCookie("nnc-font-size", val, 365);
    
    var arr = document.getElementsByTagName("link");
    var title = "fs_" + val;
    
    // Loop through all "link" elements
    for (var i=0; i<arr.length; i++) {
        var elem = arr[i];
        var elemTitle = elem.getAttribute("title");
        if (elem.getAttribute("rel").indexOf("style") != -1 && elemTitle != null) {
            if (elem.getAttribute("title") == title) {
                elem.disabled = false;
                elem.setAttribute("rel", "stylesheet");
            }
            else if (elemTitle.substr(0, 3) == "fs_")
                elem.disabled = true;
        }
    }
}

window.addEvent('domready', function(){
    // Init main nav
    startList();
    
    // Init font size / stylesheets
    currTextSize = parseInt(readCookie("nnc-font-size"), 10);
    if (isNaN(currTextSize))
        currTextSize = 0;

    setStylesheet(currTextSize);
});


/*********************************/
/* MISC                          */
/*********************************/

function popup(url) {
    var x = (window.getWidth() / 2) - 300;
    if (x < 0)
        x = 0;
    
    var y = (window.getHeight() / 2) - 300;
    if (y < 0)
        y = 0;
    
    window.open(url, '', 'width=650,height=300,top=' + y + ',left=' + x + ',resizable=yes,scrollbars=yes');
    return false;
}


window.addEvent('domready', function() {
    var menu_container = $("pi-dropdown-container");
    var menu = $("pi-dropdown");
    var trigger = $("top-pi-link");
    
    if (menu_container == null || menu == null || trigger == null)
        return;
    
    menu_container.style.display = "block";
    
    // Effect for PI dropdown
    var fx = new Fx.Slide(menu, {
        duration: 400
    });
    fx.target = menu;
    fx.mouseIsInMenu = false;
    fx.mouseIsInTrigger = false;
    fx.hide();
    
    // Setup mouse event triggers for menu behavior
    trigger.addEvent('click', function(e){
        new Event(e).stop();
    });
    
    trigger.addEvent('mouseenter', function(e){
        fx.mouseIsInTrigger = true;
        clearTimeout(fx.leaveMenuTimer);
        fx.slideIn();
    });
    
    trigger.addEvent('mouseleave', function(e){
        fx.mouseIsInTrigger = false;
        fx.leaveTriggerTimer = setTimeout(
            function() {
                if (!fx.mouseIsInMenu)
                    fx.slideOut();
            },
            50);
    });
    
    menu_container.addEvent('mouseenter', function(e){
        fx.mouseIsInMenu = true;
        clearTimeout(fx.leaveTriggerTimer);
    });
    
    menu_container.addEvent('mouseleave', function(e){
        fx.mouseIsInMenu = false;
        fx.leaveMenuTimer = setTimeout(
            function() {
                if (!fx.mouseIsInTrigger)
                    fx.slideOut();
            },
            50);
    });
});