﻿var addcmt = '<img class="addcmt" src="../images/comment_add.png" alt="Add Comment" onclick="ShowComment(\'';
var aid = '';
var uid = '';  var email = '';var b=false;var lstId=6000;
function pgload() {


    outerLayout = $('body').layout({
        minSize: 100	// ALL panes
			, west__size: 250
			, east__size: 400
			, north__size: 130
			, west__onresize: 'westLayout.resizeAll'
			, east__onresize: 'eastLayout.resizeAll'
    });

    westLayout = $('div.ui-layout-west').layout({
        minSize: 50	// ALL panes
			, center__paneSelector: ".west-center"
			, south__paneSelector: ".west-south"
			, south__size: 70
    });

    eastLayout = $('div.ui-layout-east').layout({
        minSize: 50	// ALL panes
			, center__paneSelector: ".east-center"
			, south__paneSelector: ".east-south"
			, south__size: 100
    });
    $('div.ui-layout-north').layout({
   
        spacing_open: 0
		, resizable: false
		, slidable: false
		, fxName: "none"
    });
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetUserInfo",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{dummy:' '}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) { //ajaxError(XMLHttpRequest, textStatus, errorThrown);
        },
        success: function(result) {
            var res = JSON.parse(result.d);
            if (!res.IsAuth) document.location = "../accessdenied.aspx";
            else {
                if (res.IsCurrMem==false) document.location = "../MembershipRenewal.aspx";
                else {
                    email = res.eml;
                    $("#uname").html("Welcome back " + res.name);
                    if (res.IsAdmin) {
                        $("#addTA").css("display", ""); $("#addP").css("display", ""); $("#liAdm").css("display", "");
                        $("#addTA").click(function() { document.location = "http://stockbee.biz/Maintenance/AddEditArticle.aspx?type=TA"; });
                        $("#addP").click(function() { document.location = "http://stockbee.biz/Maintenance/AddEditArticle.aspx?CatID=12"; });

                    }
                    setuppg();
                }
            }
        }
    })
} 
function rc() {
    try {
        jQuery("#NodeTable").jqGrid
			({
			    
			    loadtext: "Refreshing...",
			    datatype: function(pdata) {
			    jQuery("#lui_NodeTable, #load_NodeTable").show();
			        // alert(JSON.stringify(pdata));
			        jQuery.ajax({ type: "POST",
			        url: "../asmx/Blog.asmx/GetAllComments",
			            dataType: "json",
			            data: JSON.stringify(pdata),
			            contentType: "application/json;charset=utf-8",
			            processData: false,
			            error: function(XMLHttpRequest, textStatus, errorThrown)
			            { jQuery("#lui_NodeTable, #load_NodeTable").hide(); ajaxError(XMLHttpRequest, textStatus, errorThrown); },
			            success: function(result) {
			                grdData = JSON.parse(result.d);
			                var thegrid = jQuery("#NodeTable")[0];
			                thegrid.addJSONData(grdData);
//			                
			                jQuery("#lui_NodeTable, #load_NodeTable").hide();
			            }
			        })
			    },

			    colNames: ['Date','Posted By','Title','Comment'],
			    colModel: [{ name: "PostedDate", index: "PostedDate", width: 110, sortable: false, align: "left" },
			    { name: "PostedBy", index: "PostedBy", width: 75, sortable: false, align: "left" },
			    { name: "Title", index: "Title", width: 150, sortable: false, align: "left" },
			    { name: "Comment", index: "Comment", width: 525, sortable: false, align: "left" }],
			    pager: jQuery('#pager2'),
			    rowNum: 25,
			    rowList: [10, 25, 50],
			    imgpath: '../css/sand/images',
			    sortname: 'PostedDate',
			    viewrecords: true,
			    sortorder: "desc",
			    caption: '',hidegrid:true,
			    shrinkToFit: true,pginput:false,
			   // width: 800,
			    height: 325,
			    toolbar: [false, "top"],
			    loadui: "enable",
			    altRows: true
			   // onPaging: function(pgButton) { if (pgButton == "records") rwcnt = jQuery("#RmTable").getGridParam("rowNum"); }
			});
			jQuery("#NodeTable").navGrid('#pager2', { add: false, edit: false, del: false, search: false, refresh: true });
        bldAst = false;
    } catch (ex) {alert(ex);  } 
}
   
   function setuppg(){ 
    GetTodayPost(); GetLastPost(); GetTradeAlert(); GetRC();
    $("select").change(function() {
        var arch = $(this).val()
        if (arch != '') { window.location = '../archivearticles.aspx?catid=12&' + arch; }
    });
    $("#liBlog").click(function() { });
        $("#liCBlog").click(function() { document.location = "../Blog.aspx"; });

    $("#liMM").click(function() { document.location = "../marketmonitor.aspx"; });
    $("#liTT").click(function() { document.location = "../tradingSizeTool.aspx"; });
  //  $("#liTr").click(function() { document.location = "http://www.stockbeetraining.com/"; });
    $("#liM50").click(function() { document.location = "../Month50.aspx"; });
    $("#liY10").click(function() { document.location = "../Year10.aspx"; });
    $("#liWP").click(function() { document.location = "../WPBlog.aspx"; });
    $("#liTG").click(function() { document.location = "../TradingGuide.aspx"; });
    $("#liAdm").click(function() { document.location = "../Maintenance/Default.aspx"; });
    
    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 300,
        width: 500,
        modal: true,
        buttons: {
            'Add Comment': function() {

                $.ajax({ type: "POST",
                    url: "../asmx/Blog.asmx/PostComment",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'id':'" + aid + "','txt':'" + $('#comm').val() + "', 'email':'" + email + "'}",
                    processData: false,
                    error: function(XMLHttpRequest, textStatus, errorThrown) { //ajaxError(XMLHttpRequest, textStatus, errorThrown); 
                    },
                    success: function(result) {
                        $('#comm').val('');
                        $("#dialog").dialog('close');
                    }
                })
            }
    ,
            Cancel: function() { $('#comm').val('');
                $(this).dialog('close');
            }
        },
        close: function() {
            // allFields.val('').removeClass('ui-state-error');
        }
    });
    $("#D2").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 430,
        width: 900,
        modal: false
      
    });
    rc();
}
function srch() {document.location = "../searcharticles.aspx?txt=" + $("#stxt").val();}
function showallcomments() {
   // $("#allComm").click(function() {
    //   alert('dd');
    jQuery("#NodeTable").trigger("reloadGrid"); 
        $('#D2').dialog('open');
    //});
}
function GetTodayPost() {
    $("#wTP").css("display", "");
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetTodayPost",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{dummy:' '}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {$("#wTP").css("display", "none");
            //ajaxError(XMLHttpRequest, textStatus, errorThrown); 
        },
        success: function(result) {
            var res = JSON.parse(result.d);
            if (res.ID != '') {
                var str = '';
                for (var i = 0; i < res.length; i++) {
                    str = str + '<p class="rc" id="' + res[i].ID + '" >' + addcmt + res[i].ID + '\')"/>&nbsp;<span class="title" id="tp' + res[i].ID + '"><a href="http://stockbee.biz/ShowArticle.aspx?ID=' + res[i].ID +'">' +res[i].Title + '</a></span>' + '<span id="mr' + res[i].ID + '" class="more">&nbsp;<img src="../images/book_open.png" alt="more..."/></span></p>';
                }
                if (str == '') $("#todaypost").html('No posts for today yet.'); else
                    $("#todaypost").html(str);
            }
            $("#wTP").css("display", "none");
            setTimeout('GetTodayPost()', 180000);
        }
    });
}
function GetTradeAlert() {
    $("#wTA").css("display", "");
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetTradeAlert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id':'" + lstId + "'}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $("#wTA").css("display", "none"); //ajaxError(XMLHttpRequest, textStatus, errorThrown);
        },
        success: function(result) {
           
            
            var res = JSON.parse(result.d);$("#tadt").html("<br/>Last Update: " + res.UpdDt);
            if (res.arrPosts.length > 0) {
                var str = '';
                for (var i = 0; i < res.arrPosts.length; i++) {
                    str = str + '<p class="item" id="' + res.arrPosts[i].Id + '">' + addcmt + res.arrPosts[i].Id + '\')"/><span class="tag">&nbsp;' + res.arrPosts[i].PostDate + '</span><br/>' + res.arrPosts[i].Body + '</p>';
                    lstId = res.arrPosts[i].Id;
                }
                if (str == '') $("#TA").html('No trade alerts for today yet.'); else
                { $("#TA").html(str); }
            }
            $("#wTA").css("display", "none");
            setTimeout("GetTradeAlert()", 10000);
        }
    });
}
function GetRC() {
    $("#wRC").css("display", "");
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetRecentComments",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{dummy:' '}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $("#wRC").css("display", "none"); // ajaxError(XMLHttpRequest, textStatus, errorThrown);
        },

        success: function(result) {
       
       
            var res = JSON.parse(result.d); $("#rcdt").html("<br/>Last Update: " + res.UpdDt);
            var str = '';
            for (var i = 0; i < res.arrComments.length; i++) {
                str = str + '<p class="rc"><span class="title" id="cmt' + res.arrComments[i].Id + '">' + addcmt + res.arrComments[i].Id + '\')"/>&nbsp;' + res.arrComments[i].Title + '</span>' + '<br/><span class="rctag">' + res.arrComments[i].Author + '&nbsp;&nbsp;' + res.arrComments[i].CommentDate + '</span><br/>' + res.arrComments[i].Body + '</p>';
            }
            $("#RC").html(str);
            
            if (!b) {
//                $('.addcmt').click(function() {
//                    aid = this.id;
//                    // $('#dialog').data('title.dialog', 'Add Comment: ' + title); 
//                    $('#dialog').dialog('open');
//                });
                $('.more').click(function() {
                GetPost(this.id.slice(2));
                }); 
                b = true;
            } $("#wRC").css("display", "none");
            setTimeout("GetRC()",10000);
        }
    });
}
function ShowComment(id) {
    aid = id;
    // $('#dialog').data('title.dialog', 'Add Comment: ' + title); 
   $('#dialog').dialog('open');
}
function GetLastPost() {
    $("#wLP").css("display", "");
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetLastPost",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id':'" + lstId + "'}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $("#wLP").css("display", "none"); //ajaxError(XMLHttpRequest, textStatus, errorThrown); 
        },
        success: function(result) {
            try {
                var res = JSON.parse(result.d);
                if (res.Title != null && res.Title != "") {
                    $("#title").html(addcmt + res.Id + "')\"/>&nbsp;" + res.Title);
                    $("#dt").html("<b>Posted by:</b> " + res.Author + " on " + res.PostDate);
                    $("#post").html(res.Body); //$("#comment").html(res.Comments);
                    lstId = res.Id;
                } $("#wLP").css("display", "none");
            } catch (e) { }
            setTimeout('GetLastPost()', 180000);
        }
    });
}
function GetPost(id) {
    $.ajax({ type: "POST",
        url: "../asmx/Blog.asmx/GetPost",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'id':'"+id+"'}",
        processData: false,
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        ajaxError(XMLHttpRequest, textStatus, errorThrown);
         },
        success: function(result) {
            var res = JSON.parse(result.d);

            $("#mr"+res.Id).html('<br/><span class="tag"><b>Posted by:</b> '+res.Author+' on '+res.PostDate+'</span><br/>'+res.Body);
            //$("#post").html(res.Body);
        }
    });
}