var CheckedOpen = {
	open:function(menuID,CSSclass,var_open,var_height)
	{
		$(menuID).animate({
                        height: var_height
                            }, 700);
		if(var_open)
		  $(menuID).addClass(CSSclass);
	
	},
	openMenu: function($target,innerheader,CSSclass,over)
	{
	  $($target).slideDown(over);
	  $(innerheader).addClass(CSSclass);
	},
    close: function(menuID,CSSclass)
    {
        if($('.'+CSSclass+'_sub').length <= 0)
        {
            $(menuID).animate(
            {
                height: 30
            },
            400);
           $(menuID).removeClass(CSSclass);
        }
			else
		  $(menuID).stop(true,true);
    },
	closeMenu: function(menuID,innerheader,CSSclass,out)
    {
	    $(menuID).slideUp(out);
        $(innerheader).removeClass(CSSclass);
		$(menuID).stop(false,true);
    }
}
jQuery.extend({
    droplinemenu: function(menuID){
        var that = this;
        this.menuid = '#'+menuID;
        this.openClass = 'open_menu';
        this.animateduration = {
            over: 700,
            out: 500
        };
        this.arrowimage = {
            classname: 'downarrowclass',
            src: 'js/down.gif',
            leftpadding:5
        };

        this.Opened = function()
        {
            return $(that.menuid).hasClass(that.openClass);
        }
        this.buildmenu = function(){
            var $mainmenu=$(that.menuid+">ul");
            var $headers=$mainmenu.find("ul").parent();
            $headers.each(function(i){
                var $curobj=$(this);
                var $subul=$(this).find('ul:eq(0)');
                this._dimensions={
                    h:$curobj.find('a:eq(0)').outerHeight()
                };

                this.istopheader=$curobj.parents("ul").length==1? true : false;
                if (!this.istopheader)
                    $subul.css({
                        left:0,
                        top:this._dimensions.h
                    });
                var $innerheader=$curobj.children('a').eq(0);
                $innerheader=($innerheader.children().eq(0).is('span'))? $innerheader.children().eq(0) : $innerheader; //if header contains inner SPAN, use that
                $innerheader.append(
                    '<img src="'+ that.arrowimage.src
                    +'" class="' + that.arrowimage.classname
                    + '" style="border:0; padding-left: '+that.arrowimage.leftpadding+'px" />'
                    );
                $curobj.hover(
                    function(e){
                        var $targetul=$(this).children("ul:eq(0)");
                        if ($targetul.queue().length<=1) //if 1 or less queued animations
                            if (this.istopheader)
                                $targetul.css({
                                    left: 0,
                                    top: this._dimensions.h
                                });

                        if (document.all && !window.XMLHttpRequest) //detect IE6 or less, fix issue with overflow
                            $mainmenu.find('ul').css({
                                overflow: (this.istopheader)? 'hidden' : 'visible'
                            });
					CheckedOpen.open(that.menuid,that.openClass,!that.Opened(),25+$targetul.outerHeight());		
					CheckedOpen.openMenu($targetul,$innerheader,that.openClass+'_sub',that.animateduration.over);
                    },
                    function(e){
					var $targetul=$(this).children("ul:eq(0)");
					if(!$targetul.is(":hidden"))
					{
					 var timeoutFunc = function(){
						  CheckedOpen.closeMenu($targetul,$innerheader,that.openClass+'_sub',that.animateduration.out);}
						setTimeout(timeoutFunc,300);
					}
					
                    if(that.Opened())
					{
						 var timeoutFunc2 = function(){
						 CheckedOpen.close(that.menuid,that.openClass);}
						setTimeout(timeoutFunc2,500);
					
					   }
                    }
                    ); //end hover
            }); //end $headers.each()
            $mainmenu.find("ul").css({
                display:'none',
                visibility:'visible',
                width:$mainmenu.width()
            }); //end document.ready
        }
    }
});