// JavaScript Document
/*
	tagsToHtml()
	converts {filename:width,height,align} to
		<img src=filename width=width height-height />
*/
/*
	properties pertaining to media tags to HTML img tags
*/
var imgMaxWidth = 480;
var imgMaxHeight = 500;
var imgBorderSize = 1;
var imgPaddingSize = 0;
var imgBorderColor = "#CCCCCC";
//
function tagsToHtml( txt ) {
	RegExp.lastIndex = 0;
	var mediaTag = /\{[^}]+\}/g;
	var oneMatch;
	try{	
		while ( (oneMatch = mediaTag.exec(txt)) != null) {
			for( var a=0; a<oneMatch.length ; a++ ) {
				var newTag = parseMediaTag(oneMatch[a]);
				txt = txt.replace(oneMatch[a],newTag);
			}
		}
		return txt;
	}catch(e){
		alert("ERROR performing pattern matching: " + e);
		return null;
	}
}
//--- supporting classes
/*
	{filename:width,height,alignment,idName,visible}
						||
	{source:specs[0],specs[1],specs[2],specs[3],specs[4]}
*/
function parseMediaTag(tag) {
	tag = tag.substr(1,tag.length);
	var data = tag.split(':');
	//alert("Splitted by ':' "+data[0] + " | " + data[1]);
	var source = data[0];
	var moredata = source.split('.');
	var sourceExt = moredata[moredata.length-1];
	//---dims
	var specs = data[1].split(',');
	var alignment = specs[2].replace('}','');
	var wi = specs[0];
	var hi = specs[1];
	var idName = specs[3];
	var isVisible = true;
	if ( specs[4] != null || specs[4] != undefined ) {
		isVisible = specs[4].replace('}','');
	}
	var divStyle = "";
	if (idName == null || idName == undefined)
		idName = source;
	if (isVisible != false) {
		isVisible = true;
		divStyle = "display: block;"
	} else {
		divStyle = "display: none;";
	}
	//---resize
	var ratio = 1;
	if (wi > imgMaxWidth) {
		ratio = imgMaxWidth/wi;
	} else if (hi > imgMaxHeight) {
		ratio = imgMaxHeight/hi;
	}
	wi = ratio * wi;
	hi = ratio * hi;
	//---
	if (sourceExt != "swf") {	//---Standard images
		var htmlTag = "<div style='" + divStyle + "' id='" + idName + "' align='" + alignment + "'><span style='border:0px; solid "+imgBorderColor+"; padding:"+imgPaddingSize+"px'><img alt='" + source + "' align='"+alignment+"' id='" + source + "' src='blogimg/" + source + "' width='" + wi+ "' height='" + hi + "' style='background:#FBFBFB; padding:" + "3" + "px; margin:2px;border:"+"1"+"px solid " + imgBorderColor + "'/></span></div>";
		return htmlTag;
	} else if (sourceExt == "swf") {	//---Flash movies
		var htmlTag = "<div style='" + divStyle + "' id='" + idName + "' align='" + alignment + "'><object width='" + wi + "' height='" + hi + "'><param name='movie' value='flv/" + source + "'><embed src='flv/" + source + "' width='" + wi + "' height='" + hi + "'></embed></object></div>";
		return htmlTag;
	}
}