
/**
 * Calls a json configuration utility URL
 *
 * @param elem the element to add the video to
 * @param jsonURL the config url to call
 */
function Player(elem, jsonURL, width, height) {
    this._width = width;
    this._height = height;
    if (typeof(width) == "undefined") this._width = "100%";
    if (typeof(height) == "undefined") this._height = "100%";
    this.element = elem;
    if (!swfobject.hasFlashPlayerVersion("9.0.0")) {
        var that = this;
        $.getJSON(jsonURL, jQuery.proxy(that.configLoaded, that));
    }
}

Player.prototype = {
    config  : null,
    element : null,
    authURL : "/ws/authws.json.php?do=",
    _width  : "100%",
    _height : "100%",
    
    /**
     * Configuration loaded with data
     *
     * @param data some json string
     */
    configLoaded : function(data) {
        //save config
        this.config = data;
        //display meta information
        this.displayMeta();
    },
    
    /**
     * Displays headlines and a big 
     * play button, if a video is available
     */
    displayMeta : function() {
        //load the image into the alternative content
        $('#' + this.element).append($('<img src="' + this.config.PREVIEW_PATH + '">'));
        //check, if video exists
        if (this.config.ITEMS != null) {
            this.play();
        } else {
            $('#' + this.element).append($('<h1>').html(this.config.NAME + " (nicht verf&uuml;gbar)"));            
        }
    },
    
    /**
     * Authenticates the user,
     * displays the html 5 video control
     */
    play : function() {
        var that = this;
        var authType = "authStream";
        if (this.config.VIDEO_TYPE == "sportline") {
            this.config.SERVICE_SUFFIX = "wbr-free/sportline";
            authType = "authSportline";
        } else if (this.config.VIDEO_TYPE == "free") {
            authType = "authFree";
        } else {
            authType = "authStream";
        }
        $.getJSON(
					this.authURL + authType + "&sid=" + ppf.getSID() + "&streamUrl=" + "http://btd-flv-lbwww-01.odmedia.net/sec/" + this.config.SERVICE_SUFFIX + "/" + this.config.TYPE_PATH_SUFFIX + this.config.ITEMS[0].PATH,
					jQuery.proxy(that.displayVideo, that)
				);
    },
    
    /**
     * Displays the video element inside of the 
     * alternate content
     *
     * @param data json object containing the streaming url
     */
    displayVideo : function(data) {
        $('#' + this.element).empty();
        var video = $('<video src="' + data.streamUrl + '" controls poster="' + this.config.PREVIEW_PATH + '" width="' + this._width + '" height="' + this._height + '">');
        $('#' + this.element).append(video);
    }
}

/**
 * A Teaser Video is playing,
 * switch of the respective meta
 * field in the teaser,
 * stop it from cycling
 */
function teaserVideoPlay() {
    var myTeaser = topteaser;
    if (myTeaser == null) myTeaser = teaser1;
    myTeaser.hideMeta();
    myTeaser._removeTimer();
}

/**
 * A Teaser Video is playing,
 * switch of the respective meta
 * field in the teaser,
 * stop it from cycling
 */
function teaserVideoStop() {
    var myTeaser = topteaser;
    if (myTeaser == null) myTeaser = teaser1;
    myTeaser.showMeta();
    if (!myTeaser.stopped) myTeaser._addTimer();
}

