/*
* Function for retrieving the extension of a link to a file
* @param link: the full link to the file
* @return string: The file extension
-----------------------------------*/
function mp_get_file_extension(link){
	return link.substring(link.lastIndexOf('.'));
}

/*
* Function for returning the height for the flash player window
* @param link: The link to the file to be played
* @return string: The height of the player including px value
-----------------------------------*/
function mp_get_flash_height(link){
	var extension = mp_get_file_extension(link);
	return ((extension == '.mp3') || (extension == '.mp4')) ? '1px' : '200px';
}

/*
* Function for applying an extra class to the player wrapper if it is an audio file
* @param link: the full link to the media file
* @return: A string to be added as a class attribute if the link points to an audio file and flash is installed
-----------------------------------*/
function mp_get_holder_class(link){
	var extension = mp_get_file_extension(link);
	var flash_version = swfobject.getFlashPlayerVersion();
	return ((flash_version.major > 8) && ((extension == '.mp3') || (extension == '.mp4'))) ? 'audio-player-container' : null;
}

$(document).ready(function() {
    var $yt_links = $("a[href*='http://www.youtube.com/watch']");
    var $media_links = $("a[href$='flv'], a[href$='mp4'], a[href$='mp3']");
    $.each($yt_links, function(i) {
        var $holder = $('<div />');
        $(this).replaceWith($holder);
        var link = $(this).attr('href').split("=")[1];
        $holder.player({id:'yt'+i, type:'yt', media:link,playerWidth:'330px',flashWidth:'330px',flashHeight:'200px'});
    });
   $.each($media_links, function(i) {
        var $holder = $('<div />');
        var link = $(this).attr('href');
        var extra_class = mp_get_holder_class(link);
        var height = mp_get_flash_height(link);
        if(extra_class){
        	$($holder).addClass(extra_class);
        }
        $(this).replaceWith($holder);
        $holder.player({id:'jw'+i, url:'player.swf', type:'jw', media:link,playerWidth:'330px',flashWidth:'330px',flashHeight:height});
    }); 
});
