$(document).ready(function() {

    function initTabs() {
        $(".tab_sc").each(function() {
            $(this).parents(".tab").siblings(".tab_sm").hide();
        });
        $(".tab_so").parents(".tab").next(".tab_sm").show();
     }

    initTabs();
    
    var mouseIsOver = false;

    //open/close tab    
    $(".tab").click(function() {
        var tabStatus = $(this).children(".tab_s:first");
	//tab is opened
	if (tabStatus.hasClass("tab_so")) {
            tabStatus.removeClass("tab_so").addClass("tab_sc");
            $(this).next(".tab_sm").addClass("closing");
            tabStatus.removeClass("tab_so").addClass("tab_sc");
            $(".closing").slideUp("normal").removeClass("closing");
	} else {
            tabStatus.removeClass("tab_sc").addClass("tab_so");
            $(this).next(".tab_sm").addClass("opening");
            $(".tab_sm").not(".opening").slideUp("normal");
            $(".tab_sm").not(".opening").prev(".tab").children(".tab_s").addClass("tab_sc").removeClass("tab_so");
            $(".opening").slideDown("normal").removeClass("opening");
        }
    });

    $(".menuItem").mouseenter(function() {
            mouseIsOver = true;
	    var portruder = $(this).children(".menuItem_p");
	    var popupMenu = $(this).children(".menuItem_pu");
	    portruder.addClass("opening");
	    portruder.children(".menuItem_s").addClass("menuItem_so").removeClass("menuItem_sc");
	    portruder.animate({width: "325px"},{duration:"normal",complete: function() {
									      popupMenu.css({"left":"170px","top":"25px"});
									      popupMenu.slideDown("normal");
									      popupMenu.addClass("pu_so");
						   		      }}).removeClass("opening");
    });
	
    $(".menuItem").bind('mouseleave.menuItem',menuItemMouseLeave);
    function menuItemMouseLeave() {
        var portruder = $(this).children(".menuItem_p:first");
      	var popupMenu = $(this).children(".menuItem_pu:first");
	   if (popupMenu.hasClass("pu_so")) {
                popupMenu.slideUp("fast",function() {
			    	/*when the popup is closed, we still need to close the menuItem (push it back to menu), so we call
				      this function again*/
				      $(this).trigger("mouseleave.menuItem");
				    }).removeClass("pu_so").addClass("pu_sc");
           } else {
            //Clear the animation queue
                portruder.stop(true);
                portruder.children(".menuItem_s").removeClass("menuItem_so").addClass("menuItem_sc");
                portruder.animate({width: "153px"},{duration: "fast"});
	   }
	   portruder.dequeue();
    }
	
    $(".pmi").mouseenter(function() {
        mouseIsOver = true;
	var portruder = $(this).children(".pmi_p:first");
	var popupMenu = $(this).children(".pmi_pu:first");
	portruder.addClass("opening");
	if (!portruder.children(".pmi_s").hasClass("pmi_sn")) {
            portruder.children(".pmi_s").addClass("pmi_so").removeClass("pmi_sc");
            portruder.animate({width: "325px"},{duration:"normal",complete: function() {
                                                                    popupMenu.css({"left":"170px","top":"26px"});
                                                                    popupMenu.slideDown("normal");
                                                                    popupMenu.addClass("pu_sc");
						   		}
                                            }).removeClass("opening");
        }
    });
	
    $(".pmi").bind('mouseleave.pmi',pmiMouseLeave);
    function pmiMouseLeave() {
        var portruder = $(this).children(".pmi_p:first");
	var popupMenu = $(this).children(".pmi_pu:first");
	if (!portruder.children(".pmi_s").hasClass("pmi_sn")) {
            if (popupMenu.hasClass("pu_so")) {
                popupMenu.slideUp("fast",function() {
                    /*when the popup is closed, we still need to close the submenuItem (push it back to menu), so we call
                    this function again*/
                    portruder.stop(true);
                    portruder.children(".pmi_s").removeClass("pmi_so").addClass("pmi_sc");
                    portruder.animate({width: "154px"},{duration: "fast"});
                 }).removeClass("pu_so").addClass("pu_sc");
            } else {
                //Clear the animation queue
		          portruder.stop(true);
		          portruder.children(".pmi_s").removeClass("pmi_so").addClass("pmi_sc");
		          portruder.animate({width: "154px"},{duration: "fast"});
            }
        portruder.dequeue();
	}
    }

    $(".pmi").click(function() {
        //Temporarily unbind parent's click event
	$(this).parents(".menuItem").unbind("click");
	if ($(this).find("a:first").length > 0) {
            var url = $(this).find("a:first").attr("href");
            document.location.href=url;
            $(this).parents(".menuItem").bind("click");
        }
    });
	
    $(".menuItem:not(.pmi)").click(function () {
	if ($(this).find("a:first").length >0 ) {
            var url = $(this).find("a:first").attr("href");
            document.location.href=url;
	}
    });
});
