window.addEvent('domready', function() {
	var scroll = new Fx.Scroll(window, {
		wait: false,
		duration: 2000,
		offset: {'x': 0, 'y': -20},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	var glinks = $$('.gallery_link');
	
	glinks.each(function(l) {
		l.addEvent('click', function(e) {
			new Event(e).stop();
			scroll.toElement('gallery');
		});
	});
	
	var alinks = $$('.about_link');
	
	alinks.each(function(l) {
		l.addEvent('click', function(e) {
			new Event(e).stop();
			scroll.toElement('about');
		});
	});
	
	var hlinks = $$('.home_link');
	
	hlinks.each(function(l) {
		l.addEvent('click', function(e) {
			new Event(e).stop();
			scroll.toElement('home');
		});
	});
	
	var clinks = $$('.consult_link');
	
	clinks.each(function(l) {
		l.addEvent('click', function(e) {
			new Event(e).stop();
			scroll.toElement('consult');
		});
	});
	
	var clinks = $$('.contact_link');
	
	clinks.each(function(l) {
		l.addEvent('click', function(e) {
			new Event(e).stop();
			scroll.toElement('contact');
		});
	});
	
	flickrResponse();
});

function flickrResponse() {
	new Ajax("http://www.dreamdevelopmentco.com/cgi-bin/flickr.cgi", {
			method: 'get',
			onComplete: function() {
					$('thumbnails').innerHTML="";
					
					var data = eval( '(' + this.response.text + ')' );
					
					if (data.stat != "ok")
					{
						// something broke!
						$('thumbnails').setHTML("We apologize, but the connection to our photo database failed. Please refresh the page to try again . . .")
						return;
					}
					
					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;
						new Element('a', {
							'href' : "#",
							'class' : 'thumb_link'
						}).setHTML("<img src='" + src + "_s.jpg' width='60' height='60' class='thumbs' />").injectInside('thumbnails');
					}
					
					var links = $$('#thumbnails .thumb_link');
					var thumbs = $$('#thumbnails .thumbs');
					links.each(function(link, i) {
						link.addEvent('click', function(e) {
							new Event(e).stop();
							//Build new image string
							var src = thumbs[i].getProperty('src');
							l = src.length - 1;
							var new_src = src.substring(0,l-5);
							loadImage(new_src + ".jpg");
						});
					});
					
					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");
					}
				}
	}).request();
	
	load_random_quote();
}

function loadImage(source) {
	var myElement = $('gallery_img');
	var myFx = new Fx.Style(myElement, 'opacity', {duration:1250});
	myFx.start(0).chain(
		function() {
			myElement.innerHTML="<img src='" + source + "' />";
			myFx.start(0,1);
		});
}

function load_random_quote() {
	var quotes = ["1","2","3","4","5"];

	// Use the random() method and the modulus (%) operator to
	// pick a random letter from the above array.
	var quote = quotes[Math.floor( Math.random() * 10 ) % quotes.length];

	$('quotes').setHTML("<img src='images/quote" + quote + ".jpg' alt='Welcome Quote' />");
}