﻿/*================================
	Communities Area functions
==================================*/
var communityIntroTimeout;
var communityIntroIndex = 0;
var communityIntroDelay = 5500;
var orginalReplyTextId;

$J(document).ready(function () {

    //Community interaction hover
    //=============================//
    var communitySet = $J('.communityInteractions')[0];
    var commPanelSet = $J('.commPreview', communitySet);
    var commLinkSet = $J('.communityInteractionNav a', communitySet);
    var focusArrow = $J('.focusArrow', communitySet)[0];
    var newCommPreview = $J(commPanelSet)[0];
    var oldCommPreview;

    $J('.communityInteractionNav a', communitySet).bind('click', function () {
        var tempLeft = $J(this)[0].offsetLeft + 3;
        if ($J(this)[0] == $J(commLinkSet)[0]) tempLeft += 28;

        //Deal with link and focus        
        $J(focusArrow).css('left', tempLeft + 'px');
        $J(commLinkSet).removeClass('selected');
        $J(this).addClass('selected');

        //Deal with the panels
        oldCommPreview = newCommPreview;

        for (var i = 0; i < $J(commLinkSet).length; i++) {
            if ($J(commLinkSet)[i] == $J(this)[0]) {
                newCommPreview = $J(commPanelSet)[i];
                communityIntroIndex = i;
            }
        }

        if (oldCommPreview != newCommPreview) {
            $J(oldCommPreview).stop().css('z-index', '49').fadeTo(400, 0.0, function () { $J(this).hide(); });
            $J(newCommPreview).stop().css({ opacity: '0', zIndex: '50' }).show().fadeTo(400, 1.0);
        }

        resetIntroCommunityTimeout();

        $J(this).blur();
        return false;

    });

    $J('.commPreview').mousemove(function () {
        resetIntroCommunityTimeout();
    });

    resetIntroCommunityTimeout();


    //Community Group panels & forms
    //=============================//
    $J('.openCreateDiscForm').click(function () {
        $J('.startDiscussionForm').css('display', 'block');
        $J('.closeCreateDiscForm').css('display', 'block');
        $J(this).css('display', 'none');

        $J(this).blur();
        return false;
    });

    $J('.closeCreateDiscForm').click(function () {
        $J('.startDiscussionForm').css('display', 'none');
        $J('.openCreateDiscForm').css('display', 'block');
        $J(this).css('display', 'none');

        $J(this).blur();
        return false;
    });

    $J('.startDiscussionForm .closeLink').click(function () {
        $J('.startDiscussionForm').css('display', 'none');
        $J('.closeCreateDiscForm').css('display', 'none');
        $J('.openCreateDiscForm').css('display', 'block');

        $J(this).blur();
        return false;
    });


    //Community discussion posts
    //=============================//
    var discussionContent = $J('.postCommunityContent');
    var hiddenReplyForm = $J('.hiddenPostForm', discussionContent)[0];

    var replyBtns = $J('.openReplyForm', discussionContent);
    var selectedReplyBtn;

    if ($J('.postCommunityContent').length > 0 && discussionContent) {
        var hiddenReplyFormHtml = $J(hiddenReplyForm).html();
        $J(hiddenReplyForm).html();
    }

    $J('.openReplyForm', discussionContent).click(function () {

        if ($J(this).hasClass('selected') == false) {
            //Remove form
            $J(replyBtns).removeClass('selected');
            $J('.replyFormHolder', discussionContent).html('');


            //Add form
            $J(this).addClass('selected');
            tempParent = $J(this).parent().parent();
            tempPostId = $J('input[type=hidden]', tempParent).val();
            tempFormHold = $J('.replyFormHolder', tempParent)[0];

            $J(tempFormHold).html(hiddenReplyFormHtml);              //move form
            $J('input[type=hidden]', tempFormHold).attr('id', 'useless');
            $J('input[type=hidden]', tempFormHold).attr('name', 'useless');

            //discussionReplyFormTextArea required(front and back) for function CheckIsRequiredWatermarkDiscussionReply
            $J('textarea ', tempFormHold).attr('id', 'discussionReplyFormTextArea');
            $J('textarea ', tempFormHold).attr('name', 'discussionReplyFormTextArea');

            //$J('input[type=hidden]', tempFormHold).val(tempPostId);  //update hidden field

            temp = $J('input[type=hidden]', hiddenReplyForm).val(tempPostId);

            //Dynamic jquery events
            updateListenersAgain();

        } else {
            //Remove form
            $J(replyBtns).removeClass('selected');
            $J('.replyFormHolder', discussionContent).html('');
        }

        $J(this).blur();
        return false;
    });

    // button add comment clicked
    $J('.addNewPostBtnWrapper').find('a').click(function () {
        $J('.discussionPostForm').find('.addPostAuthenticated').show();
        $J('.addPostAuthenticated').find('textarea').text('Enter Your Comment');
        $J('.discussionPostForm').find('.addPostModeration').hide();
    });

});


//Community interactions panel
//=============================//
function resetIntroCommunityTimeout(){
    if(communityIntroTimeout) clearTimeout(communityIntroTimeout);
    tempLinks = $J('.communityInteractions .communityInteractionNav a');

    //Check if it's intro page
    if(tempLinks && tempLinks.length > 0){
        communityIntroTimeout = setTimeout(function(){
            clearTimeout(communityIntroTimeout);
            communityIntroIndex++;
            if(communityIntroIndex >= tempLinks.length) communityIntroIndex = 0;
            $J(tempLinks[communityIntroIndex]).trigger('click');
        
        }, communityIntroDelay);
    }
}


//Custom Validation
//=============================//
function CheckIsRequiredWatermarkDiscussionReply(control, args) {
    if ($J("#discussionReplyFormTextArea")[0].value == $J("#discussionReplyFormTextArea")[0].title) {
        args.IsValid = false;
        $J("#discussionReplyFormTextArea").parent().addClass("fieldError");
    }
    
}


//Move the post form
//=============================//
function updateListenersAgain(){
    //Focus text areas
    $J('.textareaWrapper textarea').focus(function(){
        
        //remove the filled class or clear the watermark
        if($J(this).hasClass('filled') == true) $J(this).removeClass('filled');
        else $J(this).val('');
        
        //if the field isn't in error state, change to focus state
        if($J(this).parent().hasClass('fieldError') == false) $J(this).parent().addClass('fieldFocus');
        
    });
    
    //Blur text fields
    $J('.textareaWrapper textarea').blur(function(){
    
        //if the field isn't in error state, remove focus state
        if($J(this).parent().hasClass('fieldError') == false) $J(this).parent().removeClass('fieldFocus');
        
        if($J(this).val() != "" && $J(this).val() != $J(this).attr('title')){ 
            //Keep text black and don't add watermark
            $J(this).addClass('filled');        
        }else{
            temp = $J(this).attr('title');
            $J(this).val(temp);
        }
    });
    
    //Send btn
    //=====================// 
    $J('input.btnSendSubmit').hover(function(){
        $J(this).addClass('btnSendSubmitOver');
    }, function(){
        $J(this).removeClass('btnSendSubmitOver');
    });
}


