// JavaScript Document

function clearField(which, what) {
	if($(which).value == what) {
		$(which).value = '';
		if($(which).className == 'textDefault') {
			$(which).className = 'text';	
		}
	}
}
function populateField(which, what) {
	if($(which).value == '') {
		if($(which).className == 'text') {
			$(which).className = 'textDefault';	
		}
		$(which).value = what;
	}
}
function processSignup() {
	var where = "http://editor.ne16.com/clientforms/drtavelprofileform/?p=29f363ff-ee31-442a-b4fb-0a85039dbc9c&email=";
	if($('emailSignup').value != 'Your email address') {
		window.location= where + $('emailSignup').value;
	}
}

var Subsubnav = Class.create({
	initialize: function() {
		this.currently_displayed = null;
		$$('ul.subsubnav a').each(function(link) {
			var id = link.readAttribute('rel');
			if ($(id).visible()) {
				this.currently_displayed = id;
			}
		}, this);
		$$('ul.subsubnav a').invoke('observe', 'click', this.display.bind(this));
	},
	display: function(e) {
		var id_to_display = e.element().readAttribute('rel');
		if (id_to_display != this.currently_displayed) {
			$(this.currently_displayed).hide();
			$('link_' + this.currently_displayed).removeClassName('current');
			$('link_' + id_to_display).blur()
			$('link_' + id_to_display).addClassName('current');
			$(id_to_display).show();
			this.currently_displayed = id_to_display;
			e.stop();
		}
	}
});

var Faq = Class.create({
	initialize: function() {
		this.currently_displayed = null;
		$$('ol.faq a').each(function(link) {
			var id = link.readAttribute('rel');
			if (!Prototype.Browser.MobileSafari && !Prototype.Browser.WebKit) {
				$(id).setStyle({'position': 'absolute'});
			}
			if ($(id).visible()) {
				this.currently_displayed = id;
			}
		}, this);
		$$('ol.faq a').invoke('observe', 'click', this.display.bind(this));
	},
	display: function(e) {
		var id_to_display = e.element().readAttribute('rel');
		if (id_to_display != this.currently_displayed) {
			if (Prototype.Browser.WebKit || Prototype.Browser.MobileSafari) {
				$(this.currently_displayed).hide();
				$(id_to_display).show();
			} else {
				$(this.currently_displayed).fade({duration: 0.4});
				$(id_to_display).appear({duration: 0.4});
			}
			this.currently_displayed = id_to_display;
			e.stop();
		}
	}
});

var FaceMagnifier = Class.create({
	initialize: function() {
		this.faces = [];
		this.preload_images();
		$$('ul#faces img').invoke('observe', 'mouseover', this.explode.bind(this));
		$(document).observe('mousemove', function(e) {
			$$('img.large').each(function(img) {
				var offsets = img.cumulativeOffset();
				var size = img.getDimensions();
				if ((e.pointerX() < offsets.left) || 
					(e.pointerY() < offsets.top)  ||
					(e.pointerX() > (offsets.left + size.width)) ||
					(e.pointerY() > (offsets.top + size.height))) {
						img.remove();
				}
			});
		});
	},
	preload_images: function() {
		$$('ul#faces img').each(function(img) {
			var lg_img = new Image();
			lg_img.src = img.readAttribute('src').gsub(/_sm\.gif$/, '_lg.gif')
		});
	},
	explode: function(e) {
		var small_image = e.element();
		var large_image = new Element('img', {
			'class': 'large', 
			'src': small_image.readAttribute('src').gsub(/_sm\.gif$/, '_lg.gif'),
			'border': '4px solid #000'
		});
		var offsets = small_image.cumulativeOffset();
		large_image.setStyle({
			'position': 'absolute',
			'z-index': 100,
			'left': (offsets.left - 47) + 'px',
			'top': (offsets.top - 50) + 'px'
		});
		$(document.body).insert(large_image);
	}
});


var sfHover = function() {
	if ($('mainNav')) {
		var sfEls = $('mainNav').getElementsByTagName('LI');
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}


var page_height = 0;
var resize_tile = function() {
	var height = $(document.viewport).getHeight() > page_height ? $(document.viewport).getHeight() : page_height;
	$$('div.bodyTop').first().setStyle({
		'height': (height - 25) + 'px'
	});
}
Event.observe(window, 'load', function() {
	original_page_height = page_height = $(document.body).getHeight();
	resize_tile();	
});

Event.observe(window, 'resize', function() {
	resize_tile();
});

if (window.attachEvent) window.attachEvent("onload", sfHover);