function loadHome()
{
    LoadDynamicControls(
        ["", "/Public/Controls/RSSTopics.ascx"],
        ["phLeftPanel", "phLeftPanel"],
        ["RemoveAll", "Add"],
        ["", ""],
        []
    );
    loadRightContent("homeImage");
}

function loadRSSItem(itemGuid)
{
    LoadDynamicControls(
        ["", "/Public/Controls/RSSContent.ascx"],
        ["phRightPanel", "phRightPanel"],
        ["RemoveAll", "Add"],
        ["", ""],
        [itemGuid]
    );
}

function loadRightContent(tag)
{
    LoadDynamicControls(
        ["", "/Public/Controls/ContentLoader.ascx"],
        ["phRightPanel", "phRightPanel"],
        ["RemoveAll", "Add"],
        ["", ""],
        [tag]
    );
}

var playlist = new Array();
function addVideo(id, filename, title, description, width, height, autoStart) {
    playlist.push({id:id, file: filename, title: title, description:description, width: width, height: height, autoStart: autoStart });
}

function addSectionHeader(headerText, isCollapsed) {
    playlist.push({ headerText: headerText, isCollapsed: isCollapsed });
}

function createPlaylist(playlistContainerID, playerContainerID) {
    var divsToCollapse = new Array();
    var playlistContainer = document.getElementById(playlistContainerID);
    playlistContainer.innerHTML = "";
    var firstVideoIndex = -1;
    for (i = 0; i < playlist.length; i++) {
        if (playlist[i]["headerText"] != null) {
            playlistContainer.innerHTML += "<div id='sectionHeader_" + i + "' class='sectionHeader expanded' onclick='expandCollapseSection(this)'>" + playlist[i]["headerText"] + "</div>";
            if (playlist[i]["isCollapsed"])
                divsToCollapse.push(i);
        } else {
            if (firstVideoIndex < 0)
                firstVideoIndex = i;
            playlistContainer.innerHTML += "<a id=\"videolink_" + playlist[i]["id"] + "\" href=\"javascript://\" onclick=\"setCurrentPlayingVideo(this);playVideo('" + playlist[i]["file"] + "', '" + playerContainerID + "', " + playlist[i]["width"] + ", " + playlist[i]["height"] + ", " + playlist[i]["autoStart"] + ")\">" + playlist[i]["title"] + "</a><div class='description'>" + playlist[i]["description"] + "</div>";
        }
    }

    for (i = 0; i < divsToCollapse.length; i++) {
        var divToCollapse = document.getElementById("sectionHeader_" + divsToCollapse[i]);
        expandCollapseSection(divToCollapse);
    }
    if (getURLParameter("video") == "") {
        if (firstVideoIndex > -1)
            playVideo(playlist[firstVideoIndex]["file"], playerContainerID, playlist[firstVideoIndex]["width"], playlist[firstVideoIndex]["height"], playlist[firstVideoIndex]["autoStart"]);
    }
    else {
        var videoLink = document.getElementById("videolink_" + getURLParameter("video"))
        if (videoLink != null) {
            videoLink.onclick();
            var allDivs = document.getElementsByTagName("DIV");
            for (j = 0; j < allDivs.length; j++) {
                if (allDivs[j].id != null && allDivs[j].id.indexOf("sectionHeader_") == 0 && allDivs[j].className.indexOf("expanded") > -1) {
                    expandCollapseSection(allDivs[j]);
                }
            }
            var previousElement = videoLink.previousSibling;
            while (previousElement.id == null || previousElement.id.indexOf("sectionHeader_") == -1)
                previousElement = previousElement.previousSibling;
            expandCollapseSection(previousElement);
        }
        else
            alert("Video not found!");
    }
}

function playVideo(filePath, divId, width, height, autoStart) {
    if (filePath.indexOf(".swf") == filePath.length - 4) {
        playSWF(filePath, divId, width, height);
    }
    else {
        playNonSWF(filePath, divId, width, height, autoStart);
        createTooltip(divId, width);
    }
}

function createTooltip(divId, width) {
    var videoContainer = document.getElementById(divId);
    videoContainer.innerHTML += "<div id='tooltip' style='text-align:left;width:" + width + "px;'><br/>Please click the icon <img src='/assets/images/fullscreenIcon.png' border='0'/> in the movie control to view in full screen mode.</div>";
    //var lMargin = width - 55;
    //DSTooltip.show('<div onclick="DSTooltip.hide();"></div>', 200, 'document.getElementById("tooltip")', 40, lMargin);
}

function playNonSWF(filePath, divId, width, height, autoStart) {
    /*var so = new SWFObject('http://web.screama.com/assets/video/player.swf', 'mpl', width.toString(), height.toString(), '7');
    alert(so);
    so.addParam('allowscriptaccess', 'always');
    so.addParam('allowfullscreen', 'true');
    so.addParam('wmode', 'transparent', 'true');
    so.addVariable("file", filePath);
    so.addVariable("autostart", autoStart.toString());
    so.write(divId);
*/
    var videoContainer = document.getElementById(divId);
    videoContainer.innerHTML = '<embed src="/assets/video/player.swf" width="' + width + '" height="' + height + '" bgcolor="undefined" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" flashvars="file=' + filePath + '&autostart=' + autoStart + '" />';
}

function playSWF(filePath, divId, width, height) {
    var videoContainer = document.getElementById(divId);
    videoContainer.innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' + width + '" HEIGHT="' + height + '" id="' + filePath + '" ALIGN=""><PARAM NAME=movie VALUE="' + filePath + '"><PARAM NAME=quality VALUE=high><param name="wmode" value="transparent"><EMBED src="' + filePath + '" quality=high WIDTH="' + width + '" HEIGHT="' + height + '" NAME="' + filePath + '" ALIGN="" wmode="transparent" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED> </OBJECT>';
}

function expandCollapseSection(section) {
    var displayStyle = "";
    if (section.className == "sectionHeader expanded") {
        section.className = "sectionHeader collapsed";
        displayStyle = "none";
    }
    else {
        section.className = "sectionHeader expanded";
    }
    var nextElement = section.nextSibling;

    while (nextElement != null && (nextElement.nodeName.toLowerCase() == "a" || nextElement.nodeName.toLowerCase() == "#text" || nextElement.className == "description")) {
        if (nextElement.nodeName.toLowerCase() != "#text") {
            nextElement.style.display = displayStyle;
        }
        nextElement = nextElement.nextSibling;
    }
}

function setCurrentPlayingVideo(anchorLink) {
    var allAnchors = document.getElementsByTagName("A");
    for (j = 0; j < allAnchors.length; j++) {
        if (allAnchors[j].id.indexOf("videolink_") == 0)
            allAnchors[j].className = "";
    }
    anchorLink.className = "currentPlaying";
}

function getURLParameter(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];
}

var DSTooltip = function() {
    var id = 'tt';
    var top = 3;
    var left = 3;
    var maxw = 300;
    var speed = 10;
    var timer = 20;
    var endalpha = 95;
    var alpha = 0;
    var tt, t, c, b, h;
    var ie = document.all ? true : false;
    return {
        show: function(v, w, pointToElementEval, topMargin, leftMargin) {
            tt = document.getElementById(id);
            if (tt == null) {
                tt = document.createElement('div');
                tt.setAttribute('id', id);
                t = document.createElement('div');
                t.setAttribute('id', id + 'top');
                c = document.createElement('div');
                c.setAttribute('id', id + 'cont');
                b = document.createElement('div');
                b.setAttribute('id', id + 'bot');
                tt.appendChild(t);
                tt.appendChild(c);
                tt.appendChild(b);
                document.body.appendChild(tt);
                tt.style.opacity = 0;
                tt.style.filter = 'alpha(opacity=0)';
            }
            if (c != null) {
                tt.style.display = 'block';
                c.innerHTML = v;
                tt.style.width = w ? w + 'px' : 'auto';
                if (!w && ie) {
                    t.style.display = 'none';
                    b.style.display = 'none';
                    tt.style.width = tt.offsetWidth;
                    t.style.display = 'block';
                    b.style.display = 'block';
                }
                if (tt.offsetWidth > maxw) { tt.style.width = maxw + 'px' }
                clearInterval(tt.timer);
                tt.timer = setInterval(function() { DSTooltip.fade(1) }, timer);
            }
            h = parseInt(tt.offsetHeight) + top;
            this.absolutePos(pointToElementEval, topMargin, leftMargin);
        },
        pos: function(e) {
            tt = document.getElementById(id);
            var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
            var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
            if (u < h)
                tt.style.top = (u + 20) + 'px';
            else
                tt.style.top = (u - h) + 'px';
            if (left < -1)
                tt.style.left = (l + 20) + 'px';
            else
                tt.style.left = (l + left) + 'px';
            if (l > document.body.offsetWidth - tt.offsetWidth) {
                tt.style.left = '';
                tt.style.right = '20px';
            }
        },
        absolutePos: function(pointToElementEval, topMargin, leftMargin) {
            tt = document.getElementById(id);
            var pointTo = eval(pointToElementEval);
            if (pointTo == null || pointTo == '')
                return;
            var curleft = curtop = 0;
            if (pointTo.offsetParent) {
                do {
                    curleft += pointTo.offsetLeft;
                    curtop += pointTo.offsetTop;
                } while (pointTo = pointTo.offsetParent);
            }
            u = curtop + topMargin;
            l = curleft - 193 + leftMargin;
            if (u < h)
                tt.style.top = (u + 20) + 'px';
            else
                tt.style.top = (u - h) + 'px';
            if (left < -1)
                tt.style.left = (l + 20) + 'px';
            else
                tt.style.left = (l + left) + 'px';
            if (l > document.body.offsetWidth - tt.offsetWidth) {
                tt.style.left = '';
                tt.style.right = (document.body.offsetWidth - l) + 'px';
            }
        },
        fade: function(d) {
            tt = document.getElementById(id);
            var a = alpha;
            if ((a != endalpha && d == 1) || (a != 0 && d == -1)) {
                var i = speed;
                if (endalpha - a < speed && d == 1) {
                    i = endalpha - a;
                } else if (alpha < speed && d == -1) {
                    i = a;
                }
                alpha = a + (i * d);
                tt.style.opacity = alpha * .01;
                tt.style.filter = 'alpha(opacity=' + alpha + ')';
            } else {
                clearInterval(tt.timer);
                if (d == -1) { tt.style.display = 'none' }
            }
        },
        hide: function() {
            clearInterval(tt.timer);
            tt.timer = setInterval(function() { DSTooltip.fade(-1) }, timer);
        }
    };
} ();
