/*
    This file is part of "Category menu for VirtueMart".

    "Category menu for VirtueMart" is under the terms of the 
    GNU General Public License as published by the Free Software
    Foundation, either version 3 of the License.

    "Category menu for VirtueMart" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied
    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Category menu for VirtueMart". If not, see 
    http://www.gnu.org/licenses/

    Category menu for VirtueMart  Copyright (C) 2009  Balint Polgarfi
 */

window.addEvent('domready', function() {
	var isParent = function(other, submenu, clss) {
	    if (submenu.getProperty("class") != clss) return false;
	    if (other.getProperty("id") == submenu.getProperty("id")) return true;
	    else return isParent(other, submenu.getParent().getParent(), clss);
	};
	var menu = $$("div#gi_cat div#gi_cat_menu div.menu");
	var submenus = $$("div#gi_cat div#gi_cat_menu div.submenus");
	var transform = {wait: false, duration: time, transition: Fx.Transitions.linear};
	var fx = new Fx.Elements(menu, transform);
	var mx = new Fx.Elements(submenus, {
		wait: false,
		duration: time,
		transition: Fx.Transitions.linear,
		onComplete: function() {
		    submenus.each(function(submenu, i) {
		        if (submenu.getStyle("opacity") == 0) {
					submenu.setStyle("display", "none");
				}
			});
  		}
	});
	
	$E("div#gi_cat div#gi_cat_menu").setStyle("width", menuwidth);
	$$("div#gi_cat div#gi_cat_menu div.menuborder").each(function(border, i) {
	    border.setStyle("opacity", out);
	});
	$$("div#gi_cat div#gi_cat_menu div.menutopbottom").each(function(border, i) {
	    border.setStyle("width", menuwidth-7);
	});
	$$("div#gi_cat div#gi_cat_menu div.menucenter").each(function(submenu, i) {
	    submenu.setStyle("width", menuwidth-40);
	});
	submenus.each(function(submenu, i) {
	    submenu.setStyles({
	        width: menuwidth,
	        left: menuwidth,
	        opacity: 0
	    });
	});
	
	menu.each(function(submenu, i) {
	    var p = {}, o = {};
	    submenu.setStyle("opacity", out);
	    submenu.setStyle("width", menuwidth);
	    submenu.addEvent("mouseenter", function(event) {
	        menu.each(function(other, j) {
	            var opac = other.getStyle("opacity").toFloat();
	            if (i != j && !isParent(other, submenu.getParent().getParent(), "menu")) {
	                o[j] = {opacity: [opac, out]};
	            } else {
	                o[j] = {opacity: [opac, over]};
				}
	        });
	        submenus.each(function(other, j) {
                var opac = other.getStyle("opacity").toFloat();
                if (submenu.getProperty('id') == other.getParent().getProperty('id')
				||	isParent(other, submenu.getParent(), "submenus")) {
                    p[j] = {opacity: [opac, 1]};
                    other.setStyle("display", "block");
                } else if (other.getStyle("display") == "block") {
                    p[j] = {opacity: [opac, 0]};
                }
            });
	        mx.start(p);
	        fx.start(o);
	    });
        submenu.addEvent("mouseleave", function(event) {
            var haveSubmenu = $E('div#'+submenu.getProperty('id')+' .submenus');
            if (haveSubmenu) {
                submenus.each(function(other, j) {
                    if (other.getProperty('id') == haveSubmenu.getProperty('id')) {
                        var opac = other.getStyle("opacity").toFloat();
                        p[j] = {opacity: [opac, 0]};
                        mx.start(p);
                        return;
                    }
                });
            }
            if (submenu.getParent().getProperty('class') == "submenus"
            &&  $E('div#'+submenu.getParent().getProperty('id')+' .menu').getProperty('id') == submenu.getProperty('id')) {
                menu.each(function(other, j) {
                    if (other.getProperty('id') == submenu.getProperty('id')) {
                        var opac = other.getStyle("opacity").toFloat();
                        o[j] = {opacity: [opac, out]};
                        fx.start(o);
                        return;
                    }
                });
            }
        });
	});

	$E("div#gi_cat div#gi_cat_menu").addEvent("mouseleave", function(event) {
	    var p = {}, o = {};
	    menu.each(function(submenu, i) {
	        o[i] = {opacity: [submenu.getStyle("opacity").toFloat(), out]};
		});
		submenus.each(function(submenu, i) {
	        p[i] = {opacity: [submenu.getStyle("opacity").toFloat(), 0]};
		});
		mx.start(p);
		fx.start(o);
	});
});