/**
 * AGT COOL MENU
 * @author talipkulakoglu
 */
(function(a){
	
	function agtUlLiMenu(A,S){
		this.r = function(){			
			E();			
		};
		function E(){
			T.find('li ul').hide();
			T.find('li').css({'position' : 'relative'}).each(function(){				
				if(a(this).find('ul:first').is('ul')){
					a(this).css({'backround' : 'none'}).prepend('<div class="m-button" />');
				}
			});
			T.find('.m-button').bind('click',function(){
				a(this).toggleClass('m-button-open');
				a(this).parents('li:first').children('ul').slideToggle();
			});
		};
		var T = a(A),O = a.extend({},a.fn.agtCoolUl.defaults,S),w = true;
	}
		
    function agt(A, S){
        /**
         * Modul start function run construct
         *
         * @return void
         */
        function r(){
            var w = a(window);
            var wt = Math.ceil((w.height() - e.height()) / 2 - 80);       
            a('<div />').css({}).addClass('menu-button').appendTo(e);
            e.css({
                'left': 0,
                'top': wt,
                'left': '-' + ww + 'px'
            });
            var li = e.children('div.menu-content').children('ul').children('li');
            li.hoverIntent(function(){
            
                a(this).children('ul').wrap('<div class="wrap" />');
                a(this).children('.wrap').append('<div />').children('ul').css({
                    'border-top': 0
                }).fadeIn(500).children('li:first').css({
                    'border-top': 0
                });
            }, function(){
                var lh = a(this);
                var content = lh.children('.wrap').children('ul').hide();
                lh.children('div').remove();
                content.appendTo(lh)
                
            })
            e.children('.menu-button').hover(function(){
                
					a(this).addClass('active');
					mt = true;
					mh = false;
					e.animate({
						left: 0
					}, {
						duration: m.settings.delay,
						complete: function(){
						
						}
					})
			
/*
			if (mh) {	}
				else {
					mh = true;
					a(this).removeClass('active');
					mt = false;
					e.animate({
						left: '-' + ww + 'px'
					}, {
						duration: m.settings.delay
					})
				}
*/
            });
            a("body").bind('click', ch);
        }
        function ch(event){
        
            if (mt) {
                if (!a(event.target).is('#agt-cool-menu *')) {
                    a('.menu-button').removeClass('active');
                    mt = false;
					mh = true;
                    e.animate({
                        left: '-' + ww + 'px'
                    }, {
                        duration: m.settings.delay
                    })
                }
            }
            
        }
        
        this.settings = a.extend({}, a.fn.agtCoolMenu.defaults, S);
        var m = this, e = a(A), t = 0, mt = false,ww = e.width(),mh = true;
        r();
        
    }
	/**
	 * agt cool menu
	 * @param {Object} option
	 */
    a.fn.agtCoolMenu = function(option){
        var menu = new agt(this, option);
    }
    a.fn.agtCoolMenu.defaults = {
        'event': 'click',
        'delay': 200
    }
	/**
	 * agt cool ul li menu
	 * @param {Object} o
	 */
	a.fn.agtCoolUl = function(o){
		return this.each(function(){
			var R = new agtUlLiMenu(this,o);
			R.r();
		});
	}
	a.fn.agtCoolUl.defaults = {
		events : 'click'
	}
})(jQuery);

