$(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($('#SiteContactForm').length > 0 ){ // check if element is on page
			$("#SiteContactForm").validate({
				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
					});
				},
				rules: {
					txt_title: "required",
					txt_firstname: "required",
					txt_lastname: "required",
					txt_email: {
						required: true,
						email: true
					},
					txt_enquiry: "required"
				},
				messages: {
					txt_title: "Please enter your title.",
					txt_firstname: "Please enter your first name.",
					txt_lastname: "Please enter your last name.",
					txt_email: "Please enter your email address.",
					txt_enquiry: "Please enter your enquiry."
				}
			});
		}
	}
	
	/* 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.inputfocus){
		if($('#FormSignup form div.text').length > 0 ){ // check if element is on page
			//apply form field effect
			$('form div.text').inputfocus({
				bgColourFocus:'#EFFCFF',
				bgColour:'#FFFFFF',			
				animate:true
			});
		}
		if($('form div.input').length > 0 ){ // check if element is on page
			//apply form field effect
			$('form div.input').inputfocus({
				bgColourFocus:'#EFFCFF',
				bgColour:'#FFFFFF',			
				animate:true
			});
		}
	}
	
	
	//Hide (Collapse) the toggle containers on load
	$("#help-login").hide(); 

	//Switch the "Open" and "Close" state per click
	$("#right-side-panel-1 a").toggle(function(){
		$(this).addClass("active");
		$(this).text("hide");
		}, function () {
		$(this).removeClass("active");
		$(this).text("click here");
	});

	//Slide up and down on click
	$("#right-side-panel-1 a").click(function(){
		$("#help-login").slideToggle("slow");
	});
	
	//add close button to help panel
	if($("#ICContainer").length > 0 ){
		$("#ICContainer").live("mouseover", function(){
			if(!($(this).children('.closer').length > 0)){			
				$(this).append('<div class="closer"><a href="#">close [X]</a></div>');
				$('div.closer').live("click", function(){
					$("#ICContainer").hide();	
					return false;
				});
			}
		});
		$("#ICContainer").css({
			'position':'fixed',
			'top':'10px',			
			'left':'38%'
		});
	}
	
	//rotate page elements
	if(jQuery.fn.cycle){
		if($('#quotes').length > 0 ){ // check if element is on page
			$('#quotes').cycle({fx:'fade',timeout:'7000'});
		}	
	}

});

