// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

if (rails_env != 'production') {
	Ajax.Request.prototype.dispatchException = function(exception) {
		throw exception;
	};
}

var NotifyUtil = Class.create({
initialize: function(elems) {
				if (!Object.isArray(elems)) elems = $A([elems]);
				elems.each(function(elem) {
					var notify_text = elem.readAttribute('_notifytext');
					if (notify_text) {
						var box_type = elem.getAttribute('type');
						elem.addClassName('notify')
								.writeAttribute('type', 'text') // make it work with password boxes
								.setValue(notify_text);
						elem.observe('focus', function() {
							if (elem.hasClassName('notify')) {
								elem.setValue('')
										.writeAttribute('type', box_type)
										.removeClassName('notify');
							}
						});
						elem.observe('blur', function() {
							if (elem.getValue() == '') {
								elem.addClassName('notify')
										.writeAttribute('type', 'text')
										.setValue(notify_text);
							}
						});
					}
				});
			}
});

function track_pageview(a) {
	if (pageTracker) {
		pageTracker._trackPageview(a.getAttribute('href'));
	}
}

function setup_application() {

	function login_register_handler(ev, modal) {
		ev.stop();
		var login_form = $('login-form');
		new Ajax.Request(login_form.action, {
method: 'post',
postBody: login_form.serialize(),
onSuccess: function(res) {
			window.location.reload();
		},
onFailure: function(res) {
			if (res.responseText.strip() != '')
				$('modal-form-errors').update(res.responseText);
		}
		});
	}

	function forgot_password_handler(ev, modal) {
		ev.stop();
		var login_form = $('login-form');
		new Ajax.Request(login_form.action, {
method: 'post',
postBody: login_form.serialize(),
onSuccess: function(res) {
			if (res.responseText.strip() != '') {
				$('modal-form-footer').update(res.responseText);
				modal.close.bind(modal).delay(3);
			}
		},
onFailure: function(res) {
			if (res.responseText.strip() != '')
				$('modal-form-errors').update(res.responseText);
		}
		});
	}

	function bind_to_modal(handler, modal) {
		return handler.bindAsEventListener(null, modal);
	}

	Modal.addMethods({
clicked: function(ev) {
			 track_pageview(ev.element());
		 }
	});
	new Modal('login-link', {
after_load: function(modal) {
				$('login-form').observe('submit', bind_to_modal(login_register_handler, modal));
			}
	});
	new Modal('register-link', {
after_load: function(modal) {
				$('login-form').observe('submit', bind_to_modal(login_register_handler, modal));
			}
	});
	new Modal('forgot-password-link', {
after_load: function(modal) {
				$('login-form').observe('submit', bind_to_modal(forgot_password_handler, modal));
			}
	});

	var logout_link = $('logout-link');
	if (logout_link) {
		logout_link.observe('click', function(ev) {
			ev.stop();
			track_pageview(ev.element());
			new Ajax.Request(logout_link.href, {
method: 'post',
onSuccess: function(res) {
			window.location.reload();
		}
			});
		}.bindAsEventListener());
	}

	$('nav-pullouts').immediateDescendants().each(function(item) {
		var open = false;
		var a = item.down('a');
		var submenu = item.down('.submenu');
		if (submenu) {
			a.observe('click', function(ev) {
				ev.stop();
				open ? submenu.hide() : submenu.appear({duration: .4});
				open = !open;
			});
		}
	});

	['browse_price_range'].each(function(price_range_form) {
		price_range_form = $(price_range_form);
		var price_range_select = price_range_form.down('select');
		price_range_select.observe('change', function() {
			if (price_range_select.options.selectedIndex != 0)
				price_range_form.submit();
		});
	});

	if ($('results-sorter')) {
		var results_sorter_form = $('results-sorter').down('form');
		var results_sorter_select = results_sorter_form.down('select');
		results_sorter_select.observe('change', function() {
			if (results_sorter_select.options.selectedIndex != 0)
				results_sorter_form.submit();
		});
	}

	$$('a.add-to-wishlist').each(function(a) {
		a.observe('click', function(ev) {
			ev.stop();
			track_pageview(ev.element());
			new Ajax.Request(a.href, {
onSuccess: /remove/.test(a.href) ? function(res) {
			   a.up('div.result').dropOut({
afterFinish: function(fx) {
				 fx.element.remove();
				 //$('results').forceRerendering()?
				 if (Prototype.Browser.IE) $('results').hide().show();
			 }
			   });
			   if (Prototype.Browser.IE) $('results').hide().show();
		   } : function(res) {
			   a.replace(res.responseText);
		   }
			});
		});
	});

	['email-bio-link', 'email-wishlist-link'].each(function(class_name) {
		new Modal(class_name, {
after_load: function(modal) {
				var form = $(class_name.replace('-link', '-form'));
				form.observe('submit', function(ev) {
					ev.stop();
					track_pageview(ev.element());
					new Ajax.Request(form.action, {
method: 'post',
postBody: form.serialize(),
onSuccess: function(res) {
			if (res.responseText.strip() != '') {
				$('modal-form-footer').update(res.responseText);
				form.name.value = form.email.value = '';
				(function() {
					$('modal-form-footer').update();
				}).delay(4);
			}
		},
onFailure: function(res) {
			if (res.responseText.strip() != '')
				$('modal-form-errors').update(res.responseText);
		}
					});
				});
			}
		});
	});

	var refresh_lead_form = Prototype.emptyFunction;
	var bio_sections = $('bio-sections');
	var lead_form_container = $('bio-how-to-book');
	if (lead_form_container) {
		refresh_lead_form = Prototype.Browser.IE ? function() {
			// force IE to redraw from scratch
			bio_sections.hide().show();
			lead_form_container.hide().show();
		} : function() {
			lead_form_container.show();
		};
	}

	if ($('event-date')) {
		var calendar_manager = new scal('event-date-calendar', 'event-date', {
updateformat: 'mm/dd/yyyy',
openeffect: function(element) {
				  return element.appear({
duration: .25,
afterFinish: refresh_lead_form
				  });
			  },
closeeffect: function(element) {
				  return element.fade({
duration: .25,
afterFinish: refresh_lead_form
				  });
			  }
		});
		$('calendar-toggle').observe('click', function(ev) {
			ev.stop();
			calendar_manager.toggleCalendar();
		});
	}

	if ($('speakercarousel')) {
		// load carousel support code
		var carousel_manager = new Carousel('speakercarousel', 175, 127, {}, {
setSize: 3,
duration: .5,
direction: 'horizontal'
		});
		carousel_manager.load();

		///// carousel mouseovers

		var speakercarousel = $('speakercarousel');
		var current_img;

		speakercarousel.observe('mouseover', function(ev) {
			// swell the new image, de-swelling the current one if necessary
			var img = ev.element();
			if (img.hasClassName('speakerimagessm')) {
				if (current_img) current_img.removeClassName('swell');
				img.addClassName('swell');
				current_img = img;
			}
		});

		speakercarousel.observe('mouseout', function(ev) {
			// de-swell any currently swelled image
			if (current_img) current_img.removeClassName('swell');
			current_img = undefined;
		});
	}

	if ($('static-speaker-select-form')) {
		var speaker_sel_form = $('static-speaker-select-form');
		var speaker_sel = $('static-speaker-select');
		speaker_sel.observe('change', function(ev) {
			ev.stop();
			if (speaker_sel.options.selectedIndex != 0)
				location.href = $F(speaker_sel);
		});
	}

	if ($('static-topic-select-form')) {
		var topic_sel_form = $('static-topic-select-form');
		var topic_sel = $('static-topic-select');
		topic_sel.observe('change', function(ev) {
			ev.stop();
			if (topic_sel.options.selectedIndex != 0)
				location.href = $F(topic_sel);
		});
	}

	if ($('topic_search')) {
		$('topic_search').reset();
		$('topic_search').observe('click', function(event) {
			var cb = event.element();
			if (cb.match('input') && cb.getAttribute('type') == 'checkbox') {
				var li = cb.up('li');
				cb.checked ?
						li.addClassName('selected') :
						li.removeClassName('selected');
			}
		});
	}

	$$('.expand-collapse').each(function(a) {
		a.observe('click', function(event) {
			event.stop();
			var list = a.up('h3').next('ul');
			list.visible() ? list.hide() : list.appear({duration: .25});
		});
	});

	var qqqq=$('query');

	new NotifyUtil(qqqq);

	$('search').observe('submit', function(ev) {
		if ($('query').getValue() == '' || $('query').hasClassName('notify'))
			ev.stop();
	});
}

function utmx_section(s) {
}