var LB = {
	getBaseUrl : function()
	{
		return location.protocol + '//' + window.location.host;
	},
	bootstrap_ajax: function()
	{
		$.ajaxSetup(
			{
				'type':'post',
				'beforeSend': function(xhr) {xhr.setRequestHeader("Range", "content")},
				'error':	function(xhr) {
					if(xhr.status == 401)
					{
						alert('Your session has ended. Please open a new tab, log in to ListBaby, then come back here and try again');
					}
				}
			});
	},
	getServerAlertsUL: function()
	{
		alerts_container = $('#ajax_alerts');
		if(! alerts_container.length) alerts_container = $('.lb_section.alerts');
		return alerts_container;
	},
	clearServerAlerts: function()
	{
		LB.getServerAlertsUL().find('li').remove();
	},
	addServerAlert: function(strMsg, strDisposition, showForSeconds)
	{
		strDisposition = (strDisposition) ? strDisposition : 'neutral';
		switch(typeof(showForSeconds))
		{
			case 'number':
			case 'undefined':
				showForSeconds = (showForSeconds) ? showForSeconds : 3;
				the_alert = $('<li class="' + strDisposition + '_alert">' + strMsg + '</li>').css('display','inline').hide();
				LB.getServerAlertsUL().append(the_alert);
				the_alert.fadeIn(200).delay(showForSeconds * 1000).fadeOut(1000);
				break;
			case 'string':
				switch(showForSeconds)
				{
					case 'modal':
						modal_options = {
							'modal':true,
							'autoOpen':true,
							'title':'Server Alert',
							'buttons':{
								'dismiss':function(){$(this).dialog('close')}
							}
						};
						$('<div class="modal_alert ' + strDisposition + '"><p class="' + strDisposition + '_alert">' + strMsg + '</p></div>').dialog(modal_options);
						break;
				}
				break;
			case 'object':
				$('.modal_alert').remove();
				$('<div class="modal_alert ' + strDisposition + '"><p class="' + strDisposition + '_alert">' + strMsg + '</p></div>').dialog(showForSeconds);
				break;
			
		}
	},
	removeThingAfterDelay: function (thing, showForSeconds)
	{
		switch(typeof(showForSeconds))
		{
			case 'number':
			case 'undefined':
				showForSeconds = (showForSeconds) ? showForSeconds : 60;
				$(thing).delay(showForSeconds * 1000).fadeOut(1000);
				break;
		}
		
	},

	/**
	 * Binds click to every link with class "ajax_modal" so it
	 * loads with ajax into a modal dialog, then moves the first 
	 * header it finds into the title bar of the dialog
	 */
	bootstrap_modal_ajax_links: function()
	{
		modal = $('#primary_ajax_modal');
		if(! modal.length) {
			modal = $('<div id="primary_ajax_modal"></div>').dialog({
				'autoOpen':false,
				'title':'ListBaby',
				'modal':true,
				'width':600,
				/*'height':300, jb 11/2 - Each modal is different and is set on the link that spawns each instance. It is set as modal_height="" */
				'resizable': false,
			});
		}
		$('a.ajax_modal').each(function(){
			var link = $(this);
			if(link.data('ajax_modal_initialized')) 
				return;
			link.data('ajax_modal_initialized', true);
			link.click(
			function(e) {
				e.preventDefault();
				var link = $(this);
				var modal = $('#primary_ajax_modal');
				iframe_width = '600';
				iframe_height = '800';
				if(link.attr('modal_width'))
				{
					modal.dialog('option', 'width', link.attr('modal_width'));
					iframe_width = link.attr('modal_width');
				}
				if(link.attr('modal_height'))
				{
					modal.dialog('option', 'height', link.attr('modal_height'));
					iframe_height = link.attr('modal_height');
				}
				var the_href = link.attr('href');
				if(-1 == the_href.indexOf('layout=', 0)){
				   	the_href = the_href + '?layout=just_view';
				} 
				if(link.attr('modal_as_iframe'))
				{	
					modal.dialog('option', 'width', (new Number(iframe_width) + 0) + 'px');
					modal.dialog('option', 'height', (new Number(iframe_height) + 40));
					modal.empty();
					modal.append($('<iframe class="' + link.attr('iframe_class') + '" src="' + the_href + '" width="' + iframe_width + '" height="' + iframe_height + '"  >iframe fail!</iframe>'));
				} else {
					modal.load(the_href, function(){
						title = $('#primary_ajax_modal').find('.lb_section h1, .lb_section h2').first().remove().html();
						$('#primary_ajax_modal').dialog('option', 'title', title);		
					});
				}
				modal.dialog('open');
			});
		});
	},

	bootstrap_section_loader: function()
	{
		$.fn.refresh_lb_section = function()
		{
			var elem = $(this);
			if( ! elem.attr('lb_section_href') )
				return;
			$.get(elem.attr('lb_section_href'), [], LB.replace_matching_section);
		}
	},
	replace_matching_section: function(data)
	{
		var new_elem = $(data);
		var old_elem = $('[lb_section_href='+new_elem.attr('lb_section_href') + ']');
		old_elem.replaceWith(new_elem);
		
	},

	delete_elements_by_selector: function(selector)
	{
		$(selector).remove();
	},
	close_ajax_modal: function()
	{
		$('#primary_ajax_modal').dialog('close');
	}
};

$(function(){ 
	LB.bootstrap_modal_ajax_links(); 
	$(this).ajaxComplete(LB.bootstrap_modal_ajax_links);
} );
$(function(){ LB.bootstrap_section_loader(); } );

LB.bootstrap_ajax();
$(function() {
	$('.alerts').each(function(){
		LB.removeThingAfterDelay(this, 15);
	}
		);
});

$(function () {
	//check all, requires that the "check all" checkbox has an attribute called match_name that is set to the same value as the name attribute of the checkboxes which should be checked/unchecked.
	$('.checkall').click(function () {
		check_all = $(this);
		check_all.closest('.lb_section').find(':checkbox[name='+check_all.attr('match_name') +'],:checkbox[match_name=' + check_all.attr('match_name') + ']').attr('checked', this.checked);
	});
	// Here we have the datepicker function that is part of the jquery ui package. nice!
	$(function() {
		$("#datepicker").datepicker();
	});
	$(".accordion").accordion({
		//active shows none at pageload
		active: false,
		//collapsible allows all divs to close
		collapsible: true,
		autoHeight: false
	});
	
}); //this is the closing tag for the doc ready function


