// JavaScript Document
function validateAll() {
	var ret = false;
	if (!validateColor(document.createForm.background_color.value, 'background color')) {
		return ret;
	}
	else if (!validateColor(document.createForm.text_color.value, 'text color')) {
		return ret;
	}

	var flashWidth = document.createForm.width.value;
	if (flashWidth < 200 || flashWidth > 700) {
		alert('invalid width!');
		document.createForm.width.value = 300;
		return ret;
	}
	return true;
}
function getFlashVars() {
	var flashVars = 'background_color=' + document.createForm.background_color.value + '&';
	flashVars += 'text_color=' + document.createForm.text_color.value + '&';
	flashVars += 'is_transparent=' + selectedValue(document.createForm.is_transparent) + '&';
	flashVars += 'is_24_hour=' + selectedValue(document.createForm.is_24_hour) + '&';
	return flashVars;
}

function getHtmlFromFlashVars(myFlashVars) {
	getHtmlFromFlashVars(myFlashVars, 300);
}

function getHtmlFromFlashVars(myFlashVars, width) {
	var height = (90 * width) / 300; 
	var html =	'<br /><div style="white-space: nowrap; line-height:0px; vertical-align: baseline"><a href="http://www.stunme.com" target="_blank"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +	
          'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ' +
          'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ' +
		  'width="' + width + '" height="' + height + '">' +
		  '<param name="movie" value="http://www.stunme.com/flash/digital_clock.swf" />' +
		  '<param name="quality" value="high" />' +
		  '<param name="menu" value="false" />' +
		  '<param name="wmode" value="transparent" />' +
		  '<param name="FlashVars" value="' + myFlashVars + '">' +
		  '<embed src="http://www.stunme.com/flash/digital_clock.swf" quality="high" menu="false"  wmode="transparent" ' +    
		  'pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" ' +
		  'type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" ' +
		  'FlashVars="' + myFlashVars + '"></embed></object></a><br />';
	if (width == 300) {
		  html += '<a href="http://www.stunme.com" target="_blank"><img src="http://www.stunme.com/images/stunme_bar_300.gif" alt="Cool Widgets" width="300" height="18" border="0" /></a><br />';
	}
	else if (width == 250) {
		  html += '<a href="http://www.stunme.com" target="_blank"><img src="http://www.stunme.com/images/stunme_bar_250.gif" alt="Cool Widgets" width="250" height="18" border="0" /></a><br />';
	}
	else {
		var blackWidth = width - 190;
		html += '<a href="http://www.stunme.com" target="_blank"><img src="http://www.stunme.com/images/stunme_bar_90.gif" alt="Cool Widgets" width="90" height="18" border="0" /><img src="http://www.stunme.com/images/black.gif" alt="Stun Me" width="' + blackWidth + '" height="18" border="0" /><img src="http://www.stunme.com/images/stunme_bar_get_cool_widgets.gif" alt="Stun Me" width="100" height="18" border="0" /></a><br />';
	}
	html += '</div>';
	return html;
}
function getCode() {
	if (!validateAll()) {
		return;
	}
	var flashVars = getFlashVars();
	var flashWidth = document.createForm.width.value;
	document.createForm.embed_code.value = getHtmlFromFlashVars(flashVars, flashWidth);
	document.getElementById('gigya').src = '/widgets/digital_clock/gigya?width=' + flashWidth + '&flash_vars=' + escape(flashVars);	
	document.getElementById('code').style.display = 'block';
}
function preview() {
	if (!validateAll()) {
		return;
	}
	var flashVars = getFlashVars();
	var flashWidth = document.createForm.width.value;
	document.getElementById('preview').innerHTML = getHtmlFromFlashVars(flashVars, flashWidth);
}