//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-item-1-bg-mouseover.gif',
				 '/css/skins/default/img/buttons/btn-item-2-bg-mouseover.gif',
				 '/css/skins/default/img/buttons/btn-send-mouseover.gif',
				 '/css/skins/default/img/buttons/btn-search-mouseover.gif'
				 ],function() {

  /* now preload css stuff */
  $.preLoadCSSImages(function() {
     $('#loader').remove();
  })
});

//xhtml work around to open all links with re=new-window as new tab or window
$(function(){	
	//check all hrefs, if external, open in blank window
	$('a').filter(function(){
		pattern = /http|ftp(s)?/i;			//basic regex patterm case insensitive
		result = pattern.test($(this).attr('href'));	//rung using test method
				
		if(result == true){
			$(this).attr({
				target: "_blank", 
				title: "Opens in a new window"
			});
		}
	});
});

$(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($('#ListMailPro').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;
				} 		
			);
		
			$("#ListMailPro").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[ListMailPro][first_name]": {  
							required: true,  
							notLabelText: true            
					},
					"data[ListMailPro][last_name]": {  
							required: true,  
							notLabelText: true            
					},
					"data[ListMailPro][email]": {
						required: true,
						email: true
					}
				},
				messages: {
					"data[ListMailPro][first_name]": "Please enter your firstname.",
					"data[ListMailPro][last_name]": "Please enter your lastname.",
					"data[ListMailPro][email]": "Please enter your email address."
				}
			});
		}
		
		if($('#search-form').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;
				} 		
			);
		
			$("#search-form").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[Site][keywords]": {  
							required: true,  
							notLabelText: true            
					}
				},
				messages: {
					"data[Site][keywords]": "Please enter your search word."
				}
			});
		}
	}	
	
	if(jQuery.fn.inputfocus){
		if($('form#ListMailPro div.input').length > 0 ){ // check if element is on page
			//apply form field effect
			$('form#ListMailPro div.input').inputfocus({
				bgColourFocus:'#EEEAE8',
				bgColour:'#E8E9DC',			
				animate:true
			});
		}
		if($('form#search-form').length > 0 ){ // check if element is on page
			//apply form field effect
			$('form#search-form div.input').inputfocus({
				bgColourFocus:'#EEEAE8',
				bgColour:'#E8E9DC',	
				animate:true
			});
		}
	}
	
	if(jQuery.fn.cycle){
		if($('#quotes').length > 0 ){ // check if element is on page
			if($('#quotes').hasClass('no-effect')==false){
				$('#quotes').css('height','110px');//set height of quotes box
				$('#quotes').cycle({fx:'fade',timeout:'7000'});
			}
		}
		if($('#sidebar-images').length > 0 ){ // check if element is on page
			if($('#sidebar-images').hasClass('no-effect')==false){
				$('#sidebar-images').css('height','110px');//set height of quotes box
				$('#sidebar-images').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');
				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');
				});
			}
		});
	}

	if($('#main #main-left-panel .side-panel #links.item ul li').length > 0 ){ // check if element is on page
		$('#main #main-left-panel .side-panel #links.item ul li:odd').css({'background-color':'#00B0D8'});
		$('#main #main-left-panel .side-panel #links.item ul li:odd a').css({'background-image':'url("/css/skins/default/img/content/item-links-li-2-bg.jpg")'});
	}
	
});
