/** 
 * Yalster.com flow-player API script for external partners
 * based on a script supplied by netpoint-media. thanks a lot!
 * 
 * we made some additions concerning the watermark and playbutton and modified the script to take
 * external keys from calling page...
 */

// this is only the fallback if base-url was not set within referring play-script
var __flow_player_base_url = 'http://www.yalster.com/flow/'; 

/**
 * let's rock
 */
function __flow_player () {
	this.key        = '';
	this.height 	= 300;
	this.width   	= 400;
	this.background	= "";	
	this.play	    = __flow_player_base_url + "play.png";	
	this.url_swf 	= __flow_player_base_url + "flowplayer.commercial-3.0.7.swf";
	this.url_cpi	= __flow_player_base_url + 'flowplayer.content-3.0.2.swf';
	this.url_pse	= __flow_player_base_url + 'flowplayer.pseudostreaming-3.0.2.swf';
	this.url_play   = __flow_player_base_url + 'play.png';
	this.url_wmark  = __flow_player_base_url + 'y_watermark.png';
	this.overlay	= {};
	
	this.playlist 	= [],
	
	this.defaults 	= {
		autoPlay: false, 
		autoBuffering: true, 
		baseUrl: ''	
	}
	this.skin 	= {	
		/*
		borderRadius: '15',
		sliderGradient: 'none',
		buttonColor: '#5F747C',
		backgroundGradient: [0.6,0.3,0,0,0],
		progressColor: '#015B7A',
		bufferColor: '#6c9cbc',
		durationColor: '#ffffff',
		backgroundColor: '#25353C',
		timeColor: '#ef9e1b',
		sliderColor: '#000000',
		buttonOverColor: '#728B94',
		bufferGradient: 'none',
		progressGradient: 'medium',
		opacity:1.0
		*/
		borderRadius: '15',
		sliderGradient: 'none',
		buttonColor: '#5F747C',
		backgroundGradient: [0.6,0.3,0,0,0],
      	progressColor: '#015B7A',
      	bufferColor: '#6c9cbc',
      	durationColor: '#ffffff',
      	backgroundColor: '#25353C',
      	timeColor: '#01DAFF',
      	sliderColor: '#000000',
      	buttonOverColor: '#728B94',
      	bufferGradient: 'none',
      	progressGradient: 'medium',
	   	opacity:1.0
	}
	this.content 	= {
		url: this.url_cpi,
		bottom: 5,
		left: Math.round((this.width-260)),
		width: 260,
		height:30,							
		backgroundColor:'#000000',
		backgroundGradient: 'low',
		borderRadius: 0,
		borderColor: '#222222',
		align: 'center',
		opacity: 0
	}

	this.setKey = function (key) {
		this.key = key;
	}
	this.setHeight = function (height) {
	     this.height = height;
  	}
  	this.setWidth = function (width) {
	     this.width = width;
  	}
	this.setBackground = function (background) {
	     this.background = background;
  	}
	this.setDefaults = function (autoPlay, autoBuffering, baseUrl) {
	     this.defaults.autoPlay = autoPlay;
	     this.defaults.autoBuffering = autoBuffering;
	     this.defaults.baseUrl = baseUrl;
  	}

	
	this.isdef = function (el) {
		if (typeof(window[el]) != 'undefined' && typeof el != "undefined" && el != "") {
			return true;
		}
		return false;
	}
	this.error = function (er) {
		if (this.isdef(er)) {
			alert(er);
		} else {
			alert("Es ist ein Fehler aufgetreten!");
		}
	}
	this.el = function (id) {
		return document.getElementById(id);
	}
	this.initPlaylist = function (url) {
		this.playlist = [ 
			{
				url: url,
				scaling: "fit"
			}
		]
	}
	this.addItemPlaylist = function (pos, element, isAd, size) {		
		if (isAd == true) {			
			if (size.x > this.width || this.height < size.y)
				return false;
		}
		
		this.content.left = Math.round((this.width-260)/2);
		var pl2 = [];		
		for(i=0; i<this.playlist.length; i++) {
			if (i == pos) {
				pl2.push(element);
			}
			pl2.push(this.playlist[i]);
		}
		
		if (pos >= this.playlist.length) {
			pl2.push(element);
		}
		this.playlist = pl2;	
	}

	this.addOverlay = function (add) {
		this.overlay = add;
	} 

	this.init = function (id) {	
		
		if (this.el(id)	== null) {			
			return false;
		}
		var id2 = id + "_" + Math.round(10324*(Math.random()));
		var _pl_div_0 = document.createElement("div");
		var _pl_div = document.createElement("div");		
		_pl_div.setAttribute("style", "display:block; cursor:pointer; text-align:center; height:" + this.height + "px; width:" + this.width + "px;margin: auto; background:url(" + this.background + ") center center no-repeat");
		_pl_div_0.setAttribute("style", "display:block;margin: auto; height:" + this.height + "px; width:" + this.width + "px;background-color:#25353C;");
		
		
		_pl_div.setAttribute("id", id2);
		var _pl_img = document.createElement("img");	
		_pl_img.setAttribute("src", this.play);
		_pl_img.setAttribute("alt", "yalster.com Video abspielen");
		_pl_img.setAttribute("title", "yalster.com Video abspielen");
		_pl_img.setAttribute("style", "margin-left:150px;margin-top: "+Math.round(this.height/2-50)+"px;");
		_pl_img.style.cssText = "margin-left:150px;margin-top: "+Math.round(this.height/2-50)+"px;";
		
		_pl_div_0.appendChild(_pl_div);
		_pl_div.appendChild(_pl_img);
		this.el(id).appendChild(_pl_div_0);
		this.init_player(id2);	
	}
	
	
	this.init_player = function (id) {
		$f(id, this.url_swf,  { 	
			//key: '#@c457d0e7c5399dd7838',
			key: this.key,
   			clip: this.defaults,
   			plugins:  { 
				controls: 	this.skin,				
				content: 	this.content    
    		},           
			playlist: this.playlist,
			screen: { 
			    width: this.width, 
			    height: this.height
			}, 
			canvas: { 
			    backgroundColor: '#000000', 
			    borderRadius:8 
			}, 
 			play: {
				replayLabel: 'Video nochmals abspielen', 
        		url: this.url_play, 
				width: 83,
				height: 83
			} 
		});
		$f(id).play(0);
	}
}