$(document).ready(function() {
    
    $("a.window-box").click(window_box);
    set_current_page();
    
    if($("form#comment").length != 0) {
        
        $("form#comment").submit(function() {
            
            if($("#person").val() === "NAMN (obligatoriskt)") {
                return false;
            };

            if($("#message").val() === "SKRIV EN KOMMENTAR...") {
                return false;
            };
            
        });
        
        form_place_holders();
        validate_comment_form();
    }
    
    $('a#facebook-tab-holder').toggle(function(event) {
        open_social_tab();
    }, function() {
        close_social_tab();
    });

});

function window_box(event) {
    
    event.preventDefault();
    var url = this.href;
    
    $("#content").append('<div id="dialog"></div>');
    
    $.get(url, function(data) {
        
        var content = $(data).find("#window-box");
        var window_box = $("#dialog").dialog({minWidth: 770, minHeight: 600, maxWidth: 770, maxHeight: 600, position: ['center', 50], modal: true, draggable: false, dialogClass: 'window-box', resizable: false, closeOnEscape: true}).html(content);
        
        $("#window-box").prepend('<a id="close-window" href="/" title="Stäng fönster"><img src="/images/layout/close-window.png" alt="Stäng fönster" /></a>');
        $("#close-window").click(function(event) {
            event.preventDefault();
            window_box.dialog("destroy").remove();
        });
        
    });    
    
}

function set_current_page() {
    
    var current_page = window.location;
    
    var link = $('a[href="'+current_page+'"]');
    link.css("text-decoration", "underline");
    
}

function form_place_holders() {
    
    var place_holder = {
        "person": "NAMN (obligatoriskt)",
        "email": "E-POST",
        "url": "HEMSIDA",
        "message": "SKRIV EN KOMMENTAR..."
    };
    
    $("#person").val(place_holder.person);
    $("#email").val(place_holder.email);
    $("#url").val(place_holder.url);
    $("#message").val(place_holder.message);
    
    $(".comment-field").focus(function(){
        $(this).val("").addClass("active");
    });
    
    $(".comment-field").blur(function(){
        
        var field = $(this);
        
        if(field.val() == "") {
            var id = field.attr("id");
            field.val(place_holder[id]).removeClass("active"); 
        }
        
    });
    
}


function validate_comment_form() {
    
    $("form#comment").validate({
        rules: {
            name: {
                required: true,
                minlength: 3
            },
            email: {
                required: true
            },
            comment: {
                required: true
            },
            captcha: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            name: {
                required: "Vänligen fyll i ditt namn"
            },
            email: {
                required: "Vänligen fyll i en e-post adress"
            },
            comment: {
                required: "Glöm inte att skriva något"
            },
            captcha: {
                required: "Skriv något så vi vet att du inte är en bot"
            }
        },
        errorPlacement: function(error, element) { 
            error.insertAfter(element.prev());
        }
    });    
}


function open_social_tab() {
    //$('div#facebook').css({'margin': '0'});
    $('div#facebook').animate({
        'margin-right': 0
    });
}

function close_social_tab() {
    $('div#facebook').animate({
        'margin-right': -300
    }, 'fast');
}


