/*
  jQuery ui.dropslide - 0.2
  http://code.google.com/p/jquery-utils/

  (c) Maxime Haineault <haineault@gmail.com> 
  http://haineault.com

  MIT License (http://www.opensource.org/licenses/mit-license.php

*/

(function($) {
    $.widget('ui.dropslide', $.extend({}, $.ui.mouse, {
        getter: 'showLevel showNextLevel',
        init: function() {
            var next = this.element.next();
            this.wrapper = next.hasClass('ui-dropslide') && next || this.options.tree || false;

            if (this.wrapper)
			{
                this.element.bind(this.options.trigger + '.dropslide', onActivate);
                this.wrapper
                    .data('dropslide', this)
                    .css({ width: this.options.width })
                    .find('li, li ol li').bind('mouseover.dropslide', onLiMouseover)
                               .bind('click.dropslide', onLiClick)
                               .bind('mouseout.dropslide', onMouseout)
                               .end()
                    .find('ol').bind('mousemove.dropslide', onOlMousemove).hide();
            }
        },
        // show specified level, id is the DOM position
        showLevel: function(id, selTime) {
			
            if (this.wrapper) {
				var dropslide = this;				
                var ols = this.wrapper.find('ol');
				var spanWrap = this.wrapper;
				
                if (id == 0) {
                    ols.eq(0).css('left', this.element.position().left);
					
					if (selTime){
						
						setTimeout(function() {
							
							var offset = dropslide.element.position().left + dropslide.options.left;

							ols.each(function(i){
								if (selTime.numTimeArr[i] != null){
									
									// clearing old selection
									$(this).find('li').removeClass('hover');
									$(this).find('span').removeClass('hover');
									
									// add new selection
									var liForSelect = selTime.numTimeArr[i]+1;
									
									
									// cheking out of range
									if ((liForSelect>$(this).find('li').length)){
										liForSelect = selTime.numTimeArr[i]+1-$(this).find('li').length;
									}
									
									// set time hovered
									$(this).find('li:nth-child('+(liForSelect)+')').addClass('hover');
									$(this).find('li:nth-child('+(liForSelect)+')').find('span').addClass('hover');
									
									// making next ol active									
									var nextOl = $(this).removeClass('active').show().next('ol');									
									nextOl.addClass('active').show();
								}
							});
							
							
							
							// reposition each ol
					        ols.each(function(i)
							{
					            prevOL = $(this).prevAll('ol:visible:first');

					            if (prevOL.get(0))
								{
					                prevLI = prevOL.find('li.hover, li:first').eq(0);
									
					                $(this).css({
					                    top: prevOL.position().top + prevLI.height() + dropslide.options.top + prevLI.position().top,
					                    left: prevLI.position().left + offset
					                });
					                offset = offset + prevLI.position().left + dropslide.options.left;
					            }
					            else if (i > 0 && dropslide.is3d()) {
					                $(this).css('margin-left', dropslide.options.left);
					            }
					        });							
							
		                }, this.options.showDelay);
						
						this.checkHideout();
						return;						
					}
                }
				
                setTimeout(function() {
                    ols.removeClass('active').eq(id).addClass('active').show();
                }, this.options.showDelay);
				
				this.checkHideout();
            }
        },
        // guess what it does
        showNextLevel: function() {
            if (this.is2d()) {
                this.wrapper.find('ol.active').removeClass('active').next('ol').addClass('active').show();
            }
            else {
                this.wrapper.find('ol.active').removeClass('active').find('li.hover > ol').addClass('active').show();
            }
			this.checkHideout();
        },
        getSelection: function() {
            return $.makeArray(this.wrapper.find('span.hover')
                .map(function() { return $(this).text(); }));
        },
        // show level 0 (shortcut)
        show: function(isSelTime, selTime) {
			if (isSelTime){
				this.showLevel(0, selTime);
			}else{
				this.showLevel(0);
			}
            
        },
        // hide all levels
        hide: function() {
            var self2 = this;
            setTimeout(function() {
                self2.wrapper.find('ol').hide();
            }, self2.options.hideDelay);
        },
        // determine dropdown type
        is2d: function() {
            return !this.is3d();
        },
        is3d: function() {
            return !!this.wrapper.find('ol > li > ol').get(0);
        },
        checkHideout: function() {
            var self2 = this;
            if(self2.hideid)
                clearTimeout(self2.hideid);
            self2.hideid = setTimeout(function() {
                self2.wrapper.find('ol').hide();
            }, self2.options.hideoutDelay);
        },
        dropHideout: function() {
            var self2 = this;
            if(self2.hideid)
                clearTimeout(self2.hideid);
            self2.hideid = null;
        }
    }));

    $.ui.dropslide.defaults = {
        // options
        tree: false,
        trigger: 'mouseover',
        top: 6,
        left: 0,
        showDelay: 0,
        hideDelay: 0,
		open: true,
        // events
        select: function() { },
        click: function(e, dropslide) {
            dropslide.hide();
        }
    };

    function onActivate(e) {
        var dropslide = getDropSlide(this);
		/*if(dropslide.options.open)
		{
			$ol = $('ol.active');
			dropslide.showLevel(0);
		}
		else*/
		dropslide.show();
    };

    function onLiMouseover(e) {
        var dropslide = getDropSlide(this);
		
        $(this).siblings()
            .find('ol').hide().end()
            .find('span.hover').andSelf().removeClass('hover');
        $(this).find('ol').show().end().children(0).andSelf().addClass('hover');

        dropslide.showNextLevel();
        dropslide.dropHideout();
    };

    function onMouseout(e) {
        var dropslide = getDropSlide(this);
        dropslide.checkHideout();
    };
    
    function onLiClick(e) {
		var dropslide = getDropSlide(this);
		        
        $(dropslide.element).triggerHandler('dropslideclick', [e, dropslide], dropslide.options.click);
        $(dropslide.element).triggerHandler('select', [e, dropslide], dropslide.options.select);
    };

    function onOlMousemove(e) {
		handleMove.call(this);
    }
	
	function handleMove()
	{
		var dropslide = getDropSlide(this);
        var prevLI = false;
        var prevOL = false;
        var nextOL = false;
        var pos = false;
        var offset = dropslide.element.position().left + dropslide.options.left;
		var offsetTop = dropslide.element.position().top + dropslide.options.top;
        var ols = dropslide.wrapper.find('ol');
		
        // reposition each ol
        ols.each(function(i)
		{
            prevOL = $(this).prevAll('ol:visible:first');
            if (prevOL.get(0))
			{
                prevLI = prevOL.find('li.hover, li:first').eq(0);
			
                $(this).css({
                    top: prevOL.position().top + prevLI.height() + dropslide.options.top + prevLI.position().top,
                    left: prevLI.position().left + offset
                });
                offset = offset + prevLI.position().left + dropslide.options.left;
            }
            else if (i > 0 && dropslide.is3d()) {
                $(this).css('margin-left', dropslide.options.left);
            }
        });
	}

    function getDropSlide(el) {
        return $(el).data('dropslide')
                || $(el).parents('.ui-dropslide').data('dropslide');
    };
})(jQuery);
