//onload preload all images, both inline and css
//$.preLoadAllImages(); //generic option to load all css and inline images
$.preLoadImages([
				 'css/skins/default/img/buttons/btn-take-tour-mouseover.png',
				 'css/skins/default/img/buttons/btn-contact-us-mouseover.png',
				 'css/skins/default/img/buttons/btn-contact-us-right-mouseover.png',
				 'css/skins/default/img/buttons/btn-take-tour-right-mouseover.png',
				 'css/skins/default/img/buttons/btn-login-mouseover.png',
				 'css/skins/default/img/buttons/btn-submit-mouseover.gif',
				 'css/skins/default/img/buttons/btn-tour-mouseover.gif'
				 ],function() {

  /* now preload css stuff */
  $.preLoadCSSImages()
});

$(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($('#SiteContactUsForm').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;
				} 		
			);
		
			$("#SiteContactUsForm").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: {
					txt_title: {  
						required: true,  
						notLabelText: true            
					},
					txt_firstname: {  
						required: true,  
						notLabelText: true            
					},
					txt_lastname: {  
						required: true,  
						notLabelText: true            
					},
					txt_email: {
						required: true,
						email: true
					}
				},
				messages: {
					txt_title: "Please enter your title.",
					txt_firstname: "Please enter your first name.",
					txt_lastname: "Please enter your lastname.",
					txt_email: "Please enter your email address."
				}
			});
		}
		if($('#SiteLoginForm').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;
				} 		
			);
		
			$("#SiteLoginForm").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: {
					txt_username: {  
						required: true,  
						notLabelText: true            
					},
					txt_password: {  
						required: true,  
						notLabelText: true            
					}
				},
				messages: {
					txt_username: "Please enter your user name.",
					txt_password: "Please enter your password."
				}
			});
		}
	}
	
	/* 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'
			});
		}
	}
	
	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($('.button').length > 0 ){ // check if element is on page
		//loop through all a tags if one has a nested image then apply a fade to it unless the image has a class set to 'nofade'	
		$('.button').each(function(){
			
			//if it's an embedded
			if($(this).attr('src')){
				var img = $(this).attr('src');
				var img_extension = img.substring(img.lastIndexOf(".")+1,img.length);
				var img_name_mouse_out = img.substring(0,img.lastIndexOf("."));
				var img_name_mouse_over = img_name_mouse_out + '-mouseover.' + img_extension;

				$(this).mouseover(function(){
					$(this).attr('src',img_name_mouse_over);
					$(this).addClass('mouseover');
				});
				
				$(this).mouseout(function(){
					$(this).attr('src',img);
					$(this).removeClass('mouseover');
				});
			}
			//if it's a submit check with filter method
			else if($(this).filter('input[type=submit],div,a')){
				//should be using css to render background image so change css for element
				var $e = $(this).filter('input[type=submit],div,a');
				
				var img = $e.css('background-image');
								
				$e.css('background', 'none');
				$e.css('background-image', img);
				
				var img_extension = img.substring(img.lastIndexOf(".")+1,img.length);
				var img_name_mouse_out = img.substring(0,img.lastIndexOf("."));
				var img_name_mouse_over = img_name_mouse_out + '-mouseover.' + img_extension;
				
				$e.mouseover(function(){
					$e.css({'background-image':img_name_mouse_over });
					$e.addClass('mouseover');
				});
				
				$e.mouseout(function(){
					$e.css({'background-image':img });
					$e.removeClass('mouseover');
				});
			}
		});
	}
	
	
	
	
	
	
	
	
	if($('.tourbutton').length > 0 ){ // check if element is on page
		//loop through all a tags if one has a nested image then apply a fade to it unless the image has a class set to 'nofade'	
		$('.tourbutton').each(function(){
			
			//if it's an embedded
			if($(this).attr('src')){
				var img = $(this).attr('src');
				var img_extension = img.substring(img.lastIndexOf(".")+1,img.length);
				var img_name_mouse_out = img.substring(0,img.lastIndexOf("."));
				var img_name_mouse_over = img_name_mouse_out + '-mouseover.' + img_extension;

				$(this).mouseover(function(){
					$(this).attr('src',img_name_mouse_over);
					$(this).addClass('mouseover');
				});
				
				$(this).mouseout(function(){
					$(this).attr('src',img);
					$(this).removeClass('mouseover');
				});
			}
			//if it's a submit check with filter method
			else if($(this).filter('input[type=submit],div,a')){
				//should be using css to render background image so change css for element
				var $e = $(this).filter('input[type=submit],div,a');
				
				var img = $e.css('background-image');
								
				$e.css('background', 'none');
				$e.css('background-image', img);
				
				var img_extension = img.substring(img.lastIndexOf(".")+1,img.length);
				var img_name_mouse_out = img.substring(0,img.lastIndexOf("."));
				var img_name_mouse_over = img_name_mouse_out + '-mouseover.' + img_extension;
				
				$e.mouseover(function(){
					$e.css({'background-image':img_name_mouse_over });
					$e.addClass('mouseover');
				});
				
				$e.mouseout(function(){
					$e.css({'background-image':img });
					$e.removeClass('mouseover');
				});
			}
		});
	}
	
	
});
