// --------------------------------------------------
//
// Carousel
//
// USAGE: galleryVideos = new PP2.embedVideo(target, options);
//
// AUTHOR: MC
//
// DEPENDENCIES:
// jQuery 1.6 or later
//
// --------------------------------------------------

PP2.embedVideo = function(target, options){
	var base = this;

	base.options = {
		flashVersion: '10.0.0',
		videoAutoPlay: false,
		flashExpressIntall: false
	};

	if(typeof options == 'object'){
		$.extend(base.options, options);
	}

	base.elTarget = target;

	if (swfobject.hasFlashPlayerVersion(base.options.flashVersion)) {
		base.replaceElement();
	}
};

PP2.embedVideo.prototype = {
	replaceElement:function(){
		var base = this,
			flashTarget = $('<div />');

		for(var i=0; i<base.elTarget.length; i++){
			base.flashVideo = $(base.elTarget[i]).attr('data-video');
			base.flashSWF = $(base.elTarget[i]).attr('data-swf');
			base.flashBorder = $(base.elTarget[i]).attr('data-border');
			base.flashBorderPadding = $(base.elTarget[i]).attr('data-border-padding');
			base.flashHeight = $(base.elTarget[i]).attr('height');
			base.flashWidth = $(base.elTarget[i]).attr('width');
			base.flashVarHeight = base.flashHeight;
			base.flashVarWidth = base.flashWidth;
			base.flashVideoId = 'flash-' + $(base.elTarget[i]).attr('id');
			base.flashVideoPoster = $(base.elTarget[i]).attr('src');

			$('#' + base.flashVideoId).remove();

			flashTarget.attr('id', base.flashVideoId);
			base.elTarget.hide();
			base.elTarget.after(flashTarget);

			if (base.flashBorderPadding) {
				base.flashWidth = (base.flashBorderPadding.split(',')[0] * 2) + parseInt(base.flashWidth);
				base.flashHeight = (base.flashBorderPadding.split(',')[1] * 2) + parseInt(base.flashHeight);
			}

			base.embedSWF();
		}
	},

	embedSWF:function(){
		var base = this,
			settings = {
				id: base.flashVideoId,
				path: base.flashSWF,
				height: base.flashHeight,
				width: base.flashWidth,
				version: base.options.flashVersion,
				expressInstall: base.options.flashExpressIntall
			},
			flashvars = {
				videoPath: base.flashVideo,
				posterPath: base.flashVideoPoster,
				videoHeight: base.flashVarHeight,
				videoWidth: base.flashVarWidth,
				autoPlay: base.options.videoAutoPlay,
				borderPath: base.flashBorder,
				borderPoint: base.flashBorderPadding,
				videoId: base.flashVideoId,
				volume: '.5',
				callback: 'PP2.flashReady'
			},
			params = {
				bgcolor: '#000000',
				allowscriptaccess: 'always',
				wmode: 'transparent',
				allowFullscreen: 'true'
			},
			attributes = {};

		swfobject.embedSWF(settings.path, settings.id, settings.width, settings.height, settings.version, settings.expressInstall, flashvars, params, attributes);
	}
};
