$(document).ready(function(){
						   
	//-------------------------------------------------------------------------------------------------
	//--- LINKS - NEW WINDOW ---						   
	//xhtml work around to open all EXTERNAL 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"
				});
			}
		});
	});	
	//--- LINKS - NEW WINDOW ---		
	//-------------------------------------------------------------------------------------------------
	
	
	//-------------------------------------------------------------------------------------------------
	//--- EMBED MOVIES - flv/flash ---
	
	//replace all html editor 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 = '/img/content/flash-default.gif';
		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 = '/img/content/flash-default.gif';
		
		//
		switch (flash_extension)
		{
			case "flv":
			//case "swf":
				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);	
				//alert(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();
	});
	

	//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'))
				{	
					//get width and height of movie to use for replacement below
					flash_width = $(this).attr('width');
					flash_height = $(this).attr('height');
					alert(flash_width+":"+flash_height);
					
					embed_type = $(this).attr('type');
					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 = '/img/content/flash-default.gif';
					alert("type:"+embed_type+"ext:"+flash_extension+"flash_src:"+flash_src);
	
					//give the element an id
					$(this).attr('id','embed-'+cnt);	

					switch (flash_extension)
					{						
						/*case "flv":
							alert(flash_height);
							
							if (jQuery.fn.media)
							{	
								//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,
									play: false,
									width : flash_width,
									height : flash_height
									//width : 425,
									//height : 344
								});
								
								//alert(flash_height);
								
								//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;*/
						case "swf":
							if (jQuery.fn.flash)
							{
								alert("swf");	
									
								//replace the embed obect with a holding div
								$(this).replaceWith('<div class="flash-movie"><div id="'+flash_div+'"></div></div>');
								//use swfobject to embded the flash movie
								$('#'+flash_div).flash({ 
									// test.swf is the flash document 
									swf: flash_src,
									width : flash_width,
									height : flash_height	
									//width : 425,
									//height : 344
								}); 
							}
							else
							{
								alert('You need to have the swfobject plugins installed.');
							}
						break;
						default:
						break;
					}
				}
			}
		cnt++;
	});
	//--- EMBED MOVIES - flv/flash ---
	//-------------------------------------------------------------------------------------------------


	//-------------------------------------------------------------------------------------------------
	/* 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();
	}
	//-------------------------------------------------------------------------------------------------	
	

});
