
window.addEvent('domready', function() {
	LitVideo.checkMobile();
  $$("a.litvideo").each(function (obj) {
		LitVideo.load(obj);
	});
});

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

	load: function(obj) {
	  var trailer_div=obj.getParent();
		var width = obj.getSize().x;
		if (width==0) width=250;
		var height =  width * 0.5625;
		var flashheight = height + 20;
		
		var mp4=obj.get('href');
		var pic=obj.getChildren()[0].get('src');		
		var ogg=mp4.substr(0,mp4.length-3)+'ogg';
		ogg=ogg.replace(/mp4/g, "ogv");
		//alert(ogg);
		var evtId = LitVideo1.trailer.length;
    LitVideo.trailer[evtId] = pic;

    var html = "<!-- GEMA-HINWEIS: Ansprechpartner fuer die GEMA-Abrechnung des eingebetteten Werbevideos ist die LitVideo GmbH, Hamburg -->\r";
    //var player='http://46.4.59.142/trailerdatenbank/flashplayer/player.swf';
    //var swfconf='http://46.4.59.142/trailerdatenbank/flashplayer/config.xml';
    var player='http://litvideoserver.de/trailerdatenbank/flashplayer/player_flv_maxi.swf';
    var swfconf='http://litvideoserver.de/trailerdatenbank/flashplayer/config.xml';
    
    if (LitVideo.mobile) {
      html += '<video id="litvideo_'+evtId+'" class="video-js" width="'+width+'" height="'+height+'" controls="controls" poster="'+pic+'" preload="none">'+
        '<source src="'+mp4+'" type=\'video/mp4; codecs="H.264, avc1.42E01E, mp4a.40.2"\'/>' +
        '<source src="'+ogg+'" type=\'video/ogg; codecs="theora, vorbis"\'/>' +
        '</video>';
    } else {
      html += '<object id="player" name="player" width="'+width+'" height="'+flashheight+'"> ' +
        '<param name="movie" value="'+player+'"/> ' +
        '<param name="flashvars" value="flv='+mp4+'&startimage='+pic+'&configxml='+swfconf+'&evtId='+evtId+'"/> ' +
        '<param name="configxml" value="'+swfconf+'"/>' +
        '<param name="allowscriptaccess" value="always"/>' +
        '<embed' +
        ' type="application/x-shockwave-flash"' +
        ' id="player2"' +
        ' name="player2"' +
        ' src="'+player+'"' +
        ' width="'+width+'"' +
        ' height="'+flashheight+'"' +
        ' allowfullscreen="true"' +
        ' allowscriptaccess="always"' +
        ' configxml="'+swfconf+'"' +
        ' flashvars="flv='+mp4+'&startimage='+pic+'&configxml='+swfconf+'&evtId='+evtId+'"' +
        '/>' +
        '</object>';
    }
    trailer_div.set('html',html);
    //trailer_div.show();
    //$("video.video-js").addEvent("pause", LitVideo.stop);
    //$("video.video-js").addEvent("ended", LitVideo.stop);
  },

	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");
		LitVideo.submitStop(idx, pos);
	},

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

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

