/* ******************************************************************************
  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.flashNow").wms_pluginMedia();

		// set up popup video player
		jQuery('a.popup_play').each(function(){
			jQuery(this).click(function(event){
				event.preventDefault();
				var loadHref = jQuery(this).attr('href');
				jQuery.post(loadHref,{'displayType':'ajax'},function(data) {
					jQuery('#mediaPlayer').html(data);		
	
					jQuery("#mediaPlayer a.PluginContainer").wms_pluginMedia(); 
					jQuery('#mediaPlayer').addClass('show');
					jQuery('#mediaPlayer a.close').click(function(event){ 
						event.preventDefault();
						jQuery('#mediaPlayer').html('');
						jQuery('#mediaPlayer').removeClass('show');
					});
					
					jQuery('#mediaPlayer').css({'top':jQuery(window).scrollTop(),'bottom':jQuery(window).scrollTop()+jQuery(window).height()});
					
					jQuery(window).bind('scroll resize', function() {
						jQuery('#mediaPlayer').css({'top':jQuery(window).scrollTop(),'bottom':jQuery(window).scrollTop()+jQuery(window).height()});
					});

	
	
				})	
	
			});
		});

});

jQuery.fn.wms_pluginMedia = function(options) {
	
	var defaults = {  
			play: false
	};
	var options = jQuery.extend(defaults, options); 
		
	return this.each(function(v,videoCont) {	
		
		
		//console.log(jQuery(videoCont).css('color'));
		var hlColor = rgb2hex(jQuery(videoCont).css('color')).replace('#','');	
		
		
		if(jQuery(videoCont).hasClass('Vimeo')){  //vimeo player
		
			if(options.play==true){
				var autoplay = "&amp;autoplay=true";
			}else{
				var autoplay = "";				
			}
			jQuery(videoCont).hide().parent().append('<div class="PluginContainer Video Vimeo">');

			jQuery(videoCont)
				.parent()
				.find('div.PluginContainer')
				.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'+autoplay+'&amp;var=test'),
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%',
					width: '100%'
			});
			
		}else if (jQuery(videoCont).hasClass('YouTube')){ 

			if(options.play==true){
				var autoplay = "&amp;autoplay=1";
			}else{
				var autoplay = "";				
			}
			
			//youtube player
			youtubeMatch = jQuery(this).attr("href").match(/youtube.([a-z.]{2,6})\/(watch\??v=|v\/)([-A-Za-z0-9_^\?]+)/);
			jQuery(this).hide().insertAfter(jQuery('<div class="PluginContainer Video">').addClass('YouTube').insertAfter(jQuery(this)).flash(   {   
					src: 	'http://youtube.'+youtubeMatch[1]+'/v/'+youtubeMatch[3]+autoplay+'&fs=1&',
					wmode:'transparent',
					allowfullscreen:'true',
					height: '100%', width: '100%'
			}));
			
		}else if (jQuery(videoCont).hasClass('BBC')){ 
			//youtube player
			jQuery(this).hide().insertAfter(jQuery('<div class="PluginContainer 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).hide().insertAfter(jQuery('<div class="PluginContainer 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";
			}
			//console.log(mediaClass);
			//local flv player
			jQuery(this)
				.hide()
				.insertAfter(jQuery('<div class="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%'
		}));

	});

};


jQuery.fn.wms_pluginMediaReset = function(options) {
	
	var defaults = {  

	};
	var options = jQuery.extend(defaults, options); 
		
	return this.each(function(v,videoCont) {	
		jQuery(videoCont).parent().find('div.PluginContainer').remove();
		jQuery(videoCont).parent().find('a.PluginContainer').show();
	});
}

