$(document).ready(function () {
/*
	$( "#opt_size" ).slider({
		value: 1,
		min: 0,
		max: 2,
		step: 1,
		slide: function( event, ui ) {
			var size = "medium";
			if (ui.value == 0) size = "small";
			if (ui.value == 2) size = "big";
			$( ".opt_size" ).val("Size: "+size);
		}
	});*/

	co_width = 100;
	co_height = 20;
	co_template_id = 2;
	co_pad = 0;
	co_value = 1;
	co_num_len = 10;
	co_s_width = 10;

	// Set initial values.
	co_template_id = $(".counter_selector:checked").attr("value");
	size_now = $("#opt_size").attr("value");
	$.ajax({
		"type":"post",
		"url":"/ajax.php",
		"data":"get_size_id=1&template_id="+co_template_id+"&req_size="+size_now,
		"success":function (html) {
			eval("vals="+html);
			co_template_id = vals[0];
			co_width = parseInt(vals[1])*co_num_len+10;
			co_height = parseInt(vals[2])+2;
			co_s_width = parseInt(vals[1]);

			co_num_len = parseInt($("#opt_num").attr("value"));
			co_pad = $(".r_col2 input:checked").attr("value");
			co_width = co_s_width*co_num_len+10;
			co_value = $(".r_col3 input").attr("value");
			if (co_value == "") co_value = 1;

			update_preview();
		}
	});

	$(".counter_selector").change(function () {
		co_template_id = $(this).attr("value");
		opt_size_change();
	});


	function opt_size_change() {
		var val = $("#opt_size").attr("value");
		$.ajax({
			"type":"post",
			"url":"/ajax.php",
			"data":"get_size_id=1&template_id="+co_template_id+"&req_size="+val,
			"success":function (html) {
				eval("vals="+html);
				co_template_id = vals[0];
				co_width = parseInt(vals[1])*co_num_len+10;
				co_height = parseInt(vals[2])+2;
				co_s_width = parseInt(vals[1]);
				update_preview();
			}
		});
	}
	
	$("#opt_size").change(opt_size_change);

	$("#opt_num").change(function () {
		co_num_len = $(this).attr("value");
		co_width = co_s_width*co_num_len+10;
		update_preview();
	});

	$(".r_col2 input").change(function () {
		co_pad = $(this).attr("value");
		update_preview();
	});

	$(".r_col3 input").change(function () {
		co_value = $(this).attr("value");
		update_preview();
	});

	function update_preview() {
		$("#preview_counter").html('<iframe scrolling="no" frameborder="0" style="border: none; width: '+(co_width)+'px; height: '+(co_height)+'px" src="http://c1.counter.nu/counter/'+co_template_id+'?preview&pad='+co_pad+'&value='+co_value+'&num_width='+co_num_len+'"></iframe>');
	}

	function overlay_click() {
		$(this).fadeOut(100);
		$("#copy").fadeIn(100, function () {
			$("#copy").zclip({
				path: "/jquery/zclip/ZeroClipboard.swf",
				copy: function() {
					return $("#code").val();
				}
			});
		});
		get_code();
		return false;
	}
	$("#code_overlay").unbind("click");
	$("#code_overlay").click(overlay_click);

	$("#code").click(function () {
		$(this).select();
		return false;
	});

	$("input,select").change(function () {
		$("#code_overlay").fadeIn(100, function () {
			$("#code_overlay").unbind("click");
			$(this).click(overlay_click);
		})
		$("#copy").fadeOut(100);
	});

	function get_code() {
		$.ajax({
			"type":"post",
			"url":"/ajax.php",
			"data":"get_code=1&template_id="+co_template_id+"&num_width="+co_num_len+"&value="+co_value+"&pad="+co_pad,
			"success":function (html) {
				$("#code").val(html);
				$("#code").select();
				var image = new Image(1,1);
				image.src = "http://www.googleadservices.com/pagead/conversion/969136752/?value=10&label=5BoRCNCVxwMQ8LSPzgM&guid=ON&script=0";
			}
		});
	}

	

	$("#copy").click(function () {
		return false;
	});

	// Blogger installation.
	$("#blogger_install").click(function () {
		$.ajax({
			"type":"post",
			"url":"/ajax.php",
			"data":"get_code=1&template_id="+co_template_id+"&num_width="+co_num_len+"&value="+co_value+"&pad="+co_pad,
			"success":function (html) {
				$("body").append('<form method="post" id="blogger_form"  action="http://www.blogger.com/add-widget"><input type="hidden" name="go" value="Add to Blogger" /><textarea style="display: none;" name="widget.content">'+html+'</textarea><input type="hidden" name="widget.template" value="<data:content/>"></form>');
				$("#blogger_form").submit();
			}
		});
		return false;
	});
});

