/* ******************************************************************************
  JavaScript Media Player(s) Controller
	
 	Copyright (c) 2010  ()
	Powered by: JQuery
 	Date: 19/02/2010
		 
****************************************************************************** */

jQuery(document).ready(function() {
		//alert('yup');
		var 	swfParams = {"allowfullscreen":"true"};
		
		//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('wms_pluginMedia');
		
		var hlColor = rgb2hex(jQuery(videoCont).css('color')).replace('#','');	

		jQuery(videoCont).click(function(event){ event.preventDefault(); });
		
		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')){ 


			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
			
			var __BGImage = jQuery('<img style="position: absolute; left: -999999px;top: -999999px; z-index:0;" src="'+jQuery(this).find('img:first').attr('src')+'" />');
			jQuery('body').append(__BGImage);
			if(jQuery(__BGImage).width()>0){
				var __imgRatio = jQuery(__BGImage).width()/jQuery(__BGImage).height();
			}else{
				var __imgRatio = 1;
			}
						
			jQuery(__BGImage).width(jQuery(this).width());
			if( jQuery(__BGImage).height() < jQuery(this).height() ){
				jQuery(__BGImage).height(jQuery(this).height())
				jQuery(__BGImage).width(jQuery(__BGImage).height()*__imgRatio);
			}
			var __cont_H = jQuery(this).height();
			var __cont_W = jQuery(this).width();			
			var __thumb_H = jQuery(__BGImage).height();
			var __thumb_L = (jQuery(this).width()-jQuery(__BGImage).width())/2;
			var __thumb_T = (jQuery(this).height()-jQuery(__BGImage).height())/2
			
			jQuery(__BGImage).remove()

			if(__cont_H>0 && __cont_W >0){
				jQuery(this).html('<img src="'+jQuery(this).find('img:first').attr('src')+'" style="position: absolute; left:'+__thumb_L+'px; top:'+(__thumb_T)+'px; height:'+__thumb_H+'px; width:'+(__thumb_H*__imgRatio)+'px;" /><div class="'+mediaClass+'" style="position: absolute; top:0; right: 0; bottom: 0; left: 0; background: none; width: '+(__cont_W)+'px; height: '+__cont_H+'px;"><div></div></div>');
			
				jQuery(this).find('.'+mediaClass+':first div')
					.replaceWith(
						jQuery('<div>')
							.css({'overflow':'hidden','position':'relative'})
							.flash({   
							src: 	"/images_site/media-player/MediaPlayer.swf?mediaURL="+decodeURIComponent(mediaURL),
							height: __cont_H+'px', width: (__cont_W)+'px',
							wmode:'transparent',
							allowfullscreen:'true',
							flashvars:{	'mediaTitle':'test',
													'fsEnable':'TRUE',
													'colHL':hlColor}
						})
					);
					
			}else{ //admin
			
				if(mediaClass=='Video'){
					var def_H = '260px';
				}else{
					var def_H = '40px';					
				}
				jQuery(this).html('<div class="'+mediaClass+'" style="position: absolute; top:0; right: 0; bottom: 0; left: 0; background: none; width:100%; height: '+def_H+'"><div></div></div>');
				
				jQuery(this).find('.'+mediaClass+':first div')
					.replaceWith(
						jQuery('<div>')
							.css({'overflow':'hidden','position':'relative'})
							.flash({   
							src: 	"/images_site/media-player/MediaPlayer.swf",
							height: def_H, width:'100%',
							wmode:'transparent',
							allowfullscreen:'true',
							flashvars:{	'mediaTitle':'test',
													'mediaURL':decodeURIComponent(mediaURL),
													'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%'
		}));

	});

};

