// 
// 		@author Copyright (c) 2008-2011 Vipperland.com, all rights reserved
// 		@version SWFWriter 2.0.01
// 
// 		Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 			- Redistributions of source code must retain the above copyright notice, this list of conditions and the author credits.
//
var SWFCore = {};
var SWFEngine = {};
SWFCore._checkFlashPlayerVersion = function (){
	var version = 0;
	var msiexplorer = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	if(msiexplorer){
		var axoIE = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		version = axoIE.GetVariable("$version").split(" ")[1].split(",")[0];
	}else{
		var plugins = navigator.plugins.length;
		if (plugins > 0) {
			for (i = 0; i < plugins; i++) {
				if(navigator.plugins[i].name.indexOf("Shockwave Flash")>-1){
					currentVersion = navigator.plugins[i].description.split(" ")[2].split(".")[0];
					if(currentVersion >= version) version = currentVersion;
				}}}}
	return version;
}
	
SWFCore._addSWFinto = function (target, ignoreVersion){
	if(!target){
		if(!this.target) alert("Erro: SWF.write, o parametro target não pode ser nulo");
		else target = this.target;
	}
	this.target = target;
	var msiexplorer = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var flashVars = this.strVars != "" ? this.strVars : "";
	var swfObject = "<object ";
	swfObject += 	"type=\"application/x-shockwave-flash\" ";
	swfObject += 	"id=\"" + this.name + "\" ";
	swfObject += 	"width=\"" + this.width + "\" ";
	swfObject += 	"height=\"" + this.height + "\" ";
	for(var flashVar in this.flashVars) if(this.flashVars[flashVar]!==null) flashVars += (flashVars == "" ? "" : "&") + (flashVar + "=" + this.flashVars[flashVar]);
	swfObject += 	msiexplorer ? ("><param name=\"movie\" value=\"" + this.src + "\">") : " data=\"" + this.src + "\" ";
	for(var param in this.params) if(this.params[param]!==null) swfObject += msiexplorer ? ("<param name=\"" + param + "\" value=\"" + this.params[param] + "\">") : (param + "=\"" + this.params[param] + "\" "); 
	if(flashVars != "") swfObject += msiexplorer ? ("<param name=\"flashvars\" value=\"" + flashVars + "\">") : ("flashvars=\"" + flashVars + "\" "); 
	swfObject += 	msiexplorer ? "</object>" : " />";
	if(this.playerVersion >= this.version || ignoreVersion){
		if(target){ 
			this.container = document.getElementById(target);
			this.container.innerHTML = swfObject;
			this.container.style.height = this.height;
		}else{ 
			document.write(swfObject); 
		}
		this.content = document.getElementById(this.name);
		return true;
	}else{ return false; }
	this.refresh();
}
SWFCore._clearSWFContainer = function() { this.container.innerHTML = "" };
SWFCore._removeSWFinstance = function(){
	this.close();
	if(SWFEngine.lastCreatedInstance) { if(SWFEngine.lastCreatedInstance.name == this.name) SWFEngine.lastCreatedInstance = null };
	delete SWFCore._instances[this.name];
}

SWFCore._editSWFscale = function(width, height){
	var w = width ? width : this.width;
	var h = height ? height : this.height;
	this.content.style.width = w;
	this.content.style.height = h;
	if(this.container){
		this.container.style.width = w;
		this.container.style.height = h;
	}
	this.width = w;
	this.height = h;
	this.refresh();
}

SWFCore._refreshSWFBounds = function(){
	var w = this.width == "100%" ? window.innerWidth : this.width;
	var h = this.height == "100%" ? window.innerHeight : this.height;
	if(w < this.minWidth) this.content.width = this.minWidth; 
	else if(this.content.width !== this.width && w >= this.minWidth) this.content.width = this.width;
	if(h < this.minHeight) this.content.height = this.minHeight;
	else if(this.content.height !== this.height && h >= this.minHeight)this.content.height = this.height;
	if(this.container){
		this.container.style.width = this.content.width;
		this.container.style.height = this.content.height;
	}
}

SWFCore._rewriteSWF = function(src, target, width, height, version){
	this.container.innerHTML = "";
	this.src = src ? src : this.src;
	this.width = width ? width : this.width;
	this.height = height ? height : this.height;
	this.version = version ? version : this.version;
	this.write(target ? target : this.target, version ? false : true);
}

SWFCore._instanceCount = 0;
SWFCore._instances = {};
SWFCore.flashPlayerVersion = SWFCore._checkFlashPlayerVersion();
SWFCore.lastCreatedInstance = null;

SWFCore._createSWF = function(src, width, height, version){
	SWFCore._instanceCount += 1;
	this.src = src;
	this.width = width ? width : "100%";
	this.height = height ? height : "100%";
	this.version = version ? version : 10;
	this.params = {};
	this.flashVars = {};
	this.strVars = "";
	this.target = "";
	this.name = "instance" + SWFCore._instanceCount;
	this.playerVersion = SWFCore.flashPlayerVersion;
	this.container = null;
	this.content = null;
	this.target = null;
	this.minWidth = 540;
	this.minHeight = 480;
	this.remove = SWFCore._removeSWFinstance;
	this.write = SWFCore._addSWFinto;
	this.close = SWFCore._clearSWFContainer; 
	this.scaleTo = SWFCore._editSWFscale;
	this.rewrite = SWFCore._rewriteSWF;
	this.refresh = SWFCore._refreshSWFBounds;
	SWFCore._instances[this.name] = this;
}

SWFEngine.refreshAll = function(){ for (var i in SWFCore._instances) SWFCore._instances[i].refresh(); }
SWFEngine.getHashtag = function(){ return window.location.hash.substr(1); }
SWFEngine.setHashtag = function(value){ window.location.hash = value; return true; }
SWFEngine.getByName  = function(name) { return SWFCore._instances[name]; }
SWFEngine.create = function(src, width, height, version) { 
	SWFEngine.lastCreatedInstance = new SWFCore._createSWF(src, width, height, version);
	return SWFEngine.lastCreatedInstance;
}
