$(document).ready(function(){
	var boxElements = [ "linkLocation", "linkTargt", "linkText", "linkTextColor", "linkTextSize", "linkTextStyle", "linkTextDecoration", "linkImage" ];
	
	jQuery.each(boxElements, function() {
		$("#" + this).change(function () {
			linkUpdate();
		}).keyup(function () {
			linkUpdate();
		});
	});
	
	$("#linkText").focus(function() { if ($(this).attr("value") == "Example Text") { $(this).attr("value",""); } });
	
	$('#linkTextColor').ColorPicker({
		targetID : '',
		newTimeout : '',
		
		eventName: 'focus',
		
		onSubmit: function(hsb, hex, rgb, el) {
			$(el).val("#" + hex);
			$(el).ColorPickerHide();
			linkUpdate();
		},
		onChange: function(cal, hex, el) {
			$("#" + targetID).val('#' + hex);
			
			newTimeout = setTimeout("linkUpdate();", 2500); //IE gets slow
		},
		onBeforeShow: function () {
			$(this).ColorPickerSetColor(this.value);
			targetID = this.id;
		}
	});
	
	setType();
	linkUpdate();
});

function linkUpdate()
{
	var cssObj = {
		'color' : $("#linkTextColor").val(),
		'font-size' : $("#linkTextSize").val() + 'px',
		'font-family' : $("#linkTextStyle").val(),
		'text-decoration' : $("#linkTextDecoration").val()
	}
	
	$("#textPreview").css(cssObj);
	$("#textPreview").html($("#linkText").val());
	
	if ($("#linkType").val() == "image")
	{
		if ($("#linkImage").val() != '')
		{
			$("#imagePreviewURL").attr("src", $("#linkImage").val());
		}
	}
}

function setType()
{
	if ($("#linkType").val() == "image")
	{
		$("#textLink").css("display", "none");
		$("#imageLink").css("display", "inline");
		$("#textPreview").css("display", "none");
		$("#imagePreview").css("display", "inline");
	}
	else
	{
		$("#textLink").css("display", "inline");
		$("#imageLink").css("display", "none");
		$("#textPreview").css("display", "inline");
		$("#imagePreview").css("display", "none");
	}
}