window.addEvent('domready', function()
{
	/***********************
	  links to new windows
	***********************/
	
	$$('a.newWindow').each(function(item, index)
	{
		item.addEvent('click', function(e)
		{
			new Event(e).stop();
			window.open(this.get('href'));
		});
	});
	
	/***********************
	      input fields
	***********************/
	
	var hoverInput = function(item)
	{
		item.addEvent('focus', function(e)
		{
			item.toggleClass('active');
		});
		
		item.addEvent('blur', function(e)
		{
			item.toggleClass('active');
		});
	};
	
	$$('input.text').each(function(item, index)
	{
		hoverInput(item);
	});
	
	$$('textarea.text').each(function(item, index)
	{
		hoverInput(item);
	});
	
	/***********************
	      page search
	***********************/
	
	if($('searchForm') && $('pageNameField') && $('searchValueField'))
	{
        $('searchForm').addEvent('submit', function(e)
        {
            new Event(e).stop();
			
			var href = window.location.protocol + "//" + window.location.hostname;
			if($('pageNameField').value) href = href + "/" + $('pageNameField').value;
			if($('searchValueField').value.clean()) href = href + "/archive/search/" + $('searchValueField').value.clean().replace(/ /gi, "+");
			window.location.href = href + "/";
		});
		
		var containerDiv = $('searchForm').getElement('div');
		
		containerDiv.getElement('input.text').addEvent('focus', function(e)
		{
			containerDiv.toggleClass('active');
		});
		
		containerDiv.getElement('input.text').addEvent('blur', function(e)
		{
			containerDiv.toggleClass('active');
		});
	}
	
	/***********************
	        flash
	***********************/
	
	var swiffObjects = {};
	
	var embedFlash = function(url, divId, width, height)
	{
		// create swiff object
		var swiff = new Swiff(url,
		{
			id: divId,
			width: width,
			height: height,
			container: $(divId),
			params:
			{
				wmode: 'transparent',
				menu: 'false'
			}
        });
        
		if(document.forms[0])
        {
            window[divId] = document.forms[0][divId];
        }
		
		swiffObjects[divId] = swiff;
	};
	
	$$('div.flashobject').each(function(item, index)
	{
		var id = item.id;
		var width = item.getElement('div.swfWidth').innerHTML;
		var height = item.getElement('div.swfHeight').innerHTML;
		var url = item.getElement('div.swfUrl').innerHTML;
		
		embedFlash(url, id, width, height);
	});
	
	/***********************
	      latest video
	***********************/
	
	if($('latestvideo'))
	{
		// request
		var request = new Request(
		{
			url: '/requests/latestvideo.php',
			method: 'get',
			onSuccess: function(url)
			{
				embedFlash(url, 'latestvideo', 400, 300);
			},
			onFailure: function()
			{
			}
		});
		request.send();
	}
	
	/***********************
	     latest tweets
	***********************/
	
	if($('latesttweetmsg'))
	{
		// tweet functions
		var myTween = new Fx.Morph($('latesttweetmsg'), {'duration': '500'});
		var tweets;
		var tweetNr;
		
		function setTweetValues(tweet)
		{
			$('latesttweetmsg').getElement('span.twittername').innerHTML = tweet.screenname;
			$('latesttweetmsg').getElement('span.twittertext').innerHTML = tweet.text;
			$('latesttweetmsg').getElement('a.twittertime').innerHTML = tweet.created;
			$('latesttweetmsg').getElement('a.twittertime').href = "http://twitter.com/rosewoodband/status/" + tweet.id;
		}
		
		function showTweet(nr)
		{
			if(tweetNr == null) // first time
			{
				setTweetValues(tweets[0]);
				myTween.start({'opacity': [0,1]});
				tweetNr = 0;
				showTweet.periodical(5000);
			}
			else // cycle
			{
				myTween.start({'opacity': [1,0]}).chain(function()
				{
					setTweetValues(tweets[tweetNr]);
					myTween.start({'opacity': [0,1]});
				});
			}
			
			tweetNr = tweetNr + 1 >= tweets.length ? 0 : tweetNr + 1;
		}
		
		// request
		var request = new Request(
		{
			url: '/requests/latesttweets.php',
			onRequest: function()
			{
				$('latesttweetmsg').fade('hide');
			},
			onSuccess: function(response)
			{
				tweets = JSON.decode(response, true);
				showTweet(0);
			},
			onFailure: function()
			{
				$('latesttweetmsg').innerHTML = 'Could not connect to Twitter...';
			}
		});
		request.send();
	}
	
	/***********************
	      blog archive
	***********************/
	
	if($$('.archiveYear').length && $$('.archiveItems').length)
	{
		$$('.archiveYear').each(function(item)
		{
			item.addEvent('click', function(e)
			{
				new Event(e).stop();
			});
		});
		
		var myAccordion = new Fx.Accordion($$('.archiveYear'), $$('.archiveItems'),
		{
			onActive: function(toggler)
			{
				toggler.addClass('archiveYearOpen');
			},
			onBackground: function(toggler)
			{
				toggler.removeClass('archiveYearOpen');
			}
		});
	}
	
	/***********************
	     tour road map
	***********************/
	
	if($('mapCanvas'))
	{
		var address = $('mapLocation').innerHTML;
		
		var myOptions = {
			zoom: 9,
			center: new google.maps.LatLng(0, 0),
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
		}
		var map = new google.maps.Map($('mapCanvas'), myOptions);
		
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({'address': address}, function(results, status)
		{
			if (status == google.maps.GeocoderStatus.OK)
			{
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker(
				{
					map: map, 
					position: results[0].geometry.location,
					title: address
				});
			}
			else
			{
				alert("Ooops, could not geocode for the following reason: " + status);
			}
		});
	}
	
	/***********************
	     photo gallery
	***********************/
	
	if($('photoalbum'))
	{
		$('photolist').getElements('a.thumb').each(function(item, index)
		{
			// add thumbnail event
			item.addEvent('click', function(e)
			{
				new Event(e).stop();
				
				// switch image
				var img = $('currentPhoto').getElement('img');
				Asset.image(item.href,
				{
					onLoad: function()
					{
						var myTween = new Fx.Morph(img, {'duration': '250'});
						myTween.start({'opacity': [1,0]}).chain(function()
						{
							img.src = item.href;
							img.alt = item.title;
							img.title = item.title;
							$('currentPhoto').getElement('h3').innerHTML = item.title;
							myTween.start({'opacity': [0,1]});
						});
					}
				});
			});
		});
	}
	
	/***********************
	        contact
	***********************/
	
	// contact
	if($('contactForm'))
	{
		var contactRequest = new FormRequest('contactForm',
		{
			url: '/requests/validation.php',
			buttonId: 'contactButton',
			onSuccess: function(response)
			{
				if(response == "true")
				{
					new Fx.Reveal($('contactFormContainer')).dissolve();
					new Fx.Reveal($('contactFormSuccess')).reveal();
				}
				else
				{
					alert(response);
				}
			}
		});
	}
	
	// carve
	if($('carveForm'))
	{
		// request
		var contactRequest = new FormRequest('carveForm',
		{
			url: '/requests/validation.php',
			buttonId: 'carveButton',
			onSuccess: function(response)
			{
				if(response == "true")
				{
					window.location.href = window.location.href;
				}
				else
				{
					alert(response);
				}
			}
		});
		
		// max chars
		if($('charsLeft'))
		{
			var maxEl = $('charsLeft').getElement('span');
			var maxChars = maxEl.innerHTML;
			
			$('msgCaField').addEvent('keyup', function(e)
			{
				if (this.value.length > maxChars)
				{
					this.value = this.value.substring(0, maxChars);
				}
				else
				{
					maxEl.innerHTML = maxChars - this.value.length;
				}
			});
		}
	}
});

// init shadowbox
Shadowbox.init();
