function loadMedia($mediaId) {

    newMediaInfo = GetSelectedMediaXml($mediaId);
    LoadMediaOnPlayer(newMediaInfo);
}

function loadSearchedMedia(sermonTitle, $sermonOtherInfo) {

    var newMediaInfo = new Array();
    newMediaInfo["ShortTitle"] = sermonTitle;
    newMediaInfo["ShortDescription"] = GetTruncatedString($sermonOtherInfo.children(".searchSermonShortDescription").text(), 50);
    newMediaInfo["Duration"] = $sermonOtherInfo.children(".searchSermonDuration").text();
    newMediaInfo["DateReleased"] = $sermonOtherInfo.children(".searchSermonReleaseDate").text();
    newMediaInfo["MediaLongDescription"] = $sermonOtherInfo.children(".searchSermonLongDescription").text();
    newMediaInfo["MediaId"] = $sermonOtherInfo.children('.searchMediaId').text();
    newMediaInfo["MediaURL"] = $sermonOtherInfo.children(".searchSermonSourcePath").text();

    LoadMediaOnPlayer(newMediaInfo);
}

function LoadMediaOnPlayer(newMediaInfo) {

    $("#shortDescription").text(newMediaInfo["ShortDescription"]);
    $("#duration").text(newMediaInfo["Duration"]);
    $("#dateReleased").text(newMediaInfo["DateReleased"]);
    $("#mediaLongDescription").text(newMediaInfo["MediaLongDescription"]);
    $("div.mediaInfo > div.mediaId").text(newMediaInfo["MediaId"]);

    var serverURL = "php/AjaxLoadMovie.php";
    var parameters = 'MediaURL=' + newMediaInfo["MediaURL"];
    var state = "LoadMovie";
    var type = "POST";

    $("div#innerPlayerHolder").css({ display: "none" });
    PerformAsyncServerCall(type, serverURL, parameters, state);
}

function GetSelectedMediaXml($mediaId) {

    var newMediaInfo = new Array();
    $selectedMediaXml = null;
    
    if ($mediaId != null) 
    {
        $mediaTree = GetMediaXmlTree();                   
        $("Sermon[Id='"  + $mediaId + "']", $mediaTree).each(function(i) {
            newMediaInfo["ShortTitle"] = $(this).attr("Title");
            newMediaInfo["ShortDescription"] = GetTruncatedString($(this).attr("ShortDescription"), 50);
            newMediaInfo["Duration"] = $(this).attr("Duration");
            newMediaInfo["DateReleased"] = $(this).attr("ReleaseDate");
            newMediaInfo["MediaLongDescription"] = $(this).attr("LongDescription");
            newMediaInfo["MediaURL"] = $(this).attr("SermonSourcePath");
            newMediaInfo["MediaId"] = $mediaId;
            return false;
        });
    }
    return newMediaInfo;
}

function LoadSermonByType()
{
    var serverURL = "php/LoadSermonByType.php";
    var state = "LoadSermonByType";
    var type = "POST";
    var parameters = "SermonType=Video";
    
    PerformAsyncServerCall(type, serverURL, parameters, state);
}


function LoadSermon(parameters) {
    var serverURL = "php/AjaxLoadMovie.php";
    var state = "LoadMovie";
    var type = "POST";
    
    PerformAsyncServerCall(type, serverURL, parameters, state);
}

function SearchWkiccMediaGallery() {

    $searchText = jQuery.trim($("#searchBox").val());
    if ($searchText.length == 0) {
        $("div#searchResultHeader").hide(400);
        return;
    }

    $searchWords = $searchText.split(" ");
    var keywordsToSearch = "";
    $.each($searchWords, function(intIndex, objValue) {
        if (objValue.toString().length > 2) {
            keywordsToSearch += objValue + "#";
        }
    });
    keywordsToSearch = keywordsToSearch.substring(0, keywordsToSearch.length -1) 

    var serverURL = "php/SearchSermon.php";
    var parameters = 'SearchKeywords=' + keywordsToSearch + "&FullSearchKeyword=" + $searchText;
    var state = "LoadSearchedSermon";
    var type = "POST";

    $("div#innerSearchResultHolder").css({ display: "none" });
    PerformAsyncServerCall(type, serverURL, parameters, state);        
    $("div#searchResultHeader").show(400);
}

// This function truncates a string at a maximum number of characters and adds "..." after the closes space
function GetTruncatedString(originalString, maxLength) {
    var retString = originalString;
    if (originalString.length > maxLength) {
        if (originalString.indexOf(" ") < 0) {
            retString = originalString.substring(0, (maxLength - 3)) + " ...";
        }
        else {
            var tempString = originalString.substring(0, maxLength);
            if (tempString.indexOf(" ") < 0) {
                retString = tempString.substring(0, (maxLength - 3)) + " ...";
            }
            else {
                retString = tempString.substring(0, tempString.lastIndexOf(" ")) + " ...";
            }
        }
    }
    return retString;
}



