var lightBoxStyle = {
    overlaySpeed: 100,
    centered: false,
    closeClick: false,
    closeOutside: false,
    closeSelector: '.close_btn',
    overlayCSS: {background: '#EEEEEF', opacity: 1}
};

$(function() {
    
    if (!/(\.se\/|index\.php)$/.test(String(window.location))) return;
    
    // show
    var $s = $('#campaign').lightbox_me(lightBoxStyle);
    
    // bind
    $('.intro, .start', $s).click(function(){
        var $t = $(this);
        if ($t.hasClass('intro')) {
            $t.removeClass('intro').addClass('start');
        } else if ($t.hasClass('start')) {
            goto_form();
        }
    });
    $('.logo-link, .num1 .read-link, .num2 .read-link', $s).click(function(event){
        var $t = $('.content', $s);
        if (!$t.hasClass('start')) {
            $('.tabs', $s).removeClass('tabs-form');
            $t.removeClass('form intro').addClass('start');
            $(window).unbind('keypress');
            event.stopPropagation();
        }
    });
    $('.webshop', $s).click(function(){
        window.location = 'http://www.sjohav.se/produkter-c-1.html';
    });
    $('.order', $s).click(goto_form);
    $('.send', $s).click(submit_form);
    
    // automatic switch
    window.setTimeout(function(){
        if ($('.content', $s).hasClass('intro')) $('.content', $s).click();
    }, 10*1000);
    
    // background
    function switch_bg (){
        var r = Math.round(Math.random()*5) + 1;
        $s.removeClass().addClass('bg'+r);
    }
    //window.setInterval(switch_bg, 7*1000);
    switch_bg();
    
    // form
    function goto_form () {
        if ($('.content', $s).hasClass('form')) return;
        $('.content', $s).removeClass('intro start').addClass('form');
        $('.tabs', $s).addClass('tabs-form');
        $(window).keypress(function (event) {
            if (event.keyCode == '13') {
                submit_form();
                event.preventDefault();
            }
        });
    }
    function submit_form () {
        var v, err = [];
        
        // num?
        var n1 = $('.num1 input', $s).val();
        if ($.trim(n1) && !/^[1-9][0-9]*$/i.test(String(n1))) err.push('Felaktigt antal 3-pack.');
        var n2 = $('.num2 input', $s).val();
        if ($.trim(n2) && !/^[1-9][0-9]*$/i.test(String(n2))) err.push('Felaktigt antal 4-pack.');
        if (!n1 && !n2) err.push('Inga produkter beställda.');
        
        // name
        v = $('.name input', $s).val();
        if (!$.trim(v)) err.push('Företag/namn saknas.');
        
        // address
        v = $('.ship_addr1 input', $s).val();
        if (!$.trim(v)) err.push('Leveransadress saknas.');
        v = $('.ship_addr3 input', $s).val();
        if (!$.trim(v)) err.push('Postnummer och ort saknas.');
        
        // validate email
        v = $('.email input', $s).val();
        if ($.trim(v) && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(String(v))) err.push('Felaktig e-postadress.');
        
        if (err.length) {
            alert('Följande fel uppstod:\n - ' + err.join('\n - '));
        } else {
            var d = {};
            $('.form input', $s).each(function(){
                d[this.name] = this.value;
            });
            $.post('/campaign-post.php', d, function() {
            });
            $('.tabs', $s).removeClass().addClass('tabs tabs-thanks');
            $('.content', $s).removeClass('form').addClass('thanks');
        }
    }
    
});