function initializeExternalLinks() {
	
	$$('a.external').each(function(link) {
		link.observe('click', function(e){
			window.open(link.href, 'external');
			Event.stop(e);
		})
		
	})
	
	$$('a.popped').each(function(link) {
		link.observe('click', function(e){
			var options = 'toolbars=0,directories=0,scrollbars=1,resizable=1,location=0,width=600,innerWidth=600,height=700,innerHeight=700'
			if (link.rev) {
				options = link.rev;
			}
			
			popped = window.open(link.href, 'popped', options);
			popped.focus();
			Event.stop(e);
		})
		
	})
	
}

function initializeBannerAlerts() {
	
	$$('a.bannerAlert').each(
		function(link) {
			link.observe('click', function(e) {
				
				var url = "/banner_alert.php?src=" + encodeURIComponent(link.id)
				new Ajax.Request(url,{
					method: 'get',
					  onComplete: function(transport) {
						/* if target on link, or link marekd as external, new window is already open, do nothing */
						if (!link.target && !link.hasClassName('external')) {
							window.location.href = link.href;
						}
					  }
					});
				
				/* if there's no target, or link is not marked as external, stop event, we'll process it later */
				if (!link.target && !link.hasClassName('external')) {
					Event.stop(e);
				}
			});
		
		}
	)
}

function initializeQuickSearch() {
	
	$('qsselect').observe('change', function(e) {
		document.location.href = $F('qsselect');
		Event.stop(e);
	});
	
}

function initializeBookingForm() {
	
	if ($('booking_request')) {
		$('booking_request').observe('submit', function(e) {
			var errors = new Array()
			var email = $F('booking_request_email');
			var name = $F('booking_request_name');
			
			if (name.strip().length == 0) {
				errors.push('Please enter your name')
			}
			
			if (email.strip().length == 0) {
				errors.push('Please enter your email address')
			} else if (!wellFormedEmail(email)) {
				errors.push('Please enter a valid email address')
			}
			
			if (errors.length > 0) {
				alert("We were not able to process your request. Fix these problems and try again: \n" + errors.join("\n"))
				Event.stop(e);
			}
		})
	}
	
}

function wellFormedEmail(email) {
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (email.match(emailRegEx)) {
		return true;
	}
	return false;
}


NavBuilder = {
	
	topNavHtml: '<ul>' +
		'<li><a href="/">home</a></li>' +
		'<li><a href="/map/">map</a></li>' + 
		'<li><a href="http://forum.1stopthailand.com/">forum</a></li>' +
		'<li><a href="http://blogs.1stopthailand.com/">blogs</a></li>' +
		'<li><a href="/travel_news/">news</a></li>' +
		'<li><a href="/video/">video</a></li>' +
		'<li><a href="/aboutus/">about</a></li>' +
		'<li><a href="/aboutus/#contact">contact</a></li>' +
		'</ul>',
	
	insertTopNav: function() {
		$('header').firstDescendant().insert({after: this.topNavHtml})
	}
	
	
}


document.observe('dom:loaded', function() {NavBuilder.insertTopNav()});
document.observe('dom:loaded', initializeExternalLinks);
document.observe('dom:loaded', initializeBannerAlerts);
document.observe('dom:loaded', initializeQuickSearch);
document.observe('dom:loaded', initializeBookingForm);