/******************************************************************/
/* © DMC 01 Consulting & Development GmbH - gerald.urbas@dmc01.at */
/******************************************************************/
/* Player Prototype                                               */
/******************************************************************/
/* is used by player plugins to save general funcitons and vars   */
/* provides replace funtion for finishing HTML strings            */
/******************************************************************/

/*
Prototype for different Player objects
*/

function playercode() {
    // define Object vars
    this.name = "";
    this.typ = "";
    this.detectionplugin = "";
    this.objid = "";
    this.globid = false;
    this.playerstring = "";
    this.playercontrols="";
    this.playertype=false;
    this.version = false;
    this.livestream = false;
    this.mp3stream = false;
    this.enabled = false;
    this.vsizes=false;
    this.autostart=false;
    this.enablebuffering=true;
    this.is_scriptable = false;
    this.flashstreamingserver=false;
    this.FLASHEMBEDW="";
    this.FLASHEMBEDH="";
    this.FLASHEMBEDHQW="";
    this.FLASHEMBEDHQH="";
    // define Object vars

}
function cutflv(tfilename) {
                    var streamingurl = new RegExp('.flv$','g');
                    var su = streamingurl.exec(tfilename);
                    if (su=='.flv') {
                        filename = tfilename.substr(0,(tfilename.length-4));
                        this.debug("Streaming URL cut .flv on End of stream");
                    } else {
                        filename = tfilename
                        this.debug("Streaming URL .flv isnt provided");
                    }
                    return filename;
    }
// public replace funtion
function replaceVars(pstring,conn) {
        var filename="";
        var server = "";
        var myurl = this.connurls[conn];
        if (this.typ=='flashmedia') {
            dbg("Flash Streaming URL cuttin started url:("+myurl+")");
            if (this.flashstreamingserver) {
                dbg("Streamingserver info is avaible!")
                server = this.flashstreamingserver;
                var re = new RegExp(this.flashstreamingserver,'g');
                var m = re.exec(myurl);
                if (m == null) {
                    dbg("Perfect streaming Url combination provided")
                    filename = this.cutflv(myurl);
                    this.debug("Streaming URL OHNE Server"+ server+" "+filename);
                } else {
                    dbg("Streaming server provided in URL, cut the Serverstring")
                    var serverindex = myurl.indexOf('://');
                    var tserver = myurl.substr(serverindex+3);
                    filename =  this.cutflv(tserver.replace(re,""));
                    this.debug("Streaming URL mit Server"+ server+" "+filename);
                }
            } else {
                dbg("No streamingserver provided")
                var serverindex = myurl.indexOf('://');
                var filenameindex = myurl.lastIndexOf('/');
                var tfilename = myurl.substr(filenameindex+1)
                var tserver = myurl.substr(serverindex+3);
                var t2server = tserver.substr(0,(tserver.length-tfilename.length));
                var re = new RegExp("http://",'g');
                var m = re.exec(myurl);
                if (m == null) {
                    server = t2server;
                    filename = this.cutflv(tfilename);
                    this.debug("Streaming URL Ohne benannten FMS "+ server+" "+filename);
                }else{
                    server = "";
                    filename = myurl
                    this.debug("HTTP File DL "+ server+" "+filename);
                }
            }
            if (filename=='' && server=='') {
                this.debug("ALERT: FMS Streaming URLS failed to parse!");
            }
        }
        var m3uindex = myurl.lastIndexOf('.m3u');
        var mp3index = myurl.lastIndexOf('.mp3');
        var mp3index_upp = myurl.lastIndexOf('.MP3');
        var isaudio = "0";
        var audiomode = 3;
        var ismp3 = "0";
        var islive = "0";
        
        if (mp3index!=-1 || mp3index_upp!=-1 || m3uindex!=-1) {
            ismp3 = "1";
            isaudio = "1";
            audiomode = 3;
        }

        if (this.livestream == true) {
            islive = "1";
        }
        if (this.mp3stream == true) {
            isaudio = "1";
            if (this.livestream == false) {
                audiomode = 0;
                islive="0";
            }
            if (this.livestream == false) {
                audiomode = 3;
                islive="0";
            }
            if (this.livestream == true) {
                audiomode = 3;
                islive="1";
            }
            if (this.playertype == 'simple') {
                audiomode = 3;
            }
        }
        this.previewimage = this.getPreviewimage();
        //alert("params: this.playertype "+this.playertype+" this.livestream "+this.livestream+" this.mp3stream"+this.mp3stream);
        pstring=pstring.replace(/%FLASHEMBEDW%/g,this.FLASHEMBEDW); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDH%/g,this.FLASHEMBEDH); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDHQW%/g,this.FLASHEMBEDHQW); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDHQH%/g,this.FLASHEMBEDHQH); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDSIMPLEW%/g,this.FLASHEMBEDSIMPLEW); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDSIMPLEH%/g,this.FLASHEMBEDSIMPLEH); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDAUDIOW%/g,this.FLASHEMBEDAUDIOW); // special for flashplayer
        pstring=pstring.replace(/%FLASHEMBEDAUDIOH%/g,this.FLASHEMBEDAUDIOH); // special for flashplayer
        pstring=pstring.replace(/%PREIMG%/g,this.previewimage); // special for flashplayer
        pstring=pstring.replace(/%AUTOSTART%/g,this.autostart); // special for flashplayer
        pstring=pstring.replace(/%W%/g,this.vsizes[conn]['w']); // movie size W
        pstring=pstring.replace(/%H%/g,this.vsizes[conn]['h']); // movie size H
        pstring=pstring.replace(/%EMBEDH%/g,(this.vsizes[conn]['h']+this.playercontrolsheight[this.playertype]['embed'])); // RP + MMP needs some correction of the output size
        pstring=pstring.replace(/%OBJECTH%/g,(this.vsizes[conn]['h']+this.playercontrolsheight[this.playertype]['object'])); // RP + MMP needs some correction of the output size
        pstring=pstring.replace(/%SERVER%/g,server);
        pstring=pstring.replace(/%FILENAME%/g,filename);
        pstring=pstring.replace(/%URL%/g,myurl);
        pstring=pstring.replace(/%ISLIVE%/g,islive);
        pstring=pstring.replace(/%AUDIO%/g,isaudio);
        pstring=pstring.replace(/%ISMP3%/g,ismp3);
        pstring=pstring.replace(/%MODE%/g,audiomode);
        pstring=pstring.replace(/%BUFFER%/g,this.enablebuffering);
        pstring=pstring.replace(/%VERSION%/g,this.version);
        pstring=pstring.replace(/%EMBEDCONTROLS%/g,this.playercontrols[this.playertype]['embed']); // 4 embeds replace special player setteing (window, controls etc)
        pstring=pstring.replace(/%OBJECTCONTROLS%/g,this.playercontrols[this.playertype]['object']); // 4 objects replace special player setteing (window, controls etc)
        //alert(pstring+" OURL:"+this.connurls[conn]);
        //this.debug(pstring)
        return pstring; // return finished string...
}

// templatefunction for Player types
function writeplayercode(targetobjid,conn) {
        var pstring = this.playerstring;
        pstring=this.replace(pstring,conn);
        $j("#"+targetobjid).empty();
        $j("#"+targetobjid).append(pstring);
        //alert("auto"+ this.autostart+" eb"+this.enablebuffering+" pi"+this.previewimage);
        //alert("pt: "+ this.name+"auto"+ this.autostart+" eb"+this.enablebuffering+" pi"+this.previewimage);
        if (this.name!='Flash' && this.autostart==0 && this.enablebuffering==0 && this.previewimage!= false) {
            //alert('add Image')
            this.previewImage(targetobjid,conn);
        } else {
            this.playercall(targetobjid,conn);
        }
} 

function previewImagecall(targetobjid,conn) {
    var gid = this.getglobid();
    $j("#"+targetobjid).empty();
    
    $j("#"+targetobjid).attr('style','line-height:0; margin-bottom:3px;'); // wew (tsi mms): eliminate image gap in firefox
    $j("#"+targetobjid).append('<span id="genplayer'+gid+'" src="'+this.previewimage+'" />');
    $j("#genplayer"+gid).append('<IMG src="'+this.previewimage+'" /><br /><IMG src="/ard/static/pics/gen/playvideo.gif" />');
    $j("#genplayer"+gid).attr('globid',gid);
    $j("#genplayer"+gid).attr('tgt',targetobjid);
    $j("#genplayer"+gid).attr('conn',conn);
    //alert($j("#"+targetobjid).attr('globid'))
    $j("#genplayer"+gid).click(function () {
        var mygid = $j(this).attr('globid');
        var mytgt = $j(this).attr('tgt');
        var myconn = $j(this).attr('conn');
        var myplayer = loaded_Players[mygid];
        myplayer.players[myplayer.currentplayer].autostart=true;
        myplayer.players[myplayer.currentplayer].playercall(mytgt,myconn);
    });
}

// templatefunction for setting sizes
function setsizes(arr) {
    s = Array();
    for (as in this.getconn()) {
        s.push(arr[as]);
    } 
    this.vsizes = arr;
}

function setconn(urls) {
        if (urls.length>0) {
            dbg(urls.length + "URL avaible:");
            this.connurls=urls;
        } else {
            dbg("no URL avaible:"+urls.length+" :"+this.name+" is disabled");
            this.enabled = false;
        }
}

function setplayertype(pt,v,l,a,fms,b) {
    this.playertype=pt;
    this.version = v;
    this.livestream = l;
    this.mp3stream = a;
    this.flashstreamingserver = fms;
    if (b==true)
    {
        this.enablebuffering = 1;
    } else {
        this.enablebuffering = 0;
    }

}

function setautostart(bool) {
    if (bool == true) {
        current_Player = this.globid;
        current_PlayerDomId = this.objid;
        this.autostart = "1";
    }else {
        this.autostart = "0";
    }
}

function setglobid(str) {
    this.globid = str;
}

function getglobid() {
    return this.globid;
}


function playerPopup() {
	this.debug("Open Popup");
	if (player.popup_url!=false) { // wew (tsi mms): use custom popup url
		obj_f_popup = window.open(player.popup_url,'fpopup','directories=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,location=no,hotkeys=no');
		//popup.player = player;
		obj_f_popup.focus();
	} else {
		this.debug("No Popup Url defined!");
	}
}

function carrierPopup(h) {
    this.debug("Open Carrier");
    if (player.carrier_url!=false) {
        obj_f_carrier = window.open(player.carrier_url,'fcarrier','height='+h+',width=256,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,location=no,hotkeys=no');
        //popup.player = player;
        obj_f_carrier.focus();
    } else {
        this.debug("No Carrier Url defined!");
    }
}

function playerPopupClose() {
    this.debug("Close Popup");
    this.debug("Close Popup: "+obj_f_popup+" "+parent.videoopended+" "+videoopended);
    obj_f_popup=false;
    parent.videoopended=false;
    videoopended=false;
    this.debug("Close Popup: "+obj_f_popup+" "+parent.videoopended+" "+videoopended);
}

function setPreviewImage(img) {
    this.previewimage = img;
}

function getPreviewImage(img) {
    return this.previewimage;
}
