
var flashPlacer = (function(){
	var flashList = [],
	flashElm = {},
	configParam = {
		'menu': 'false',
		'wmode': 'transparent'
	},
	configVars = {
		
	},
	selector;

	function addToElm(){
		$(selector).each(function(i){
			if(flashList[i]){
				window['flashobj'+i] = new flashplace(flashList[i], this, 'flashobj'+i);
			}
		});
	}

	function create(src){
		var flashObj = {
			src: src,
			pos: flashList.length,
			elm: $(selector+':eq('+flashList.length+')'),
			flashParams: $.extend({}, configParam),
			flashVars: $.extend({}, configVars),
			height: 318,
			width: 509
		};

		flashList.push(flashObj);
		return flashObj;
	}
	function init(select){
		selector = select;
	}
	jQuery(function(){
		if(selector){
			addToElm();
		}
		ready = true;
	});
	return {
		create: create,
		init: init
	};
})();

var flashplace = function(obj, elm, globRef){
	
	this.data = {
		flvSource: obj.src,
		flvURL: obj.flashVars.flvURL,
		imageURL: obj.flashVars.imageURL,
		embed: obj.embed === undefined ? false : obj.embed
	};
	this.elm = elm;
	if(obj.elm.parents('#main').length) {
		obj.flashVars.mediaTitle = obj.elm.parents('#wrapper-content').find('h1:first').text();
	} else { // in marginal
		obj.flashVars.mediaTitle = obj.elm.parents('div.teaser').find('h3').text();
	}
	var so = new SWFObject(this.data.flvSource, "flvPlayer", obj.width, obj.height, "8", "#FFFFFF");
	so.addParam("menu", "false");
	so.addParam("wmode", "transparent");
	so.addVariable("flvURL", this.data.flvURL);
	so.addVariable("imageURL", this.data.imageURL);
	so.addVariable("buffertime", "5");
	so.addVariable("mediaTitle", obj.flashVars.mediaTitle);
	so.addVariable("mediaGroup", "");

	so.addVariable("lightBoxCloseFunction", globRef+".closeLightBox");
	so.addVariable("lightBoxOpenFunction", globRef+".openLightBox");
	so.addVariable("playerIsInLightBox", "false");
	
	if (this.data.embed) {
		so.addVariable("embededCode", flashplace.createEmbed(obj.src, {
			flvURL: obj.flashVars.flvURL,
			imageURL: obj.flashVars.imageURL
		}));
	}
	so.write(elm);


};
flashplace.createEmbed = function(src, flashVars){
	var embedFlashVars = [];
		$.each(flashVars, function(prop, value){
			embedFlashVars.push(prop+'='+ encodeURIComponent(location.protocol+location.host+value));
		});
		var flashTemplate = '<object width="509" height="318"><param name="movie" value="'+location.protocol+location.host+src+'"></param>';
		flashTemplate += '<param name="wmode" value="transparent"></param><param name="flashvars" value="{flashVars}"></param>';
		flashTemplate += '<embed src="'+src+'" type="application/x-shockwave-flash" flashvars="{flashVars}" wmode="transparent" width="509" height="318"></embed></object>';
		return escape(flashTemplate.replace(/{flashVars}/g, embedFlashVars.join('&')));
};
flashplace.prototype = {

		closeLightBox: function () {
			Shadowbox.close();
		},
		openLightBox: function (){
   			var flvWidth = 1018;
   			var flvHeight = 636;
			
   			var href = this.data.flvSource+'?flvURL='+this.data.flvURL+'&amp;imageURL='+this.data.imageURL+'&amp;playerIsInLightBox=true';
			var a = document.createElement('a');
			a.setAttribute('href',href);
			a.setAttribute('rel','shadowbox');
			var next = $(this.elm).next();
			if(!next.is('.details')){
				next = next.next();
			}
			if(next.is('.details')){
				a.setAttribute('title',  next.text());
			}
			setTimeout(function(){
  				Shadowbox.open(a,
   				{
			    	width:flvWidth,
   					height:flvHeight
   				});
			}, 100);
		}

};

flashPlacer.init('div.flashcontent');

