/**
 * Main script.
 * Initialize hover effect on <tr>
 * Confirmation window when clicking on link with class .icon-delete.
 *
 * @author Nikita Vershinin <me@endeveit.net>
 */
(function($) {
    $(document).ready(function() {
        // Labels grabbed from current page
        document.localizedLabels = {};
        // Messages grabbed from current page
        document.localizedMessages = {};
        // Errors grabbed from current page
        document.localizedErrors = {};
        
        var min_sub_width = 150;
        
        $('#triggers a img').click(function() {
            scrollTo(0, 0);    
        });
		
        //if ie
		changelast = true;
		if($.browser.msie){
			userAgent = $.browser.version;
			userAgent = userAgent.substring(0,userAgent.indexOf('.')); 
			if (userAgent == 7){
				changelast = false;
			}
		}
        if (changelast) $('#menu-items ul li:last-child').addClass('last');
		//
        $('#menu-items td').hover(
            function() {
                if ($(this).children('.sub-items').width() < $(this).outerWidth()) {
                    $(this).children('.sub-items').width($(this).outerWidth() + 5);
                }
                
                $(this).addClass('active')
                    .children('div').addClass('active')
                    .children('a').addClass('active');
                
                $(this).children('.sub-items').animate(
                    {
                        top: $(this).position().top + $(this).outerHeight(),
                        left: $(this).position().left
                    }, 0
                );
                $(this).children('.sub-items').slideDown("fast");
            },
            function() {
                $(this).removeClass("active")
                    .children('div').removeClass('active')
                    .children('a').removeClass('active');
                
                $(this).children('.sub-items').stop(true, true);
                $(this).children('.sub-items').slideUp('slow');
            }
        );
        
        $('.sub-items li').hover(
            function() {
                if ($(this).children('.sub-items').width() < min_sub_width) {
                    $(this).children('.sub-items').width(min_sub_width);
                }
                $(this).children('.sub-items').stop(true, true);
                $(this).children('.sub-items').animate({
                        top: $(this).position().top,
                        left: $(this).position().left + $(this).outerWidth()
                    }, 0
                );

                $(this).children('.sub-items').slideDown("fast");
            },
            function() {
                $(this).children('.sub-items').slideUp('fast');
            }
        );

        // Init messages and errors
        $('#localized-messages span').each(function () {
            var t = $(this).attr('id').split('-');
            type = t.splice(0, 1);
            name = t.join('-');
            if (type == 'label') {
                document.localizedLabels[name] = $(this).text();
            } else if (type == 'message') {
                document.localizedMessages[name] = $(this).text();
            } else {
                document.localizedErrors[name] = $(this).text();
            }
        });


    })
})(jQuery);
