/* 
Javascript application for all pages

Author: Alexander aka TPETb Tretyakov
*/

$(document).ready(function(){
    // Wrap framed images
    $('.framed').each(function(){
        $(this).wrap('<div class="frame" />')
        .before('<div class="frame-a" />')
        .before('<div class="frame-b" />')
        .before('<div class="frame-c" />')
        .before('<div class="frame-d" />');
        $(this).closest('.frame').css({
            width: $(this).innerWidth(),
            height: $(this).innerHeight()
        });
    });
    // Add spans to header menu
    $('#page-header nav a').each(function(){
        $(this).html('<span>' + $(this).html() + '</span>');
    });
    // Make header navigation menu elements same width
    $('#page-header nav a span').each(function(){
        if ($(this).width() > parseInt($(this).css('min-width'))) {
            $(this).closest('a').addClass('long');
        }
    });
    // 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');
    });
    // Because of header element, odd/even count starts wrong
    $('#staff-list article:odd').addClass('even');
    $('#staff-list article:even').addClass('odd');
    // Custom select
    $('select').customStyle();
    // Custom checkbox
    $('input[type="checkbox"], input[type="radio"]').ezMark();
    // ie suport for input type in css
    $('input[type="text"]').addClass('text');
    // Focus support for inputs
    $('input, textarea').focus(function(){
        $(this).addClass('focus');
    });
    $('input, textarea').blur(function(){
        $(this).removeClass('focus');
    });
    // Add span to buttons
    $('.button').each(function(){
        $(this).html('<span>'+$(this).html()+'</span>');
    });

    // Init megamenu
    $('.megamenu li > div').each(function(){
        $(this).find('ul').each(function(){
            if ($(this).find('li').length > 4) {
                $(this).addClass('double');
            }
            if ($(this).find('li').length > 8) {
                $(this).addClass('triple');
            }
        });
    });
    $('.megamenu').megamenu({
        boundary: $('#container')
    });

    // Click event for disabled buttons
    $('a.disabled').click(function(){
        return false;
    })
    // Tips
    $('.tip').focus(function(){
        if ($(this).hasClass('tip')) {
            $(this).val('');
            $(this).removeClass('tip');
        }
    }).blur(function(){
        if (!$(this).val()) {
            $(this).val($(this).attr('alt'));
            $(this).addClass('tip');
        }
    });
    // Latest in blog
    if ($('#latest-from-blog')) {
        blogLatest.init($('#latest-from-blog .slides article'), $('#latest-from-blog .slide-controls li a'), $('#latest-from-blog .slide-controls .prev-slide'), $('#latest-from-blog .slide-controls .next-slide'));
    }
    // Form submits
    $('.submit').click(function(){
        $(this).closest('form').find('.tip').val('');
        $(this).closest('form').submit();
        $(this).closest('form').find('.tip').each(function(){
            $(this).val($(this).attr('alt'));
        });
        return false;
    });
    // Forms validation
    $('form').each(function(){
        $(this).validate({
            invalidHandler: function(form, validator) {},
            errorPlacement: function() {},
            focusInvalid: false,
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    success: function(responseValue) {
                        if (responseValue == 'Success') {
                            $(form).find('.message').html('We have received your message<br/>Thank you!');
                            $(form).unbind().bind('submit', function(){
                                return false;
                            });
                            $(form).find('input, textarea').attr('readonly', true);

                            $(form).find('.content-success').show();
                            $(form).find('.content-form').hide();
                        } else {
                            $(form).find('.content-error').show();
                            $(form).find('.content-form').hide();
                        }
                    }
                });
            }
        });
    });
    // Full width elements
    $('.full-width').each(function(){
        $(this).css({
            'left': -$(this).offset().left + 'px',
            'padding-left': $(this).offset().left + 'px',
            'padding-right': $(window).width() - $(this).width() - $(this).offset().left + 'px'
        });
    });
});
