/*
 * FlashHelper
 *	
 *	@author John Axel Eriksson
 *	
 *	This is a helper that creates items and does a few other things to enable
 *	as close to seamless flash fonts using standard markup as is possible.
 *	
 */

var flashHelper={};
flashHelper.styleClasses=new Array();
flashHelper.styleNames=new Array();
flashHelper.styleSwfHash=new Array();
flashHelper.debug=false;

flashHelper.changeSwfSize = function (id, width, height) {
	//check for explorer, do it the special special microsoft way
	alert('changeSwfSize');
	if(document.all && !document.getElementById) {
		alert('explorer way');
		document.all[id].setAttribute('width', width);
		document.all[id].setAttribute('height', height);
	} else {//if not microsoft, then it is most probably standards compliant and we can do it the right way
		alert('right way');
		document.getElementById(id).setAttribute('width', width);
		document.getElementById(id).setAttribute('height', height);
	}
}

flashHelper.getMovie = function(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

flashHelper.fixExplorer = function (txt) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var regex = "(class\=)(\w*)(\>)";
		var re = new RegExp(regex, "g");
		txt=txt.replace(re, '"');
		alert('replaced: '+txt);
	}
	return txt;
}

flashHelper.createText = function (swf, id, text, width, height, style, transparency) {
	var flashvars = {};
	flashvars.text = escape(flashHelper.fixExplorer(text));
	if(width!=0) {
		flashvars.width=width;
	} else {
		flashvars.wordWrap='false';
		flashvars.autoSize='true';
	}
	if(height!=0) {
		flashvars.height=height;
	} else {
		flashvars.autoSize='true';
	}
	flashvars.id=id;
	
	//fix for the worst browser ever made
	
	
	
	alert('replaced: '+text);

	
	//-----
	
	//flashvars.embedFonts='true';
	//if(style) flashvars.style=escape(style);
	if(flashHelper.debug) flashvars.debug='true';
	
	var params = {};
	params.bgcolor="#999999";
	params.scale = "noscale";
	params.salign = "tl";
	params.align = "l";
	params.allowscriptaccess = "sameDomain";
	//if(transparency) params.wmode = "transparent";
	
	if(width==0) width=1;
	if(height==0) height=1;

	swfobject.embedSWF(
		swf, 
		id, 
		width, 
		height, 
		"9.0.0", 
		"flash/expressInstall.swf", 
		flashvars, 
		params
	);
}

flashHelper.cssStyleToString = function (styleDecl) {
	var style = ".flashStyle {";
	for(var attr in styleDecl) {
		style += attr+": "+styleDecl[attr]+"; ";
	}
	style+=" }"
	return style;
}

flashHelper.addStyleClass = function (swfName, cName, cBody) {
	var cClass = "."+cName+cBody;
	flashHelper.styleClasses.push(cClass);
	flashHelper.styleNames.push(cName);
	flashHelper.styleSwfHash[cName]=swfName;
}

flashHelper.flashTextInstanceResized = function (id, width, height) {
	//just pass it on
	flashHelper.changeSwfSize(id, width, height);
}

flashHelper.menuItemClicked = function (id) {
	alert('item '+id+' clicked');
}

flashHelper.registerFields = function () {

	var classes=flashHelper.styleClasses.join('');
	var ft=$(".flashtext");
	var cNames;
	var swfName;
	var defaultCname;
	for (var i=0; i < ft.length; i++) {
		
		cNames=ft[i].className.split(' ');
		defaultCname="";

		for(var j=0;j<cNames.length;j++) {
			if(flashHelper.styleSwfHash[cNames[j]]!=null) {
				defaultCname=cNames[j];
				swfName=flashHelper.styleSwfHash[defaultCname];
				break;
			}
		}
		var w=0;
		var h=0;

		if(ft[i].style.width!='') w=ft[i].style.width.split('px')[0];
		if(ft[i].style.height!='') h=ft[i].style.height.split('px')[0];
		
		var txt='<span class="'+defaultCname+'">'+ft[i].innerHTML+'</span>';
		alert(txt);
		flashHelper.createText(swfName, ft[i].id, txt, w, h, classes, true);
	};
}