﻿/****************************************************************
	FWS 3.0 for ActionScript by THOR (thor.net@gmail.com)		
****************************************************************/

//	need swfobject.js & flashTransfer.js	 !!!!!!!!!!!!!!!!!!!!!!

var fws3 = new Object();
fws3.instanceIds = new Array();
fws3.instances = new Object();
fws3.trans = new flashTransfer();

fws3.begin = function() {
	fws3.trans.excuteFunc = function(newUrl) {
	//alert(newUrl);
		fws3.request("UrlChanged", [location.href+""]);
	};
	fws3.trans.urlInterval(fws3.trans);
};

//add flash instance
fws3.add = function(id, src, width, height, flashVars, flashSetting) {
	if (flashSetting == null || typeof (flashSetting) == "undefined") {
		flashSetting = new Object();
	}

	if (flashSetting.allowScriptAccess == null || typeof (flashSetting.allowScriptAccess) == "undefined") flashSetting.allowScriptAccess = "always";
	if (flashSetting.menu == null || typeof (flashSetting.menu) == "undefined") flashSetting.menu = false;

	swfobject.embedSWF(src, id, width, height, "9.0.0", "", flashVars, flashSetting);

	var instance = fws3.find(id);
	fws3.instanceIds.push(id);
	fws3.instances[id] = instance;
};

//remove flash instance
fws3.remove = function(id) {
	var instance = document.getElementById(id);
	if (instance.parentNode != null || typeof (instance.parentNode) != "undefined") {
		instance.parentNode.removeChild(instance);
	}

	for (var i = fws3.instanceIds.length - 1; i >= 0; i--) {
		if (fws3.instanceIds[i] == id) {
			fws3.instanceIds.splice(i, 1);
			delete fws3.instances.id;
		}
	}
};

//find flash instance
fws3.find = function(id) {
	var result = null;

	if (navigator.appName.indexOf("Microsoft") != -1) {
		result = window[id];
	}
	else {
		result = document[id];
	}

	return result;
};

//response ...
fws3.response = function(cmd, args, id) {
	switch (cmd.toString().toLowerCase()) {
		case "urlchanged":
			//TODO: change url
			break;

		case "":
			break;
	}
};

//request ...
fws3.request = function(cmd, args, id) {
	//alert("request " + cmd.toString() + " , " + args.toString());
	if (id == null || typeof (id) == "undefined") {
		for (var i = 0; i < fws3.instanceIds.length; i++) {
			var itemId = fws3.instanceIds[i];
			var item = fws3.find(itemId);

			//alert("call " + cmd + "," + args + "," + item);
			fws3.requestHandler(item, cmd, args);
		}
	}
	else {
		//fws3.instances[id]
		var item = fws3.find(itemId);

		//alert("call " + cmd + "," + args + "," + item);
		fws3.requestHandler(item, cmd, args);
	}
};

//request handler ...
fws3.requestHandler = function(ins, cmd, args) {
	try {
		//alert(ins);
		//alert(ins.callback);
		ins.callback(cmd, args);
	}
	catch (err) {
	//	alert("ERROR: " + err.message);
	}
};



/////////////////////////////////////

fws3.setTitle = function(t) {
	document.title = t;
};

fws3.getTitle = function() {
	return document.title;
};

fws3.setStatus = function(s) {
	window.status = s;
};

fws3.getStatus = function() {
	return window.status;
};

fws3.getUrl = function() {
	return location.href + "";
};

fws3.setUrl = function(u) {
	//alert("flash set url : "+u);
	location.href = u;
};

fws3.back = function(step) {
	history.go(-step);
};

fws3.next = function(step) {
	history.go(step);
};
