var t;
function slideshow(start, last) {
	if (start > last) start = 1;
	if (start < 1) start = last;
	var interval = 5000;
	var frame = start;
	var nextframe = start+1;
	if (nextframe > last) nextframe = 1;
	if (nextframe < 1) nextframe = last;
	//new Effect.Appear('img'+frame,{duration:0.5,from:0.0,to:1.0 });
	t = setInterval(function() {
		new Effect.Parallel([
			new Effect.Fade('img'+frame,{from:1.0,to:0.0,afterSetup:function(){
				new Effect.Appear('img'+nextframe,{duration:1.0,from:0.0,to:1.0 });
				frame = nextframe;
			}}),
			new Effect.Fade('caption'+frame,{from:1.0,to:0.0,afterSetup:function(){
				new Effect.Appear('caption'+nextframe,{duration:1.0,from:0.0,to:1.0 });
				frame = nextframe;
			}})
		], { duration: 1.0, delay: 0.0, afterFinish:function() {
			nextframe = (frame == last) ? 1 : nextframe+1;
		} });
	},interval);
	return;
}
function initSlides(n) {
	var slideCount = $$('body.home img.main-photo').length;
	slideshow(n, slideCount);
	$$('a.slide-bttn').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			clearInterval(t);
			$$('body.home.editing div#home_header_editor_container').each(function(ed){ ed.fade({duration:0.3}); });
			var n = 1;
			var nxt = 1;
			$$('body.home img.main-photo').each(function(imgElement, i){
				if (imgElement.visible()) {
					n = i + 1; // currently visible
				}
			});
			
			if (e.id == 'next') {
				nxt = n + 1;
			} else {
				nxt = n - 1;
			}

			if (nxt > slideCount) nxt = 1;
			if (nxt < 1) nxt = slideCount;
			
			new Effect.Parallel([
				new Effect.Fade('img'+n,{from:1.0,to:0.0,afterSetup:function(){
					new Effect.Appear('img'+nxt,{duration:1.0,from:0.0,to:1.0 });
				}}),
				new Effect.Fade('caption'+n,{from:1.0,to:0.0,afterSetup:function(){
					new Effect.Appear('caption'+nxt,{duration:1.0,from:0.0,to:1.0 });
				}})
			], { duration: 1.0, delay: 0.0, afterFinish:function() {
					nxt = (nxt == slideCount) ? 1 : nxt+1;
				}
			});
		//alert(nxt);
			slideshow(nxt, slideCount);
		});
	});
}
function attachGallerySidebar() {
	$$('div.gallery-sidebar a').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			//alert(e.readAttribute('rel'));
			var _rel = e.readAttribute('rel');
			var _title = e.readAttribute('title');
			$$('div.gallery-sidebar a').invoke('removeClassName','selected');
			e.addClassName('selected');
			$('gallery_large_image').hide();
			$('gallery_large_image').src = e.href;
			$('gallery_large_image').alt = _rel;
			$('gallery_large_image').title = _title;
			$('gallery_large_image').appear({ duration: 0.3 });
			_gaq.push(['_trackEvent', 'Room Gallery Tabs', _rel, _title]);
		});
	});
}
function initRoomGallery() {
	$$('div.gallery-bttns a').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			var sliderWidth = $('gallery-thumbs-slider').getWidth();
			var sliderOffset = $('gallery-thumbs-slider').positionedOffset();
			var x = 0;
			if (e.hasClassName('next')) {
				if (((sliderOffset.left - 822) * -1) >= sliderWidth -1) 
					x = sliderOffset.left * -1;
				else 
					x = -822;
			} else if (e.hasClassName('prev')) {
				if (sliderOffset.left == 0) 
					x = (Math.floor((sliderWidth/822))*822) * -1;
				else 
					x = 822;
			}
			new Effect.Move('gallery-thumbs-slider', { x: x, y: 0, mode: 'relative', queue: { position: 'end', scope: 'gallery', limit: 1 } });
		});
	});
	$$('div#gallery-thumbs-slider a').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			var _rel = e.readAttribute('rel');
			var _title = e.readAttribute('title');
			$$('div#gallery-thumbs-slider a').invoke('removeClassName','selected');
			new Ajax.Request(e.href, {
				onComplete: function(t) { 
					//alert(t.responseText);
					$('gallery-container').hide();
					$('gallery-container').update(t.responseText);
					$('gallery-container').appear({ duration: 0.3, afterFinish: function(){ attachGallerySidebar(); } });
					e.addClassName('selected');
					_gaq.push(['_trackEvent', 'Room Gallery', _rel, _title]);
				}
			});
		});
	});
	attachGallerySidebar();
}
function bzValidateEmail(s){
	var regex = new RegExp(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
	var matches = regex.exec(s);
	return (matches != null && s == matches[0]);
}
function bzValidateField(inputElement) {
	var n = 0;
	if (!inputElement.present()) {
		n++;
	}
	if (inputElement.hasClassName('email') && !bzValidateEmail(inputElement.value)) {
		n++;
	}
	if (inputElement.hasClassName('email_verify') && inputElement.value != $F('email')) {
		n++;
	}
	if (n == 0) {
		$$('label[for='+inputElement.id+']').invoke('removeClassName','alert');
	} else {
		$$('label[for='+inputElement.id+']').invoke('addClassName','alert');
	}
	return n == 0;
}
function bzValidateForm(formId) {
	var n = 0;
	$$('form#'+formId+' .required').each(function(e){
		if (!bzValidateField(e)) {
			n++;
		}
	});
	return n == 0;
}
function attachMapLinks() {
	$$('div.map-bttns a').each(function(e){
		Event.observe(e, 'click', function(clkEvnt){
			clkEvnt.stop();
			$$('div#googlemap, div#localmap').invoke('hide');
			$(e.readAttribute('rel')).appear({ 
				duration: 0.3, 
				afterFinish: function(){
					if (e.readAttribute('rel') == 'googlemap') $('googlemap_iframe').src = $('googlemap_iframe').src;
					Effect.ScrollTo(e.readAttribute('rel'), { duration:'0.8', offset:-25 });
				} 
			});
		});
	});
}
function processContactForm() {
	$('tmi_contact_form-responsemsg').hide();
	$('tmi_contact_form-responsemsg').update();
	$('tmi_contact_form-responsemsg').removeClassName('alert');
	if (bzValidateForm('tmi_contact_form')) {
		$('send_bttn').disabled = true;
		$('tmi_contact_form-responsemsg').update('Sending...');
		$('tmi_contact_form-responsemsg').appear({duration:0.3});
		$('tmi_contact_form').request({
			onComplete: function(t){
//				alert(t.responseText);
				$$('form#tmi_contact_form input, form#tmi_contact_form textarea').each(function(e){ e.disabled = true; });
				$('tmi_contact_form-responsemsg').update('Thank you! Your Information has been sent.');
//				$('tmi_contact_form-responsemsg').appear({duration:0.3});
				
			}
		});
	} else {
		$('tmi_contact_form-responsemsg').addClassName('alert');
		$('tmi_contact_form-responsemsg').update('Please fill in required values and re-submit.');
		$('tmi_contact_form-responsemsg').appear({duration:0.3});
	}
}
function newsletterSignup() {
	if (bzValidateField($('subscriber_email'))) {
		$('tmi_newsletter_signup').request({
			onComplete: function(t) {
				$('tmi_newsletter_signup').hide();
				$('tmi_newsletter_signup_response').appear({ duration: 0.3 });
				if (t.headerJSON.is_test) {
					alert("***TESTING***\n"+t.headerJSON.confirmation_message);
				}
			}
		});
	}
}
Event.observe(window, 'load', function(){
	if ($(document.body).hasClassName('home')) { initSlides(1); }
	if ($(document.body).hasClassName('room-gallery')) { initRoomGallery(); }
	if ($(document.body).hasClassName('contact-form')) { 
		$$('form#tmi_contact_form .required').each(function(e){
			Event.observe(e, 'blur', function(blrEvnt){ blrEvnt.stop(); bzValidateField(e); });
		}); 
	}
	if ($(document.body).hasClassName('maps')) { attachMapLinks(); }
});
