
function flashPlayer(playerId) {

    this.flashPlayerRoot = 'http://embeddedplayer.visionip.tv/data';
    
    this.detectIphone = function() {
	var agent = navigator.userAgent.toLowerCase();
	var is_iphone = (agent.indexOf('iphone')!=-1);
        if(is_iphone) {
    	    this.iphone = true;
    	    var iphone = document.createElement('video');
	    iphone.setAttribute('id', this.playerId);
	    iphone.setAttribute('name', this.playerId);
    	    iphone.setAttribute('src', this.applehttpmobile);
    	    iphone.setAttribute('width', this.width);
    	    iphone.setAttribute('height', this.height);
    	    iphone.setAttribute('posters', this.image);
    	    iphone.setAttribute('controls', true);
	    this.player.parentNode.replaceChild(iphone, this.player);
	    return true;
	} else {
	    this.iphone = false;
	}
	return false;
    }

    this.gup = function(name, querystring) {
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(querystring);
	if(results == null) {
	    return "";
	} else {
	    return results[1];
	}
    }
    
    this.sethd = function() {
	this.load(this.hdbitrate);
    }
    this.setmobile = function() {
	this.load(this.mobilebitrate);
    }
    this.setmedium = function() {
	this.load(this.mediumbitrate);
    }
    this.sethi = function() {
	this.load(this.hibitrate);
    }
    
    this.load = function(file) {
	this.file = file;
	this.player.sendEvent("STOP", true);    
	this.player.sendEvent("LOAD",{file: file, streamer: this.streamer, provider:"rtmp"});
	this.player.sendEvent("PLAY", true);    
	this.swapButtons();
    }
    
    this.initButtons = function() {
	if(this.hdbitrate) { this.attachButton(this.playerId + 'hdbitrate', 'hd'); }
	if(this.hibitrate) { this.attachButton(this.playerId + 'hibitrate', 'hi'); }
	if(this.mediumbitrate) { this.attachButton(this.playerId + 'mediumbitrate', 'medium'); }
	if(this.mobilebitrate) { this.attachButton(this.playerId + 'mobilebitrate', 'mobile'); }
    }
    
    this.swapButtons = function() {
	this.swapButtonsSingle('hd');
	this.swapButtonsSingle('hi');
	this.swapButtonsSingle('medium');
	this.swapButtonsSingle('mobile');
    }
    
    this.swapButtonsSingle = function(bitrate) {
	var button = document.getElementById(this.playerId + bitrate + 'bitrateimage');
	if(button) {
	    var tmp;
	    if(bitrate == 'hd') tmp = this.hdbitrate;
	    if(bitrate == 'hi') tmp = this.hibitrate;
	    if(bitrate == 'medium') tmp = this.mediumbitrate;
	    if(bitrate == 'mobile') tmp = this.mobilebitrate;
	    if(tmp == this.file) {
		button.src = this.flashPlayerRoot + '/images/flashPlayer/' + bitrate + '.png';
	    } else {
	        button.src = this.flashPlayerRoot + '/images/flashPlayer/' + bitrate + '-off.png';
	    }
	}
    }

    this.initParams = function() {
	this.streamer = this.gup('streamer', this.player.data);
	this.file = this.gup('file', this.player.data);
	this.image = this.gup('image', this.player.data);
    }
    
    this.attachButton = function(id, bitrate) {
	var div = document.getElementById(id);
	if(div) {
	    var button = document.createElement('input');
	    button.setAttribute('id', playerId + bitrate + 'bitrateimage');
	    button.setAttribute('name', playerId + bitrate + 'bitratebutton');
	    button.setAttribute('type', 'image');
    	    button.setAttribute('src', this.flashPlayerRoot + '/images/flashPlayer/' + bitrate + '.png');
	    button.setAttribute('value', bitrate);
	    if(bitrate == 'hd') button.setAttribute('title',  'Play in HD quality');
	    if(bitrate == 'hi') button.setAttribute('title',  'Play in Hi quality');
	    if(bitrate == 'medium') button.setAttribute('title',  'Play in Medium quality');
	    if(bitrate == 'mobile') button.setAttribute('title',  'Play in Mobile quality');
	    button.setAttribute('onclick',  this.playerId + 'obj.set'+ bitrate +'();');
	    div.appendChild(button);
	}
    }
    
    //constructor
    this.playerId = playerId;
    this.player = document.getElementById(playerId);
	for(i = 0; i < this.player.childNodes.length; i++) {
	    if(this.player.childNodes[i].name == 'hdbitrate')	this.hdbitrate = this.player.childNodes[i].value;
	    if(this.player.childNodes[i].name == 'hibitrate')	this.hibitrate = this.player.childNodes[i].value;
	    if(this.player.childNodes[i].name == 'mediumbitrate')	this.mediumbitrate = this.player.childNodes[i].value;
	    if(this.player.childNodes[i].name == 'mobilebitrate')	this.mobilebitrate = this.player.childNodes[i].value;
	    if(this.player.childNodes[i].name == 'applehttpmobile')	this.applehttpmobile = this.player.childNodes[i].value;
	}
    this.width = this.player.width;
    this.height = this.player.height;
    this.detectIphone();
    this.initParams();
    if(!this.iphone) {
	this.initButtons();
        this.swapButtons();
    }
}
