$(document).ready(function(){
	function NetPrVideo(me,config) {
		var v, _this = this;
		var id, videoId, videoTitle, videoTextTitle;
		var videoSrc = me.find("a[href!='']").eq(0).attr("href");
		
		if (videoSrc.indexOf("pkf/") > 0) {
			id = videoSrc.split("pkf/");
			id = id[1];
			id = id.split("/");
			id = id[0];
		} else if (videoSrc.indexOf("prf/") > 0) {
			id = videoSrc.split("prf/");
			id = id[1];
			id = id.split("/");
			id = id[0];
		} else {
			id = '';
		}
		
		videoId = videoSrc.split(id);
		videoId = videoId[1];
		videoId = videoId.split("/");
		videoId = videoId[1];
		
		videoTextTitle = me.find("a").filter(function(){
			return $(this).text().length!==0;
		});
		
		if (me.find("a[title!='']").length>0) {
			videoTitle = me.find("a[title!='']").eq(0).attr("title");
		} else if (videoTextTitle!=='') {
			if(typeof String.prototype.trim !== 'function') {
				String.prototype.trim = function() {
					return this.replace(/^\s+|\s+$/g, ''); 
				};
			}
			videoTitle = videoTextTitle.text().trim();
		} else {
			videoTitle = '';
		}
		
		v = {
			companyId: id,
			videoId: videoId,
			videoTitle: videoTitle,
			videoSrc: videoSrc,
			videoThumb: me.find("img[src!='']").eq(0).attr("src")
		};
		
		_this.showVideo = function(e) {
			e.preventDefault();
			
			function loadPlayer(e) {
				if (e.success===true) {
					$("#netPrOverlayVid").fadeTo(300,0.7,function(){
						$("#netPrPlayerVid").show();
					});
				}
			}
			
			if (swfobject.hasFlashPlayerVersion("9.0.124")) {
				var flashvars = {
					file: v.videoSrc,
					image: v.videoThumb,
					abouttext: v.videoTitle,
					aboutlink: "/",
					autostart: config.autostart,
					lightcolor: "#0099cb"
				};
				var params = {};
				var attributes = {};
				swfobject.embedSWF("http://biuroprasowe.netpr.pl/m/lib/jwplayer/5.6/player.swf", "netPrPlayerAlternate", "480", "384", "9.0.124", false, flashvars, params, attributes,loadPlayer);
				if(v.videoTitle!=='') {
					$("#netPrTitle").css({
						font: "11px Tahoma, Verdana",
						"border-top": "1px solid #e6e6e6",
						background: "#f0f0f0",
						padding: "6px 8px 8px 8px"
					}).html(v.videoTitle);
				}
			} else {
				alert("Nie masz wtyczki Flash");
			}
		};
		
		_this.closeVideo = function(e) {
			e.preventDefault();
			swfobject.removeSWF("netPrPlayerAlternate");
			$("#netPrPlayerVid").hide();
			$("#netPrPlayerHoldVid").html("<div id='netPrPlayerAlternate'></div>");
			$("#netPrTitleVid").attr("style","").html("");
			$("#netPrOverlayVid").fadeOut(300);
		};
		
		$(".overlayVid, #netPrCloseVid").click(_this.closeVideo);
	}
	
	
	function triggerVideos() {
		var config = {
			autostart: true
		};
		
		var color;
		
		if (typeof config.color!=="undefined") {
			color=config.color;
		} else {
			color="#000";
		}
		
		if($(".mp4File").length>0) {
			$("body").append("<div class='overlayVid' id='netPrOverlayVid'></div><div id='netPrPlayerVid'><div id='netPrPlayerWrapVid'><div id='netPrPlayerHoldVid'><div id='netPrPlayerAlternate'></div></div><div id='netPrTitleVid'></div></div><div id='netPrPanelVid'><div id='netPrPanelWrapVid'><a href='#' id='netPrCloseVid'></a></div></div></div>");
			$("#netPrOverlayVid").css({
				display:"none",
				position:"fixed",
				top:"0px",
				bottom:"0px",
				left:"0px",
				right:"0px",
				"z-index":"100",
				background: "#000"
			});
			
			$("#netPrPlayerWrapVid").css({
				border: "1px solid #222",
				background: "#fff"
			});
			
			$("#netPrPlayerHoldVid").css({
				padding: "4px"
			});
			
			$("#netPrPlayerVid").css({
				display:"none",
				border: "6px solid #3d3d3d",
				"border-radius": "5px",
				"z-index":"1000",
				margin: "-180px 0 0 -240px",
				position: "fixed",
				"float": "left",
				top: "50%",
				left: "50%"
			});
			
			$("#netPrPanelVid").css({
				"border-color": "#3d3d3d",
				"border-width": "6px 6px 6px 0",
				"border-style": "solid",
				"border-top-right-radius": "6px",
				"border-bottom-right-radius": "6px",
				position: "absolute",
				right: "-30px",
				top: "-6px",
				width: "25px"
			});
			
			$("#netPrPanelWrapVid").css({
				"border-color": "#222",
				"border-width": "1px 1px 1px 0",
				"border-style": "solid",
				background: "#fff"
			});
			
			$("#netPrCloseVid").css({
				background: color+" url(http://biuroprasowe.netpr.pl/m/lib/po/video/1.0/close.png) no-repeat",
				display: "block",
				height: "24px"
			});
			
		}
		
		$(".mp4File").each(function(){
			var thisVideo = new NetPrVideo($(this),config);
			$(this).find("td, .title").click(thisVideo.showVideo);
		});
	}
	
	/* czy jest swf object na stronie? */
	if (typeof swfobject!=="undefined") {
		triggerVideos();
	} else {
		var script= document.createElement('script');
		script.src= 'http://biuroprasowe.netpr.pl/m/lib/swfobject/swfobject.js';
		$("body").append(script);
		triggerVideos();
	}
	
});

