$(document).ready(function(){
	//replace all fck inserted movies with swfobject
	//needed for accessibility options
	var cnt = 1;
	var next_img = null;
	//find all embed objects
	$('div.flash-movie').each(function(){
									   
		flash_preview_img = '/content/image/CH49.jpg';
		var next_img = $(this).siblings('img');
		var flash_preview_img = null;
		var flash_div = $(this).children("a");
		var flash_href = flash_div.attr('href');
		var flash_extension = flash_div.attr('href').substring(flash_div.attr('href').lastIndexOf(".")+1,flash_div.attr('href').length);
		var flash_preview_img = '/content/image/CH49.jpg';
		
		/**/
		switch(flash_extension){
			case "flv":
				if(next_img.length > 0 && next_img.attr('src').length > 0){
					flash_preview_img = next_img.attr('src');
					//remove the image from the page
					next_img.remove();
				}
				flash_href = flash_href + '&image=' + flash_preview_img;
				flash_div.attr('href',flash_href);
			break;
		}
		
		flash_div.media();				
		//if there is an img next to the flash video then use that image as the image preview
		/*
		if(next_img.length > 0 && next_img.attr('src').length > 0){
			flash_preview_img = next_img.attr('src');
			//remove the image from the page
			next_img.remove();
		}
		*/
							
		//set default player location
		//$.fn.media.defaults.flvPlayer = '/css/skins/default/flash/player.swf';
		//run media plugin on all a links with media class
		//$('#'+flash_div+' a').media();
	});
	
	$('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);
					flash_preview_img = '/content/image/CH49.jpg';
					
					//give the element and id
					$(this).attr('id','embed-'+cnt);
					
					switch(flash_extension){	
						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($('#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."
			}
		});
	}
	
	//if the livequery plugin is detected
	if(jQuery.fn.ajaxify){
		if($('#content-loading').length > 0 ){ // check if element is on page
			
			$('#main #main-center-panel').css({'position':'relative'});
			$('#main #main-center-panel #load-content').css({'position':'relative'});
			$('#content-loading').css({
									  'position':'absolute',
									  'width':'100%',
									  'height':'100px',
									  'text-align':'right',
									  'position':'absolute',
									  'top':'0',
									  'left':'0'
									  });
			
			$('a.ajax').livequery(function(){
		  
				$(this).ajaxify({
					onStart:function(options){
						$('#content-loading').fadeIn('slow');
					},
					onError:function(options,data){			
						$('#content-loading').html("<p class='error'>Error: Couldn't open &quot;"+options.link+"&quot;.</p>");	
					},
					onSuccess:function(options,data){
						$('#content-loading').css('display','none');
					},
					onComplete:function(options){
						$('#content-loading').css('display','none');
					},
					target: '#article #main #main-center-panel #load-content',
					loadHash:'attr:href',
					loading_txt:'Page loading...',
					loading_img:'/img/jquery/ajax-loader.gif',
					loading_target:'#content-loading'			
				});
			
			});	
		}
	}
	
	if(jQuery.url){
		
		// get the file
		//alert(jQuery.url.attr("file")); // returns 'index.html'
		// get the path
		//alert(jQuery.url.attr("path")); // returns '/information/about/index.html'
		// get the first segment from the url path
		// get the second segment from the url path
		jQuery.url.segment(0) // returns 'information'
		
		$('#main-center-panel div.col-1 ul#listing-1 li a').each(function(){
			if($(this).hasClass('selected')){
				$(this).removeClass('selected');
			}
			
			if($(this).attr('href') == jQuery.url.attr("path")){
				$(this).addClass('selected');
			}
			//caveat for home page section taking the first section of the URL
			if(jQuery.url.segment(0) == $(this).attr('title')){
				$(this).addClass('selected');
			}
		});
	}
	
	/* 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();
	}

});