﻿/*#############################################################################
##                                                                           ##
##  MediaItem Functions                                         Version 1.0  ##
##  author: Jay Baldwin IV                                  2010-02-09.1726  ##
##                                                                           ##
##  This code is owned and maintained by World Harvest Church.               ##
##                                                                           ##
##  A changelog is located at the bottom of this file.                       ##
##                                                                           ##
#############################################################################*/

/*  
Usage in an HTML document:

<div id="divVideo1"></div>
<script type="text/javascript">
MediaItemDraw('divVideo1', 1);
</script>
*/

function MediaItemDraw(strDivID, intMediaItemID) {

    if (window.XMLHttpRequest) // will return false in IE 5/6
        MediaXhttp = new XMLHttpRequest(); // The W3C Endorsed... for Internet Exploder 7+, Firehash, Plastic, Lost-in-the-woods, and Screeching
    else
        MediaXhttp = new ActiveXObject("Microsoft.XMLHTTP"); // for Internet Exploder 5/6


    // Declare the callback portion so that the method doesn't
    // halt all other scripts waiting for a response

    MediaXhttp.onreadystatechange = function() {
        if (MediaXhttp.readyState != "4") return;
        if (MediaXhttp.status != 200) return;

        MediaItemHandleXhttpCallback(MediaXhttp, strDivID);
    }

    MediaXhttp.open("GET", "/global/media/ajax_GetMediaItem.aspx?id=" + intMediaItemID, true);
    MediaXhttp.send("");

    // We're purposefully remaining "messy" (not destroying variables) 
    // to keep the variables in the global scope of the page...

}

function MediaItemHandleXhttpCallback(MediaXhttp, strDivID) {

    if (MediaXhttp == null || MediaXhttp == undefined) return;
    if (strDivID == null || strDivID == undefined) return;

    var xmlDoc = MediaXhttp.responseXML;

    var obj = strDivID;
    if (!obj.style) obj = document.getElementById(obj);

    var strUrl = '';
    var strHttpSource = '';
    //var strTitle = '';
    //var strDescription = '';
    var strWidth = '';
    var strHeight = '';

    try {
        if (xmlDoc.childNodes.length == 0) return;
        if (xmlDoc.getElementsByTagName("MediaItems")[0].childNodes.length == 0) return;

        var MediaItems = xmlDoc.getElementsByTagName("MediaItems")[0];
        var MediaItem = MediaItems.getElementsByTagName("MediaItem")[0];

        // The if / then / else format below of setting these variables is to support all browsers.
        //   It goes like this:
        //      strVariable = ThisWillOnlyBeTrueOnIE ? ThenUseTheValue : OhYouMustBeFirehashOrW3cBrowser;

        strUrl = MediaItem.getElementsByTagName("Url")[0].text ? MediaItem.getElementsByTagName("Url")[0].text : MediaItem.getElementsByTagName("Url")[0].textContent;
        strHttpSource = MediaItem.getElementsByTagName("HttpSource")[0].text ? MediaItem.getElementsByTagName("HttpSource")[0].text : MediaItem.getElementsByTagName("HttpSource")[0].textContent;
        //strTitle = MediaItem.getElementsByTagName("Title")[0].text ? MediaItem.getElementsByTagName("Title")[0].text : MediaItem.getElementsByTagName("Title")[0].textContent;
        //strDescription = MediaItem.getElementsByTagName("Description")[0].text ? MediaItem.getElementsByTagName("Description")[0].text : MediaItem.getElementsByTagName("Description")[0].textContent;
        strWidth = MediaItem.getElementsByTagName("Width")[0].text ? MediaItem.getElementsByTagName("Width")[0].text : MediaItem.getElementsByTagName("Width")[0].textContent;
        strHeight = MediaItem.getElementsByTagName("Height")[0].text ? MediaItem.getElementsByTagName("Height")[0].text : MediaItem.getElementsByTagName("Height")[0].textContent;

        // For use within normal web clients 
        var isiPad = navigator.userAgent.match(/iPad/i) != null;

        // For use within iPad developer UIWebView
        // Thanks to Andrew Hedges!
        var ua = navigator.userAgent;
        var isiOS = /iPad/i.test(ua) || /iPhone/i.test(ua);

        var strHtml;
        if (!isiOS) {
            strHtml = '<iframe src="##URL##&wmode=transparent" width="##WIDTH##" height="##HEIGHT##" scrolling="no" frameborder="0" allowtransparency="true"></iframe>';
        }
        else {
            strHtml = '<video id="vidIPhone" oncanplay="HideLoading();" src="##HTTPSOURCE##" width="##WIDTH##" height="##HEIGHT##" controls="controls" />';
        }
        
        strHtml = strHtml.replace('##URL##', strUrl);
        strHtml = strHtml.replace('##HTTPSOURCE##', strHttpSource);
        strHtml = strHtml.replace('##WIDTH##', strWidth);
        strHtml = strHtml.replace('##HEIGHT##', strHeight);

        obj.innerHTML = strHtml;
        obj.style.width = strWidth + 'px';
        obj.style.height = strHeight + 'px';



        // Let's clean up after ourselves!! :)

        strHtml = null;

        MediaItem = null;
        MediaItems = null;

        strUrl = null;
        //strTitle = null;
        //strDescription = null;
        strWidth = null;
        strHeight = null;

        obj = null;
        xmlDoc = null;
    }
    catch (e) { }

}





/*#############################################################################
##  
##           Please resepect the 80 character line limit in this section. :)  ^
##  
##  
##  CHANGELOG
##  
##  AUTHOR    DATE          CHANGE                                                      
##  --------  ------------  --------------------------------------------------
##  JJBIV     2010-02-09    Initial Version.
##  JJBIV     2010-02-18    Resizes containing div to match video size.
##  
#############################################################################*/


