var rollOver = {
    allMenus : $w("aboutkobe-menu activities-menu i_students-menu researchers-menu alumni-menu corporations-menu"),

    over : function(event)
    {
	var elem = event.element();
	var menu = elem.identify().sub("-image", "")

	if (this.allMenus.indexOf(menu) >= 0) {
	    menu = menu.sub("-menu", "")
	    var src = elem.src;
	    var ro = src.sub(menu, "ro_" + menu)
	    if (src != ro) {
		elem.src = ro
	    }
	}
    },

    restore : function(event)
    {
	var elem = event.element();
	var menu = elem.identify().sub("-image", "");

	if (this.allMenus.indexOf(menu) >= 0) {
	    menu = menu.sub("-menu", "")
	    var src = elem.src;
	    var orig = src.sub("ro_", "")
	    if (src != orig) {
		elem.src = orig
	    }
	}
    }
};

rollOver.over.bind(rollOver);
rollOver.restore.bind(rollOver);

var quickMenu = {
    popupNum : 0,
    allMenus : $w("aboutkobe-menu activities-menu i_students-menu researchers-menu alumni-menu corporations-menu"),
    counter : 0,
    origHeights : new Hash(),

    log : function()
    {
	this.counter++;
//	console.log(this.counter + ":" + $A(arguments).join(''));
    },

    initHeights : function()
    {
	var heights = this.origHeights;
	for (var i=0; i < this.allMenus.size(); i++) {
	    var ID = this.allMenus[i]
	    heights.set(ID, $(ID).getHeight())
	    this.log("ID=" + ID + " height=" + $(ID).getHeight())
	}
    },

    hideMenu : function(event)
    {
	var elem = event.element();

	this.log("hide; ID=" + elem.identify() + " popup=" + this.popupNum + " elem=" + elem + " ancs=" + elem.ancestors());

	if (this.popupNum > 0 &&
	    ! elem.descendantOf("navibar") && ! elem.descendantOf("quick-menu")) {
	    for (var i=0; i < this.allMenus.size(); i++) {
		var ID = this.allMenus[i];
		
		if ($(ID).visible()) {
		    $(ID).toggle();
		    this.popupNum--;
		}
	    }
	    if (this.popupNum != 0) {
		this.log("invalid popnum:" + this.popupNum)
	    }
	}
    },

    showMenu : function(event)
    {
	var elem = event.element();

	this.log("show; ID=" + elem.identify() + " popup=" + this.popupNum + " elem=" + elem + " ancs=" + elem.ancestors())

	if (elem.descendantOf("navibar")) {
	    var menu = null;
	    for (var i=0; i < this.allMenus.size(); i++) {
		var ID = this.allMenus[i];
		var img = ID + "-image";

		if (elem.identify() == ID || elem.identify() == img) {
		    menu = ID;
		    break;
		}
	    }

	    this.log("menu=", menu)

	    if ($(menu)) {
		if (! $(menu).visible()) {
		    if (this.popupNum > 0) {
			for (var i=0; i < this.allMenus.size(); i++) {
			    var ID = this.allMenus[i];
			    if (ID != menu && ($(ID).visible())) {
				$(ID).toggle();
				this.popupNum--;
			    }
			}
			
			if (this.popupNum != 0) {
			    this.log("invalid popnum:" + this.popupNum)
			}
		    }
		    
		    $(menu).setStyle("height:" + this.origHeights.get(menu) + "px");
		    $(menu).toggle();
		    this.popupNum++;
		}
	    }
	}
    },

    MenuComponent : Class.create( {
	initialize: function(alt, url, target) {
	    this.url = url;
	    this.alt = alt;
	    this.target = target;
	    if (! target) {
		this.target = "_self";
	    }
	},
	toHTML: function() {
	    return ('<li><a href="#{url}" target="#{target}">#{alt}<' + '/a><' + '/li>').interpolate(this);
	}
    } ),

    createMenu : function(menuID)
    {
	var string = ""
    
	var menuComponents = null;
	if (menuID == "aboutkobe-menu") {
	    menuComponents = [
		new this.MenuComponent('Introduction', '/en/about-kobe-university/introduction/index.htm'),
		new this.MenuComponent('History', '/en/about-kobe-university/history/index.htm'),
		new this.MenuComponent('Organization', '/en/about-kobe-university/organization/organization.htm'),
		new this.MenuComponent('Statistics', '/en/about-kobe-university/statistics/statistics.htm'),
		new this.MenuComponent('Office of the President', 'http://www.kobe-u.ac.jp/president/'),
		new this.MenuComponent('Departments', '/en/about-kobe-university/departments/departments.htm')
	    ];
	} else if (menuID == "activities-menu") {
	    menuComponents = [
		new this.MenuComponent('Research', '/en/activities/research/index.htm'),
		new this.MenuComponent('International Activities', '/en/activities/international-activities/index.htm'),
		new this.MenuComponent('Education', '/en/activities/education/index.htm'),
		new this.MenuComponent('Kobe University and Society', '/en/activities/society/index.htm'),
		new this.MenuComponent('Campus Manners', '/en/activities/campus-manners/index.htm')
	    ];
	} else if (menuID == "i_students-menu") {
	    menuComponents = [
		new this.MenuComponent('Prospective Students', '/en/international-students/prospective/index.htm'),
		new this.MenuComponent('Current Students', '/en/international-students/current/index.htm'),
		new this.MenuComponent('Life in Japan', '/en/international-students/life-in-japan/index.htm'),
		new this.MenuComponent('International Student Center', '/en/international-students/international-student-center/index.htm')
	    ];
	} else if (menuID == "researchers-menu") {
	    menuComponents = [
		new this.MenuComponent('Research Fields', '/en/activities/research/research-fields.htm'),
		new this.MenuComponent('Directory of Researchers', '/en/activities/research/directory-researchers.htm'),
		new this.MenuComponent('Repository Kernel', 'http://www.lib.kobe-u.ac.jp/kernel/e_index.html'),
		new this.MenuComponent('International Researchers&rsquo; Handbook', '/en/researchers/handbook/researchers-handbook.htm'),
		new this.MenuComponent('Accommodation', '/en/researchers/accommodation/residential-facilities.htm'),
		new this.MenuComponent('Pre-Arrival Information', '/en/researchers/pre-arrival/immigration-procedure.htm'),
		new this.MenuComponent('Life in Japan', '/en/researchers/life-in-japan/index.htm'),
		new this.MenuComponent('Before Returning Home', '/en/researchers/before-returning-home.htm'),
		new this.MenuComponent('University Hospital', '/en/about-kobe-university/departments/hospital.htm'),
		new this.MenuComponent('University Library', '/en/about-kobe-university/departments/library.htm')
	    ];
	} else if (menuID == "alumni-menu") {
	    menuComponents = [
		new this.MenuComponent('Welcome Message', '/en/alumni/introduction.htm'),
		new this.MenuComponent('International Student Center', 'http://www.kisc.kobe-u.ac.jp/english/i_alumni.html'),
		new this.MenuComponent('GAKUYUKAI', 'http://www.kobe-u.com/'),
		new this.MenuComponent('Tokyo Office', '/en/alumni/tokyo-office.htm'),
		new this.MenuComponent('Research Fields', '/en/activities/research/research-fields.htm'),
		new this.MenuComponent('Directory of Researchers', '/en/activities/research/directory-researchers.htm'),
		new this.MenuComponent('Repository Kernel', 'http://www.lib.kobe-u.ac.jp/kernel/e_index.html'),
		new this.MenuComponent('University Hospital', '/en/about-kobe-university/departments/hospital.htm'),
		new this.MenuComponent('University Library', '/en/about-kobe-university/departments/library.htm')
	    ];
	} else if (menuID == "corporations-menu") {
	    menuComponents = [
		new this.MenuComponent('Center for Collaborative Research and Technology Development', '/en/about-kobe-university/departments/facilities-research-center/collaborative-research-technology.htm'),
		new this.MenuComponent('Office of Promoting Regional Partnership', '/en/activities/society/regional-partnership.htm'),
		new this.MenuComponent('Tokyo Office', '/en/alumni/tokyo-office.htm'),
		new this.MenuComponent('Research Fields', '/en/activities/research/research-fields.htm'),
		new this.MenuComponent('Directory of Researchers', '/en/activities/research/directory-researchers.htm'),
		new this.MenuComponent('Repository Kernel', 'http://www.lib.kobe-u.ac.jp/kernel/e_index.html'),
		new this.MenuComponent('University Hospital', '/en/about-kobe-university/departments/hospital.htm')
	    ];
	} else {
	    alert("unknown menu:" + menuID);
	    return;
	}

	string += '<ul id="' + menuID + '" style="display: none; height: auto">';
	for (var i=0; i < menuComponents.size(); i++) {
	    var obj = menuComponents[i];
	    
	    string += obj.toHTML();
	}
	string += "<" + "/ul>";

	return string;
    }
};

quickMenu.initHeights.bind(quickMenu);
quickMenu.log.bind(quickMenu);
quickMenu.showMenu.bind(quickMenu);
quickMenu.hideMenu.bind(quickMenu);
quickMenu.createMenu.bind(quickMenu);

