

/*-------------------------------------------------------------------------
*
*This is an example of usage of the Unicorn Media SDK in communication with the
*Unicorn Media Player API, and is being provided for your use on an “as
*is” basis and without warranty implied or otherwise.
*Use of this software and example is at your own risk and it may not function properly if modified
*or altered in such a manner as it ceases to interface properly with the Unicorn Media System.
*If you are having difficulty with the function or
*implementation of this software, please contact support@unicornmedia.com.
*
*---------------------------------------------------------------------------*/

/**
 * @param totalValue
 * @param oldValue
 * @param newValue
 */
function Replace(totalValue,oldValue,newValue)
{
    while(totalValue.indexOf(oldValue) > -1)
        totalValue=totalValue.replace(oldValue,newValue);
    return totalValue;
}

/**
 * @param TagName
 * @param node
 */
function getNode(TagName, node)
{
    var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) :
    items[node].getElementsByTagName(TagName);
    if(currentNode.length > 0)
    {
        if(currentNode[0].firstChild)
            return currentNode[0].firstChild.nodeValue;
        else
            return '';
    }
    else
        return '';
}
function getTrimmedNode(TagName, node) {
    var currentNode = (node == null) ? xmlDoc.getElementsByTagName(TagName) :
    items[node].getElementsByTagName(TagName);
    if (currentNode.length > 0) {
        if (currentNode[0].firstChild)
            return currentNode[0].firstChild.nodeValue.substring(0, 76) + '...';
        else
            return '';
    }
    else
        return '';
}

var tabLinks = new Array();
var contentDivs = new Array();

function init()
{

    // Grab the tab links and content divs from the page
    var tabListItems = document.getElementById('tabs').childNodes;
    for ( var i = 0; i < tabListItems.length; i++ )
    {
        if ( tabListItems[i].nodeName == "LI" )
        {
            var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
            var id = getHash( tabLink.getAttribute('href') );
            tabLinks[id] = tabLink;
            contentDivs[id] = document.getElementById( id );
        }
    }

    // Assign onclick events to the tab links, and
    // highlight the first tab
    var i = 0;

    for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 )
        {
        tabLinks[id].className = 'selected';
        i++;
        contentDivs[id].className = 'tabContent';
        } 
      }
}

function showTab()
{
    var selectedId = getHash( this.getAttribute('href') );

    // Highlight the selected tab, and dim all others.
    // Also show the selected content div, and hide all others.
    for ( var id in contentDivs )
    {
        if ( id == selectedId )
        {
            tabLinks[id].className = 'selected';
            contentDivs[id].className = 'tabContent';
        } 
        else
        {
            tabLinks[id].className = '';
            contentDivs[id].className = 'tabContent hide';
        }
    }

    // Stop the browser following the link
    return false;
}
/**
 * @param element
 * @param tagName
 */
function getFirstChildWithTagName( element, tagName )
{
    for ( var i = 0; i < element.childNodes.length; i++ )
    {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
    }
}

/**
 * @param url
 */
function getHash( url )
{
    var hashPos = url.lastIndexOf ( '#' );
    return url.substring( hashPos + 1 );
}

/**
 * @param feedTabId
 * @param feedURL
 */
function feedSet(feedTabId, feedURL)
{ 
    var xmlhttp=null;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null)
    {
        xmlhttp.open("GET", feedURL, true);
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {
                    items = xmlhttp.responseXML.getElementsByTagName('item');
                    var buffer = '<div class="feed" style="height:290px; overflow:auto"><table cellpadding="2" cellspacing="2" >';
                    for(var i = 0; i < items.length; i++)
                    {
                        var output = '<tr><td align="left" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #CCCCCC;">' + '<img src = "' + getNode('MediaItemThumbnailURL', i) + '" width="100" height="57" onclick = "RemoveStopScreen(\'flashobject\', false); LoadMediaItemInQueueAndPlay(\'flashobject\', \'' + getNode('MediaItemGuid', i) + '\');" />' + '</td><td valign="top" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #CCCCCC; color:white; ">' + getNode('MediaItemName', i) + ' <br /><span style="color: rgb(139, 130, 117); font-size: 11px; font-style: normal; font-family: Arial,Helvetica,sans-serif;">' + getTrimmedNode('MediaItemDescription', i) + '</span></td></tr>';
                        buffer += output;
                    }
                    buffer += '</table><div>';
                    document.getElementById(feedTabId).innerHTML = buffer;
                }
                else
                {
                   // alert("Failed to receive RSS file from the server - file not found");
                    return false;
                }
            }
        }
        xmlhttp.send(null);
    }
    else
    {
        alert("Your browser does not support XMLHTTP");
    }
}
function feedSetWithoutDescription(feedTabId, feedURL) {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp != null) {
        xmlhttp.open("GET", feedURL, true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    items = xmlhttp.responseXML.getElementsByTagName('item');
                    var buffer = '<div class="feed" style="height:290px; overflow:auto"><table cellpadding="2" cellspacing="2" >';
                    for (var i = 0; i < items.length; i++) {
                        var output = '<tr style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #CCCCCC; "><td  align="left" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #CCCCCC; ">' + '<img src = "' + getNode('MediaItemThumbnailURL', i) + '" width="80" height="55" onclick = "RemoveStopScreen(\'flashobject\', false); LoadMediaItemInQueueAndPlay(\'flashobject\', \'' + getNode('MediaItemGuid', i) + '\');" />' + '</td><td valign="top" style="border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: #CCCCCC; color:#8c8883; font-family:Georgia,Times New Roman,Times,serif; font-size:12px; font-weight:bold;">' + getNode('MediaItemName', i) + ' <br /></td></tr>';
                        buffer += output;
                    }
                    buffer += '</table><div>';
                    document.getElementById(feedTabId).innerHTML = buffer;
                }
                else {
                    //alert("Failed to receive RSS file from the server - file not found");
                    return false;
                }
            }
        }
        xmlhttp.send(null);
    }
    else {
        alert("Your browser does not support XMLHTTP");
    }
}





