
var outerLayout;
var lastRecentTagSelected, lastCodeCentralExampleSelected;
var lastTimelineSelected, lastLatestPostSelected;

function BasicOnLoad()
{


    // PAGE LAYOUT
    outerLayout = $('body').layout({
        applyDefaultStyles: true
	    , west__onresize: function() { }
	    , west__size: 320
	    , east__size: 320
    });


    if (Silverlight.isInstalled("2.0.31005.0")) {

        window.setTimeout(function() {
            SilverlightRecentTags();

            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);
    }

}

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: '/service.svc/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: '/service.svc/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: '/service.svc/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/bookmarkshandler.ashx?userid=b736cdb9-a881-4de8-8412-15fade3f64d1,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: '/service.svc/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 onSLError(sender, args)
{
    alert("Silverlight error");
}

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;
}



