//Depends on swfobject
//Depends on jquery
/**
	 * ...
	 * @author Leonardo Emanuel
	 */
var AjaxEncoding = new function () {

	//private vars
	var _flashId = "AjaxEncodingFlashProxy";
	var _divFlashContainerId = "AjaxEncodingFlashProxyContainer"
	var _flashFileName = "AjaxEncoding.swf"
	var _flashSrc = "" + _flashFileName;
	

	//var _setupSwfCalled = false;
	var inited = false;
	var _flashMovie = null;
	var _passedSettings = null;
	var isReady = false;
	var timer = null;
	
	//private methods
	this.init = function (path) {
		if (path == undefined || path == null){
			path = "";
		}
		_flashSrc = path + _flashFileName;
		//window.AjaxEncoding = this;
		$(function(){
			setupSwf();
		});
		inited = true;
	}
	
	var _thisMovie = function _thisMovie(movieName) {
		//alert("called");
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	};
	
	var setupSwf = function() {
		var $div = $("<div id='"+_divFlashContainerId+"'>Ajax Flash Placeholder</div>");
		//alert($div);
		$div.appendTo('body');
		//$("body").append($div);
		var flashvars = {
		};
		var params = {
			menu: "false",
			scale: "noScale",
			allowFullscreen: "true",
			allowScriptAccess: "always",
			bgcolor: "#FFFFFF"
		};
		var attributes = {
			id:_flashId
		};
		swfobject.embedSWF(_flashSrc, _divFlashContainerId, "1", "1", "9.0.0", "expressInstall.swf", flashvars, params, attributes, function(e){
			//e.success bool
			//e.id
			//e.ref
			AjaxEncoding.checkReady(e.ref);
		});
	}
	
	//public methods
	this.load = function (settings) {
		_passedSettings = settings;
		
		if (!isReady) {
			if(!inited){
				this.init();
			}
			return;
		} else {
			clearTimeout(timer);
		}
		
		_flashMovie.load(_passedSettings.url,"AjaxEncoding.onSuccess");
	};
	
	this.onSuccess = function(data) {
		var successF = _passedSettings.success;
		_passedSettings = undefined;
		successF(data);
	};
	
	this.checkReady = function (htmlObjectRef){
		if (!_flashMovie){
			if (htmlObjectRef){
				_flashMovie = htmlObjectRef;
			} else {
				_flashMovie = _thisMovie(_flashId);
			}
		}
		if (_flashMovie) {
			if (_flashMovie.load != undefined){
				isReady = true;
				//alert("ready!")
				if (_passedSettings != null){
					this.load(_passedSettings);
				}
				return;
			}
		}
		if (!isReady) {
			timer = setTimeout("AjaxEncoding.checkReady()",50);
		}
	}
	window.AjaxEncoding = this;
	
	//init.call(this,path);
}

//Desired API: (jQuery copy)
/*
AjaxEncoding.load({
	type: "GET",
	url: xmlUrl,
	dataType: "xml",
	success: function(xml) {
		var $xml = $(xml);
		var $ProductList = $xml.find('ProductList').first();
		
		var $ProductList = $ProductList.find(" > Product");
		
		$ProductList.each(function(index, element){
			var $Product = $(element);
			
			var isSpecialOffer = $Product.attr("IsSpecielOffer");
			
			var $UnitPrice = $Product.find("> UnitPrices > UnitPrice");
			
			var currency = $UnitPrice.find("Currency").text();
			var price = $UnitPrice.find("Price").text();
			var priceExVat = $UnitPrice.find("PriceExVat").text();
			var priceBefore = $UnitPrice.find("PriceBefore").text();
			var priceBeforeExVat = $UnitPrice.find("PriceBeforeExVat").text();
			
			thisMovie("flashCMS").setPrice(index.toString(),currency, price, priceExVat, priceBefore, priceBeforeExVat);
			
		});
	}
});
*/
