﻿(function($)
{
	$.fn.ajaxUpload = function(onSubmit, onComplete)
	{
		return this.each(function()
		{
			new AjaxUpload("#" + $(this).attr("id"),
				{
					action: $(this).attr("href"),
					name: $(this).attr("id") + "File",
					onSubmit: onSubmit,
					onComplete: onComplete
				});
		});
	};
})(jQuery);


$(document).ready(function()
{
	$("a.confirm").click(function()
	{
		return confirm($(this).attr("title"));
	});

	$(".submit").click(function()
	{
		$(this).parents("form:first").submit();
		return false;
	});

	$(".focus").focus();

	// watermarking		
	$(":text, textarea").each(function(i, item)
	{
		if ($(item).val().length == 0)
		{
			$(item).val($(item).attr("title")).addClass("watermarked");

			$(item).focus(function()
			{
				if ($(this).is(".watermarked"))
				{
					$(this).removeClass("watermarked").val("");
				}
			});

			$(item).blur(function()
			{
				if (!$(this).is(".watermarked") && $(this).val().length == 0)
				{
					$(this).addClass("watermarked").val($(this).attr("title"));
				}
			});
		}
	});

	$("form").submit(function()
	{
		$(this).clearWatermarks();
		return true;
	});

	(function($)
	{
		$.fn.clearWatermarks = function()
		{
			$(this).find("input.watermarked").val("").removeClass("watermarked");
			return $(this);
		};
	})(jQuery);

	// feedback
	$(".feedback").click(function()
	{
		$.blockUI(
		{
			message: $("#feedback"),
			centerY: true,
			centerX: true,
			focusInput: false,
			css:
			{
				width: "448px",
				top: "50px",
				left: "50%",
				margin: "0 0 0 -224px",
				border: "0",
				backgroundColor: "transparent",
				cursor: "normal"
			},
			overlayCSS:
			{
				backgroundColor: "#000",
				opacity: 0.8,
				cursor: "normal"
			}
		});

		return false;
	});

	$("#feedback a.cancel").click(function()
	{
		$.unblockUI();
		return false;
	});
});


// Rich Text Editing

var editorRef = null;

try
{
	editorRef = CKEDITOR;
} 
catch (e)
{
	// don't care
}

if (editorRef != null)
{
	CKEDITOR.addStylesSet('dpd_styles',
	[
		{ name: 'Overskrift', element: 'h2' },
		{ name: 'Brødtekst', element: 'p' }
	]);

	$(document).ready(function()
	{
		$("textarea.editor").each(function(i, item)
		{
			CKEDITOR.replace($(item).attr("id"),
			{
				uiColor: '#ddeaf5',
				toolbar:
				[
					($(item).hasClass("full") ? ['Styles'] : null),
					['Bold', 'Italic', 'Strike', 'Subscript', 'Superscript', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'Image'],
					['JustifyLeft', 'JustifyCenter', 'JustifyRight'],
					['Undo', 'Redo', 'RemoveFormat'],
					['Source']
				],
				resize_enabled: false,
				toolbarCanCollapse: false,
				forcePasteAsPlainText: true,
				disableObjectResizing: true,
				filebrowserBrowseUrl: "/view.aspx/mediaadmin/index",
				stylesCombo_stylesSet: "dpd_styles"
			});
		});
	});
}
