
var dropdownMenuItemOptions =
	{
	    select: undefined,
	    url: undefined,
	    name: undefined,
	    caption: undefined,
	    disabled: false,
	    submenu: undefined,
	    separator: false,
	    loader: undefined,
	    className: 'dropdownMenuItem',
	    element: undefined
	}

function dropdownMenuItem(menu, options) {
    if (options == undefined) options = dropdownMenuItemOptions;
    for (var option in dropdownMenuItemOptions)
        if (options[option] == undefined) options[option] = dropdownMenuItemOptions[option];

    this.menu = menu;

    this.disabledFunction = function(event) { event.stopPropagation(); return false; }

    if (options.element) {
        var element = options.element;
        this.name = $(element).attr('key');
    }
    else {
        var element = document.createElement('li');
        $(menu.element).append(element);
    }

    $(element).addClass(options.className);
    this.element = element;

    dropdownMenuItem.parentClass.constructor.call(this, element);

    if (options.separator) $(element).addClass('separator');

    if (!$(element).find("a").length) $(element).append('<a />');

    if (options.url) $(element).find("a").attr('href', options.url);

    if (options.name) this.name = options.name;
    if (options.caption) $(element).find("a").text(options.caption);

    if (options.disabled) this.disable();
 if ($(this.element).hasClass('selected')) {}
 else if (options.submenu) this.addSubmenu(options.submenu);

    var itemObject = this;

    if (options.loader) this.loader = options.loader;
    if (menu.isOpen()) this.load();

    $(this.element).mouseenter(function() { itemObject.select() });
    $(this.element).mouseleave(function() { itemObject.deselect() });
}

extend(dropdownMenuItem, control);

dropdownMenuItem.prototype.element;
dropdownMenuItem.prototype.menu;
dropdownMenuItem.prototype.name;
dropdownMenuItem.prototype.submenu;

dropdownMenuItem.prototype.getType = function() {
    return 'dropdownMenuItem';
}

dropdownMenuItem.prototype.isSelected = function() {
    return $(this.element).is('.current');
}

dropdownMenuItem.prototype.show = function() {
    //		dropdownMenuItem.parentClass.show.apply(this);
    this.load();
}

dropdownMenuItem.prototype.hide = function() { }

dropdownMenuItem.prototype.select = function() {
    if (!this.isSelected()) {
        var itemObject = this;

        var items = this.menu.listItems();
        for (var i = 0; i < items.length; i++)
            if (items[i] != this) items[i].deselect();

        $(this.element).addClass('current');
        if (this.submenu) setTimeout(function() { if (itemObject.isSelected()) itemObject.openSubmenu(); }, 50);
    }
}

dropdownMenuItem.prototype.deselect = function() {
    if (this.isSelected()) {
        var itemObject = this;
        if (this.submenu) {
            $(this.submenu.element).mouseenter(function() { $(itemObject.element).addClass('current'); })
            setTimeout(function() { if (!itemObject.isSelected()) itemObject.closeSubmenu(); }, 50);
        }
        $(this.element).removeClass('current');
    }
}

dropdownMenuItem.prototype.load = function() {
    if (this.loader && !$(this.element).is('.loading')) {
        $(this.element).addClass('loading');
        var itemObject = this;
        this.loader.call(itemObject);
    }
}

dropdownMenuItem.prototype.remove = function() {
    $(this.element).remove();
}

dropdownMenuItem.prototype.openSubmenu = function() {
    if (this.submenu) {
        var itemOffset = $(this.element).position();
        if ($(this.element).hasClass('leftItem')) {

            if (this.menu.isVertical()) {
                var offset = {
                    left: itemOffset.left + $(this.element).width() - 470,
                    top: itemOffset.top + 120
                }
            }
            else {
                var offset = {
                    left: itemOffset.left,
                    top: itemOffset.top + $(this.element).height()
                }
            }
            this.submenu.open(offset.left, offset.top);
        }
        else {
            if (this.menu.isVertical()) {
                var offset = {
                    left: itemOffset.left + $(this.element).width(),
                    top: itemOffset.top
                }
            }
            else {
                var offset = {
                    left: itemOffset.left,
                    top: itemOffset.top + $(this.element).height()
                }
            }
            this.submenu.open(offset.left, offset.top);
        }
    }
    //$('.lay1').pngFix();
}



dropdownMenuItem.prototype.addSubmenu = function(submenu) {

    if (submenu == undefined) submenu = new dropdownMenu({ align: 'vertical' });
    this.submenu = submenu;
    submenu.parentItem = this;
    $(this.element).addClass('hasSubmenu');

    return submenu;
}

dropdownMenuItem.prototype.removeSubmenu = function() {
    if (this.submenu) {
        this.closeSubmenu();
        $(this.submenu.element).remove();
        this.submenu = undefined;

        //$('.lay1').hide();
    }
    $(this.element).removeClass('hasSubmenu');
}

dropdownMenuItem.prototype.closeSubmenu = function() {
    if (this.submenu) this.submenu.close();
}

dropdownMenuItem.prototype.isDisabled = function() {
    return $(this.element).is('.disabled');
}

dropdownMenuItem.prototype.disable = function() {
    $(this.element)
			.addClass('disabled')
			.find("a").click(this.disabledFunction);
}

dropdownMenuItem.prototype.enable = function() {
    $(this.element)
			.removeClass('disabled')
			.find("a").unbind('click', this.disabledFunction);
}

dropdownMenuItem.prototype.bind = function(type, fn) {
    $(this.element).find("a").bind(type, { dropdownMenuItem: this }, fn);
}

dropdownMenuItem.prototype.unbind = function(type, fn) {
    $(this.element).find("a").unbind(type, fn);
}

var dropdownMenuOptions =
	{
	    element: undefined,
	    className: 'dropdownMenu',
	    itemOptions: dropdownMenuItemOptions,
	    align: undefined,
	    parentItem: undefined
	}

function dropdownMenu(options) {
    if (options == undefined) options = dropdownMenuOptions;
    for (var option in dropdownMenuOptions)
        if (options[option] == undefined) options[option] = dropdownMenuOptions[option];
    if (options.element) {
        var element = options.element;
        $(element).children('li').dropdownMenuItem(this, options.itemOptions);
    }
    else {
        var div2 = document.createElement('div');
   
           var div1 = document.createElement('div');
                            $(div1).addClass('lay1');  
        $(div2).addClass('lay2');
        var element = document.createElement('ul');
        div1.appendChild(div2);  
        div1.appendChild(element);      
        $(".top ul.menu").append(div1);
        $(div1).hide();
        $(div2).hide();
        $(element).hide();
    }

    this.element = element;
    
    this.parentItem = options.parentItem;

    $(element).addClass(options.className);

    if (options.align) $(element).addClass(options.align)

    dropdownMenu.parentClass.constructor.call(this, element);

    var menuObject = this;
    this.autoCloseFunction = function() {
        menuObject.close();
    }
}

extend(dropdownMenu, control);

dropdownMenu.prototype.element;
dropdownMenu.prototype.parentItem;

dropdownMenu.prototype.getType = function() {
    return 'dropdownMenu';
}

dropdownMenu.prototype.isVertical = function() {
    return $(this.element).is('.vertical');
}

dropdownMenu.prototype.isHorizontal = function() {
    return $(this.element).is('.horizontal');
}

dropdownMenu.prototype.addItem = function(options) {
    return new dropdownMenuItem(this, options);
}

dropdownMenu.prototype.addSeparator = function() {
    //return new dropdownMenuItem(this, { separator: true });
}

/**
*	@todo fix
*/
dropdownMenu.prototype.getDefaultItem = function() {
    var items = this.listItems();
    for (var i = 0; i < items.length; i++)
        if (items[i].isDefault) return items[i];
}

dropdownMenu.prototype.listItems = function() {
    var items = new Array();
    itemElements = $(this.element).find('li').get();
    for (var i = 0; i < itemElements.length; i++)
    {
        if (itemElements[i].control) items.push(itemElements[i].control);
    }
    return items;
}

dropdownMenu.prototype.eachItem = function(fn) {
    var items = this.listItems();
    for (var i = 0; i < items.length; i++)
        fn.call(items[i]);
}

dropdownMenu.prototype.open = function(left, top) {

    if (top != undefined) $(this.element).parent().css('top', top + 'px');
    if (left != undefined) $(this.element).parent().css('left', left + 'px');
    if (this.isHidden()) {
        this.show();
        $(this.element).parent().show();
        $(this.element).parent().find('.lay2').fadeIn(400);
        $(document).click(this.autoCloseFunction);
    }
}

dropdownMenu.prototype.isOpen = function() {
    return !$(this.element).parent().is(':hidden');
}

dropdownMenu.prototype.close = function() {

    //if (this.isOpen()) {
    //alert('TEST')
        if (this.parentItem) this.parentItem.deselect();
        this.eachItem(function() { this.deselect() });
        this.hide();
        $(this.element).parent().hide();

        //$(this.element).parent().find('.lay2').hide();
        $(document).unbind('click', this.autoCloseFunction);
        //this.close;
    //}
}

dropdownMenu.prototype.show = function() {
    dropdownMenu.parentClass.show.apply(this);
    this.eachItem(function() { this.show() });
            $(this.element).parent().find('.lay2').fadeIn(400);

}

dropdownMenu.prototype.hide = function() {
    this.eachItem(function() { this.hide() });
    dropdownMenu.parentClass.hide.apply(this);
}

jQuery.fn.dropdownMenu = function(options) {
    var elements = this.get();
    for (var i = 0; i < elements.length; i++) {
        options.element = elements[i];
        var menu = new dropdownMenu(options)
    }

    return menu; // ?
}

jQuery.fn.dropdownMenuItem = function(menu, options) {
    var elements = this.get();
    for (var i = 0; i < elements.length; i++) {
        options.element = elements[i];
        new dropdownMenuItem(menu, options)
    }
}

jQuery.fn.dropdownMenuClick = function(options) {
}

jQuery.fn.dropdownMenuOver = function(options) {
    var menu = new dropdownMenu(options)
    var elements = this.get();
    for (var i = 0; i < elements.length; i++) {
        var mouseenter = function(event) {
            setTimeout(function() { menu.open(event.clientX, event.clientY); }, 50);
        }

        var mouseleave = function() {
            //				menu.close();
        }

        $(elements[i]).mouseenter(mouseenter);
        $(elements[i]).mouseleave(mouseleave);
    }

    return menu;
}



// TEMP
/*
	
$(document).ready(function(){
function loader()
{
var item = this;

var complete = function(data)
{
loader.data = data;

var itemElements = $(data).find("[component='page.navigation.menu'] item[key='"+ item.menu.parentItem.name +"'] > list > item").get();

if(itemElements.length)
{
for(var i = 0; i < itemElements.length; i++)
{
var name = $(itemElements[i]).attr('key');
var link = $(itemElements[i]).children('link');
var url = link.attr('uri');

var options = {caption: link.text(), url: url};
item.menu.addItem(options);
}

item.remove();
}
else item.menu.parentItem.removeSubmenu();
}

var data = {
componentFilter: 'page.navigation.menu',
outputMimeType: 'application/xml',
'page.navigation.menu.items.submenus.display' : 1
};

if(loader.data) complete(loader.data);
else $.get('/', data, complete, 'xml')
}

var itemFunction = function() { this.addSubmenu().addItem({loader: loader}) }
var menu = $("ul.mainMenu");
if(menu.length)	menu.dropdownMenu({className: 'menu', itemOptions: {className: 'item'}}).eachItem(itemFunction);
})
*/
