window.addEvent('domready', function() {
	if ($('exterior')) {
		var load_text = "<img src='/images/loading.gif' /><br/><br/>Loading Selected Gallery<br/><br/>Please Wait . . .";
	
		$('exterior').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('exterior');
		});
		
		$('interior').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('interior');
		});
		
		$('living').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('living');
		});
		
		$('kitchen').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('kitchen');
		});
		
		$('bedroom').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('bedroom');
		});
		
		$('bathroom').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('bath');
		});
		
		$('process').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').innerHTML= load_text;
			flickrResponse('process');
		});
	}
	
	if ($('featured_links')) {
		flickrTags();
	}
	
	if ($("featured_project")) {
		flickrResponse($('featured_tag').value);
	}
	
	if ($("featured_homepage")) {
		flickrHomepage();
	}
	
	if ($('sei')) {
		$('sei').addEvent('click', function(e) {
			new Event(e).stop();
			$('picture_bin').set('html', "<img src='/images/loading.gif' /><br/><br/>Loading Selected Gallery<br/><br/>Please Wait . . .");
			flickrResponse('sei');	
		});
	}
});

//Use this to load the main image into the Viewing bin
function loadImage(source) {
	var myElement = $('picture_bin');
	var myFx = new Fx.Tween(myElement, 'opacity', {duration:1250});
	myFx.start(0).chain(function() {
		$('picture_bin').empty();
		myElement.set("html", "<img src='" + source + "' />");
		myFx.start(0,1);
	});
}

//This function grabs the tags from Flickr for Featured Projects and generates the links to their respective galleries
function flickrTags() {
	new Request({
		url: "/get_featured_tags",
		method: 'get',
		onComplete: function() {
			var data = eval( '(' + this.response.text + ')');
			
			if (data.stat != 'ok') {
				return
			}
			else {
				if ($('sierra_gallery')) {
					var drop = new Array("bath","bedroom","kitchen","living","interior","exterior","process","sierra","featured","homepage","orebaugh","calhoon","gundrum","johnson","sei","mcdonald","threebears");
				}
				else {
					var drop = new Array("bath","bedroom","kitchen","living","interior","exterior","process","sierra","featured","homepage","seicalhoon","sei","seigreen","seijohnson","seilahontan","seilahonton","seimaui","seiorebaugh","seithreebears","seithree");	
				}
				for (var i=0; i<data.who.tags.tag.length; i++) {
					if (drop.contains(data.who.tags.tag[i]._content)==false) {
						var featured = $("featured_links");
						var tag = data.who.tags.tag[i]._content;
						if ($('sierra_gallery')) {var tag = tag.substr(3);}
						var featuredLink = new Element('a', {
							'href': "#",
							'rel' : data.who.tags.tag[i]._content,
							'html' : tag.capitalize()
						}).inject(featured);
						featured.set('html', featured.innerHTML + "<br/>");
					} 
				}
				var featuredLinks = $$("#featured_links a");
				featuredLinks.each(function(link) {
					link.addEvent('click', function(e) {
						new Event(e).stop();
						$('picture_bin').set('html', "<img src='/images/loading.gif' /><br/><br/>Loading Selected Gallery<br/><br/>Please Wait . . .");
						flickrResponse(link.getProperty('rel'));
					});
				});
			}
		}
	}).send();
}

function flickrResponse(tag) {
	new Request({
			url: "/get_flickr/?tag=" + tag,
			method: 'get',
			onComplete: function() {
					$('thumbnails').innerHTML="";
					$('slide_link_div').innerHTML="";
						
					//Parse the response
					var data = eval( '(' + this.response.text + ')' );
					
					//Verify that the response didn't come back empty
					if (data.stat != "ok")
					{
						// something broke!
						$('thumbnails').set('html', "We apologize, but the connection to our photo database failed. Please try again . . .");
						return;
					}
					
					//Add Slide Show
					$('picture_bin').empty();
					
					var currentSlides = new Array();
					for (var i=0; i<data.photos.photo.length; i++)
					{
						var url = "http://farm" + data.photos.photo[i].farm + ".static.flickr.com/" + data.photos.photo[i].server + "/" + data.photos.photo[i].id + "_" + data.photos.photo[i].secret + ".jpg";
						new Element('img', {
							'src': url,
							'id' : "cf" + i
						}).injectInside('picture_bin');
						currentSlides[i] = "cf" + i;
					}
					
					new Element('a', {
						'href' : "#"
					}).set('html', "<img src='/images/play_btn.png' />").addEvent('click', function(e) {
						new Event(e).stop();
						// $('picture_bin').empty();
						$clear(slideshow.periodical);
						$('picture_bin').empty();
						var currentSlides = new Array();
						for (var i=0; i<data.photos.photo.length; i++)
						{
							var url = "http://farm" + data.photos.photo[i].farm + ".static.flickr.com/" + data.photos.photo[i].server + "/" + data.photos.photo[i].id + "_" + data.photos.photo[i].secret + ".jpg";
							new Element('img', {
								'src': url,
								'id' : "cf" + i
							}).injectInside('picture_bin');
							currentSlides[i] = "cf" + i;
						}

						var slideshow = new Crossfader(currentSlides, 1000, 4000);	
					}).injectInside('slide_link_div');
						
					new Element('a', {
						'href' : "#"
					}).set('html', "<img src='/images/stop_btn.png' />").addEvent('click', function(e) {
						new Event(e).stop();
						if (slideshow) {
							slideshow._stop();
						}
					}).injectInside('slide_link_div');
					
					//Build Thumbnails
					for (var i=0; i<data.photos.photo.length; i++)
					{
						var src = "http://farm" + data.photos.photo[i].farm + ".static.flickr.com/" + data.photos.photo[i].server + "/" + data.photos.photo[i].id + "_" + data.photos.photo[i].secret;
						var aThumb = new Element('img', {
							'src' : src + "_s.jpg",
							'width' : '60',
							'height' : '60',
							'rel' : src + ".jpg",
							'class' : 'thumb_image'
						}).inject('thumbnails');
					}
					
					var thumbs = $$('#thumbnails .thumb_image');
					thumbs.each(function(thumb, i) {
						thumb.addEvent('click', function() {
							if (slideshow) {slideshow._stop();}
							$('picture_bin').empty();
							new Element('img', {
								'src' : thumb.getProperty('rel')
							}).inject('picture_bin');
						});
					});
					
					//Start the Slideshow
					var slideshow = new Crossfader(currentSlides, 1000, 4000);
					//new SimpleImageViewer();
					
					//Preload Image Assets for faster loading
					// $('preload_bin').set('html', "");
					// 
					// for (var i=0; i<data.photos.photo.length; i++)
					// {
					// 	var pic = data.photos.photo[i];
					// 	var source = "http://farm" + pic.farm + ".static.flickr.com/" + pic.server + "/" + pic.id + "_" + pic.secret + ".jpg";
					// 	new Asset.image(source).injectInside("preload_bin");
					// }
				}
	}).send();
}

