﻿liteweb.AppendInit(function() {
var dur = 300;
	var easing = "easeOutQuad";

	$(".topmenu>li").each(function(i, el) {
		el.menu = createMenu(el, null, i);
	});

	function createMenu(el, parent) {
		var m = {};
		m.el = $(el);
		m._a = $(el).children("a");

		m.div = $(el).children("div.topmenu-sub");
		m._ol = m.div.children("ol");
		if (m._a.length == 0 || m._ol.length == 0)
			return;

		var span = m._a.children("span");
		/*if (span.length > 0)
		this._a = span;
		*/
		m.a = m._a[0];
		m.ol = m._ol[0];

		m.li = m._ol.children("li");
		//alert();
		m.maxHeight = m._ol.outerHeight() + 0; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);

		m.visible = false;
		m.hideMe = false;
		m.parent = parent;
		m.a.menu = m;
		m.div[0].menu = m;

		m.show = function() {
			this.hideMe = false;
			if (this.visible)
				return;
			this.visible = true;

			this.div.css({
				overflow: "visible",
				visibility: "visible",
				height: 0,
				top: this.parent === null ?
				27 :
				this._a.offset().top - $(this.a.parentNode.parentNode).offset().top - 7,
				left: this.parent === null ?
					this._a.offset().left - $(this.a.parentNode.parentNode).offset().left -1 :
					this._a.outerWidth() + 2,
				width: this.div.width() < this._a.width()? this._a.width() - 0: this.div.width()
			});
			this.div.stop();
			var animateTo = { height: this.maxHeight };
			if (!$.browser.msie)
				$.extend(animateTo, { opacity: 1 });
			this.div.animate(animateTo, { duration: dur, easing: easing });
			this._a.addClass("_hover");
		};
		m.hide = function() {
			this.hideMe = true;
			window.setTimeout(function() {
				m._hide();
			}, 50);
		}
		m._hide = function() {
			if (!this.hideMe)
				return;
			this.visible = false;
			for (var i = 0; i < this.children.length; i++) {
				if (this.children[i].visible)
					return;
			}
			this.div.stop();
			var animateTo = { height: 0 };
			if (!$.browser.msie)
				$.extend(animateTo, { opacity: 0 });

			this.div.animate(animateTo, { duration: dur / 2, easing: easing,
				complete: function() {
					if (!this.menu.hideMe)
						return;
					this.menu.div.css("visibility", "hidden");
					this.menu._a.removeClass("_hover");
				}
			});
		}
		m.children = [];
		m.li.each(function(i, el) {
			m.children.push(el);
			el.menu = createMenu(el, this);
			var $a = $($(this).children("a"));
			if($(el).children("div.topmenu-sub").length == 0)
				$a.addClass("arrow1");
		});
		if (m.children.length > 0 && m.parent != null) {
			m._a.addClass("arrow");
		}
		m._a.bind("mouseover", function() {
			if (this.menu) {
				this.menu.show();
			}
		});
		m._a.bind("mouseout", function() {
			if (this.menu)
				this.menu.hide();
		});
		m.div.bind("mouseover", function() {
			if (this.menu)
				this.menu.show();
		});
		m.div.bind("mouseout", function() {
			if (this.menu)
				this.menu.hide();
		});
		return m;
	}
	function hideMenu(el) {
		window.setTimeout(function() {
			_hideMenu(el);
		}, 200);
	}
	function _hideMenu(el) {
		if (el.childVisible)
			return;
		alert(el);
		el._sub.css("display", "none");
		el._ol.css("height", 0)
		el._a.removeClass("_hover");
	}
});

liteweb.AppendInit(function() {

 $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
							removeFilter($(this)[0]);
						});
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

});

function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

