Event.observe(this, 'load', function(){

if ($('latham-clicker')){
	Event.observe('latham-send-butan', 'click', processLatham)
	Event.observe('latham-clicker', 'click', contract);

}


if ($('saratoga-springs-clicker')){

	Event.observe('saratoga-springs-clicker', 'click', contract);
	Event.observe('saratoga-send-butan', 'click', processSaratoga)

}

if ($('history')){
	Event.observe('latham-send-butan', 'click', processHistory)


}


if ($('gallery-main')){
	//rotateOtherImages.delay(5, $$('.gallery-item')[0]);

	$$('.gallery-item').each(function(s) {
  Event.observe(s, 'click', insertGalleryImage);
  });

}


if ($('front-img')){
	rotateImages.delay(5, $$('.front-imgs')[0]);
}


function processLatham(){
	//alert('processing');
	new Ajax.Updater('latham', '/form.php', {
  parameters: { name: $F('latham-name'), email: $F('latham-email'), phone: $F('latham-phone'), date_time: $F('latham-time'), store: 'latham', order: $F('latham-order')},
  method: 'get'
});

}

function processHistory(){
	alert('processing');
	new Ajax.Updater('history', '/form.php', {
  parameters: { name: $F('history-name'), email: $F('history-email'), phone: $F('history-phone'), date_time: $F('history-time'), store: 'history', order: $F('history-text')},
  method: 'get'
});

}
function processSaratoga(){
	//alert('processing');
	new Ajax.Updater('saratoga-springs', '/form.php', {
  parameters: { name: $F('saratoga-name'), email: $F('saratoga-email'), phone: $F('saratoga-phone'), date_time: $F('saratoga-time'), store: 'saratoga', order: $F('saratoga-order')},
  method: 'get'
});

}

function contract(){
	if ($('latham')){$('latham').morph('height:58px;', {duration:0.5})};
	if ($('saratoga-springs')){$('saratoga-springs').morph('height:58px;', {duration:0.5});}
	expand(this.ancestors());
}

function expand(element){
	element[0].morph('height:400px;', {duration:0.5});

}


function expand2(){
	this.morph('height:400px;', {duration:0.5});

}
function insertGalleryImage(){

/*
	var contentToInsert = this.down();
	var oldContent = $('gallery-main').down();
	oldContent.setAttribute(contentToInsert.getAttribute('src'));
//	this.update(oldContent);
	//this.update('lol');
	
	*/
	
		$('gallery-main').firstDescendant().setAttribute('src', this.firstDescendant().readAttribute('src'));
	//$('zoomer').firstDescendant().setAttribute('src', this.firstDescendant().readAttribute('src'));


}

var iterator = 0;

function rotateImages(toThis){

	var nextItem = $$('.front-imgs')[0];
	//alert(nextItem)

	var contentToInsert = toThis.down();
	var oldContent = $('front-img').down();
	new Effect.Opacity('front-img', { from: 1.0, to: 0, duration: 0.3 });
	swap.delay(0.4, toThis, $('front-img'));
	new Effect.Opacity('front-img', { from: 0, to: 1, duration: 0.5, delay:0.7 });
	
	if (iterator == 4){
		rotateImages.delay(5, $$('.front-imgs')[0]);
		iterator = 0;
	
	} else {
			rotateImages.delay(4, toThis.next());
	iterator++;
	}
	//rotateImages(toThis.next());
	
}

function rotateOtherImages(toThis){
	//alert($$('.gallery-item').size())
	//var nextItem = $$('.front-imgs')[0];
	//alert(nextItem)

	var contentToInsert = toThis.down();
	var oldContent = $('gallery-main').down();
	new Effect.Opacity('gallery-main', { from: 1.0, to: 0, duration: 0.3 });
	swap.delay(0.4, toThis, $('gallery-main'));
	new Effect.Opacity('gallery-main', { from: 0, to: 1, duration: 0.5, delay:0.7 });
	
	if (iterator == ($$('.gallery-item').size() -1)){
		rotateOtherImages.delay(5, $$('.gallery-item')[0]);
		iterator = 0;
	
	} else {
			rotateOtherImages.delay(4, toThis.next());
	iterator++;
	}
	//rotateImages(toThis.next());
	
}

function swap(a, b){
	var contentToInsert = a.down();
	var oldContent = b.down();
	b.update(contentToInsert);
	a.update(oldContent);
}


})
