/*
 * litvideo Trailer-DB v2.4
 * http://litvideo.de
 *
 *
 * Datum: 2011-06-18
 */

window.addEvent('domready', function() {
	LitVideo1.checkMobile();
  $$("div[id*='trailer_ean_']").each(function (obj) {
		LitVideo1.load(obj.id);
	});
});

var LitVideo1 = {
	trailer: [],
	mobile: false,

	load: function(id) {
		var trailer_div = $(id);
		var width = trailer_div.getSize().x;
		if (width==0) width=250;
		var height =  width * 0.5625;
		var flashheight = height + 20;
		if (id) {
			var tiles = id.split("_");

			if (tiles.length == 3 || tiles.length == 4) {
				var ean = tiles[2];
				var sid = tiles.length == 4 ? tiles[3] : "";
				var req = new Request.JSONP ({
					url: "http://litvideoserver.de/trailerdatenbank/trailer.devel.php",
					data: {ean: ean, sid: sid},
					log:true,
					onComplete: function (data) {
						var root_url = "http://litvideoserver.de/trailerdatenbank";
						var video_url = "http://litvideoserver.de/trailer/"+sid;

						if (data.filename && (ean == data.ean)) {
							var filename = data.filename;
							var id = data.id;

							var evtId = LitVideo1.trailer.length;
							LitVideo1.trailer[evtId] = video_url + "/" + ean + "/posterpic/" + id + "-" + filename + ".jpg;";

							var html = "<!-- GEMA-HINWEIS: Ansprechpartner fuer die GEMA-Abrechnung des eingebetteten Werbevideos ist die LitVideo GmbH, Hamburg -->\r";

							if (LitVideo1.mobile) {
								html = '<video id="litvideo_'+evtId+'" class="video-js" width="'+width+'" height="'+height+'" controls="controls" poster="'+video_url+'/'+ean+'/posterpic/'+id+'-'+filename+'.jpg" preload="none">' +
									'<source src="'+video_url+'/'+ean+'/mp4/'+id+'-'+filename+'.mp4" type=\'video/mp4; codecs="H.264, avc1.42E01E, mp4a.40.2"\'/>' +
									'<source src="'+video_url+'/'+ean+'/ogv/'+id+'-'+filename+'.ogg" type=\'video/ogg; codecs="theora, vorbis"\'/>' +
									'</video>';
							} else {
								html += '<object id="player" name="player" width="'+width+'" height="'+flashheight+'"> ' +
									'<param name="movie" value="'+root_url+'/flashplayer/player_flv_maxi.swf"/> ' +
									'<param name="flashvars" value="flv='+video_url+'/'+ean+'/mp4/'+id+'-'+filename+'.mp4&startimage='+video_url+'/'+ean+'/posterpic/'+id+'-'+filename+'.jpg&configxml='+root_url+'/flashplayer/config.xml&evtId='+evtId+'"/> ' +
									'<param name="configxml" value="'+root_url+'/flashplayer/config.xml"/>' +
									'<param name="allowscriptaccess" value="always"/>' +
									'<embed' +
									' type="application/x-shockwave-flash"' +
									' id="player2"' +
									' name="player2"' +
									' src="'+root_url+'/flashplayer/player_flv_maxi.swf"' +
									' width="'+width+'"' +
									' height="'+flashheight+'"' +
									' allowfullscreen="true"' +
									' allowscriptaccess="always"' +
									' configxml="'+root_url+'/flashplayer/config.xml"' +
									' flashvars="flv='+video_url+'/'+ean+'/mp4/'+id+'-'+filename+'.mp4&startimage='+video_url+'/'+ean+'/posterpic/'+id+'-'+filename+'.jpg&configxml='+root_url+'/flashplayer/config.xml&evtId='+evtId+'"' +
									'/>' +
									'</object>';
							}
							trailer_div.set('html',html);
							trailer_div.show();
							$("video.video-js").bind("pause", LitVideo1.stop);
							$("video.video-js").bind("ended", LitVideo1.stop);
						}
					}
				});
				alert('send');
				req.send();
			}
		}
	},

	stop: function(evt) {
		var idx = parseInt(evt.currentTarget.id.replace(/litvideo_/, ""));
		var pos = Math.round(evt.currentTarget.currentTime / evt.currentTarget.duration * 100);

		$(evt.currentTarget).unbind("pause ended");
		LitVideo1.submitStop(idx, pos);
	},

	submitStop: function(idx, pos) {
		if ((idx >= 0) && (pos >= 0)) {
			if (LitVideo1.trailer[idx]) {
				$.get(LitVideo1.trailer[idx] + pos);
				LitVideo1.trailer[idx] = null;
			}
		}
	},

	checkMobile: function() {
		LitVideo1.mobile = (navigator.userAgent.toLowerCase().search(/(iphone|ipod|ipad|series60|symbian|android|windows ce|blackberry|palm|mobile|maemo|nokia|ericsson|sagem|fennec)/) > -1);
	}
};

