/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */

componentFlash = function(name)
{
	//inherited	
	this.componentElement = componentElement;
	this.componentElement(name);
	this.resourcesStatus = "online";
	this.resourcesURL = "";
	
	this.bgcolor = "transparent";
	this.quality = "high";
	this.loop = "true";
	this.wmode = "transparent";
	this.scale = "showall";
	this.salign = "CC";
	this.flashVars = "";
};

componentFlash.prototype = new componentElement;
componentFlash.prototype.type = "componentFlash";

componentFlash.prototype.getFlashMovieObject = function(movieName) 
{
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1) {
		if (document.embeds && document.embeds[movieName]) {
			return document.embeds[movieName];
		}
	} else {
		return document.getElementById(movieName);
	}
}

componentFlash.prototype.onBaseCreate = function(obj)
{
	this.disableOnlineOfflineMix = true; //mk: flash CAN'T be loaded from other domain than course
	this.resourcesURL = resourcesURL;
	this.captureMouseEvents();
	if (this.execution) {
		this.resourcesStatus = "online";
		this.resourcesURL = resourcesURL;
		if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
			this.resourcesStatus = lms.resourcesStatus();
			if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
			else this.resourcesURL = lms.resourcesURL();
		}
	}
    this.resourcesURL = unescape(this.resourcesURL);
};

componentFlash.prototype.onCreate = function(obj)
{
	if (is.ie) this.captureMouseEvents();
};

componentFlash.prototype.formatVar = function(s)
{
	var o = null;
	do {
		o = s.match(/%([A-Za-z0-9.\(\)'"]+)%/);
		if (o != null) {
			s = s.replace(o[0], encodeURI(eval(o[1])));
		}
	} while (o != null)
	return s;
}

componentFlash.prototype.createFlashObject = function(obj)
{
	var movieW = 0;
	var movieH = 0;
	if (this.showScrollbars){
		movieW = this.movieWidth;
		movieH = this.movieHeight;
		this.obj.style.cssText += '; overflow: auto;';
	}else{
		movieW = this.w;
		movieH = this.h;
	};
	var t = "";
	var fv = "";
	if (PageInterface.GetValue("state") != psDesigning) {
		fv = "mediaStatus=" + lms.mediaStatus() + "&" + this.formatVar(this.flashVars);
	}
	
	while (fv.indexOf('+') > -1) { //pg2- '+' in .setHTML() is converted in several ways, and finally simple '+' is passed as parameter and is missing in output flash
		fv = fv.replace('+', '_2B_'); //it will be converted to proper value '%2B' in 'element.js'
	}
	
	var backgroundColor = (this.useDefinedBG?this.bgColor:""); //pg2
	
	if (is.ie) {
		t += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" width="'+ movieW +'" height="'+ movieH +'" id="'+ this.id +'flashPlayer" align="middle">';
		t += '  <param name="movie" value="'+ this.resourcesURL + this.src +'">';
		t += '  <param name="allowScriptAccess" value="sameDomain" />';
		t += '  <param name="BGColor" value="' + backgroundColor + '">';
		t += '  <param name="quality" value="' + this.quality + '">';
		t += '  <param name="play" value="false">';
		t += '  <param name="loop" value="' + this.loop + '">';
		t += '  <param name="WMode" value="' + this.wmode + '">';
		t += '  <param name="scale" value="' + this.scale + '">';
		t += '  <param name="salign" value="' + this.salign + '">';
		t += '  <param name="FlashVars" value="' + this.flashVars + '">'; //&veriable_1=value_1&veriable_2=value_2
		
		t+= '   <param name="allowFullScreen" value="true">';
		
		t += '</object>';
	} else {
		t += '  <embed ';
		t += 'wmode="' + this.wmode + '" ';
		t += 'src="'+ this.resourcesURL + this.src +'" ';
		t += 'quality="' + this.quality + '" ';
		t += 'BGColor="' + backgroundColor + '" ';
		t += 'id="'+ this.id +'flashPlayer" ';
		t += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" ';
		t += 'width="'+ movieW +'" ';
		t += 'height="'+ movieH +'" ';
		t += 'FlashVars="' + fv + '" ';
		
		t += 'scale="' + this.scale + '" '; //pg2- missing parameters
		t += 'loop="' + this.loop + '" '; 
		t += 'salign="' + this.salign + '" ';
		
		t += '  allowFullScreen="true" ';
		
		t += 'allowScriptAccess="sameDomain"> ';
		t += '</embed>';
	}
	
	this.setHTML(t);
};

componentFlash.prototype.onPaint = function(obj)
{

	this.createFlashObject();
	var flashPlayer = document.getElementById(this.id + "flashPlayer");
	try {
		if (this.showAndStop) {
			if (typeof(flashPlayer.Stop) != 'undefined')
			flashPlayer.Stop();
		} else {
			if (typeof(flashPlayer.Play) != 'undefined')
			flashPlayer.Play();
		}
	} catch(e) {}
};

componentFlash.prototype.componentFlashBaseSetSizeScaledElement = componentFlash.prototype.setSizeScaledElement;
componentFlash.prototype.setSizeScaledElement = function(x, y, w, h)
{
	var flashPlayer = document.getElementById(this.id + "flashPlayer");
	if (flashPlayer){
		flashPlayer.width = w;
		flashPlayer.height = h;
	};
	this.componentFlashBaseSetSizeScaledElement(x, y, w, h);
};

componentFlash.prototype.actionFlashPlay = function(obj)
{
	var flashPlayer = document.getElementById(this.id + "flashPlayer");
	try {
		if (typeof(flashPlayer.Play) != 'undefined')
		flashPlayer.Play();
	} catch(e) {
		showMessage("This flash file is generated in newest version of Flash Player and does not support Play method. If you want to avoid this message please use component Simple Flash.");
	}
};

componentFlash.prototype.actionFlashPlayAgain = function(obj)
{
	this.createFlashObject();
	
	var flashPlayer = document.getElementById(this.id + "flashPlayer");
	try {
		if (typeof(flashPlayer.Play) != 'undefined')
		flashPlayer.Play();
	} catch(e) {
		showMessage("This flash file is generated in newest version of Flash Player and does not support this method. If you want to avoid this message please use component Simple Flash.");
	}
};

componentFlash.prototype.actionMediaStop = function(obj)
{
	this.actionFlashStop();
};

componentFlash.prototype.actionMediaPlay = function(obj)
{
	this.actionFlashPlay();
};

componentFlash.prototype.actionFlashStop = function(obj)
{
	var flashPlayer = document.getElementById(this.id + "flashPlayer");
	try {
		this.clearObject();
	} catch(e) {
		if (obj == page) {
			this.clearObject();
		} else {
			showMessage("This flash file is generated in newest version of Flash Player and does not support Stop method. If you want to avoid this message please use component Simple Flash.");
		}
	}
};

componentFlash.prototype.clearObject = function(obj)
{
	this.setHTML(" ");
}

componentFlash.prototype.actionFlashRewind = function(obj)
{
	this.createFlashObject();
};
