$(function() {

    $('#video_wrapper').addClass('video');

    // 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');

    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');
				}
			);
    }

    $('input.default')
		.focus(
			function() {
			    if ($(this).val() == $(this)[0].title) {
			        $(this).val('');
			    }
			}
		)
		.blur(
			function() {
			    if ($(this).val() == '') {
			        $(this).val($(this)[0].title);
			    }
			}
		)
		.blur();

    // Header > Animation
    $('#header .animation')
		.append('<div class="controls" />')
		.cycle(
			{
			    activePagerClass: 'selected',
			    cleartypeNoBg: true,
			    fx: 'fade',
			    pager: '#header .animation .controls',
			    pauseOnPagerHover: 1,
			    slideExpr: 'ul li',
			    speed: 1000,
			    timeout: 4000
			}
		);

    /* 
    * Header Navigation
    */
    if ($.browser.msie && $.browser.version.substr(0, 1) < 8) {
    } else {
        $('#header .nav ul li').hover(
		function() {
		    if ($.browser.msie) {
		        $('ul', this).stop().show();
		    }
		    else {
		        $('ul', this).stop().fadeTo(400, 0.98);
		    }
		},
		function() {
		    if ($.browser.msie) {
		        $('ul', this).stop().hide();
		    }
		    else {
		        $('ul', this).stop().fadeOut();
		    }
		}
	);
    }

    // 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');
			    }
			}
		);

    // Google Maps Banner
    
    if ($('.banner .map').length) {
        var map = $('.banner .map')
			.gmap3(
				{
				    lat: 53.476522,
				    lng: -2.244129,
				    zoom: 15
				}
			);

        map.setTypeTerrain();

        map.addMarkerByLatLng(53.476522, -2.244129);
    }

    // Page > Home
    if ($('body').hasClass('home')) {
        $('#content').append('<div class="overlay" />');
    }

    // 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 limit(element, charlabel, max_chars)
{
    var base = charlabel;
    var max_chars = 350;
	 
	if(element.value.length > max_chars) {
        element.value = element.value.substr(0, max_chars);
    } else {
	   	if (document.getElementById(base).innerHTML)
	   		document.getElementById(base).innerHTML = max_chars - element.value.length;
	   	else
	   		document.getElementById(base).innerText = max_chars - element.value.length;

    }
}
