function rowOver(object, cls) {
	$(object).addClass('over');
}

function rowOut(object, cls) {
	$(object).removeClass('over');
}

function redirect(url) {
	if (url.length) {
		window.location = url;
	} else {
		return false;
	}
}

function escTo(e, url) {
	var key  = (window.event) ? event.keyCode : e.keyCode; 
	var esc = (window.event) ? 27 : e.DOM_VK_ESCAPE;
	if (key == esc) {
		alert(window.location);
		redirect(url);
	}
}

function enterSubmitForm(e, form) {
	var key  = (window.event) ? event.keyCode : e.keyCode;

	if ((key == 13) && (navigator.userAgent.indexOf("MSIE") > 0)) {
		$(form).submit();
	}
}

function initTmpInputs() {
	if (!window.tmpInputs) tmpInputs = new Object();
}

function collectItem(tid, pid, id) {
	// Deklarer Global tmpArray om ikke finnes
	if (!window.tmpInputs) tmpInputs = new Object();
	if (!tmpInputs[tid]) tmpInputs[tid] = new Object();
	if (!tmpInputs[tid][pid]) tmpInputs[tid][pid] = new Object();
	tmpInputs[tid][pid][id] = 'tmpstr';
}

String.prototype.format = function(){
	var pattern = /\{\d+\}/g;
	var args = arguments;
	return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}

function createNewDynamicTemplate()
{
	$('body').append('<div id="newDocument" class="modalbox" style="display: none; text-align: left;"></div>');
	
	$('#newDocument').load("/ajax/document/new/", null, function()
	{
		$.blockUI({
			css: {
				top: '50px',
				//left: '50px',
				padding: '5px',
				'-moz-border-radius': '10px',
				'-webkit-border-radius': '10px',
				'margin-left': 'auto',
				'margin-right': 'auto',
				width: '500px'
			},
			message: $('#newDocument')
		});
		$('.blockPage').center({vertical: false});
		$('.blockOverlay').attr('title','Click to unblock').click(function()
		{
			$('#newDocument').remove();
			$.unblockUI();
		});
	});
	
	return false;
}

function copyDocument(id)
{
	$('body').append('<div id="copyDocument" class="modalbox" style="display: none; text-align: left;"></div>');
	
	$('#copyDocument').load("/ajax/document/copy/", {id: id}, function()
	{
		$.blockUI({
			css: {
				top: '50px',
				//left: '50px',
				padding: '5px',
				'-moz-border-radius': '10px',
				'-webkit-border-radius': '10px',
				'margin-left': 'auto',
				'margin-right': 'auto',
				width: '600px'
			},
			message: $('#copyDocument')
		});
		$('.blockPage').center({vertical: false});
		$('.blockOverlay').attr('title','Click to unblock').click(function()
		{
			$('#copyDocument').remove();
			$.unblockUI();
		});
		$('input[name=doc_name]').select();
	});
	
	return false;
}

$.fn.extend({
	immediateText: function() {
		return this.clone().find("*").remove().end().text();
	}
});

$(function()
{
  $('.section_button.warning').click(function()
  {
    return confirm('Trykk OK for å slette seksjonen.');
  });
  
  $('select[id$=_width]').livequery('change', function()
  {
  	var img = $(this).closest('div.item').find('[id$=_preview] img');
  	
  	img.css('width', $(this).val()+'%');
  });
  
  $('select[id$=_alignment]').livequery('change', function()
  {
  	var div = $(this).closest('div.item').find('[id$=_preview]');
  	
  	div.css('text-align', $(this).val());
  });
  
  $('.rowbutton.copy').each(function()
  {
  	$(this).click(function()
  	{
  		copyDocument($(this).attr('rel'));
  		return false;
  	});
  });
  
  $('h1 a.edit').click(function()
  {
  	$(this).hide();
  	var $h1 = $(this).parent();
  	$h1.find('.header').hide();
  	$('#edit_title').show().find('input').focus().select();
  	
  	return false;
  });
});
