$(function(){
//    inputValueToggle
    $(".toggleValue").focus(function(){if(this.value==this.defaultValue)this.value='';}).bind('blur',function(){ if(this.value=='')this.value=this.defaultValue;});

//    content trigger
    $('.trigger').click(function(e){
        e.preventDefault();
        $(this).toggleClass('toggled').siblings('.dest').toggle();
    })
    $('.dest .x').click(function(e){
        e.preventDefault();
        $(this).closest('.dest').toggle().siblings('.trigger').toggleClass('toggled');
    })

//    toggler

    var initialHeight = $('.menuRow').find('.target').eq(0).outerHeight(),
        initialPadding = parseInt($('.menuRow').find('.exp').eq(0).css('padding-bottom')),
        expandedPadding = initialPadding / 2,
        initialDecorationPos = Math.abs(parseInt($('.menuRow').find('.thelper').eq(0).css('bottom'))),
        initialTogglerPos = Math.abs(parseInt($('.menuRow').find('.toggler').eq(0).css('bottom'))),
        expandedTogglerPos = -16,
        initialTogglerZIndex = parseInt($('.menuRow').find('.thelper').eq(0).css('z-index')),
        expandedTogglerZIndex = 1,
        mainRowHeight = 312,
        speed = 150;
    function expand(el){
        var container = el.closest('.exp');
        container.closest('.menuItem').addClass('triggered');
        el.animate({height: el.find('.target-inner').outerHeight()}, speed, function(){
            container.addClass('triggered');
        })
        container.animate({paddingBottom: expandedPadding}, speed);
        container.closest('.cnt').siblings('.thelper').animate({bottom: -initialPadding / 2 +5}, speed, function(){
            $(this).css({'z-index': expandedTogglerZIndex});
        });
        container.find('.toggler').animate({bottom: expandedTogglerPos}, speed);
    }
    function collapse(el){
        var container = el.closest('.exp');
        container.closest('.menuItem').removeClass('triggered');
        el.stop().animate({height: initialHeight}, speed, function(){
            container.removeClass('triggered');
        })
        container.stop().animate({paddingBottom: initialPadding}, speed);
        container.closest('.cnt').siblings('.thelper').stop().animate({bottom: initialDecorationPos}, speed).css({'z-index': initialTogglerZIndex});
        container.find('.toggler').stop().animate({bottom: initialTogglerPos}, speed);
    }
    $('.target').css({height: initialHeight});
    $('.menuRowMain').css({height: mainRowHeight});
    $('.toggler').removeClass('triggered');
    $('.toggler').click(function(e){
        e.preventDefault();
        var target = $(this).siblings('.target');
        if (!target.closest('.exp').hasClass('triggered')) {
            expand(target);
        } else {
            collapse(target);
        }
    });
    $('.menuItem').mouseleave(function(){
        collapse($(this).find('.target'));
    })

	$('.cols').scroll(function(e){
        $(this).scrollTop(0).scrollLeft(0);
    });

})
