﻿
function ClipInfo()
{
    this.code = "";
    this.title = "";
    this.brief = "";
}

function ClipPlayer(ref, controlSrc, displayWidth, titleWidth)
{
    this.wmpControls = new Image();
    this.wmpControls.src = controlSrc || "/images/wmpcontrol_content.jpg";
    this.me = ref;

    this.playingObject = null;
    this.lastID = 0;
    this.div = null;
    this.clips = new Array();

    this.displayHeight = "348px";
    this.displayWidth = displayWidth|| "428px";
    this.titleWidth = titleWidth || "422px";
    this.briefWidth = "422px";
    this.briefHeight = "25px";

    this.mediaDisplayClass = "mediaDisplay";
    this.mediaTitleClass = "mediaTitle";
    this.mediaBriefClass = "mediaBrief";

    this.load = function(div)
    {
        this.div = document.getElementById(div);
        if ( !this.div )
            return false;
        
        loading = document.getElementById("videoloading");
        if ( loading )
            loading.style.display = 'none';
        
        embed = this.div.getElementsByTagName("div");
        if ( embed.length == 0 )
            return false;
        
        for ( i = 0; i < embed.length; i ++ )
        {
            if ( embed[i].id == "videoloading" )
                continue;
            
            embed[i].id = "mediaElement_" + this.clips.length;
            embed[i].style.display = 'none';

            c = new ClipInfo();
            c.title = embed[i].getAttribute("title")+"";
            c.brief = embed[i].getAttribute("brief")+"";
            
            // for video
            spans = embed[i].getElementsByTagName("span");
            if ( spans && spans.length && spans.length > 0 )
            {
                for ( d = 0; d < spans.length; d ++ )
                {
                    type = spans[d].getAttribute("type");
                    if ( type == "bigthumb" )
                    {
                        img = document.createElement("img");
                        img.src = this.wmpControls.src;
                        img.style.width = displayWidth || "453px";
                        img.style.height = "44px";
                        br = document.createElement("br");
                        spans[d].appendChild(br);
                        spans[d].appendChild(img);

                        spans[d].id = "mediaBigThumb_" + this.clips.length;
                        spans[d].style.display = 'none';
                        spans[d].onclick = function () { pPlayer.play(this.id.replace("mediaBigThumb_", "")); }
                    }
                    else if ( type == "object" )
                    {
                        spans[d].id = "mediaObject_" + this.clips.length;
                        spans[d].style.display = 'none';
                    }
                    else if ( type == "embed" )
                    {
                        spans[d].id = "mediaEmbed_" + this.clips.length;
                        spans[d].style.display = 'none';
                    }
                }
            }

            this.clips[this.clips.length] = c;
        }

        if ( this.clips.length > 1 )
        {
            for ( i = 0; i < this.clips.length; i ++ )
            {
                t = document.createElement("div");
                t.id = "mediaTitle_" + i;
                t.className = this.mediaTitleClass;
                t.style.width = this.titleWidth;
                if ( i == 0 )
                    t.style.borderTop = "solid 1px black";
                if ( this.clips[i].title == "" )
                    t.innerHTML = "video";
                else
                    t.innerHTML = this.clips[i].title;
                t.onclick = function () { pPlayer.show(this.id.replace("mediaTitle_", "")); }
                this.div.appendChild(t);

                b = document.createElement("div");
                b.id = "mediaBrief_" + i;
                b.style.width = this.briefWidth;
                b.style.display = 'none';
                
                if ( this.clips[i].brief != "" )
                {
                    b.style.height = this.briefHeight;
                    b.innerHTML = this.clips[i].brief;
                    b.style.overflow = "hidden";
                    b.className = this.mediaBriefClass;
                }
                else
                {
                    b.innerHTML = "";
                    b.style.height = "0px";
                }
                
                this.div.appendChild(b);
            }
        }

        this.show(0);
        this.div.style.display = '';
        
        return true;
    }
    
    this.play = function(idx)
    {
        bigthumb = document.getElementById("mediaBigThumb_"+idx);
        if ( bigthumb ) bigthumb.style.display = 'none';
        
        embed = document.getElementById("mediaEmbed_"+idx);
        if ( !document.all && embed )
        {
            embed.innerHTML = embed.innerHTML.replace('autostart="0"', 'autostart="1"');
            embed.style.display = '';
        }

        activex = document.getElementById("mediaObject_"+idx);
        if ( document.all && activex )
        {
            objs = activex.getElementsByTagName("object");
            if ( objs && objs.length && objs.length >= 0 )
            {
                objs[0].play();
                this.playingObject = objs[0];
            }

            activex.style.display = '';
        }
    }

    this.show = function(idx)
    {
        if ( this.playingObject != null )
            this.playingObject.stop();

        element = document.getElementById("mediaElement_"+this.lastID);
        embed = document.getElementById("mediaEmbed_"+this.lastID);
        bigthumb = document.getElementById("mediaBigThumb_"+this.lastID);
        activex = document.getElementById("mediaObject_"+this.lastID);
        title = document.getElementById("mediaTitle_"+this.lastID);
        last = document.getElementById("mediaBrief_"+this.lastID);

        if ( element) element.style.display = 'none';
        if ( title ) title.className = this.mediaTitleClass;
        
        element = document.getElementById("mediaElement_"+idx);
        title = document.getElementById("mediaTitle_"+idx);
        embed = document.getElementById("mediaEmbed_"+idx);
        bigthumb = document.getElementById("mediaBigThumb_"+idx);
        activex = document.getElementById("mediaObject_"+idx);
        next = document.getElementById("mediaBrief_"+idx);

        if ( bigthumb ) bigthumb.style.display = '';
        if ( embed ) embed.style.display = 'none';
        if ( activex ) activex.style.display = 'none';
        if ( element ) element.style.display = '';
        if ( title ) title.className = this.mediaTitleClass + "Active";

        if ( last && last.innerHTML != "" )
            slideup(last.id);
        if ( next && next.innerHTML != "" )
            slidedown(next.id);

        this.lastID = idx;
    }
}
