var MainMenu = Class.create();

Object.extend(MainMenu, {
	currentMenu: null,
	showDropDown: false,
	refreshPageAfterTimedOutLogin: false,

	initialize: function(){
		/* hide the menu when the body is cl\icked */
		Event.observe( document.body, 'click', this.deselectMenu.bind(this) );
		
		Event.observe( 'mnuSupport', 'click', 
		    function(event){
		        var support = $('support');
		        support.style.display=(support.style.display=='block') ? 'none' : 'block';
		        Event.stop(event);
		    }.bind(this)
		);
		
		var menuItems = $$("#mainMenu a.dropdown");
		for(var i=0; i<menuItems.length; i++){
			var menuItem = menuItems[i];
					
			/* Show the drop down when the top item is clicked */
			Event.observe(menuItem, 'click', this.selectMenu.bind(this) );
			
			/* Allow hovering over other menus */
			Event.observe(menuItem, 'mousemove', this.hoverSelectMenu.bind(this) );
		}
	},
		
	selectMenu: function(event){
		var elm = Event.element(event);
		var a = ((elm.tagName == "SPAN") ? elm.parentNode : elm);
		var li = a.parentNode;
		
		if(li == this.currentMenu) {
			this.deselectMenu();
			Event.stop(event);
			return;
		}
		
		/* Remove currently selected drop down */
		if(this.currentMenu){
			li.className = (li.className == 'selected hover') ? 'selected' : '';
		}
		
		li.className = (li.className == 'selected') ? 'selected hover' : 'hover';
		
		this.currentMenu = li;
		this.showDropDown = true;

		a.blur();
		
		Event.stop(event);
	},
	
	deselectMenu: function(event){
		this.showDropDown = false;
		if(this.currentMenu){
			this.currentMenu.className = (this.currentMenu.className == 'selected hover') ? 'selected' : '';
			this.currentMenu = null;
		}
	},
	
	hoverSelectMenu: function(event){
		var elm = Event.element(event);
		var a = ((elm.tagName == "SPAN") ? elm.parentNode : elm);
		var li = a.parentNode;
		
		if(this.showDropDown == true && a.className == 'levelOne dropdown' && this.currentMenu != li){
			this.deselectMenu(event);
			this.selectMenu(event);
		}
	},
		 
    logOut: function(){
	    MsgBox.show({ text:"Are you sure you want to log out?", title:"Logout", okButton:{ clickHandler:"location.href='../Login/Login.aspx?logout=true';" } });
	},
	
	setRefreshPageAfterTimedOutLogin: function(value)
	{
	    this.refreshPageAfterTimedOutLogin = value;	    
	},
	
	timedOutLogin: function()
	{   
        Ajax.Login($F("timedOutUserName"), $F("timedOutPassword"), 
            function(res)
            {
                if(! res.error)
                {
                    Windows.closeAll();
                    
                    if (this.refreshPageAfterTimedOutLogin)
                    {
                        location.href = location.href;
                    }
                } else {
                    Windows.closeAll();
                    location.href = "../Login/Login.aspx?logout=true";
                }
            }
        );
	},
	
	sendIssue: function(){
		if($F('issueTrackerBody').isNullOrEmpty(true))
		{
			var selected = document.getElementsByName('issueType');

			var issueType = 'broken';
		    
			for (var i = 0; selected.length > i; i++)
			{
				if (selected[i].checked==true)
					issueType = selected[i].id;
			}

			var retval = Ajax.LogIssue(issueType, $F('issueTrackerBody'));
			
			if (retval.value == true)
			{
			    MsgBox.alert("Thanks &ndash; your issue has been submitted to the Xero<br/>Team. A record of the issue will be emailed to you soon.", "Issue Submitted");
			    $('issueTrackerBody').value = "";
			    
			    $('support').hide();
			}			
			else
			{
			    MsgBox.alert("There was a problem while submitting your feedback.  Please try again", "Issue Could Not Be Submitted");			    
			}
		}
		else
		{
		    MsgBox.alert("Sorry &ndash; please enter the details of this issue.", "Issue Submitted");
		}
	}
});

