$(document).ready(function() {

    if ($('.periodinput').length > 0 && typeof $(".periodinput").datepicker == 'function') {
        $.datepicker.setDefaults($.datepicker.regional['et']);

        $(".periodinput").datepicker({
            dateFormat: 'dd.mm.yy',
            firstDay: 1
        });
    }

    $('#newsletter_email').focus(function() {
        $(this).val('');
    })

    $(".newsletterbox button").click(function() {
        email = $('#newsletter_email').val();

        $.getJSON('json.php?controller=jsonservices&action=addnewsletter', {email: email}, function(json) {
            if (json.status == 'ok') {
                if (json.result == 'success') {
                    $('.newsletterbox .success em').text('Klar!');
                    $('.newsletterbox .success span').html('Tack för att ni!');

                } else {
                    $('.newsletterbox .success em').text('Fail!');
                    $('.newsletterbox .success span').html(json.result);
                }

                $(".newsletterbox form").fadeOut(100, function() {
                    $(".newsletterbox .success").fadeIn();
                })
            }
        });

        return false;
    });

    $(".newsletterbox .action-link").click(function() {
        email = $('#newsletter_email').val();

        $.getJSON('json.php?controller=jsonservices&action=removenewsletter', {email: email}, function(json) {
            if (json.status == 'ok') {
                if (json.result == 'success') {
                    $('.newsletterbox .success em').text('Klar!');
                    $('.newsletterbox .success span').html('E-mail removed!');

                } else {
                    $('.newsletterbox .success em').text('Fail!');
                    $('.newsletterbox .success span').html(json.result);
                }

                $(".newsletterbox form").fadeOut(100, function() {
                    $(".newsletterbox .success").fadeIn();
                })
            }
        });

        return false;
    });

    $(".newsletterbox .close").click(function() {
        $(".newsletterbox .success").fadeOut(100, function() {
            $(".newsletterbox form").fadeIn();
        });
    });


    $(".print").click(function() {
        window.print();
        return false;
    });


    $('.social .mail').click(function() {
        $.getJSON('json.php?controller=jsonservices&action=template', {template: 'sendfriend'}, function(json) {
            if (json.status == 'ok') {
                $.fn.colorbox({
                    html: json.html,
                    overlayClose:false,
                    close:"",
                    opacity:0.7,
                    transition:"elastic"
                });

                $('form#sendfriend').live('submit', sendfriend);
            }
        });
    })

    sendfriend = function() {
        $.post('json.php?controller=jsonservices&action=sendfriend', $('form#sendfriend').serialize(), function(json) {
            if (json.status == 'ok') {
                $.fn.colorbox.close();

                $.getJSON('json.php?controller=jsonservices&action=template', {template: 'friendsent'}, function(json) {
                    $.fn.colorbox({
                        html: json.html,
                        overlayClose:false,
                        close:"",
                        opacity:0.7,
                        transition:"none"
                    });
                });

            } else {
                box = $('form#sendfriend .msg-error');
                box.removeClass('hidden');
                box.find('h3').html(json.error);
                box.find('p').html(json.fields);
                $("#cboxLoadedContent").css("overflow", "hidden");
                $.fn.colorbox.settings.transition = 'none';
                $.fn.colorbox.resize();
            }
        }, 'json');

        return false;
    }


});
