$(document).ready(function(){
	//replace all fck inserted movies with swfobject
	//needed for accessibility options	
	//apply form validation
	if(jQuery.fn.validate){			
		if($('#FormSignup').length > 0 ){ // check if element is on page
			$("#FormSignup").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#C6D880'); //green border for valid data
					});
				},
				rules: {
					"data[FormSignup][firstname]": {  
						required: true           
					},
					"data[FormSignup][lastname]": {  
						required: true           
					},
					"data[FormSignup][email]": {
						required: true,
						email: true
					}
				},
				messages: {
					"data[FormSignup][firstname]": "Please enter your firstname.",
					"data[FormSignup][lastname]": "Please enter your last name.",
					"data[FormSignup][email]": "Please enter your email address."
				}
			});
		}
		if($('#FormContact').length > 0 ){ // check if element is on page
			$("#FormContact").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#C6D880'); //green border for valid data
					});
				},
				rules: {
					"data[FormContact][firstname]": {  
						required: true           
					},
					"data[FormContact][surname]": {  
						required: true           
					},
					"data[FormContact][company]": {  
						required: true           
					},
					"data[FormContact][telephone]": {  
						required: true           
					},
					"data[FormContact][email]": {
						required: true,
						email: true
					}
				},
				messages: {
					"data[FormContact][firstname]": "Please enter your firstname.",
					"data[FormContact][surname]": "Please enter your surname.",
					"data[FormContact][company]": "Please enter your company name.",
					"data[FormContact][telephone]": "Please enter your telephone number.",
					"data[FormContact][email]": "Please enter your email address."
				}
			});
		}
		if($('#FormCallmeback').length > 0 ){ // check if element is on page
			$("#FormCallmeback").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('color','#000000'); //red background for invalid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#C6D880'); //green border for valid data
					});
				},
				rules: {
					"data[FormCallmeback][firstname]": {  
						required: true           
					},
					"data[FormCallmeback][surname]": {  
						required: true           
					},
					"data[FormCallmeback][telephone]": {  
						required: true           
					}
				},
				messages: {
					"data[FormCallmeback][firstname]": "Please enter your firstname.",
					"data[FormCallmeback][surname]": "Please enter your surname.",
					"data[FormCallmeback][telephone]": "Please enter your telephone number."
				}
			});
		}
	}
	
	/* EMBED MEDIA */
	if(jQuery.fn.media){
		//set default player location
		$.fn.media.defaults.flvPlayer = '/projects/thomson-habitats/css/site/flash/mediaplayer.swf';
		//run media plugin on all a links with media class
		$('a.media').media();
	}
	
	
	if(jQuery.fn.showhide){
		//apply show hide plugin for use in gallery
		if($('a.showhide').length > 0 ){
			$('a.showhide').showhide({
				animate:true,
				event:'click',
				target:'#large_img',
				show_closer:false
			});
		}
	}
	
	
	if(jQuery.fn.inputfocus){
		if($('#header-outer-wrapper form div.input').length > 0 ){ // check if element is on page
			//apply form field effect
			$('#header-outer-wrapper form div.input').inputfocus({
				bgColourFocus:'#F7E6F0',
				bgColour:'#FFFFFF',			
				animate:true
			});
		}
	}
	
	if(jQuery.fn.cycle){
		if($('#quotes').length > 0 ){ // check if element is on page
			$('#quotes').css('height','160px');//set height of quotes box
			$('#quotes').cycle({fx:'fade',timeout:'7000'});
		}
		if($('#questions').length > 0 ){ // check if element is on page
			$('#questions').css('height','120px');//set height of quotes box
			$('#questions').cycle('fade');
		}
		if($('div.banner-item div.col-1').length > 0 ){ // check if element is on page
			$('div.banner-item div.col-1').cycle('fade');
		}		
	}
	
	jQuery.each(jQuery.browser, function(i) {
		//safari only css amend using jquery
	  if($.browser.safari){
		$('#nav-wrapper ul li a').css({'font-size':'1.1em', 'padding-right':'60px'});
		$('#nav-wrapper ul li a.long').css({'padding-right':'5px'});
	  }
	});
	
	// say the container is expandable 
	$('div.banner-item').bind('toggleContent', function(e, url, wrapper, img) { 
		//$(this).find('div.col-1').css('border','1px solid #FF0000');
		
		if(jQuery.fn.media){
			
			if(jQuery.fn.cycle){
				$(this).children(wrapper).cycle('pause');
			}
			//remove any existing divs
			if($(this).children(wrapper).children('div.flash-movie').length > 0){
				$(this).children(wrapper).children('div.flash-movie').remove();
				$(this).children(wrapper).children('div.closer').remove();
			}
			$(this).children(wrapper).append('<div class="flash-movie"><a class="{width:584, height:330}" href="'+url+'"></a></div>');
			$(this).children(wrapper).children('div.banner-img').css('height', '330px');
			$(this).children(wrapper).css('height', '330px');
			$.fn.media.defaults.flvPlayer = '/projects/thomson-habitats/css/site/flash/player.swf';
			$.fn.media.defaults.bgColor = '#000000';
			//set the default image as a flash var.
			//set the flash stretching to full screen as a flash var
			$.fn.media.defaults.flashvars = {'image':'/projects/thomson-habitats/img/site/content/movie-wallpaper.jpg',
											 'stretching':'exactfit'
											 };
			$.fn.media.defaults.params = { wmode: "transparent"};
			$(this).children(wrapper).children('div.flash-movie').children('a').media();
			$(this).children(wrapper).append('<div class="closer"><p>close [X]</p></div>');
			$(this).children(wrapper).children('div.closer').click(function(){ 
																			$(this).siblings('.flash-movie').remove();
																			$(this).remove(); 
																			if($('#home').length < 1 ){ // resize banner for none home pages
																				$('#banner-wrapper div.col-1').css('height', '221px');
																				$('#banner-wrapper div.col-1 div.banner-img').css('height', '221px');
																			}
																	});
		}
			
	}); 
	 
	// bind the expansion/contraction to the toggleElement 
	$('a.show-vid').click(function() { 
		var href = $(this).attr('href');
		$(this).parent().trigger('toggleContent', [ href, '.col-1' ]);
		return false;
	}); 

	//-------------------------------------------------------------------------------------------------
	//--- EXPOSURE - GALLERY ---
	$(function(){
		$('#images').exposure({controlsTarget : '#controls',
			controls : { prevNext : true, pageNumbers : true, firstLast : false },
			visiblePages : 2,
			slideshowControlsTarget : '#slideshow',
			onThumb : function(thumb) {
				var li = thumb.parents('li');				
				var fadeTo = li.hasClass('active') ? 1 : 0.3;
				
				thumb.css({display : 'none', opacity : fadeTo}).stop().fadeIn(200);
				
				thumb.hover(function() { 
					thumb.fadeTo('fast',1); 
				}, function() { 
					li.not('.active').children('img').fadeTo('fast', 0.3); 
				});
			},
			onImage : function(image, imageData, thumb) {
				// Check if wrapper is hovered.
				var hovered = $('.exposureWrapper').hasClass('exposureHover');
				
				// Fade out the previous image.
				$('.exposureWrapper > .exposureLastImage').stop().fadeOut(500, function() {
					$(this).remove();
				});
				
				// Fade in the current image.
				image.hide().stop().fadeIn(1000);
				
				var hasCaption = function() {
					var caption = imageData.find('.caption').html();
					var extra = imageData.find('.extra').html();
					return (caption && caption.length > 0) || (extra && extra.length > 0);
				}
				
				var showImageData = function() {
					imageData.stop().show().animate({bottom:0+'px'},{queue:false,duration:160});
				}
				var hoverOver = function() {
					$('.exposureWrapper').addClass('exposureHover');
					// Show image data as an overlay when image is hovered.
					var hasCpt = hasCaption();
					
					if (hasCpt) {
						showImageData.call();
					}
				};
				
				var hideImageData = function() {
					var imageDataBottom = -imageData.outerHeight();
					imageData.stop().show().animate({bottom:imageDataBottom+'px'},{queue:false,duration:160});
				}
				var hoverOut = function() { 
					$('.exposureWrapper').removeClass('exposureHover');
					// Hide image data on hover exit.
					if (hasCaption()) {
						hideImageData.call();
					}
				};
				
				$('.exposureWrapper').hover(hoverOver,hoverOut);
				imageData.hover(hoverOver,hoverOut);
										
				if (hovered) {
					if (hasCaption()) {
						showImageData.call();
					} else {
						hideImageData.call();	
					}	
				}

				if ($.exposure.showThumbs && thumb && thumb.length) {
					thumb.parents('li').siblings().children('img.selected').stop().fadeTo(200, 0.3, function() { $(this).removeClass('selected'); });			
					thumb.fadeTo('fast', 1).addClass('selected');
				}
			},
			onPageChanged : function() {
				$('.exposureThumbs li.current').hide().stop().fadeIn('fast', function() {
					var imageHeight = $(this).find('img').height();
					if (imageHeight > 0) {
						$(this).height(imageHeight);
					}
				});
			}
		});
	});
	//--- EXPOSURE - GALLERY ---
	//-------------------------------------------------------------------------------------------------

	
});
