$(document).ready(function(){
	//replace all fck inserted movies with swfobject
	//needed for accessibility options
	var cnt = 1;
	//find all embed objects
	$('embed').each(function(){
			//if they are flash movies
			if($(this).attr('type') == 'application/x-shockwave-flash'){
				
				//get the source fo the flash movie
				if($(this).attr('src')){
					
					flash_src = $(this).attr('src');
					flash_div = 'flash-'+cnt;
					flash_extension = $(this).attr('src').substring($(this).attr('src').lastIndexOf(".")+1,$(this).attr('src').length);
					
					switch(flash_extension){
						case "flv":
						if(jQuery.fn.media){
							//replace the embed obect with a holding div
							$(this).replaceWith('<div id="'+flash_div+'"><a class="{width:425, height:344}" href="'+flash_src+'"></a></div>');
						
							//set default player location
							$.fn.media.defaults.flvPlayer = '/css/skins/default/flash/mediaplayer.swf';
							//run media plugin on all a links with media class
							$('#'+flash_div+' a').media();
						}else{
							alert('You need to have the media and meta plugins installed.');
						}
						break;
						default:
						if(jQuery.fn.flash){
							//replace the embed obect with a holding div
							$(this).replaceWith('<div id="'+flash_div+'"></div>');
							//use swfobject to embded the flash movie
							$('#'+flash_div).flash({ 
								// test.swf is the flash document 
								swf: flash_src,
								width : 425,
								height : 344
							}); 
						}else{
							alert('You need to have the swfobject plugins installed.');
						}
						break;
					}
				}
			}
		cnt ++;
	});
	
	//apply form validation
	if(jQuery.fn.validate){			
		if($('#form-newsletter').length > 0 ){ // check if element is on page
		
			//custom validation rule - text is not the prefilled content     
			$.validator.addMethod("notLabelText",
				
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				} 		
			);
		
			$("#form-newsletter").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','000000'); //red for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
						$(element).animate({opacity: 0.8});
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','#000000'); //green for valid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#8B9D3E'); //green border for valid data
						$(element).animate({opacity: 0.8});
					});
				},				
				rules: {
					"data[FormSignup][firstname]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormSignup][lastname]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormSignup][email]": {
						required: true,
						email: true
					}
				},
				messages: {
					"data[FormSignup][firstname]": "Please enter your firstname.",
					"data[FormSignup][lastname]": "Please enter your lastname.",
					"data[FormSignup][email]": "Please enter your email address."
				}
				
			});
		}
		
		if($('#form-contact').length > 0 ){ // check if element is on page
		
			//custom validation rule - text is not the prefilled content     
			$.validator.addMethod("notLabelText",
				
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				} 		
			);
		
			$("#form-contact").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','000000'); //red for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
						$(element).animate({opacity: 0.8});
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','#000000'); //green for valid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#8B9D3E'); //green border for valid data
						$(element).animate({opacity: 0.8});
					});
				},				
				rules: {
					"data[FormContact][name]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormContact][email]": {
						required: true,
						email: true
					},
					"data[FormContact][telephone]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormContact][enquiry]": {  
							required: true,  
							notLabelText: true            
					}
				},
				messages: {
					"data[FormContact][name]": "Please enter your firstname.",
					"data[FormContact][email]": "Please enter your email address.",
					"data[FormContact][telephone]": "Please enter your telephone number.",
					"data[FormContact][enquiry]": "Please enter your enquiry."
				}
			});
		}
		if($('#form-feedback').length > 0 ){ // check if element is on page
		
			//custom validation rule - text is not the prefilled content     
			$.validator.addMethod("notLabelText",
				
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				} 		
			);
		
			$("#form-feedback").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','000000'); //red for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
						$(element).animate({opacity: 0.8});
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','#000000'); //green for valid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#8B9D3E'); //green border for valid data
						$(element).animate({opacity: 0.8});
					});
				},				
				rules: {
					"data[FormFeedback][subject]": {  
						required: true,  
						notLabelText: true            
					},
					"data[FormFeedback][date]": {  
						required: true,
						date: true,
						notLabelText: true  
					},
					"data[FormFeedback][name]": {  
						required: true,  
						notLabelText: true  
					},
					"data[FormFeedback][email]": {
						required: true,
						email: true
					}
				},
				messages: {
					"data[FormFeedback][reserve_name]": "Please enter the reserve name.",
					"data[FormFeedback][date]": "Please enter date.",
					"data[FormFeedback][name]": "Please enter your name.",
					"data[FormFeedback][email]": "Please enter your email address."
				}
			});
		}

		if($('#BuyForm').length > 0 ){ // check if element is on page
		
			//custom validation rule - text is not the prefilled content     
			$.validator.addMethod("notLabelText",
				
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				} 		
			);
		
			$("#BuyForm").validate({
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','000000'); //red for invalid data
						$(element).css('background-color','#FBE3E4'); //red background for invalid data
						$(element).css('border-color','#FBC2C4'); //red border for invalid data
						$(element).animate({opacity: 0.8});
					});
				},
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.8
					}, 400, function () {
						$(element).css('color','#000000'); //green for valid data
						$(element).css('background-color','#E6EFC2'); //green background for valid data
						$(element).css('border-color','#8B9D3E'); //green border for valid data
						$(element).animate({opacity: 0.8});
					});
				},				
				rules: {
					"name": {  
							required: true,  
							notLabelText: true            
					},
					"address": {  
							required: true,  
							notLabelText: true            
					},
					"country": {  
							required: true,  
							notLabelText: true            
					},
					"postcode": {  
							required: true,  
							notLabelText: true            
					},
					"email": {
						required: true,
						email: true
					},
					"tel": {  
						required: true,  
						phoneUK: true,
						notLabelText: true            
					}
				},
				messages: {
					"name": "Please enter your firstname.",
					"address": "Please enter your address.",
					"country": "Please enter your country.",
					"postcode": "Please enter your country.",
					"email": "Please enter your email address.",
					"tel": "Please enter your telephone number."
				}
				
			});
		}
	}
	
	/* EMBED MEDIA */
	if(jQuery.fn.media){
		//set default player location
		$.fn.media.defaults.flvPlayer = '/css/skins/default/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'
			});
		}
		//apply show hide plugin for use in gallery
		if($('div.action a.toggle').length > 0 ){
			$('a.toggle').showhide({
				animate:false,
				event:'toggle'
			});
		}
	}
	
	
	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:'#EEEAE8',
				bgColour:'none',			
				animate:true
			});
		}
		if($('#form-newsletter').length > 0 ){ // check if element is on page
			//apply form field effect
			$('#form-newsletter div.input').inputfocus({
				bgColourFocus:'#EEEAE8',
				bgColour:'none',			
				animate:true
			});
		}
		if($('#main #main-left-panel .side-panel #form-contact').length > 0 ){ // check if element is on page
			//apply form field effect
			$('#main #main-left-panel .side-panel #form-contact div.input').inputfocus({
				bgColourFocus:'#EEEAE8',
				bgColour:'none',			
				animate:true
			});
		}
	}
	
	if(jQuery.fn.cycle){
		if($('#quotes').length > 0 ){ // check if element is on page
			$('#quotes').css('height','110px');//set height of quotes box
			$('#quotes').cycle('fade');
		}
	}
	
	if($('div.bg-img, img.bg-img').length > 0 ){ // check if element is on page
		$('div.bg-img').each(function(){
			if($(this).attr('title').length > 0 ){
					 $(this).css({'position':'relative'})	//position parent
							.append(
								   		$('<div class="accredit"></div>')	//create accrediation element
											.css({
												 	'position':'absolute',	//position
													'opacity':0.7
												})	
											.html($(this).attr('title'))	//apply parent title property to elements html
							   		);
			}
		});
		$('img.bg-img').each(function(){
			if($(this).attr('title').length > 0 ){
				
				
				var width =  $(this).outerWidth();
				var height =  $(this).outerHeight();
				
				var position = $(this).position();
				
				var left = position.left + width - 100;
				var top =  position.top + height - 20;
				
				
					 $(this).before(
								   		$('<div class="accredit"></div>')	//create accrediation element
											.css({
													'width':'100px',
													'height':'20px',
													'left':left+'px',
													'top':top+'px',
													'opacity':0.7
												})
											.html($(this).attr('title'))	//apply parent title property to elements html
											//.text("left: " + left + ", top: " + top)
							   		);
			}
		});
	}
	//apply accordian behaviour to ul li structure
	if($('.accordion').length > 0 ){ // check if element is on page
		
		$('.head').each(function(){
			$(this).click(function() {
				$(this).siblings('ul').children().toggle();
				return false;
			}).siblings('ul').children().hide();
		});
	}
	
	// Datepicker
	if(jQuery.fn.datepicker){
		$('.datepicker').datepicker({
			inline: true,
			dateFormat: 'd MM yy'
		});
	}
	
});