﻿///<reference path="jquery-1.3.2.min.js" />

/*
*$Author: liwenzha $
*$Date: 2010-02-24 16:31:11 +0000 (Wed, 24 Feb 2010) $
*$Revision: 19 $
*/

$(document).ready(function() {

    DoAjaxCommentPost();
    DoAjaxNewsletterPost($('#rhs_newsletter_submit'));
    DoAjaxNewsletterPost($('.enternow'));

    $(document).ajaxStart(function() { $('.ajaxIndicator').show(); }).ajaxStop(function() { $('.ajaxIndicator').hide(); });

    $('.headlines').cycle({ fx: 'fade', speed: 2000, timeout: 5000, prev: '.prev-next .prev', next: '.prev-next .next', pause: 1 });

    $('.prev-next .open').click(function() {
        $('.headlines').cycle('pause');
        return false;
    })

    $('.jsScrollPanel').jScrollPane({ scrollbarWidth: 7, scrollbarMargin: 5 });

    $('div.nav ul li').hover(
        function() {
            $(this).children('ul').fadeIn('fast');
        },
        function() {
            $(this).children('ul').hide(0);
        }
    );

    $('div.nav ul li ul').hover(
        function() {
            $(this).parent().addClass('js_active');
        },
        function() {
            $(this).parent().removeClass('js_active');
        }
    );

    $('#newsletter-signup').click(function() {
        $('#newsletter-pop').slideToggle('fast');
        return false;
    });

    $('.closeSignup').click(function() {
        $('#newsletter-pop').slideUp('fast');
        return false;
    });

    $('.clearOnFocus').bind('focus', function() {
        $(this).attr('value', '');
    });

    $('.navbar .holder').tabs();
    $('.ArchiveMonthly').tabs();
    $('.ArchiveYearly').tabs();


    $('#videoSlide').before('<div id="nav">').cycle({ fx: 'fade', speed: 'fast', timeout: 0, pager: '#nav' });
    $('.txt-info-box-answer').find('p:eq(1)').prepend("<span class='answerDropCap'>A:</span>");
    $(".diff-last").find("li:last").css("border", "medium none").css("background", "transparent none repeat scroll 0 0");
});

function DoAjaxCommentPost() {
    $("#CommentSubmit").click(function() {
        var $form = $(this).closest('form');

        $.ajax({
            type: "POST",
            url: $form.attr('action'),
            data: $form.serialize(),
            error: function(xhr, status, error) {
                $form.parent().parent().before("<span class='validation-summary-errors'>An error has occured, please refresh the page and try again.</span>");
            },
            success: function(response) {
                ProcessCommentResponse(response);
                DoAjaxCommentPost();
            }
        });

        return false;
    });
}

function ProcessCommentResponse(response) {
    $('#ajaxCommentsReplacement').html($(response).find('#ajaxCommentsReplacement').html());
    var $validationError = $(response).find('.validation-summary-errors');
    // comment posted successfully
    if ($validationError.length == 0) {
        $.scrollTo('.inner-comment-box:last');
        $('.inner-comment-box:last').effect("highlight", { color: "#ffff00" }, 4000);
        // error returned, hightlight error.
    } else {
        $.scrollTo('.validation-summary-errors');
        $('.validation-summary-errors').effect("highlight", { color: "#ffff00" }, 3000);
    }
}

function DoAjaxNewsletterPost(submitElement) {
    $(submitElement).click(function() {
        var $form = $(this).closest('form');

        $.ajax({
            type: "POST",
            url: $form.attr('action'),
            data: $form.serialize(),
            dataType: "text",
            error: function(xhr, status, error) {
                $form.siblings('.warning').html("An error has occured, please refresh the page and try again.");
            },
            success: function(response) {
                $form.siblings('.warning').html(response);
            }
        });

        return false;
    });
}
