$(document).ready(function() {
	// Hide broken images
	$('img').error(function() {
		$(this).css('display', 'none');
	});
	
	// Open remote links in a new window
	$('a').click(function(eventObject) {
		if (!(LOCAL_URL_REWRITE.test(this.href)) && !(/javascript:/.test(this.href))) {
			this.target = "_blank";
		}
	});
	
	$("input[type='text']:first").focus();
	
	// Show/hide box content when title is clicked
	$('.box_title span img').css('cursor', 'pointer')
							.click(function() {
		var box = $(this).parent().parent();
		box.next().next().slideToggle();
		box.find('img.expand').toggle();
		box.find('img.collapse').toggle();
	});
	
	// Enable date picker for date fields
	$('.date').datepicker({
		showButtonPanel: true,
		showAnim: 'fadeIn'
	});
	
	$('.draggable .box_title').css('cursor', 'move');
	
	$(".left").sortable({ 
		revert: true, 
		connectWith: ('.left', '.right'),
		tolerance: 'pointer',
		handle: '.box_title',
		stop: save_order
	});
	$(".right").sortable({ 
		revert: true, 
		connectWith: ('.right', '.left'),
		tolerance: 'pointer',
		handle: '.box_title',
		stop: save_order
	});
	
	$('.more').click(function() {
		var pool = $(this).next().next();
		pool.children().insertBefore($(this));
		
		$(this).remove();
		
		return false;
	});

});

function save_order(event, ui)
{
	$.post(LOCAL_URL + 'index/post', { 
		'do': 'save_order',
		url: location.href,
		'left[]': $('.left').sortable('toArray'),
		'right[]': $('.right').sortable('toArray')
	});
}

function load(id, page)
{
	if ($(id).html().length > 0) {
		$(id).slideDown();
	} else {
		$(id).load(page);
	}
	
	return false;
}


function new_window(link)
{
	window.open(link.href, '_blank');
	
	return false;	
}