$(function() {
    var element1 = '.slidingTabs .tab .inner';
    var element2 = '.slidingButtons li.level1';
    var element3 = '.slidingButtons ul li';
    var speed = 500;
    var buttonHeight = 40;
    var buttonOffset = 20;
    var parentHeight;
    var height;
    
    $.fn.slide = function(options) {
	    // default options
	options = $.extend({
            speed: 500
        }
        ,options || {});
	    
	    // stop running animation
        $(this).addClass('open');
        
        $(this).stop().animate(options, function() {
            if (!$(this).hasClass('mouseenter')) $(this).removeClass('open');
	},
        function() {
            // $(element).corner($(element).css('border-radius'));
            if (!$(this).hasClass('mouseenter')) $(this).removeClass('open');
        });
    };
    
        /* init effects */
    $(element1).hover(function() {
        $(this).slide({height: $(this).children('.tabContent').outerHeight()});
	$(this).addClass('mouseenter');
    },
    function() {
	    // stop running animation
        $(this).slide({height: '212px'});
	$(this).removeClass('mouseenter');
    });
    
    $(element2).hover(function() {
        if (!$(this).hasClass('mouseenter')) {
                // define height
            var height = $(this).children('ul').outerHeight() + buttonHeight;
            parentHeight = height;
            parentTop = (height - buttonHeight)/2;
            
            if (height > buttonHeight) {
                $(this).slide({height: height, top: -parentTop});
                $(this).addClass('mouseenter');
            }
        }
    },
    function() {
        if ($(this).hasClass('mouseenter')) {
                // stop running animation
            $(this).slide({height: buttonHeight, top: 0});
            $(this).removeClass('mouseenter');
        }
    });
    
    $(element3).hover(function() {
        if (!$(this).hasClass('mouseenter') && $(this).parent().parent().hasClass('mouseenter')) {
                // define values
            var imgHeight = $(this).children('.linkOuter').find('img').outerHeight();
            var height = $(this).children('a').outerHeight() + imgHeight;
                
            if (imgHeight > 0) {
                $(this).slide({height: height});
                $(this).parent().parent().slide({height: parentHeight + imgHeight, top: -(parentTop + (imgHeight/2)), queue: true});
                $(this).addClass('mouseenter');
            }
       }
    },
    function() {
            // stop running animation
        if ($(this).hasClass('mouseenter')) {
            $(this).slide({height: 21});
            $(this).parent().parent().slide({height: parentHeight, top: -parentTop});
            $(this).removeClass('mouseenter');
        }
    });
});
