﻿// JScript File
// VideoContainer
// ContextContainer
function MRSSItem(node)
{
    // Public Members
    this.isNull = isValid(node);
    this.title = GetElementValue(node, "*", "title", "");
    this.srcUrl = GetElementValue(node, "*", "link", "");
    this.pubDate = GetElementValue(node, "*", "pubDate", "");
    this.imgUrl = GetElementValue(node, "http://search.yahoo.com/mrss/", "content", "url");
    this.duration = GetElementValue(node, "http://search.yahoo.com/mrss/", "content", "duration");
    this.description = GetElementValue(node, "http://search.yahoo.com/mrss/", "description", "");
    this.thumbnail = GetElementValue(node, "http://search.yahoo.com/mrss/", "thumbnail", "url");
    this.embedCode = GetElementValue(node, "*", "embedcode", "");
    this.rank = GetElementValue(node, "*", "rank", "");
    this.itemId = GetElementValue(node, "*", "itemid", "");
    this.categoryId = GetElementValue(node, "*", "catid", "");
    this.providerId = GetElementValue(node, "*", "pid", "");
    this.contentId = GetElementValue(node, "*", "ctid", "");
    this.pubts = GetElementValue(node, "*", "pubts", "");
    this.provider = GetElementValue(node, "*", "provider", "");
    this.category = GetElementValue(node, "*", "category", ""); 
    
    this.isValidItem = function()
    {
        if (this.isNull)
            return false;
        return true;
    }
    
    function isValid(node)
    {
        if (node != null)
            return false;
        return true;
    }
    
    function GetElementValue(node, ns, element, attribute)
    {
        var val = "";
        var elementRoot;
        
        if (node != null)
        {
            if(typeof(node.getElementsByTagNameNS) == "undefined")
            {
                return getElementsByTagNameNSWrapper(node, ns, element, attribute);
            }

            if (node.getElementsByTagNameNS(ns, element).length != 0)
            {
                elementRoot = node.getElementsByTagNameNS(ns, element);
                
                if (attribute == "")
                {
                    if (elementRoot != null && elementRoot.length != 0 && elementRoot[0].childNodes.length > 0)
                        val = elementRoot[0].childNodes[0].nodeValue;
                   
                    else if (elementRoot.length > 1)
                    {
                        if (elementRoot != null && elementRoot.length != 0 && elementRoot[1].childNodes.length > 0)
                            val = elementRoot[1].childNodes[0].nodeValue;
                    }
                }
                else 
                {
                    if (elementRoot != null && elementRoot[0].getAttribute(attribute))
                        val = elementRoot[0].getAttribute(attribute);
                }
            }
        }
        return val;
    }
    function getElementsByTagNameNSWrapper(node, ns, element, attribute)
    {
        var val = "";
        var elementRoot;
        
        if ( ns == "http://search.yahoo.com/mrss/")
            element = "media:" + element;
        
        if (node != null)
        {
            if (node.getElementsByTagName(element).length != 0)
            {
                elementRoot = node.getElementsByTagName(element);
                
                if (attribute == "")
                {
                    if (elementRoot != null && elementRoot.length != 0 && elementRoot[0].childNodes.length > 0)
                        val = elementRoot[0].childNodes[0].nodeValue;
                }
                else 
                {
                    if (elementRoot != null && elementRoot[0].getAttribute(attribute))
                        val = elementRoot[0].getAttribute(attribute);
                }
            }
        }
        return val;
    }
}


function EmbedDetails()
{    
    var PIXSY_XML_FEED_URL = "http://xml.pixsy.com/";
    //var PIXSY_XML_FEED_URL = "http://xml-beta.pixsy.com/";

    var query = "";
    var previousPg = "";
    var itemId;
    var videoEmbed;
    var infoContainer;
    var relatedContainer;
    var errorContainer;
    //this.module = new RelatedModule();
    
    function $(el)
    {
        return YAHOO.util.Dom.get(el);
    }
    
    this.init = function()
    {
        videoEmbed = $("VideoContainer");
        infoContainer = $("ContextContainer");
        relatedContainer = $("RelatedContainer");
        errorContainer = $("ErrorContainer");
        
        
        query = gup("q");
        previousPg = unescape(gup("src"));      
        
        /*if (Cookie.get("pQuery"))
            query = Cookie.get("pQuery");*/
        /*if (Cookie.get("pPrevPg"))
            previousPg = Cookie.get("pPrevPg");*/
        
        if (previousPg.lastIndexOf("%26") > (previousPg.length - 4))
            previousPg = unescape(previousPg.substring(0, previousPg.lastIndexOf("%26")));
        if (previousPg.lastIndexOf("&") == (previousPg.length - 1))
            previousPg = previousPg.substring(0, previousPg.lastIndexOf("&"));
    }
    
    this.getResults = function()
    {   
        itemId = gup("i");
        var url = PIXSY_XML_FEED_URL + "?psg=AC70711B-1768-48DE-A9E0-695C55A78143&format=pixsymrss&itemid=" + itemId;
        var surl = "proxy.ashx?content-type=text/xml&uri=" + escape(url);
        var request = YAHOO.util.Connect.asyncRequest('GET', surl, { success:successHandler, failure:failureHandler });
    }
    
    function gup( name )
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        if( results == null )
            return "";
        else
            return results[1];
    }
    
    function successHandler(obj)
    {
        var xmlDoc = obj.responseXML.documentElement;
        var item = new MRSSItem(xmlDoc.getElementsByTagName("item")[0]); 
        setPageResults(item);
    }

    function failureHandler(obj)
    {
        alert(o.status + " " + o.statusText);
    }
    
    /* Hard code providers who don't have scalable flash players here */
    function pidException(pid)
    {
        var pidArray = new Array("242","1088","1107","1108","1113");
        for (var i = 0; i < pidArray.length; i++)
        {
            if (pid == pidArray[i])
                return true;
        }
        return false;
    }
    function setPageResults(item)
    {
        if (!item.isValidItem())
        {
            errorContainer.innerHTML = "<span>The item you selected no longer exists in our database. </span>" + "<a href='default.aspx' target='_top'> Click here </a>" + "<span>to return to the homepage.</span>";
            videoEmbed.style.display = "none";
            infoContainer.style.display = "none";
        }
        else
        {
            if (!pidException(item.providerId))
                videoEmbed.innerHTML = formatEmbed(item.embedCode);
            else
                videoEmbed.innerHTML = item.embedCode;
                
            infoContainer.innerHTML = getContextInfo(item);
            
            if (query != "")
                query += "&catid=" + item.categoryId;
            else
                query = "catid=" + item.categoryId;
            
            var module = new RelatedModule();
            module.init('18', "itemid=" + itemId, '');
            relatedContainer.style.display = "block";
        }
    }
    
    function formatEmbed(obj)
    {
        var code = obj;
        
        if (code.search("&lt;") != -1)
            code.replace(/&lt;/g, "<");
        if (code.search("&gt;") != -1)
            code.replace(/&gt;/g, ">"); 
        
        if (code.search(/width/i) != -1)
        {
            var array = code.split(" ");
            var str = "";
            for (var i = 0; i < array.length; i++)
            {
                var tmpStr;
                var paramIndex;
                var startIndex;
                var endIndex;
                
                if (array[i].search(/width/i) != -1)
                {
                    tmpStr = array[i];
                    tmpStr.replace("'",'"');
                    
                    paramIndex = tmpStr.toLowerCase().indexOf("width");
                    startIndex = tmpStr.indexOf('"', paramIndex);
                    endIndex = tmpStr.indexOf('"', startIndex + 1);
                    
                    tmpStr = tmpStr.substring(0, startIndex) + "'525'" + tmpStr.substr(endIndex + 1); 
                    
                    array[i] = tmpStr;
                    //array[i] = "width='525'";
                }
                if (array[i].search(/height/i) != -1)
                {
                    tmpStr = array[i];
                    tmpStr.replace("'",'"');
                    
                    paramIndex = tmpStr.toLowerCase().indexOf("height");
                    startIndex = tmpStr.indexOf('"', paramIndex);
                    endIndex = tmpStr.indexOf('"', startIndex + 1);
                    
                    tmpStr = tmpStr.substring(0, startIndex) + "'525'" + tmpStr.substr(endIndex + 1); 
                    
                    array[i] = tmpStr;
                    //array[i] = "height='525'";
                }
                    
                str += array[i] + " ";
            }
            code = str;
        }
        //alert(code);
        return code;
    }
    
    function formatDate(timestamp)
    {
        var date = "";
        
        if (timestamp != "")
        {
            var arry = timestamp.split("-");
            var year = arry[0];
            var month = arry[1];
            var dayArry = arry[2].split("T");
            var day = dayArry[0];
            var time = dayArry[1];
            var pm = false;
            var timeArry = time.split(":");
            var hour = timeArry[0];
            var min = timeArry[1];
            if (hour >= 12 && hour != 24)
                pm = true;
            hour = hour % 12;
            
            date += month + "/" + day + "/" + year + " " + hour + ":" + min;
            if (pm)
                date += " PM GMT";
            else
                date += " AM GMT";
        }
        
        return date;
    }
    
    function formatDuration(duration)
    {
        var min = "";
        var sec = duration;
        
        if (duration > 60)
        {
            min = Math.floor(duration / 60);
            sec = duration % 60;
            
            duration = min + " min " + sec + " sec";
        }
        else
            duration = sec + " sec";
            
        return duration;
    }
   
    function getContextInfo(item)
    {    
        var html = "";
        var deliciousLink = "<a target='_blank' href='http://del.icio.us/post?v=4&noui&jump=close&url=" + escape(location.href) + "&title=" + escape(document.title) + "','delicious','toolbar=no,width=700,height=400'); return false;'><img src='/images/master/delicious.gif' alt='' border='0' />Delicious</a>";
        var technoratiLink = "<a target='_blank' href='http://technorati.com/faves?add=" + escape(location.href) + "&title=" + escape(document.title) + "','Technorati','toolbar=no,width=800,height=600,resizable=yes,scrollbars=yes'); return false;'><img src='/images/master/technorati.png' alt='' title='Technorati' border='0'/>Technorati</a>";
        var diggLink = "<a target='_blank' href='http://digg.com/submit?phase=2&url=" + escape(location.href) + "&title=" + escape(document.title) + "','Digg','toolbar=no,width=800,height=600,resizable=yes,scrollbars=yes'); return false;'><img src='/images/master/digg.gif' alt='' title='Digg' border='0'/>Digg</a>";
        var stumbleLink = "<a target='_blank' href='http://www.stumbleupon.com/submit?url=" + location.href + "&title=" + document.title + "','Stumble','toolbar=no,width=800,height=600,resizable=yes,scrollbars=yes'); return false;'><img src='/images/master/stumble.gif' alt='' title='Stumble' border='0'/>Stumble</a>";
        
        
        html += "<a class='list_title_link' href='" + item.srcUrl + "'>" + item.title + "</a>";
        html += "<div class='list_graytext' style='padding-top: 5px'><a title='See more content from " + item.provider + "' href='search.aspx?pid=" + item.providerId + "'><b>" + item.provider + "</b></a> - " + formatDate(item.pubDate) + "</div>";
        html += "<div class='list_graytext' style='padding-top: 5px'><b>Category: </b><a title='See more content from the " + item.category + " category' href='search.aspx?catid=" + item.categoryId + "'>" + item.category + "</a></div>";
        if (item.duration != "")
            html += "<div class='list_graytext' style='padding-top: 5px'><b>Duration: </b>" + formatDuration(item.duration) + "</div>";
        html += "<div class='list_text' style='padding-top: 5px'>" + trimText(item.description, 250) + "</div>";
        
//        html += "<div class='thumb_linkdiv' style='padding-bottom: 5px'><a target='_top' href='emailfriend.aspx?i=" + item.itemId + "q=" + query + "'>Email</a>&nbsp;|&nbsp";
//        html += "<span id='saveLink'><a href='javascript:saveMyItem(%27" + item.itemId + "%27, %27saveLink%27);'>Savexxx</a></span>&nbsp;|&nbsp;";
        html += "<div class='thumb_linkdiv' style='padding-bottom: 5px'>";
        
               
        
        html += "<span class='tagbots'><span class='tagbot_header'>Share it: </span>";
        html += "<span class='tagbot'>" + deliciousLink + "</span>&nbsp;";
        html += "<span class='tagbot'>" + technoratiLink + "</span>&nbsp;";
        html += "<span class='tagbot'>" + diggLink + "</span>&nbsp;";
        html += "<span class='tagbot'>" + stumbleLink + "</span>&nbsp;";
        html += "</span><div style='padding-top: 12px'>This item may be subject to copyright.</div></div>";
        
        html += "<div style='font-size: 70%; padding-top: 5px;'>";
        if (previousPg != "")
            html += "<a target='_top' class='link' href='" + previousPg +"'>back to <b>results</b></a>&nbsp;>";
        html += "<a target='_top' class='link' href='search.aspx?pid=" + item.providerId + "'> more from <b>" + item.provider + "</b></a>&nbsp;>";
        html += "<a target='_top' class='link' href='search.aspx?catid=" + item.categoryId + "'> more from <b>" + item.category + "</b></a>&nbsp;</div>";
        
        return html;
    }
    
    function trimText(text, len)
    {
        var trimmed = "";
        if (text)
        {
            trimmed = (text.length > len ? text.substring(0, len) + "..." : text);
        }
        return trimmed;
    }

}
