/* shared functions */
function get_highest_value(array)
{
	return array.sort(function(a,b){ return b-a; })[0];
}


function set_up_lightboxes()
{
	$("a[class*='lightbox']").fancybox(default_fancybox_options);
}


function set_up_autoclear_fields()
{
	$('.autoclear').focus(function(){
		$(this).val('').removeClass('autoclear');
	});
}


function set_maxlength_on_field(field, value)
{
	$(field).attr('maxlength', value);
}


function is_more_link(element)
{
	return element.attr('class').search(/more/g) != -1 ? true : false;
}


function is_more_nav_content_visible()
{
	return $('#more_nav_content').css('bottom') == '0px' ? true : false;
}


function hide_more_content_indicator()
{
	$('#more_nav_content').animate({
		'bottom' : '5px'
	}, 'fast');
	
	/* remove margin-top on content */
	$('#content').css('margin-top', '0');
}


function process_form_errors(data, form_prefix, form_id)
{	
	hide_form_errors();
	errors = eval(data.errors);
	
	if(errors.__all__) $('#' + form_id).before(errors.__all__);

	for (field in errors) 
		$('#' + form_prefix + field).after(errors[field]);
	
}


function hide_form_errors()
{
	$('.errorlist').remove();
}


function add_hover_to_buttons()
{
	$(':button, :submit').bind('mouseover, mouseout', function(){
		$(this).toggleClass('hover');
	});
}


function close_fancybox()
{
	//closes fancybox from an external element
	$.fn.fancybox.close();
}

function even_item_heights(items_selector, end_class)
{
	var items = $(items_selector);
	var all_items = [];
	var items_temp = [];
	
	$.each(items, function(index, item){
		var item = $(item);
		items_temp.push(item.height())
		if (item.hasClass(end_class) || ((items.length - 1) == index))
		{
			all_items.push(items_temp);
			items_temp = [];
		}
	});
	
	//get max height of each row
	var max_heights = [];
	$.each(all_items, function(index, arr){
		max_heights.push(arr.sort(function(a,b){return b-a;})[0]);
	});
	
	//set links to max height per row
	var set_index = 0;
	$.each(items, function(index, item){
		var item = $(item);
		item.height(max_heights[set_index]);
		if (item.hasClass(end_class))
			set_index++;
	});
}


function setup_sort_forms(cookie_name)
{
	var sort_by_value = $.cookie(cookie_name);
	var sort_by_form = $('form.sort_by');
	var sort_by_select = sort_by_form.find(":select[name='sort_by']");
	var sort_by_options = sort_by_select.children('option');
	
	sort_by_select.change(function()
	{
		sort_by_form.submit();
	});
	
	if (sort_by_value != null && sort_by_value != undefined) sort_by_options.attr('selected', '');
	
	$.each(sort_by_options, function(index, element)
	{
		if ($(element).val() == sort_by_value) $(element).attr('selected', 'selected');
	});
	
	/* set up ajax form to set sorting */
	sort_by_form.ajaxForm({
		url : this.action,
		dataType : 'json',
		type : 'POST',
		success : function(json)
		{
			if (eval(json.success)) document.location = top.location;
		}
	});
}


function fade_out_elements(selector)
{
	elements = $(selector);
	
	if(elements) elements.fadeOut();
}

