function observe_dropdown(dropdown) {
	if(dropdown == 'studios') {
		Event.observe($('studios_dropdown'),'change',function() { update_schools(); });
	}
	if(dropdown == 'schools') {
		Event.observe($('schools_dropdown'),'change',function() { update_shoots(); });
	}
	if(dropdown == 'shoots') {
		Event.observe($('shoots_dropdown'),'change',function() { window.location = '/view_shoot?' + $('shoots_dropdown').serialize(); });
	}
}

function update_schools() {
	var selected = $('studios_dropdown').serialize();
	if(selected != '') {
		new Ajax.Updater('schools_dropdown_container','/includes/schools_dropdown.php', 
			{ 
				evalScripts: true, 
				method: 'get', 
				parameters: selected
			}
		);
		new Ajax.Updater('shoots_dropdown_container','/includes/shoots_dropdown.php', 
			{ 
				evalScripts: true, 
				method: 'get', 
				parameters: selected
			}
		);
	}
}

function update_shoots() {
	var selected = $('schools_dropdown').serialize();
	if(selected != '') {
		new Ajax.Updater('shoots_dropdown_container','/includes/shoots_dropdown.php', 
			{ 
				evalScripts: true, 
				method: 'get', 
				parameters: selected
			}
		);
		new Ajax.Updater('shoots_table','/includes/shoots_table.php',
			{
				evalScripts: true,
				method: 'get',
				parameters: selected
			}
		);
	}
}

function table_rollover() {
	$$('#shoots_table tr:not([class~=nohover]) > td').each(
		function(td) {
			Event.observe(td, 'mouseover', function() 
				{
					td.parentNode.addClassName('hover');
				}
			);
			Event.observe(td, 'mouseout', function() 
				{
					td.parentNode.removeClassName('hover');
				}
			);
			Event.observe(td, 'click', function() 
				{
					window.location = '/view_shoot?shoot_id=' + td.parentNode.id;
				}
			);
		}
	);
}

function nav_rollover() {
	$$('#header #navigation li').each(
		function(li) {
			Event.observe(li, 'mouseover', function() 
				{
					li.addClassName('hover');
				}
			);
			Event.observe(li, 'mouseout', function() 
				{
					li.removeClassName('hover');
				}
			);
		}
	);
}

function popup_images() {
	$$('a[class~=popup]').each(
		function(link) {
			Event.observe(link, 'click', function()
				{
					var href = link.href;
//					link.href = 'javascript:void(0)';
					do_popup(href);
				}
			);			
		}
	);
}

function do_popup(href) {
	var width = 640;
	var height = 640;
    window.open(href, '', 'width=' + (width + 2) + ',height=' + (height + 25) 
		+ ',toolbar=no,resizable=yes,menubar=no,scrollbar=yes');

    return false;
}

function disable_deleted_children() {
	$$('td.deleted input').each(
		function(input) {
			input.disable();
		}
	);
	$$('td.deleted a').each(
		function(link) {
			link.href = "#";
		}
	);
	$$('td.unposted input.download').each(
		function(input) {
			input.disable();
		}
	);
}

function batch_setup() {
	$$('form.batch_form input[type=checkbox]').each(
		function(cb,i) {
			Event.observe(cb,'click',function()
				{
					if(cb.checked == true) {
						new Ajax.Request('/_batch',
							{
								method: 'post',
								parameters: $H(
									{
										'action' : 'add',
										'batch_action' : cb.className,
										'shoot_id' : $('shoot_id').getValue(),
										'item' : cb.value
									}
								)
							}
						);
					} else if(cb.checked == false) {
						new Ajax.Request('/_batch',
							{
								method: 'post',
								parameters: $H(
									{
										'action' : 'remove',
										'batch_action' : cb.className,
										'shoot_id' : $('shoot_id').getValue(),
										'item' : cb.value
									}
								)
							}
						);
					}
				}
			);
		}
	);
}

function batch_delete_confirm() {
	$$('a.batch_delete').each(
		function(link) {
			Event.observe(link,'click',
				function() {
					if(link.href.indexOf('#') == '-1') {
						link.name	=	link.href;
						link.href	=	'#';
					}
					var agree = confirm("Deleting images cannot be undone.  Are you sure you wish to continue?");
					if(agree) {
						window.location = link.name;
						return true;
					} else {
						return false;
					}
				}
			);
		}
	);
}

function show_process_images() {
	var ticket = $('ticket').getValue();
	var shoot_id = $('shoot_id').getValue();
	new Ajax.Updater('process_images','/_process_button.php',
		{
			method: 'get',
			parameters: 'shoot_id=' + shoot_id + '&ticket=' + ticket + ''
		}
	);
}

Event.observe(window, 'load', function()
	{
		table_rollover();
		nav_rollover();
		popup_images();
		disable_deleted_children();
		batch_setup();
		batch_delete_confirm();
	}
);
