var snippets = "";
var snippetCount = 0;
var currentSnippet = 0;

function getItemCount( sInputString )
{
	return sInputString.split("|||||").length;
}

function getItem ( nIndex, sInputString )
{
	return sInputString.split("|||||")[nIndex];
}

function getItemVariable ( nIndex, sVarName, sInputString )
{
	myItem = getItem(nIndex, sInputString);
	index = myItem.indexOf("[[" + sVarName + ":");
	if ( index == -1 )
		return "";
	
	return myItem.substring(index).split("]]")[0].replace("[[" + sVarName + ":", "");
}

//----------------------------------------------------------
// Get the content for a page
//----------------------------------------------------------
function getPageContent( sPageURL )
{

  $.ajax({
    type: "POST",
    url: "WebServices/Data.asmx/getPageContent",
    data: "{'pageURL':'" + sPageURL + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
			populatePageContent(msg.d);
    }
  });
}

//----------------------------------------------------------
// Apply the page content and show the modal
//----------------------------------------------------------
function populatePageContent( sContent )
{
	
	$("#modal .modalContent .content h1").text(getItemVariable(0, "Heading", sContent));
	$("#modal .modalContent .content .scrollableContent").html(getItemVariable(0, "Content", sContent));
	$("#modal").jqmShow(); 
	$('#modal .scrollableContent').jScrollPane({ scrollbarWidth: 10 });
	setModalPosition();
	if ($.browser.msie && parseInt($.browser.version) == 6) {
        $.scrollTo(100);
	}
}

function loadSnippets() {

    $.ajax({
        type: "POST",
        url: "WebServices/Data.asmx/getSnippets",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            initSnippets(msg.d);
        }
    });

}

function initSnippets(snippetList) {
    snippets = snippetList;
    snippetCount = getItemCount(snippetList);
    currentSnippet = 0;
    getNextSnippet();
}

function getNextSnippet() {

    $(".snippet").html(getItemVariable(currentSnippet, "Snippet", snippets));

    myCurrentSnippet = currentSnippet;
    while (myCurrentSnippet == currentSnippet) {
        currentSnippet = Math.floor(Math.random() * snippetCount)
    }

//    if ((currentSnippet + 1) == snippetCount) {
//        currentSnippet = 0;
//    } else {
//        currentSnippet++;
//    }
    //alert("About to load " + getItemVariable(currentSnippet, "Snippet", snippets));

    

}


function setModalPosition()
{
		
	var footerHeight = 938;
	var footerWidth = 939;
	var minHeight = 595;
	var documentHeight = $(document).height();
	var documentWidth = $(document).width() - 1;
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	var interfaceHeight = $("#modal").height();
	var interfaceWidth = $("#modal").width();

	var footerPositionTop;
	var footerPositionLeft;

	//alert("Window width: " + windowWidth);
	//alert("Modal Width: " + interfaceWidth);
	//alert("Setting position to: " + (windowWidth - interfaceWidth) / 2);

	$("#modal").css("left", (windowWidth - interfaceWidth)/2 + "px");
	//$("#modal").css("top", (windowHeight - interfaceHeight) / 2 + "px");

	if ($.browser.msie && parseInt($.browser.version) == 6)
	    $("#modal").css("position", "absolute");
	
	if (( (windowHeight - interfaceHeight)/2) < 0 )
		$("#modal").css("top", "0px");
	
	if (( (windowWidth - interfaceWidth)/2) < 0 )
	    $("#modal").css("left", "0px");

	$("#modal").mbBringToFront();
	$(".jqmOverlay").css("z-index", "2");

}