$(function() {

	$('html').addClass('js');
	
	$('#cboxOverlay').appendTo('#aspnetForm');
	$('#colorbox').appendTo('#aspnetForm');

	if ($('#main').length > 0) {
		$('body').css('overflow', 'hidden');
	}
	
	var slideTimeout = null;
	
	$(window).resize(function () {
		
		$('#slide-wrap .content').css({'min-height': '0px'});
		
		clearTimeout(slideTimeout);
		
		function slide() {
			clearTimeout(slideTimeout);
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				$('#carousel #rotate').animate({'left': -ox }, 'slow', function(){
					var theLI = $('#carousel #rotate li:first').html();
					$('#carousel #rotate').append('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
					$('#carousel #rotate li:first').remove();
					$('#carousel #rotate').css({'left': '0'});
					$('body').removeClass('animating');
				});
			}
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		};
		
		function resetSlide() {
			clearTimeout(slideTimeout);
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		}
		
		var ox = $(document).width() - $('header').width() + 10;
		if($('#slide-panel').length > 0) {
			if($('#slide-panel').height() > $(window).height() ) {
				var oy = $('#slide-panel').height();
				$('body').css('overflow', 'auto');
				$('#slide-panel').css('min-height', '0px');
				$('#slide-panel').css('position', 'absolute');
			} else {
				var oy = $(window).height();
			}
		} else {
			if($('body').hasClass('home')) {
				if($(window).height() > 600) {
					var oy = $(window).height();
				} else {
					$('body').css({'overflow': 'auto'});
					$('body').addClass('small');
					var oy = 600;
				}
				if($(window).width() < 952) {
					$('body').css({'overflow': 'auto'});
					$('body').addClass('small');
					var ox = 952;
				}
			} else {
				var oy = $(window).height();
			}
		}
		
		$('#main #rotate img').each(function () {
			var self = this;
			var img = new Image();
			img.onload = function () {
				var cx = this.width;
				var cy = this.height;

				$(self).parent().css({
					'width': ox,
					'height': oy
				});

				var noItems = $(self).parent().parent().find('li').length;
				$(self).parent().parent().css({
					'width': ox * noItems,
					'height': oy
				});

				$(self).parent().parent().parent().css({
					'width': ox,
					'height': oy
				});

				if (ox / oy > cx / cy) {
					$(self).css({
						'width': ox,
						'height': (ox * cy / cx),
						'left': 0,
						'top': 0.5 * (oy - (ox * cy / cx)),
						'position': 'relative'
					});

				} else {
					$(self).css({
						'width': oy * cx / cy,
						'height': oy,
						'top': 0,
						'left': 0.5 * (ox - (oy * cx / cy)),
						'position': 'relative'
					});
				}
			}
			img.src = $(self).attr('src');
		});
		
		$('#carousel-nav li.next').die('click').live('click', function() {
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				$('#carousel #rotate').animate({'left': -ox}, 'slow', function(){
					var theLI = $('#carousel #rotate li:first').html();
					$('#carousel #rotate').append('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
					$('#carousel #rotate li:first').remove();
					$('#carousel #rotate').css({'left': '0'});
					$('body').removeClass('animating');
				});
			}
			resetSlide();
			return false;
		});
		
		$('#carousel-nav li.prev').die('click').live('click', function() {
			if($('body').hasClass('animating')) { } else {
				$('body').addClass('animating');
				var theLI = $('#carousel #rotate li:last').html();
				$('#carousel #rotate').prepend('<li style="width: ' + ox + 'px; height: ' + oy + 'px;">' + theLI + '</li>');
				$('#carousel #rotate li:last').remove();
				$('#carousel #rotate').css({'left': -ox});
				$('#carousel #rotate').animate({'left': '0'}, 'slow', function(){
					$('body').removeClass('animating');
				});
			}
			resetSlide();
			return false;
		});
		
		$('#rotate .panel .button').mouseover(function() {
			clearTimeout(slideTimeout);
		}).mouseout(function() {
			resetSlide();
		});
		
		if($('#rotate li').length > 1) {
			resetSlide();
		}
	
		var navHeight = $('header nav').height();
		var socialHeight = $('header ul.social').height();
		var footerHeight = $('footer').height();
		var smallerHeight = 176 + 45 + navHeight + socialHeight + footerHeight + 100;
		if($(document).width() < '951' || $(window).height() < smallerHeight) {
			$('body').addClass('small');
		} else {
			$('body').removeClass('small');
		}
		
		$('#slide-wrap .content').css({'min-height': oy});

		if($('#menus').length > 0) {
			$('body').addClass('small');
		}

	}).trigger('resize');
	

	if($('#slide-panel').length > 0) {
		clearTimeout(slideTimeout);
		$('#carousel-nav').css({'bottom': '-52px'});
	}

	$('#slide-panel .slide-link a').click(function() {
		if($(this).hasClass('closed')) {
			$(this).removeClass('closed').parent().animate({'left': '557px'});
			$(this).parent().parent().parent().animate({'left': '308px'});
			$('#carousel-nav').animate({'bottom': '-52px'});
			clearTimeout(slideTimeout);
		} else {
			$(this).addClass('closed').parent().animate({'left': '320px'});
			$(this).parent().parent().parent().animate({'left': '72px'});
			$('#carousel-nav').animate({'bottom': '20px'});
			slideTimeout = setTimeout(function () {
				slide();
			}, 8000);
		}
		return false;
	});	
																							
	
	$('#rotate li .panel .button').live('click mouseover mouseout', function(event) {
		if ( event.type == 'mouseover' ) {
			var theLink = $(this).find('a').attr('href');
			if(theLink) {
				$(this).addClass('hover');
			}
		} else if ( event.type == 'mouseout' ) {
			$(this).removeClass('hover');
		} else {
			var theLink = $(this).find('a').attr('href');
			if(theLink) {
				window.location = theLink;
			}
		}
	});

	if($('#menus').length > 0) {
		$('body').addClass('small');
	}

	if($('.content .form').length > 0) {
	    $('#aspnetForm').validate({ 
			errorElement: 'em',
			errorPlacement: function(error, element) {
				error.appendTo( element.prev() );
			},
			highlight: function(element, errorClass, validClass) {
				$(element).parent().addClass(errorClass).removeClass(validClass);
			},
			unhighlight: function(element, errorClass, validClass) {
				$(element).parent().removeClass(errorClass).addClass(validClass);
			}
		});
	}
	
	if($('#google-map').length > 0) {
		function initialize() {
			var latlng = new google.maps.LatLng(51.23758,-0.566782);
			var myOptions = {
				zoom: 15,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			var map = new google.maps.Map(document.getElementById('google-map'), myOptions);

			var image = '/images/template/mapmarker.png';
			var myLatLng = new google.maps.LatLng(51.23758,-0.566782);
			var beachMarker = new google.maps.Marker({
				position: myLatLng,
				map: map,
				icon: image
			});
		}
		initialize();
	}

	function moveDiv() {	
		$('#cboxOverlay').appendTo('body');
		$('#colorbox').appendTo('body');
	}
	function validateColorbox() {
		$('#newsForm').validate({
			errorElement: 'em',
			errorPlacement: function(error, element) {
				error.appendTo( element.prev() );
			},
			highlight: function(element, errorClass, validClass) {
				$(element).parent().addClass(errorClass).removeClass(validClass);
			},
			unhighlight: function(element, errorClass, validClass) {
				$(element).parent().removeClass(errorClass).addClass(validClass);
			}
		});	
		$('#cboxOverlay').appendTo('#aspnetForm');
		$('#colorbox').appendTo('#aspnetForm');
	}
	
	
	function addBookClass() {	
		$('#colorbox').addClass('book');
	}
	function removeBookClass() {	
		$('#colorbox').removeClass('book');
	}


	//$('header nav ul li ul li ul, #menus p.top-fixed').localScroll();

	if ($('#menus p.top-fixed').length > 0) {
	    var msie6 = $.browser == 'msie' && $.browser.version < 7;
	    if (!msie6) {
	        //var top = $('#menus .box:eq(2)').offset().top - 100;
	        var top = 635;
	        $(window).scroll(function (event) {
	            var y = $(this).scrollTop();
	            if (y >= top) {
	                $('#menus p.top-fixed').fadeIn();
	            } else {
	                $('#menus p.top-fixed').fadeOut();
	            }
	        });
	    }
	}

	var ua = navigator.userAgent;
	var checker = {
	  blackberry: ua.match(/BlackBerry/)
	};
	if (checker.blackberry) {
		$('body').addClass('blackberry');
	}
	
});



