$(document).ready(function()
{
	$("div#no_js").remove();
	$("div#main").css("display","inline");

	$(".nogo").click(function(e){
		e.preventDefault();
		return false;
	});
	if($('input#daterangepicker').length>0)
	{
		$('input#daterangepicker').daterangepicker( {
			presetRanges: [
				{text: 'Today', dateStart: 'today', dateEnd: 'today' },
				{text: 'Tomorrow', dateStart: 'tomorrow', dateEnd: 'tomorrow' },
				{text: 'This Week', dateStart: 'last sunday', dateEnd: 'next sunday' }
			]
		 });
	}

	//shorten the time that they see the raw HTML
	//Add TinyMCE Editor
	$(".tinyMCE").each(function()
	{
		$this = $(this);
		var tmce_opts = {
			script_url							: '/js/tiny_mce/tiny_mce.js',
			theme								: "advanced",
			plugins								: "paste,spellchecker",
			theme_advanced_buttons1				: "bold,italic,|,link,unlink,|,bullist,numlist,|,undo,redo,|,pasteword,selectall,spellchecker,|,formatselect,|,code",
			theme_advanced_buttons2				: "",
			theme_advanced_buttons3				: "",
			theme_advanced_buttons4				: "",
			theme_advanced_toolbar_location		: "top",
			theme_advanced_toolbar_align		: "left",
			theme_advanced_statusbar_location	: "none",
			theme_advanced_resizing				: true,
			theme_advanced_resizing_min_width	: "655",
			theme_advanced_resizing_min_height	: "75",
			theme_advanced_resizing_max_width	: "655",
			theme_advanced_resizing_max_height	: "500",
			theme_advanced_blockformats			: "p,h1,h2,h3",
			gecko_spellcheck					: true,
			spellchecker_languages				: "English=en",
			paste_auto_cleanup_on_paste			: true,
			content_css							: "/js/tiny_mce/tiny_mce.css"
		};
		//Change options for Tiny TinyMCE Editor
		$(this).css({
			"width"		: "655px",
			"height"	: "75px"
		});
		if($(this).is(".tiny"))
		{
			$(this).css({
				"width"		: "375px"
			});
			tmce_opts.theme_advanced_buttons1				= "bold,italic,|,bullist,numlist,|,undo,redo,|,pasteword,selectall,spellchecker";
			tmce_opts.theme_advanced_resizing_min_width		= "375";
			tmce_opts.theme_advanced_resizing_max_width		= "375";
			tmce_opts.theme_advanced_resizing_max_height	= "225";
			tmce_opts.theme_advanced_blockformats			= "p,h1,h2,h3,h4";

			if($(this).is(".haslinks"))
				{ tmce_opts.theme_advanced_buttons1				= "bold,italic,|,link,unlink,|,bullist,numlist,|,undo,redo,|,pasteword,selectall,spellchecker"; }
		}
		// fadein and callback
		$this.wrap("<div></div>")
			.parent("div").hide()
			.after('<p class="loading">Loading Text Editor...</p>')
			.next('.loading')
				.css({
					'width'			: tmce_opts.theme_advanced_resizing_min_width+"px",
					'height'		: tmce_opts.theme_advanced_resizing_min_height+"px",
					'line-height'	: tmce_opts.theme_advanced_resizing_min_height+"px"
				});

		tmce_opts.init_instance_callback = function(){
			//alert($this.attr("id"));
			$this.closest('form')
				.find("textarea.tinyMCE")
				.parent('div')
				.fadeIn(1000)
				.next('.loading')
					.remove();
			add_a_events($this.parent('div'));
		}
		
		$(this).tinymce(tmce_opts);
		
	});
	//a(link) functions

	//DatePicker form-field functions
	if($("input.datepicker").length>0)
	{
		var disable_first_thurs = function(){
			setTimeout(function(){
				if($($("#ui-datepicker-div .ui-datepicker-calendar tbody td").get(4)).find("a, span").length>0)
					{ var thurs = $($("#ui-datepicker-div .ui-datepicker-calendar tbody td").get(4)); }
				else
					{ var thurs = $($("#ui-datepicker-div .ui-datepicker-calendar tbody td").get(11)); }
				thurs.addClass("ui-state-disabled")
					.removeAttr("onclick")
					.unbind("click")
					.find("a")
						.unbind("click")
						.click(function(){return false;});
			},25);
		}
		$("input.datepicker").datepicker({
			dateFormat: 'yy-mm-dd',
			minDate:	minEventDate,
			maxDate:	maxEventDate,
			beforeShow: disable_first_thurs,
			onChangeMonthYear: disable_first_thurs
		});
	}	
	//TimePicker form-field functions
	$("input.start_timepicker").timepicker({
			'hourCaption'		: 'First, Select your Hour',
			'minuteCaption'		: 'Select your Specific Time',
			'inputReadOnly'		: true,
			'closeOnComplete'	: true,
			'closeOnFormclick'	: true,
			'minDivision'		: 15,
			'hourAM'			: [9,10,11],				// AM hours
			'hourPM'			: [12,1,2,3,4,5,6,7,8],		// PM hours
			'hourFormat'		: 12,						// 12 or 24
			'hourCols'			: 6							// 6 or 8 columns
	});
	$("input.end_timepicker").timepicker({
			'hourCaption'		: 'First, Select your Hour',
			'minuteCaption'		: 'Select your Specific Time',
			'inputReadOnly'		: true,
			'closeOnComplete'	: true,
			'closeOnFormclick'	: true,
			'minDivision'		: 15,
			'hourAM'			: [9,10,11],				// AM hours
			'hourPM'			: [12,1,2,3,4,5,6,7,8,9,10],// PM hours
			'hourFormat'		: 12,						// 12 or 24
			'hourCols'			: 6							// 6 or 8 columns
	});
	
	add_a_events($('body'));
	$("span").each(function(){
		if($(this).attr("title")!="")
		{
			$(this).simpletooltip();
			$(this).css("cursor","pointer");
		}
	});

});

function add_a_events($container)
{
	$container.find("a").each(function()
	{
		$(this).attr("rel",$(this).attr("title"));
		//Apply ToolTips
		if($(this).attr("title")!="")
			{ $(this).simpletooltip(); }
		//Open links in external windows if it has a class of "ext".
		if($(this).is(".ext"))
		{
			$(this).unbind("click").click(function(e)
			{
				window.open($(this).attr("href"));
				e.preventDefault();
				return false;
			});
		}
		//Pop open a dialog confirm on link click if it has a class of "confirm".
		if($(this).is(".confirm"))
		{
			$(this).unbind("click").click(function(e)
			{
				var link = $(this);
				var title = $(this).attr("rel");
				if(title == "")
					{ title = "do this? This change will be permanent."; }
				else
					{ title = "you want to "+title; }
				confirm_dialog(
					"Are you sure that you want to "+title,
					function()
						{ window.location = link.attr("href"); }
				);
				e.preventDefault();
				return false;
			});
		}
	});
}

function confirm_dialog(content,yes_callback,no_callback)
{
	$("body").append('<div id="confirm_dialog" title="Please Confirm">'+content+'</div>');
	var clicked = false;
	$("#confirm_dialog").dialog({
		'autoOpen'	: true,
		'draggable'	: false,
		'resizable'	: false,
		'modal'		: true,
		'close'		: function()
		{
			if(clicked)
				{ if(typeof yes_callback == 'function')	{yes_callback();} }
			else
				{ if(typeof no_callback == 'function')	{no_callback();}	}
			$("#confirm_dialog").remove();
		},
		'buttons'	:
		{
			"Yes":function()
			{
				clicked = true;
				$("#confirm_dialog").dialog('close');
			},
			"No":function()
				{ $("#confirm_dialog").dialog('close'); }
		}
	});
};
