$(document).ready(function(){
    // Add outbound links tracking
    $('a.outbound').click(function(){
        recordOutboundLink(this, 'Outbound Links', $(this).attr('href'));
        return false;
    });
    // target="_blank" support for links
    $('a.blank').attr('target', '_blank');
    // Add first and last classes to appropriate li
    $('li:first-child').addClass('first');
    $('li:last-child').addClass('last');
    // Add odd and even to table rows
    $('table').each(function(){
        $(this).find('tr:odd').addClass('odd');
        $(this).find('tr:even').addClass('even');
    });
    // Process subsites listing
    $('#subsites-list .item').bind('click', function(){
        location.href = $(this).find('a').attr('href');
    });
    $('#subsites-list .item').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
    // Add dropdowns handler
    $('.dropdown.hover').hover(function(){
        $(this).addClass('active');
        $(this).find('.dropdown-content').show();
    }, function(){
        $(this).removeClass('active');
        $(this).find('.dropdown-content').hide();
    });
    $('.dropdown.click .dropdown-title').click(function(){
        $(this).parents('.dropdown').toggleClass('active');
        $(this).parents('.dropdown').find('.dropdown-content').toggle();
    });
    $('.dropdown.click .close').click(function(){
        $(this).parents('.dropdown').removeClass('active');
        $(this).parents('.dropdown-content').hide();
    });
    // Preload hover images for dropdowns
    $('.dropdown').addClass('active');
    $('.dropdown-content').show(1, function(){
        $('.dropdown-content').hide();
        $('.dropdown').removeClass('active');
    });
    // Add hovers for language switch
    $('#language ul li').hover(function(){
        $(this).addClass('hover');
    }, function(){
        $(this).removeClass('hover');
    });
    // Add click event to li of language switch
    $('#language ul li').bind('click', function(){
        location.href = $(this).find('a').attr('href');
    });
    // Add captions to images inside content
    $('#content .main-column p img[alt]').each(function(){
        var caption = $('<span class="caption">'+$(this).attr('alt')+'</span>').width($(this).width()).addClass($(this).attr('class'));
        $(this).after(caption);
        // If images wasn't cached, we will need to fix caption size AFTER images is loaded (assuming no width attribute was set)
        $(this).load(function(){
            $(this).next('span.caption').width($(this).width());
        });
    });
    // Add miscellaneous last classes
    $('.employee').last().addClass('last');
    $('.sidebar-column .item').last().addClass('last');
    // Turn on scrollable
    $('.container').scrollable({
        size: 3
    });
    // Add classes to inputs
    $(':input').each(function(){
        $(this).addClass($(this).attr('type'));
    });
    // Add validator to form
    $('#contact-form').validate({
        highlight: function(element){
            $(element).addClass('error');
            $('#error').show();
        },
        unhighlight: function(element){
            $(element).removeClass('error');
            if ($('#contact-form :input.error').length == 0) $('#error').hide();
        },
        errorPlacement: function(){},
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                success: function(responseValue) {
                    $(form).find('.result').html(responseValue).show();
                    $(form).find('.submit').hide();
                }
            });
        }
    });
    $('#signup-form').validate({
        highlight: function(element){
            $(element).addClass('error');
        },
        unhighlight: function(element){
            $(element).removeClass('error');
        },
        errorPlacement: function(){},
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                success: function(responseValue) {
                    $(form).children().hide();
                    if (/.*ERROR.*/.test(responseValue)) {
                        $(form).find('.error-message').show();
                    } else {
                        $(form).find('.success-message').show();
                    }
                }
            });
        }
    });
    $('form .submit').click(function(){
        $(this).parents('form').submit();
        return false;
    });
    // Wrap mainpart block to create shadow
    $('.mainpart-block').wrap('<div class="mainpart-block-wrapper">');
    // Add removing of hints on focus for signup form
    $('#signup-form :input').each(function(){
        var backup = $(this).attr('value');
        $(this).focus(function(){
            if ($(this).attr('value') == backup) {
                $(this).attr('value', '');
            }
        }).focusout(function(){
            if ($(this).attr('value') == '') {
                $(this).attr('value', backup);
            }
        });
    });

    // Init support of youtube lightbox
    $('a.video').fancybox({
        overlayShow: true,
        frameWidth:640,
        frameHeight:360
    });
});
