﻿$(document).ready(function() {
    
    $('a.colorbox').colorbox({transition: "none", iframe: true, scrolling: false, preloading: false, href: "/review/create", width: 550, height: 650});
    
    $('form.ajax').submit(function() {
        var $inputs = $('input[type="text"], textarea');
        var button = $(this).find('input[type=submit]');
        button.attr('disabled', 'true');
        
        $('form.ajax span.status').remove();

        var action = $(this).attr('action');
        var formData = $(this).serialize();

        $.ajax({
            type: 'POST',
            url: action,
            data: formData,
            success: function(data) {
                var result = $.parseJSON(data);
                if (result.FormSuccess == 0) {
                    delete result.FormSuccess;
                    $inputs.attr('style','');
                    $.each(result, function(k, v) {
                        $("#"+k).css('border-width', '1px');
                        $("#"+k).css('border-color', '#cc0000');
                    });
                }
                else {
                    $inputs.val('');
                    $inputs.attr('style', ''); 
                    $('<span class="status">Your comment was received and will be moderated shortly</span>').insertAfter($('form.ajax input[type="submit"]'));
                    $('form.ajax span.status').fadeIn(300);
                }
                button.removeAttr('disabled');
            },
            error: function(jqXHR, textStatus, errorThrown) { 
                button.removeAttr('disabled');
                $('<span class="status">There was an error processing your request.</span>').insertAfter($('form.ajax input[type="submit"]'));
                $('form.ajax span.status').fadeIn(300);
            },
            dataType: 'html'
        });

        return false;
    });

    $('div#lead-mask input[type=radio]').click(function() {
        var $div = $('div.question');
        $div.animate({ left: '-=300' }, 400);
    });

    $('div.article, section#featured ul li').click(function(e) {
        e.preventDefault();
        window.location = $(this).attr("url");
    });

    $('a.vote').click(function() {
        var postId = $(this).attr('postId');
        $.ajax({
            type: 'POST',
            url: '/api/like/' + postId,
            dataType: 'json',
            success: function(data) {
                if (data.result == 1) {
                    var $result = $('span.result');
                    var count = parseInt($result.html()) + 1;
                    $result.html(count);
                }
            }
        });
    });

});
