var Toggle = 1;
var IL = '';
var IR = '';
var OW = 0;

/* --- */

Update(true);

/* --- */

function Update(forceIt) {
	oldWidth = $('background').style.width.replace('px','');
	oldHeight = $('background').style.height.replace('px','');
	oldLineHeight = $('content').style.lineHeight.replace('px','');
	if (oldWidth != $('width').value || oldHeight != $('height').value || oldLineHeight != $('lineHeight').value) {	
		if (oldHeight > $('height').value) {
			alert('You are making the content area shorter, any points below the new bottom edge may be cut off.  If need be, reset the line after changing the size');
		}
		ReDrawFlash();
	}
	
	$('background').style.backgroundImage 	= 'url(' + $('bgURI').value + ')';
	$('background').style.width 			= $('width').value + 'px';
	$('background').style.height 			= $('height').value + 'px';
	
	$('content').style.lineHeight 	= $('lineHeight').value + 'px';
	$('content').style.textAlign 	= $('text-align').value;
	$('content').style.fontFamily 	= $('font-family').value;
	$('content').style.fontSize 	= $('font-size').value;
	$('content').innerHTML		 	= $('contentUse').value;
}

function ReDrawFlash() {
	$('flash').parentNode.removeChild($('flash'));
	f = document.createElement('div');
	f.setAttribute('id', 'flash');
	$('background').appendChild(f);
	
	var so = new SWFObject('/f/adjustLine.swf' + ((isIE())?('?rand='+Math.random()):('')),'flashswf',($('width').value * 1 + 28),($('height').value * 1 + 28),'8');
	so.addParam("wmode","transparent");
	so.addParam("quality","high");
	so.addVariable("LH",$('lineHeight').value);
	if (IL.length > 0) {
		so.addVariable("IL",IL);	
	}
	if (IR.length > 0) {
		so.addVariable("IR",IR);	
	}
	if (OW > 0) {
		so.addVariable("OW",OW);	
	}
	so.write('flash');
}

function ToggleLine() {
	thisMovie("flashswf").ToggleLine();
	$('toggle').value = ( (Toggle == 1)?('Show Lines'):('Hide Lines') );
	Toggle *= -1;
}
function ResetLine(whichOne, forceIt) {
	thisMovie("flashswf").ResetLine(whichOne, forceIt);
}

/* --- */


function refreshDivs(Xs, l, r, w, dl) {
	IL = l;
	IR = r;
	OW = w;
	
	var livecode = '';
	var m1 = '';
	var m2 = '';
	var m3 = '';

	//Xs =  Y,xl,xr|Y,xl,xr...
	
	dlp = dl.split(','); 
	
	Xvalues = Xs.split('|');
	for(i=0; i < Xvalues.length; i++) {
		parts = Xvalues[i].split(',');
		if (parts.length == 3) {
			if (dlp[0] == 0) {
				livecode += '<div style="float:left;clear:left;height:'+$('lineHeight').value+'px;width:'+ parts[1]+'px"></div>';				
				m1 += '<div style="float:left;clear:left;height:'+$('lineHeight').value+'px;width:'+ parts[1]+'px"></div>';
				m2 += '<div class="lW" style="width:' + parts[1] + 'px;"></div>';
			}
			if (dlp[1] == 0) {				
				livecode += '<div style="float:right;clear:right;height:'+$('lineHeight').value+'px;width:'+ parts[2]+'px"></div>';
				m1 += '<div style="float:right;clear:right;height:'+$('lineHeight').value+'px;width:'+ parts[2]+'px"></div>';
				m2 += '<div class="rW" style="width:' + parts[2] + 'px;"></div>';
			}
		}
	}
	
	$('wrap').innerHTML = livecode;
	$('m1').value = m1;
	$('m2').value = m2;
	
	//Method 2 Styles
	$('m2_style').value  = '.lW { float: left; clear: left; height: '+$('lineHeight').value+'px; }'+"\n";
	$('m2_style').value += '.rW { float: right; clear: right; height: '+$('lineHeight').value+'px; }';
	
	//Method 3
	
	$('m3').value  = '<script type="text/javascript" src="shapewrapper.js"></script>'+"\n";
	$('m3').value += '<script type="text/javascript">shapeWrapper("'+$('lineHeight').value+'","' + Xs + '");</script>';
}

/* --- */

inputs = $('options').getElementsByTagName('input');
for(i=0; i<inputs.length; i++) {
	inputs[i].onchange = Update;	
}

/* --- */

function thisMovie(movieName) { 
	return (isIE()) ? window[movieName] : document[movieName];
}
function isIE() { return navigator.appName.indexOf("Microsoft") != -1; }
function trace(str) { $('output').innerHTML += "\n" + str; }
//function traceO(str) { $('output').innerHTML += "\n F: " + str; }
function $(id) { return document.getElementById(id); }