var undefined = undefined;
var sectionMap = [];

var menuControllerClose = document.getElementById("menuControllerClose");
var menuControllerMenu = document.getElementById("menuControllerMenu");
var menuOptions = document.getElementById("menuOptions");
var menuItemSelected = document.getElementById("menuItemSelected");

sectionMap["ps3"] = 1;
sectionMap["games"] = 2;
sectionMap["news"] = 3;
sectionMap["faqs"] = 4;
sectionMap["downloads"] = 5;

function Menu(startElem){
	this.elemHeight = 26;
	this.startElem = (startElem && sectionMap[startElem] ? sectionMap[startElem] : 0);
	this.currElem = this.startElem;
	

//	this.moveMenu = function(i){
//		this.currElem = i;
//		menuOptions.style.top = (-this.elemHeight * this.currElem) + "px";
//	}
	this.displayState = false;
	this.closeDisplay = function(){ // EXIST TO PREVENT A RECURSION ERROR
		this.displayState = false;
		menuControllerClose.style.display = "none";
		menuControllerMenu.style.display = "block";
		if(menuItemSelected != undefined) {
			menuItemSelected.style.display = "block";
		}
		menuOptions.style.display = "none";
//		this.moveMenu(this.startElem);

        /* Check if page contains the search field and input bar */
        var searchFieldBelowMenu = document.getElementById("p6451IASEARCH_USER_QUERY");
        var searchButtonBelowMenu = document.getElementById("p6451Isubmit-search");
        if(searchFieldBelowMenu){
            /* Enable both fields when menu is closed */
            if(searchFieldBelowMenu.disabled == true){
                searchFieldBelowMenu.disabled = false;
                searchButtonBelowMenu.disabled = false;
            }
        }
    }
    /* Check if the left, down or right key on joypad has been pressed */
    this.catchKeyPress = function(e, directionToClose) {
        switch(directionToClose) {
            case "right":
                if(e.keyCode == "39"){
                    this.closeDisplay();
                }
                break;
            case "leftOrDown":
                if(e.keyCode == "37" || e.keyCode == "40"){
                    this.closeDisplay();
                }
                break;
        }
    }
    this.toggleDisplay = function(){
		this.displayState = !this.displayState;
		menuControllerClose.style.display = (this.displayState ? "block" : "none");
		menuControllerMenu.style.display = (this.displayState ? "none" : "block");
		if(menuItemSelected != undefined) {
			menuItemSelected.style.display = (this.displayState ? "none" : "block");
		}
		if(searchCont && searchCont.displayState){
			searchCont.closeDisplay();
		}
		menuOptions.style.display = (this.displayState ? "block" : "none");
//		this.moveMenu(this.startElem);

        /* Check if page contains the search field and input bar */
        var searchFieldBelowMenu = document.getElementById("p6451IASEARCH_USER_QUERY");
        var searchButtonBelowMenu = document.getElementById("p6451Isubmit-search");
        if(searchFieldBelowMenu){
            /* Disable both fields when menu is opened */
            searchFieldBelowMenu.disabled = (this.displayState ? true : false);
            searchButtonBelowMenu.disabled = (this.displayState ? true : false);
        }

    }
    this.moveTo = function(location){
//		this.actionFlag = true;
//		this.moveMenu(location);
	}
	this.mouseReset = function(){
//		if(!this.actionFlag){
//			this.moveMenu(0);
//		} else {
//			this.actionFlag = false;
//		}
	}
	this.menu = function(){
//		this.actionFlag = false;
	}
	this.menu();
}
