var Event = YAHOO.util.Event;
var Dom = YAHOO.util.Dom;

$(document).ready(
	function()
	{
		var searchType = $.cookies.get('searchType');
		if (searchType == null) searchType = 'simple';
		select_search(searchType);
		
		if (Dom.get("datestart")) init_calendar("start");
		if (Dom.get("dateend")) init_calendar("end");
		
		if (this_script == 'search')
		{
			$('div.search-result').each(
				function()
				{
					var checkbox = $(this).find('input.select');
					
					if (checkbox.is(':checked'))
					{
						$(this).addClass('item-selected');
					}
					else
					{
						checkbox.hide();
					}
					
					$(this).mouseover(
						function()
						{
							checkbox.show();
						}
					);
					$(this).mouseout(
						function()
						{
							if (!$(this).hasClass('item-selected')) checkbox.hide();
						}
					);
					$(this).click(
						function(e)
						{
							var target = e.originalTarget || e.srcElement;
							if (target.nodeName == 'IMG') return true;
							
							if (target.nodeName == 'INPUT') checkbox.attr('checked', !checkbox.is(':checked'));
							
							if (checkbox.is(':checked'))
							{
								$(this).removeClass('item-selected');
								checkbox.attr('checked', false);
							}
							else
							{
								$(this).addClass('item-selected');
								checkbox.attr('checked', true);
							}
							
							return false;
						}
					);
				}
			);
		}
	}
);

function select_search(type)
{
	$('a.search_link_selected').removeClass('search_link_selected');
	$('a#link_' + type).addClass('search_link_selected');
	$('div.search_container:visible').hide();
	$('div#container_' + type).show();
	
	$.cookies.set('searchType', type);
}

function toggle_menu(name, offsetName, ignoreOpen)
{
	if (typeof(offsetName) != 'string') offsetName = 'null';
	if (typeof(ignoreOpen) != 'boolean') ignoreOpen = false;
	if ($('#dashboard-menu-' + name).is(':visible'))
	{
		close_menu(name);
	}
	else
	{
		open_menu(name, offsetName, ignoreOpen);
	}
}

function close_menu(menu)
{
	if (typeof(menu) == 'string') menu = $('#dashboard-menu-' + menu);
	if (menu.length < 1) return;
	var children = menu[0].childMenu;
	
	menu.hide();
	if ((typeof(children) == 'array') || (typeof(children) == 'object'))
	{
		if (children !== null)
		{
			if (children.length > 0) close_menu(children);
			menu[0] = null;
		}
	}
}

function close_menus()
{
	$('div.dashboard-menu:visible').each(
		function()
		{
			close_menu($(this));
		}
	);
}

function open_menu(name, offsetName, ignoreOpen)
{
	if (typeof(offsetName) != 'string') offsetName = 'null';
	if (typeof(ignoreOpen) != 'boolean') ignoreOpen = false;
	
	var button = $('#dashboard-button-' + name);
	var menu = $('#dashboard-menu-' + name);
	var dashboard = $('#dashboard');
	var offset = $('#' + offsetName);
	
	if (!ignoreOpen) close_menus();
	
	menu.show();
	
	if (ignoreOpen)
	{
		menu.css('bottom', (dashboard.height() + 7) + 'px');
	}
	else
	{
		menu.css('bottom', (dashboard.height() + 2) + 'px');
	}
	
	if (offset.length > 0)
	{
		menu.css('left', (offset.width() + offset.position().left + 22) + 'px');
		offset[0].childMenu = menu;
	}
	else
	{
		menu.css('left', (button.position().left - 11) + 'px');
	}
}

function show_preview(id, sid, aid, lid, live)
{
	if (sid == null) sid = 0;
	if (aid == null) aid = 0;
	if (lid == null) lid = 0;
	
	var extra = '';
	
	if (typeof(live) == 'boolean' && live === true) extra = '&live';
	
	window.open('preview.php?id=' + id + '&sid=' + sid + '&aid=' + aid + '&lid=' + lid + extra, 'ama_preview', 'location=no,status=no,width=625,height=1000,toolbar=no,scrollbars=yes,menubar=no');
}

function toggle_all_photos(value)
{
	$('div.search-result input.select').attr('checked', value).show();
	if (value)
	{
		$('div.search-result').addClass('item-selected');
	}
	else
	{
		$('div.search-result').removeClass('item-selected');
	}
}

function init_calendar(name)
{
	var Event = YAHOO.util.Event,
		Dom = YAHOO.util.Dom,
		dialog,
		calendar;

	var showBtn = Dom.get("show" + name);

	Event.on(showBtn, "click", function() {

		// Lazy Dialog Creation - Wait to create the Dialog, and setup document click listeners, until the first time the button is clicked.
		if (!dialog) {

			// Hide Calendar if we click anywhere in the document other than the calendar
			Event.on(document, "click", function(e) {
				var el = Event.getTarget(e);
				var dialogEl = dialog.element;
				if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
					dialog.hide();
				}
			});

			function resetHandler() {
				// Reset the current calendar page to the select date, or 
				// to today if nothing is selected.
				var selDates = calendar.getSelectedDates();
				var resetDate;
	
				if (selDates.length > 0) {
					resetDate = selDates[0];
				} else {
					resetDate = calendar.today;
				}
	
				calendar.cfg.setProperty("pagedate", resetDate);
				calendar.render();
			}
	
			function closeHandler() {
				dialog.hide();
			}

			dialog = new YAHOO.widget.Dialog("container" + name, {
				visible:false,
				context:["show" + name, "tl", "bl"],
				buttons:[ {text:"Reset", handler: resetHandler, isDefault:true}, {text:"Close", handler: closeHandler}],
				draggable:false,
				close:true
			});
			dialog.setHeader('Pick A Date');
			dialog.setBody('<div id="cal' + name + '"></div>');
			dialog.render(document.body);

			dialog.showEvent.subscribe(function() {
				if (YAHOO.env.ua.ie) {
					// Since we're hiding the table using yui-overlay-hidden, we 
					// want to let the dialog know that the content size has changed, when
					// shown
					dialog.fireEvent("changeContent");
				}
			});
		}

		// Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
		if (!calendar) {

			calendar = new YAHOO.widget.Calendar("cal" + name, {
				iframe:false,          // Turn iframe off, since container has iframe support.
				hide_blank_weeks:true  // Enable, to demonstrate how we handle changing height, using changeContent
			});
			calendar.render();

			calendar.selectEvent.subscribe(function() {
				if (calendar.getSelectedDates().length > 0) {

					var selDate = calendar.getSelectedDates()[0];

					var dStr = selDate.getDate();
					var mStr = selDate.getMonth() + 1;
					var yStr = selDate.getFullYear();
	
					Dom.get("date" + name).value = yStr + "-" + strpad(mStr) + "-" + strpad(dStr);
				} else {
					Dom.get("date" + name).value = "";
				}
				dialog.hide();
			});

			calendar.renderEvent.subscribe(function() {
				// Tell Dialog it's contents have changed, which allows 
				// container to redraw the underlay (for IE6/Safari2)
				dialog.fireEvent("changeContent");
			});
		}

		var seldate = calendar.getSelectedDates();

		if (seldate.length > 0) {
			// Set the pagedate to show the selected date if it exists
			calendar.cfg.setProperty("pagedate", seldate[0]);
			calendar.render();
		}

		dialog.show();
	});
}