$.Player = new Object();

/**
 * WaxPlayer embeder
 *
 * possible configurations:
 *
 * bNeedPayment - indicate if training need to be payed before user can play it
 * sRemoteUrl - domain where all data urls will be called
 * sLocale - language settiong (en_US, cs_CZ....)
 * sSrc - loacal path to datafile
 * sVersion - version of training
 * sTempKey - key for temporary access
 * sScriptAccess - allow script access to player
 * sID - ID of training
 */
$.Player = {
	init : function($ele, options) {
		this.options = $.extend({
			sUri : false,
			flashversion : "10.0.0",
			sVersion : "",
			sRemoteUrl : false
		}, options);
		this.$player = $ele;

		this.$player.data('dwc-player', this);
		this.$attachments = false;

		if (this.options.bNeedPayment == true) {
			// Training payment mode
			this.payment();

		} else {
			// Training Player mode
			this.start();
			this.aAttachments = [];
			this.aSlideHistory = [];
			/*var syncSize = function() {
				var max = $('.colmid').height() * 0.75;
				$ele.width('100%').height(max + 36);
			};

			$(window).resize(syncSize);
			syncSize();*/
		}
	},
	payment : function() {
		var options = this.options;
		alert('Need payment');
		/*
		$('#ui-bank-payment-button', this.$player).click(function() {
			$.DWC.DataService.load({
				sUri : '/mvc/Bank/Index/InitPayment',
				oParams : { "object-id" : options.sID },
				foCallback: function(data, oOptions) {
					if (data.error == 2899) {
						$.DWC.UI.message(data.message);

						setTimeout(function() {
								$.DWC.DataService.load({
										sUri : data.dest_uri, //'mvc/Bank/Index/CreateAccount',
										sTarget : '#ui-web-main'
								});
						}, 500);

					} else if (data.error > 0) {
						$.DWC.UI.error(data.message);

					} else {
						if (data.deposit_status == 1) {
							if (options.sSrc.indexOf('?') == -1) {
								location.href = options.sSrc + '?request_id=' + data.token;
							} else {
								location.href = options.sSrc + '&amp;request_id=' + data.token;
							}

						} else if (data.deposit_status == 0) {
							location.href = data.dest_uri;

						} else {
							$.DWC.UI.message(data.message);
							setTimeout(function() {
								$.DWC.DataService.load({
									sUri : 'mvc/Bank/Index/Overview',
									sTarget : "#ui-web-main"
								});
							}, 500);
						}
					}
				},
				sMethod : "post",
				bHistory : false,
				sDataType : "json",
				sMode : "json"
			});
		});*/
	},
	start : function() {
		// be able to play trainings from other instances
		var playerDomain = (this.options.sRemoteUrl) ? this.options.sRemoteUrl : top.location.protocol + '//' + top.location.host + '/';

		if (typeof WaxPlayerHtml == "function") {
			this.player = new WaxPlayerHtml($(".WaxPlayerHtml", this.$player));
			return false;
		}

		var locale = this.options.sLocale;

		this.flashvars = {
			localeChain : locale,
			resourceModuleURLs : playerDomain + "Hive/Media/Flash/WAXPlayer/locale/" + locale + ".swf",
			waxconfiguri : playerDomain + "general/index/waxconfig",
			xmlFile : this.options.sSrc,
			version : this.options.sVersion,
			id : this.options.sID,
			autostart : this.options.bAutoStart ? 1 : 0
		};

		if (this.options.sTempKey) {
			this.flashvars.tmpkey = this.options.sTempKey;
		}

		this.params = {
			wmode : "opaque",
			quality : "high",
			allowfullscreen : "true",
			allowscriptaccess : this.options.sScriptAccess
		};
		this.id = "waxplayer";
		this.attributes = {
			id : this.id,
			name : this.id
		};

		// HACK - xml is giving wrong URL
		if (this.options.bEmbedMode) {
			this.options.sUri
				= playerDomain + "Hive/Media/Flash/WAXPlayer/WAXPlayer.swf";
		} else {
			this.options.sUri
				= playerDomain + "Hive/Media/Flash/WAXPlayer/Player.swf";
		}

		swfobject.embedSWF(this.options.sUri, "flash_player", "100%", "100%", this.options.flashversion, false, this.flashvars, this.params, this.attributes);
	},
	aAttachments : [],
	addAttachment : function(sTitle, sUri, sType) {
		this.showAttachments();
		if (this.checkUri(sUri)){  // only unique documents in attachments
			//if(typeof sType == "undefined") sType = UI.Utils.getObjectTypeFromUri(sUri);
			//else sType = UI.Utils.getObjectTypeFromMime(sType);
			//if(sUri.indexOf("http://") < 0) sUri = BW.Config.ROOTURI + "oes/" + sUri;
			this.$attachments.append('<li><a target="_blank" href="' + sUri+ '" class="uiIcon ic'+ sType +'">'+ sTitle +'</a></li>');
        }
	},
	showAttachments : function() {
		// display attachments
		if(!this.$attachments) {
			this.$tabs = $(".uiTabs", this.$player.parent());
			this.$tab = $('<div dwc:type="tab" class="attachmentsTab nonremovable focus"><h3 class="thTitle icAttachments nohistory">Attachments</h3><div class="uiTrainingAttachments"><ul class="uiIcon16 uiIconLeft"></ul><div class="gfxPaperClip"></div></div></div>').appendTo(this.$tabs);
			this.$attachments = $(".uiTrainingAttachments UL", this.$tabs);
		} else {
			this.$tabs.tabs_N("focus", this.$tab.attr("id"));
		}
	},
	checkUri : function(sUri) {
		for (var i=0; i<this.aAttachments.length; i++ ) {
			if (this.aAttachments[i] == sUri) {
				return false;
			}
		}
		this.aAttachments.push(sUri);
		return true;
	},
	aSlideHistory : [],
	setActiveSlide : function(sTitle, sUri, nSlideNumber) {
		this.aSlideHistory.push({
			slidenumber : nSlideNumber,
			title : sTitle,
			uri : sUri
		});
	}
}

