﻿// contains Aris.XmlWrite.js, Aris.SWFMovie.js
//=aris.xmlwriter.js +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aris.XmlWriter = function(tagName) {
	var sb = [];
	var hasContent = false;
	var attrOpen = false;
	var xmlStr = '';
	return {
		open: function(t) {
			xml = '';
			sb = [];
			sb.push('<'+tagName);
			attrOpen = true;
			return this;
		},
		close: function() {
			if (attrOpen) {
				attrOpen = false;
				sb = [sb.join(' ')];
			}
			return this;
		},
		writeAttribute: function(attrs) {
			if ( attrOpen ) {
				forEach(attrs,function(v,n){ sb.push(n+'="'+($defined(v)?v:n)+'"'); });
			}
			return this;
		},

		write: function(str) {
			if ( attrOpen ) { this.close(); sb.push('>'); }
			sb.push(str);
			hasContent = true;
			return this;
		},
		getXML: function() {
			if (xml) return xml;
			this.close();
			sb.push(hasContent ? "</"+tagName+">" : " />" );
			return (xml = sb.join(''));
		}
	}
};
Aris.DOM.createXHTML = function(xmlJson) {
	var tag = typeof xmlJson == 'string' ? xmlJson : xmlJson.nodeName;
	var props = arguments.length > 1 ? arguments[1] : xmlJson.props;
	var tw = new Aris.XmlWriter(tag);
	tw.open();
	if ( props ) {
		var children = $remove(props,'children');
		tw.writeAttribute(props);
		if (typeof children =='string') children = [children];
		if ( children ) Array.from(children).forEach(function(c){
			if (typeof c == 'string'){ tw.write(c); }
			else if (c.nodeType==3) { tw.write(c.nodeValue); }
			else if (c.nodeType==1) { tw.write(c.innerHTML); }
			else { tw.write(c); }
		});
	}
	return tw.getXML();
};

//=aris.swfmovie.js +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(function() {
var DOM = Aris.DOM;
var Event = Aris.Event;
var Observer = Aris.Observer;

var SWFMovie = Aris.SWFMovie = Function.extend({
	constructor: function(swf, props, bypassXI) {
		props = props || {};
		var ver = $extract(props,'ver') || "0";
		//start the expressinstall procedure; bypass by passing version of 0!
		if ( !SWFMovie.hasVersion(ver) ) {
			//return an empty SWFMovie if we fail expressinstall
			if ( bypassXI === true || !SWFMovie.canXI(props) )
				return this;
			var xi = SWFMovie.getXI();
			xi.attr.width = props.width;
			xi.attr.height = props.height;
			return xi;
			
		}
		this.ver = ver;
		this.fvMode = $extract(props, 'fvMode');
		this.attr = $extract(props, 'width', 'height', 'id');
		this.attr.data = swf;
		this.params = {};
		this.flashvars = {};
		SWFMovie.onCreating.add($extract(props,'onCreating'), this);
		SWFMovie.onCreated.add($extract(props,'onCreated'), this);
		SWFMovie.onLoad.add($extract(props,'onLoad'), this);
		this.add(props);
	},
	add: function(obj) {
		if (!obj) return;
		forEach(obj, function(val, name) {
			if ("attr,params,flashvars".indexOf(name) >= 0 ) {
				$merge(this[name],val);
			}
		},this);
	},

	embedIn: function(el) {
		Aris.Event.onDOMLoaded.add(function() {
			SWFMovie.embedSWF(this, DOM.get(el));
		},this);
	},

	replace: function(selector) {
		Aris.Event.onDOMLoaded.add(function() {
			SWFMovie.embedSWF(this, DOM.match(sel));
		},this);
	}
}, {
	create: function(swf, props, bypassXI) {
		alert(swf);
		return new this(swf, props, bypassXI);
	},

	hasVersion: function(reqd) {
		reqd = (typeof reqd == 'string') ? reqd.split('.') : [].concat(reqd);
		if ( reqd.length < 2 ) { reqd[1] == reqd[1] || 0; reqd[2] = reqd[2] || 0; }
		var have = this.PlayerInfo.version;
		return reqd[0] < have[0] || (reqd[0] == have[0] && ( reqd[1] < have[1] || (reqd[1] == have[1]  && reqd[2] <= have[2]) ) );
	},

	init: function() {
		this.XI = {
			id: 'xiSWF',
			ver: [6,0,25],
			width: 215, height: 138,
			flashvars: { MMredirectURL: escape(location.href), MMpluginType: this.PlayerInfo.type },
			onCreating: function(sender, args) {
				if (this !== args.swf) { return; }
				document.title = document.title.slice(0, 47) + " - Flash Player Installation";
				args.swf.flashvars.MMdoctitle  = document.title;
			}
		};
		this.movies = {};
		if ( this.hasVersion([6,0,25]) )
			DOM.addClass(document.documentElement,'hasFlash');
	},

	onCreating: new Observer,
	onCreated: new Observer,
	onLoad: new Observer,

	PlayerInfo: function() {
		var p = {};
		p.version = [0,0,0];
		p.axo = false;
		p.type = '';
		p.attr = { 'type': 'application/x-shockwave-flash' };

		var axo = null;
		if(navigator.plugins && navigator.mimeTypes.length){
			var x = navigator.plugins["Shockwave Flash"];
			if(x && x.description) {
				p.version = x.description.replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
				p.type = 'PlugIn';
			}
		} else { 
			// do minor version lookup in IE, but avoid fp6 crashing issues
			// see http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var badversion = false;
			try{
				axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			}catch(e){
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					p.version = [6,0,21];
					axo.AllowScriptAccess = "always"; // error if player version < 6.0.47 (thanks to Michael Williams @ Adobe for this code)
				} catch(e) {
					badversion = p.version[0] == 6; // cant use $version below!
				}
				try {
					axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				} catch(e) {}
			}
			if (axo !== null) {
				if (!badversion) p.version = axo.GetVariable("$version").split(" ")[1].split(",");
				p.type = 'ActiveX';
				p.axo = true;
				p.attr.classid = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
			}
		}

		['major','minor','rev'].forEach( function(o,idx){
			p.version[idx] = p.version[o] = (p.version[idx] ? parseInt(p.version[idx],10) : 0);
		});
		return p;
	}(),

	embedSWF: function(SWFObj, nodes) {
		if ( nodes && !nodes.length ) { nodes = [nodes]; }
		if ( !SWFObj.attr.data || !nodes || !nodes[0] ) return;
		var _ie = Aris.BOM.is('ie');
		
		SWFObj.params = SWFObj.params || {};

		var params = SWFObj.params, attr = $merge(SWFObj.attr, this.PlayerInfo.attr);
		var _create = DOM.create;
		if ( _ie ||(Aris.BOM.is('opera')&&SWFObj.FSCommand) ) {
			if ( _ie ) params.movie = $remove(attr,'data');
			_create = DOM.createXHTML;
		}
		var $swf = this;
		_swfId = SWFObj.attr.id || 'swf';
		if ( !$defined($swf.movies[_swfId]) ) { $swf.movies[_swfId] = new Array() };
		forEach(nodes, function(node,idx) {
			var numMovies = $swf.movies[_swfId].length;
			if ( attr.id && numMovies >= 1) { attr.id += numMovies; }
			if ( SWFObj.FSCommand ) {
				attr.name = attr.id;
				window[attr.id+'_DoFSCommand'] = SWFObj.FSCommand;
			}
			var args = {swf: SWFObj, parent: node, attr: attr, cancel: false};
			$swf.onCreating.fire($swf, args);
			if ( args.cancel === true ) return;
			attr.children = [];
			if ( SWFObj.flashvars ){ params.flashvars = $serialize(SWFObj.flashvars); }
			if ( SWFObj.fvMode == 'query' ) {
				if (_ie) parmas.movie = params.movie.split('?')[0] + '?' + params.flashvars;
				else attr.data = attr.data.split('?')[0] + '?' + params.flashvars;
				delete params.flashvars;
			}
			forEach(params, function(val, param) {
				if ( val ) {attr.children.push(_create('param', {name: param, value: val }));}
			});
			if ( SWFObj.altContent ) {
				attr.children.push(SWFObj.altContent);
			}
			var obj = _create('object', attr);
			if (_create === DOM.createXHTML) {
				node.innerHTML = obj;
				obj = node.firstChild;
			}
			else {
				DOM.clear(node);
			 	node.appendChild(obj);
			}
			$swf.movies[_swfId].push(obj);
			Event.add(obj, 'load', function(e){$swf.onLoad.fire($swf,{movie:this})});
			DOM.addClass(node,'hasFlash');
			$swf.onCreated.fire($swf,{movie:obj});
		});
	},
	
	getMovie: function(id,idx) {
		if (this.movies[id]) {
			return this.movies[id][idx||0];
		}
		return null;
	},
	
	getMovies: function(id) {
		return this.movies[id] || [];		
	},
	
	xiSwfUrl: '/expressinstall.swf',
	
	XI: {},

	canXI: function(obj) {
		return (parseInt(obj.width,10) >= this.XI.width && parseInt(obj.height,10) >= this.XI.height);
	},

	getXI: function() {
		var xi = new this(this.xiSwfUrl, this.XI, true);
		this.getXI = function() { return {}; }
		return xi;
	}
});
})();
