$(function() {
	
	// External Links
	$('a[rel=external]')
		.click(
			function(e) {
				open(this.href);
				e.preventDefault();
			}
		)
		.attr('title', 'Opens in a new window');
		
	// Useful Stuff
	$('li:last-child').addClass('last-item');
	
	$('input.default')
		.focus(
			function() {
        		if ($(this).val() == $(this)[0].title) {
					$(this).val('');
				}
			}
		)
		.blur(
			function() {
        		if ($(this).val() == '') {
					$(this).val($(this)[0].title);
				}
			}
		)
		.blur();
		
	$('input[type=submit]').addClass('submit');
		
	if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
	}
	else {
		$('input, select, textarea')
			.blur(
				function() {
					$(this).removeClass('focus');
				}
			)
			.focus(
				function() {
					$(this).addClass('focus');
				}
			)
			.hover(
				function() {
					$(this).addClass('hover');
				},
				function() {
					$(this).removeClass('hover');
				}
			);
	}
	
	/*
	 * Header Navigation
	 */
	$('#header .nav ul li')
		.hover(
			function() {
					$('ul:first', this).stop().show();
					$(this).addClass('hover');
			},
			function() {
					$('ul:first', this).stop().hide();
					$(this).removeClass('hover');
			}
		);
		
	/*
	 * Contact Form
	 */
	$('input, textarea', '#footer .form fieldset ol li')
		.focus(
			function() {
        		if ($(this).val() == $(this).prev('label').text()) {
					$(this).val('');
				}
			}
		)
		.blur(
			function() {
        		if ($(this).val() == '') {
					$(this).val($(this).prev('label').text());
				}
			}
		)
		.blur();
		
	/*
	 * Reeds
	 */
	if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
	}
	else {
		$('#header')
			.append('<div class="reed" />');
	}
	
	/*
	 * Galleries
	 */
	$('.gallery ul')
		.each(
			function(e) {
				$(this).jcarousel(
					{
						animation      : 1000,
						buttonNextHTML : '<div>&raquo;</div>',
						buttonPrevHTML : '<div>&laquo;</div>',
						scroll         : 4,
						wrap           : 'circular'
					}
				);
			}
		);
    	
	// 
	$('.introduction .text')
		.jScrollPane();
		
    // Fancybox
    $('.gallery .items ul li a')
        .fancybox(
            {
                overlayColor   : '#000',
                overlayOpacity : .7,
                transitionIn   : 'elastic',
                transitionOut  : 'fade'
            }
        );
    	
    // Fancybox YouTube Links
    $('a[href*="youtube.com/watch?v="]')
        .click(
            function(e) {
                $.fancybox({
                    autoScale      : false,
                    height         : 385,
                    href           : this.href.replace(new RegExp('watch\\?v=', 'i'), 'v/'),
                    swf            : {
                                         allowfullscreen    : 'true',
                                         wmode           : 'transparent'
                                     },
                    overlayColor   : '#000',
                    overlayOpacity : .7,
                    titlePosition  : 'none',
                    transitionIn   : 'elastic',
                    transitionOut  : 'elastic',
                    type           : 'swf',
                    width          : 640
                });
                e.preventDefault();
            }
        );
		
	// Manipulate Umbraco's image alignment
	$('#content img')
		.each(
			function(e) {
				if ($(this).css('float') == 'left') {
					$(this).addClass('left');	
				}
				else if ($(this).css('float') == 'right') {
					$(this).addClass('right');
				}
			}
		);
		
    // Internal Page Links
    $('a[href*=#]')
        .click(
            function(e) {
                var target = $(this.hash + ', a[name=' + this.hash.slice(1) + ']').offset().top,
                    oldLocation = window.location.href.replace(window.location.hash, ''),
                    newLocation = this;

                if (oldLocation + this.hash == newLocation) {
                    if ($.browser.safari) {
                        var animationSelector = 'body:not(:animated)';
                    }
                    else {
                        var animationSelector = 'html:not(:animated)';
                    }

                    $(animationSelector).animate({ scrollTop: target }, 1000, 'swing', function() { window.location.href = newLocation; });

                    e.preventDefault();
                }
            }
        );
});

(function($) {
    function toggleLabel() {
        var input = $(this);
        setTimeout(function() {
            var def = input.attr('title');
            if (!input.val() || (input.val() == def)) {
                input.prev('span').css('visibility', '');
                if (def) {
                    var dummy = $('<label></label>').text(def).css('visibility','hidden').appendTo('body');
                    input.prev('span').css('margin-left', dummy.width() + 3 + 'px');
                    dummy.remove();
                }
            } else {
                input.prev('span').css('visibility', 'hidden');
            }
        }, 0);
    };

    function resetField() {
        var def = $(this).attr('title');
        if (!$(this).val() || ($(this).val() == def)) {
            $(this).val(def);
            $(this).prev('span').css('visibility', '');
        }
    };

    $('input, textarea').live('keydown', toggleLabel);
    $('input, textarea').live('paste', toggleLabel);
    $('select').live('change', toggleLabel);

    $('input, textarea').live('focusin', function() {
        $(this).prev('span').css('color', '#ccc');
    });
    $('input, textarea').live('focusout', function() {
        $(this).prev('span').css('color', '#999');
    });

    $(function() {
        $('input, textarea').each(function() { toggleLabel.call(this); });
    });

})(jQuery);
