
var outerLayout;
var lastRecentTagSelected=0, lastCodeCentralExampleSelected=0;
var lastTimelineSelected=0, lastLatestPostSelected=0;

function BasicOnLoad()
{


    // PAGE LAYOUT
    outerLayout = $('body').layout({
        applyDefaultStyles: true
	    , west__onresize: function() { }
	    , west__size: 320
	    , east__size: 320
    });


    if ((!useJavaScript) && (Silverlight.isInstalled("2.0.31005.0"))) 
    {

        window.setTimeout(function ()
        {

            SilverlightRecentTags();

            SilverlightTagCloud();

            SilverlightCodeCentralExamples();

            SilverlightTwitterTimeline();

            SilverlightLatestPosts();

        }, 1000);
    
    
    }
    else 
    {
        jqGridRecentTags();

        jqGridCodeCentralExamples();

        jqGridTwitterTimeline();

        jqGridLatestPosts();

        $(window).resize(function()
        {
            ResizeFlex("#flex3", $("#TwitterContainer").parent().height() - 100);
            ResizeFlex("#flex4", $("#LatestPostsContainer").parent().height() - 220);
        });

        ResizeFlex("#flex3", $("#TwitterContainer").parent().height() - 100);
        ResizeFlex("#flex4", $("#LatestPostsContainer").parent().height() - 220);

        HtmlTagCloud();
    }

}

function SilverlightLatestPosts()
{
    Silverlight.createObject(
        "/ClientBin/MoshineWebSite.SilverlightApplication.xap",  // source
        document.getElementById("LatestPostsContainer"),  // parent element
        "ccLatestPostsContainerPlugin",  // id for generated object element
        {
        width: "100%", height: "100%", background: "white",
        version: "2.0.31005.0"
        },
        { onError: onSLError, onLoad: onSLLoad },
        "",
        "context"    // context helper for onLoad handler.
    );
}

function jqGridLatestPosts()
{
    $("#LatestPostsContainer").html('<table id="flex4" class="scroll"></table><div id="flex4Pager" class="scroll"></div>');

    jQuery("#flex4").jqGrid({
        url: '/Rest/LatestPostsSource',
        datatype: 'json',
        colNames: ['Title','PublishDate','Url'],
        imgpath: 'content/themes/steel/images',
        colModel: [
            { name: 'Title', index: 'Title', width: 285 },
            { name: 'PublishDate', index: 'PublishDate', hidden: true },
            { name: 'Url', index: 'Url', hidden: true }
            ],
        viewrecords: true,
        caption: "Blog Posts",
        loadonce: true,
        pager: jQuery("#flex4Pager"),
        rowNum: 5,
        onSelectRow: function(id)
        {
            if (id && id != lastLatestPostSelected) {
                var rowData = jQuery("#flex4").getRowData(id);
                BrowseToBlogPost(rowData.Url);
            }
        }
    }).navGrid('#flex4Pager', { edit: false, add: false, del: false });

}

function SilverlightTwitterTimeline()
{
    Silverlight.createObject(
            "/ClientBin/Moshine.Twitter.Silverlight.xap",  // source
            document.getElementById("TwitterContainer"),  // parent element
            "ccTwitterSilverlightPlugin",  // id for generated object element
            {
            width: "100%", height: "100%", background: "white",
            version: "2.0.31005.0"
        },
            { onError: onSLError, onLoad: onSLLoad },
            "",
            "context"    // context helper for onLoad handler.
        );
}

function jqGridTwitterTimeline()
{
    $("#TwitterContainer").html('<table id="flex3"  class="scroll" style="height:100%"></table><div id="flex3Pager" class="scroll"></div>');

    jQuery("#flex3").jqGrid({
        url: '/Rest/TimeLineSource',
        datatype: 'json',
        colNames: ['Status'],
        imgpath: 'content/themes/steel/images',
        colModel: [
            { name: 'Text', index: 'Text', width: 285 }
            ],
        viewrecords: true,
        caption: "Twitter",
        loadonce: true,
        pager: jQuery("#flex3Pager"),
        rowNum: 5
    }).navGrid('#flex3Pager', { edit: false, add: false, del: false });

}

function ResizeFlex(id, height)
{
    $(id).parent().height(height);
}

function SilverlightCodeCentralExamples()
{
    Silverlight.createObject(
            "/ClientBin/BookMarks.Silverlight.xap",  // source
            document.getElementById("ccTags"),  // parent element
            "ccTagsSilverlightPlugin",  // id for generated object element
            {
            width: "100%", height: "100%", background: "white",
            version: "2.0.31005.0"
        },
            { onError: onSLError, onLoad: onSLLoad },
            "TagId=24,param2=value2,Title=My Examples at Code Central",
            "context"    // context helper for onLoad handler.
        );

}

function jqGridCodeCentralExamples()
{
    $("#ccTags").html('<table id="flex1" class="scroll"></table><div id="flex1Pager" class="scroll"></div>');

    jQuery("#flex1").jqGrid({
        url: '/Rest/CCTagsSource',
        datatype: 'json',
        colNames: ['Title', 'Url'],
        imgpath: 'content/themes/steel/images',
        colModel: [
            { name: 'Title', index: 'Title', width: 475 },
            { name: 'Url', index: 'Url', hidden: true }
            ],
        viewrecords: true,
        caption: "My Examples at Code Central",
        loadonce: true,
        pager: jQuery("#flex1Pager"),
        rowNum: 5,
        onSelectRow: function(id)
        {
            if (id && id != lastCodeCentralExampleSelected) {
                var rowData = jQuery("#flex1").getRowData(id);
                BrowseToPage(rowData.Url);
            }
        }
    }).navGrid('#flex1Pager', { edit: false, add: false, del: false });

}

function SilverlightRecentTags()
{
    Silverlight.createObject(
            "/ClientBin/BookMarks.Silverlight.xap",  // source
            document.getElementById("RecentTags"),  // parent element
            "ccTagsSilverlightPlugin",  // id for generated object element
            {
            width: "100%", height: "100%", background: "white",
            version: "2.0.31005.0"
        },
            { onError: onSLError, onLoad: onSLLoad },
            "TaggingUrl=http://www.moshine.com/bookmarks/Taggings/PublishedHistory?userid=fbac2120-094a-4e98-b4a7-329580a9ed82,Title=Recent BookMarks...",
            "context"    // context helper for onLoad handler.
        );
}

function jqGridRecentTags()
{
    $("#RecentTags").html('<table id="flex2" class="scroll"></table><div id="flex2Pager" class="scroll"></div>');

    jQuery("#flex2").jqGrid({
        url: '/rest/RecentTagsSource',
        datatype: 'json',
        colNames: ['Title', 'Url'],
        imgpath: 'content/themes/steel/images',
        colModel: [
            { name: 'Title', index: 'Title', width: 475 },
            { name: 'Url', index: 'Url', hidden: true }
            ],
        viewrecords: true,
        caption: "Recent Bookmarks",
        loadonce: true,
        pager: jQuery("#flex2Pager"),
        rowNum: 5,
        onSelectRow: function(id)
        {
            if (id && id != lastRecentTagSelected) {
                var rowData = jQuery("#flex2").getRowData(id);
                BrowseToPage(rowData.Url);
            }
        }
    }).navGrid('#flex2Pager', { edit: false, add: false, del: false });
}

function SilverlightTagCloud()
{
    Silverlight.createObject(
            "/ClientBin/BookMarks.Silverlight.TagCloud.xap",  // source
            document.getElementById("TagCloud"),  // parent element
            "ccTagCloud",  // id for generated object element
            {
            width: "100%", height: "100%", background: "white",
            version: "2.0.31005.0"
        },
            { onError: onSLError, onLoad: onSLLoad },
            "context"    // context helper for onLoad handler.
        );
}

function HtmlTagCloud()
{
    $('#TagCloud').load('http://www.moshine.com/bookmarks/tagcount/FindMyRecentPublishedTopTags/fbac2120-094a-4e98-b4a7-329580a9ed82',
        function (response, status, xhr)
        {
            if (status == "error") {
                var msg = "Sorry but there was an error: ";
                $("#TagCloud").html(msg + xhr.status + " " + xhr.statusText);
            }
        }
    );

        $('#TagCloud').css('padding-top', "60px");
        $('#TagCloud').css('padding-left', "40px");
    }


function onSLError(sender, args) {
    onSilverlightError(sender, args);
}

function onSLLoad(plugIn, userContext, sender)
{
}

function BrowseToPage(url)
{
    InvokeActionForUrl(url);
}

function BrowseToBlogPost(url)
{
    InvokeActionForUrl(url);
}

function BrowseToTwitterStatus(url)
{
    InvokeActionForUrl(url);
}

function InvokeActionForUrl(url)
{
    window.location.href=url;
}

function SilverlightLoader(xapEndPoint, containerId, generatedId, width, height, parameters) {

    var passthis = ""; 

    if(parameters != null) {
        passthis = parameters;
    }

    
    Silverlight.createObject(
                xapEndPoint,
                document.getElementById(containerId),  // parent element
                generatedId,  // id for generated object element
                {
                width: width, height: height, background: "white",
                version: "2.0.31005.0"
            },
            { onError: onSilverlightError, onLoad: onSilverlightLoad },
            passthis,
            "context"    // context helper for onLoad handler.
            );

}

function onSilverlightLoad() {
}

function onSilverlightError(sender, args) {
    var iErrorCode;
    var errorType = args.ErrorType;

    iErrorCode = args.ErrorCode;

    var errMsg = "\nSilverlight error message     \n";

    errMsg += "ErrorCode: " + iErrorCode + "\n";


    errMsg += "ErrorType: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "XamlFile: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }
    alert(errMsg);
}



