/**
 * Next Mediaplayer Plugin 
 * 
 * @author dan.rades
 * 
 */

(function($) {                                         
$.fn.nextMediaplayer = function(o) 
{   
	var localPlayerSrc = '/cms/thirdparty/adFlvGenericPlayer.swf';
    var o = $.extend(
		{	
			width: 320,
			height: 284,
			quality : 'high',
			allowfullscreen: true,		
			hasAds : false,
			X1_ad_params : '',
			wmode : 'transparent',
			onChange : function() {}
		}, o || {}
	);
	var sources = {
		direct : 0,		
		tare : 1,
		youtube : 2
	}

	var ofv = o.flashvars || {};
	var fv = $.extend(		
		{
			autostart : true,			
			backcolor : '0x18183A',
			frontcolor : '0xFFFFFF',
			lightcolor : '0xFFFFFF', 
			file : ''
		}, 
		ofv || {}
	);

	o.flashvars = fv;
	
	var _self = $(this);
	
	var mediaplayer = $('#mediaplayerFlv', _self);
	var previewImg = '';
	
	_self.firstRun = true;
	
	function initMediaplayer(file, source)
	{

		var options = o;
		if (source == sources.direct)
		{		

			options.src = localPlayerSrc;

			options.flashvars.file = file;
			
			if (_self.firstRun == true && previewImg != '')
			{
				options.flashvars.image = previewImg;
				options.flashvars.autostart = false;
			}
			else 
			{
				options.flashvars.autostart = true;
			}
			
			if (options.hasAd && options.X1_ad_params != '')
			{
				options.flashvars.X1_ad_params = options.X1_ad_params;
				options.flashvars.X1_pg_id = window.X1_PgId;
				options.flashvars.X1_url =escape(document.URL);
				options.flashvars.X1_referrer = escape(document.referrer);
				options.flashvars.X1_disabled = false
			}


		}
		else
		{
			options.src = file;
			try
			{
				options.width = $('#widthTare').val() || options.width;
				options.height = $('#heightTare').val() || options.height;
			}
			catch (err)
			{
			}
		}

		mediaplayer.empty();
		mediaplayer.flash(options);
		
		_self.firstRun = false;
	}
	
	if (o.flashvars.file != '' && o.flashvars.file != undefined)
	{
		return initMediaplayer(o.flashvars.file, o.source);
	}
	
	var items = $('.mediaplayerItem, .mediaplayerThumb',_self);
	var itemsSrc = $('input.mediaplayerSrc', _self);
	
	var descriptions = $('.mediaplayerDescription', _self);
	var titles = $('input.mediaplayerTitle', _self);
	previewImg = $('input.mediaplayerPreviewImg', _self).eq(0).val();
	
	var titleContainer = $('#mediaplayerTitle', _self);
	var descriptionContainer = $('#mediaplayerDescription', _self);
	
	items.each(function(i) 
	{
		var item = $(this);
		item.click(function() {
			var _this = $(this);
			var title = titles.eq(i).val();
			var description = descriptions.eq(i).val();
			var file = itemsSrc.eq(i).val();

			titleContainer.html(title);
			descriptionContainer.html(description);
			if (_this.hasClass('mediaplayerTare'))
			{
				initMediaplayer(file, sources.tare);
			}
			else
			{
				initMediaplayer(file, sources.direct);
			}

			
			items.removeClass('selected');
			items.eq(i).addClass('selected');
			
			o.onChange(item);
		});
	});
	
	items.eq(0).click();
	
	_self.play = function(i, autostart, _previewImg)
	{
	
		_self.firstRun = !autostart;
		previewImg = _previewImg;
		return items.eq(i).click();
	}
	
	_self.getItemIndex = function(el)
	{
		return items.index(el);
	}
	
	return _self;
};

})(jQuery);