var bbcodeOpt = null;
$(document).ready(function() {
    if (allowRating) {
        $("#ratingContainer").show();
        $('#rating').rater('/set_rating.ashx?page=' + pageId,
        { style: 'basic', curvalue: pageRating });
    }

    if (existComment) {
        $(".commentViewToggle").toggle(
         function() {
             $(".comment-frame").show();
             $.scrollTo('#comment', 800);

           
         }, function() {
             $(".comment-frame").hide();
         }
         );
    }
    if (allowComment) {
        bbcodeOpt =
        {
            nameSpace: "bbcode",
            onTab: { keepDefault: false, replaceWith: '\t' },
            previewParserPath: "/Themes/Default/Content/markitup/prv/previewBBCode.aspx",
            markupSet: [
		{ name: 'Podebljaj', key: 'B', openWith: '[b]', closeWith: '[/b]' },
		{ name: 'Kurziv', key: 'I', openWith: '[i]', closeWith: '[/i]' },
		{ name: 'Podvuci', key: 'U', openWith: '[u]', closeWith: '[/u]' },
		{ name: 'Precrtaj', key: 'S', openWith: '[s]', closeWith: '[/s]' },
        { separator: '---------------' },
    	{ name: 'Link', key: 'L', openWith: '[url=[![Url]!]]', closeWith: '[/url]', placeHolder: 'Ovde unsite text...' },
      { separator: '---------------' },
      { name: 'Colors', openWith: '[color=[![Color]!]]', closeWith: '[/color]', dropMenu: [
          { name: 'Yellow', openWith: '[color=#FFCC00]', closeWith: '[/color]', className: "col1-1" },
          { name: 'Orange', openWith: '[color=#FFA500]', closeWith: '[/color]', className: "col1-2" },
          { name: 'Red', openWith: '[color=#FF0000]', closeWith: '[/color]', className: "col1-3" },
          { name: 'Blue', openWith: '[color=#000099]', closeWith: '[/color]', className: "col2-1" },
          { name: 'Purple', openWith: '[color=#800080]', closeWith: '[/color]', className: "col2-2" },
          { name: 'Green', openWith: '[color=#008000]', closeWith: '[/color]', className: "col2-3" },
          { name: 'Green1', openWith: '[color=#666633]', closeWith: '[/color]', className: "col3-1" },
          { name: 'Gray', openWith: '[color=#808080]', closeWith: '[/color]', className: "col3-2" },
          { name: 'Black', openWith: '[color=#000000]', closeWith: '[/color]', className: "col3-3" }
      ]
      },

    	/*
		{ separator: '---------------' },
        { name: 'Bulleted list', openWith: '[list]\n', closeWith: '\n[/list]' },
        { name: 'Numeric list', openWith: '[list=1]\n', closeWith: '\n[/list]' },
        { name: 'List item', openWith: '[*] ' },
	    { separator: '---------------' },
	    { name: 'Citat', openWith: '[quote]', closeWith: '[/quote]' },
        { name: 'Code', openWith: '[code]', closeWith: '[/code]' },
        */
       {separator: '---------------' },
     

		{ name: 'Obriši', className: 'clean', replaceWith: function(markitup) { return markitup.selection.replace(/\[(.*?)\]/g, "") } },
		{ name: 'Izgled', className: 'preview', call: 'preview' }
     	]
        }
        $(".commentToggle").toggle(
         function() {
             showCommentFrame();
         }, function() {
             hideCommentFrame();
         }
         );
        $('#txtComment').markItUp(bbcodeOpt);
        //$('#txtComment').markItUp(bbcodeOpt);
        //initCommentValidator();
        //showCommentFrame();
        //getCaptcha('#captchaImage', newCapc, SR_LOADING);
        $("#SendComment").click(function() {
            if ($("#commentContainer").valid()) {
                var dat = [];

                dat.push("ContentId=" + pageId);
                dat.push("Captcha=" + encodeURIComponent($("#txtCaptcha").val()));
                dat.push("Name=" + encodeURIComponent($("#txtName").val()));
                dat.push("Email=" + encodeURIComponent($("#txtEmail").val()));
                dat.push("Comment=" + encodeURIComponent($("#txtComment").val()));

                $.Callback($("#commentContainer").attr("action"), {
                    loadingMsg: SR_POST_COMMENT,
                    success: function(data) {
                        if (data.IsError) {
                            alert(data.Message);
                            return;
                        }
                        hideCommentFrame();
                        try {
                            $("#commentContainer").reset();
                        } catch (ex) { }

                    },
                    data: dat.join("&").replace(/%20/g, "+")
                });
            }


        });
        $("#NewCaptcha").click(function() {
            getCaptcha('#captchaImage', newCapc, SR_LOADING);
        });







    }






});
var commentValidator;

function initCommentValidator() {
  
    var __errorFrame = $(".errorFrame");
    commentValidator = $("#commentContainer").validate({
        errorContainer: __errorFrame,
        errorLabelContainer: $("ol", __errorFrame),
        wrapper: 'li',
        meta: "validate"
    });
}
var isInitComment = false; 
function showCommentFrame() {
    if (!isInitComment) {
       
        initCommentValidator();
        getCaptcha('#captchaImage', newCapc, SR_LOADING);
        isInitComment = true;
        
    }

    $("#commentContainer").show();
    $("#txtName").focus();
    $.scrollTo('#commentContainer', 800);
    
}
function hideCommentFrame() {
    $("#commentContainer").hide();
}
function ResetAjaxMsgPanel() {
    $("#ajaxMsgPanel").css("left", "0px").css("top", "0px").css("width", "100%");
}

function getCaptcha(img, fc, loadingTxt) {
    $(img).Callback(fc, { success: function(d) {
        if (d.IsError) {
            alert(d.Message);
            return;
        }
        $(img).attr('src', d.Message);
    },
        loadingMsg: loadingTxt
    });
}
