/* ******************************************************************************
  JavaScript Media Player(s) Controller
	
 	Copyright (c) 2010 David Turner (davidturner.info)
	Powered by: JQuery
 	Date: 19/02/2010
		 
****************************************************************************** */

jQuery(document).ready(function() {
		//alert('yup');
		var 	swfParams = {"allowfullscreen":"true","wmode":"transparent"};
		
		jQuery("img.FacebookLikeBox").wms_fbLikeBox();
		jQuery("a.Plugin.Soundcloud").wms_pluginSoundcloud();
		jQuery("a.PluginContainer").wms_pluginMedia();

});

jQuery.fn.wms_pluginMedia = function(options) {
	
	return this.each(function(v,videoCont) {	
		
	
		//console.log(jQuery(videoCont).css('color'));
		var hlColor = rgb2hex(jQuery(videoCont).css('color')).replace('#','');	
		//console.log(hlColor);
		
		if(jQuery(videoCont).hasClass('Vimeo')){  //vimeo player
		
			jQuery(this).replaceWith(jQuery('<div>').addClass('PluginContainer').addClass('Video').addClass('Vimeo').insertAfter(jQuery(videoCont)).flash(   {   
					src: 	jQuery(this).attr("href")
														.replace(/vimeo.([a-z.]{2,6})\/([0-9]+)/,'vimeo.$1/moogaloop.swf?clip_id=$2&amp;color='+hlColor+'&amp;show_title=1&amp;fullscreen=1&amp;show_byline=1&amp;show_portrait=1&amp;var=test'),
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%', width: '100%'
			}));
			
		}else if (jQuery(videoCont).hasClass('YouTube')){ 
			
			//youtube player
			youtubeMatch = jQuery(this).attr("href").match(/youtube.([a-z.]{2,6})\/(watch\??v=|v\/)([-A-Za-z0-9_^\?]+)/);
			jQuery(this).replaceWith(jQuery('<div>').addClass('PluginContainer').addClass('Video').addClass('YouTube').insertAfter(jQuery(this)).flash(   {   
					src: 	'http://youtube.'+youtubeMatch[1]+'/v/'+youtubeMatch[3]+'&fs=1&',
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%', width: '100%'
			}));
			
		}else if (jQuery(videoCont).hasClass('BBC')){ 
			//youtube player
			jQuery(this).replaceWith(jQuery('<div>').addClass('PluginContainer').addClass('Video').addClass('BBC').insertAfter(jQuery(this)).flash(   {   
					src: 	'http://www.bbc.co.uk/emp/external/player.swf',
					flashVars:jQuery(this).attr("href"),
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%', width: '100%'
			}));
		}else if (jQuery(videoCont).hasClass('SlideShare')){ 
			//youtube player
			//alert(jQuery(this).attr("href"));
			jQuery(this).replaceWith(jQuery('<div>').addClass('PluginContainer').addClass('Video').addClass('SlideShare').insertAfter(jQuery(this)).flash(   {   
					src: 	'http://static.slidesharecdn.com/swf/ssplayer2.swf',
					flashvars:jQuery(this).attr("href"),
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%', width: '100%'
			}));		
		}else{
			var mediaURL =jQuery(this).attr('href');
			if(mediaURL.match(/.mp3$/)){
				var mediaClass = "Audio";
			}else{
				var mediaClass = "Video";
			}
			//local flv player
			jQuery(this)
				.replaceWith(jQuery('<div>')
				.addClass('PluginContainer')
				.addClass(mediaClass)
				.insertAfter(jQuery(this))
				.flash(   {   
						src: 	"/images_site/media-player/MediaPlayer.swf",
						height: '100%', width: '100%',
						wmode:'transparent',
						allowfullscreen:'true',
						flashvars:{	'mediaTitle':'test',
												'mediaURL':mediaURL,
												'bgImage':jQuery(this).find('img:first').attr('src'),
												'fsEnable':'TRUE',
												'colHL':hlColor}
				}
			));
			
			
		}
	})

};
			
var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
	if(rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)){
	 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}else{
		return rgb;
	}
}


function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }



jQuery.fn.wms_fbLikeBox = function(options) {
	
	return this.each(function(count) {	
		
		
		var fbSrc = jQuery(this).attr('class');
		var fbID = fbSrc.match(/fbid_([0-9]+)/)[1];
		var fbWidth = jQuery(this).parent().width();

		jQuery(this).replaceWith('<iframe src="http://www.facebook.com/plugins/likebox.php?id='+fbID+'&width='+fbWidth+'&connections=30&stream=false&header=false&height=220" class="facebookLikeBox" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:220px; background: #FFF; margin-bottom: 8px;" allowTransparency="true"></iframe>');
		
		//if(fbWidth>400){
		//	jQuery(this).attr({'src':''}).height(555);
		//}else{
			//jQuery(this).attr({'src':'http://www.facebook.com/plugins/likebox.php?id='+fbID+'&width='+fbWidth+'&connections=6&stream=false&header=false&height=265'}).height(265);
		//}
	});

};


jQuery.fn.wms_pluginSoundcloud = function(options) {
	
	return this.each(function(count) {	
		
		var scSrc = jQuery(this).attr('href');
		jQuery(this)
			.replaceWith(jQuery('<div>')
			.addClass('PluginContainer')
			.addClass('Audio')
			.addClass('Soundcloud')
			.height('81px')
			.flash({   
					swf: 	'http://player.soundcloud.com/player.swf?url='+ scSrc+'&amp;color=008376',
					params: { allowfullscreen:true,wmode:'transparent','flashVars':jQuery(this).attr("href") },
					height: '81px', width: '100%'
		}));

	});

};

