/* Author: 
Alexander aka TPETb Tretyakov
*/

$(document).ready(function(){
    // 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');
    });
    // Sliding buttons
    $('#sub-menu a').addClass('sliding');
    $('.button').addClass('sliding');
    $('.sliding').each(function(){
        $(this).html('<span class="slider-a"><span class="slider-b"><span class="t"></span><span class="content">'+$(this).html()+'</span></span></span>');
    });
    // Rounded corners
    $('.rounded').each(function(){
        $(this).find('script').remove();
        $(this).wrapInner('<div class="wrapper-b">').wrapInner('<div class="wrapper-a">');
        $(this).find('.wrapper-b').prepend('<div class="t">').append('<div class="clearfix">');
        $(this).append('<div class="b"><div></div></div>');
    });
    // Homepage slideshow
    slideShow.init($('.slides div.enabled'), $('.controls div.enabled'));
    // Lightbox
    $('article').each(function(){
        $(this).find('a[rel="lightbox"]').lightBox();
    });
    // Custom select
    $('select').customStyle();
    // Custom checkbox
    $('input[type="checkbox"], input[type="radio"]').ezMark();
    // 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');
        }
    });
    // 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;
    });
    // Validate forms
    jQuery.validator.addMethod('swissMobile', function(value, element){
        return value.match(/\+417[0-9]{8}/);
    });
    $('form').validate({
        invalidHandler: function(form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                $(form.target).find('.error-message').show();
            } else {
                $(form.target).find('.error-message').hide();
            }
        },
        errorPlacement: function(){},
        submitHandler: function(form) {
            if ($(form).hasClass('ajax')) {
                $(form).find('.error-message').hide();
                $(form).ajaxSubmit({
                    success: function(responseValue) {
                        $(form).find('.confirmation').css({visibility: 'visible'});
                    }
                });
                return false;
            } else {
                form.submit();
            }
        },
        rules: {
            'password-confirm': {
                equalTo: '#password'
            },
            'email-confirm': {
                equalTo: '#email'
            }
        }
    });
    $('#mobile').keyup(function(){
        $(this).val(('+' + $(this).val().replace(/\D/g, '').substr(0, 12)));
    });
    // Add modal box
    $('a.modal').click(function(){
        $($(this).attr('href')).dialog({
            width: 575,
            modal: true
        });
        return false;
    });
    // Add click to slide controls
    $('#banner.slideshow .controls div.enabled').click(function(){
        location.href = $(this).find('a').attr('href');
    });
    // Add blank target
    $('footer nav.legal a, a.blank').attr('target', '_blank');
});

var slideShow = {
    slides: null,
    controls: null,
    active: null,
    autoSwitch: null,
    fadeTime: 300,
    init: function (slides, controls) {
        this.slides = slides;
        this.controls = controls;
        this.active = 0;
        this.bindMouseEvents();
        this.initAutoSwitch(1);
        this.changeSlide(0);
        if ($('html').hasClass('ie7')) {
            this.fadeTime = 0;
        }
    },
    changeSlide: function (index, noAutoSwitch) {
        clearTimeout(this.autoSwitch);
        this.slides.stop(true, true);
        var self = this;
        $(this.slides[this.active]).fadeOut(0, function(){
            $(self.slides[index]).fadeIn(self.fadeTime);
        });
        $(this.controls[this.active]).removeClass('active');
        $(this.controls[index]).addClass('active');
        this.active = index;
        if (!noAutoSwitch) {
            if (index + 1 == this.slides.length) {
                this.initAutoSwitch(0);
            } else {
                this.initAutoSwitch(index + 1);
            }
        }
    },
    initAutoSwitch: function (index) {
        var self = this;
        this.autoSwitch = setTimeout(function(){
            self.changeSlide(index);
        }, 12000);
    },
    bindMouseEvents: function () {
        var self = this;
        $(this.controls).each(function(index){
            $(this).hover(function(){
                self.changeSlide(index, true);
            }, function(){
                if (index + 1 == self.slides.length) {
                    self.initAutoSwitch(0);
                } else {
                    self.initAutoSwitch(index + 1);
                }
            });
        });
    }
};
