$(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){	
		
		//setup default actions for validate plugin
		$.validator.setDefaults({
			submitHandler: function(form) {
				//on submit make sure that any inputs that have the same value as their label then blank the value
				
				//pass form id to local var
				var formId = jQuery(form).attr('id');
				

				//iterate through submit elements
				$('#'+formId+' input[type=text],select,textarea,radio,checkbox').each(function(){
					
					//get current_input 
					var current_input = $(this);
					//get label for current_input 
					var current_label = $('label[for=' + current_input.attr('id') + ']');
					//check that the element is either a text or textarea
					if(current_input.attr('type') == 'text' || current_input.attr('cols')){
						//if label and input the same then blank the value
						if(current_label.text() == current_input.val()){	
							current_input.val('');
						}
					}
				});

				//submit form
				form.submit();
			},
			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
				});
			}
		});

		$.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;
			}, "Please enter a value.");

		if($('#form-event-search').length > 0 ){ // check if element is on page

			$("#form-event-search").validate({
				rules: {
					"data[FormEventsearch][article_sub_category_id]": {  
						required:false,
						notLabelText: false // this means that value can stay as it's label and the form submit handler will blank the value on submit    
					},
					"data[FormEventsearch][name]": {  
						notLabelText: false // this means that value can stay as it's label and the form submit handler will blank the value on submit    
					}
				}
			});
		}

		if($('#form-search').length > 0 ){ // check if element is on page

			$("#form-search").validate({
				rules: {
					"data[FormSearch][searchtext]": {  
						notLabelText: false // this means that value can stay as it's label and the form submit handler will blank the value on submit    
					}
				}
			});
		}


		if($('#form-newsletter').length > 0 ){ 
			$("#form-newsletter").validate({	
				rules: {
					"data[FormSignup][firstname]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormSignup][lastname]": {  
							required: true,  
							notLabelText: true            
					},
					"data[FormSignup][email]": {
						required: true,
						email: true
					},
					"data[FormSignup][captcha]": {  
							required: true,  
							notLabelText: true 
					}
					
				},
				messages: {
					"data[FormSignup][firstname]": "Please enter your firstname.",
					"data[FormSignup][lastname]": "Please enter your lastname.",
					"data[FormSignup][email]": "Please enter your email address.",
					"data[FormSignup][captcha]": "Please re-enter the code above."
				}
				
			});
		}
		
		if($('#form-contact').length > 0 ){ // check if element is on page
		
			
		
			$("#form-contact").validate({			
				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
		
			
		
			$("#form-feedback").validate({
				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
		
			
		
			$("#BuyForm").validate({	
				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($('#form-event-search').length > 0 ){ // check if element is on page
			//apply form field effect
			$('#form-event-search 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'
		});
	}
	
});
