//onload preload all images, both inline and css
//$.preLoadAllImages(); //generic option to load all css and inline images
$.preLoadImages([
				 'css/skins/default/holding/img/buttons/btn-item-1-bg-mouseover.gif',
				 'css/skins/default/holding/img/buttons/btn-item-2-bg-mouseover.gif',
				 'css/skins/default/holding/img/buttons/btn-send-mouseover.gif',
				 'css/skins/default/holding/img/buttons/btn-search-mouseover.gif'
				 ],function() {

  /* now preload css stuff */
  $.preLoadCSSImages(function() {
     $('#loader').remove();
  })
});



$(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/holding/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-course').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-course").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_firstname: {  
						required: true,  
						notLabelText: true            
					},
					/*txt_surname: {  
						required: true,  
						notLabelText: true            
					},*/
					txt_email: {
						required: true,
						email: true
					}
				},
				messages: {
					txt_firstname: "Please enter your first name.",
					//txt_surname: "Please enter your surname.",
					txt_email: "Please enter your email address."
				}
			});
		}
	}	
	
	if(jQuery.fn.inputfocus){
		if($('#form-course').length > 0 ){ // check if element is on page
			//apply form field effect
			$('#form-course div.input').inputfocus({
				bgColourFocus:'#EEEAE8',
				bgColour:'#ebe2f3',	
				animate:true
			});
		}
	}
	
	if(jQuery.fn.cycle){
		if($('#quotes').length > 0 ){ // check if element is on page
			$('#quotes').css('height','150px');//set height of quotes box
			//$('#quotes').cycle('scrollUp'); //scrollRight scrollUp
			//$('#quotes').cycle({fx:'fade'});
			//$('#quotes').cycle({fx:'fade',cleartype:1});
			$('#quotes').cycle({fx:'scrollUp',timeout:5000});
		}
	}
	
	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');
				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(){
					$e.css({'background-image':img_name_mouse_over });
					$e.addClass('mouseover');
				});
				
				$(this).mouseout(function(){
					$e.css({'background-image':img });
					$e.removeClass('mouseover');
				});
			}
		});
	}


	//------------------------------------------------------------------
	//--- jquery nyroModal ---/
	$(function() {
		if ($('.nyroModal').length > 0 ){ // check if element is on page	
			$.nyroModalSettings({
				bgColor: '#000000',
				width: 600,
				height: 400,
				type: 'manual',
				//height: null,
				minWidth: 500, // Minimum width
				//minheight: 294, // Minimum width
				resizeable: true, // Indicate if the content is resizable. Will be set to false for swf
				autoSizable: true, // Indicate if the content is auto sizable. If not, the min size will be used
				title: null // Modal title
			});
		 }//check
	});
	
	/*
	if ($('.nyroModal').length > 0 ){ // check if element is on page	
		$('.nyroModal').nyroModal({
			width: 800,
			height: 400
		});	
	}//check
	*/
	
	//--- jquery nyroModal ---/
	//------------------------------------------------------------------	


});
