function sethoverfx(id) {
	$('#' + id + '_box').hover(
		function () {
			$('#' + this.id + ' .controls').animate({ marginTop: '0' }, 500);
		},
		function () {
			$('#' + this.id + ' .controls').animate({ marginTop: '20px' }, 500);
		}
	);
}

function flowplayer_insert(id, src, w, h, streaming) {
	var flashvars = {
			allowscriptaccess: 'always',
			cachebusting: true,
			src: src,
			wmode: 'opaque'
		},
		config = {
			clip: {
				autoPlay: true, 
				autoBuffering: true
			},
			plugins: {
				controls: null  // disable default controls
			},
			onBeforeLoad: function() {
			},
			onLoad: function() {  // called when player has finished loading
				this.setVolume(100);  // set volume property
				$('#' + this.id() + '_api').css('width', w + 'px').css('height', h + 'px');  // set width & height of video
			},
			onBegin: function () {
				// make play button (re)appear
				this.getPlugin("play").css({opacity: 1});
			}, 
			onStart: function (e) {
				if (!this.resumed) {
					this.resumed = 0;
					sethoverfx(this.id());
				};
				this.resumed ++;
			},
			onResume: function (e) {
				// show controls on hover
				if (!this.resumed) {
					this.resumed = 0;
					sethoverfx(this.id());
				};
				this.resumed ++;
			},
			onBeforeFinish: function () { 
				// hide play again button
				this.getPlugin("play").css({opacity: 0});
			},
			onFinish: function (e) {
				$('#' + this.id() + '_box .pause').removeClass('pause').addClass('play');
				var id = this.id();
				setTimeout(function () {
					$('#' + id + '_controls .progress').css('width', ($('#' + id + '_box').width() - $('#' + id + '_controls .time').width() - 76 - 4) + 'px');
				}, 50);
				$('#' + this.id() + '_controls').animate({ marginTop: '0' }, 500);
				$('#' + this.id() + '_box').unbind('mouseenter');
				$('#' + this.id() + '_box').unbind('mouseleave');
				this.resumed = 0;
			}
		};
	
	if (streaming) {
		config.clip.provider = 'lighttpd';
		config.plugins.lighttpd = { 
			url: 'flowplayer.pseudostreaming-3.1.3.swf'
		}
	}
	
	// insert flowplayer into container
	var player = $f(id, flashvars, config).controls(id + '_controls');  // install HTML controls
	
	$('#' + id + '_box').css('width', w + 'px').css('height', h + 'px');  // set width & height of container
	$('#' + id + '_controls_box').css('width', w + 'px').css('top', (h - 20) + 'px');  // set width & top of controls container
	$('#' + id + '_controls').css('width', (w - 10) + 'px');  // set width of controls
	
	// adjust track, progress and buffer bar width
	var track_width = $('#' + id + '_box').width() - $('#' + id + '_controls .time').width() - 76;
	$('#' + id + '_controls .track').css('width', track_width + 'px');
	$('#' + id + '_controls .progress, #' + id + '_controls .buffer').css({
		'width': (track_width - 4) + 'px',
		'max-width': (track_width - 4) + 'px'
	});
}

$(window).load(function () {
	var c = [],
		n = 0,
		streaming_types = {
			'f4v': 1,
			'flv': 1
		},
		types = ['f4v', 'flv', 'm4v', 'mp4'],
		urlprefix = location.protocol + '//' + location.hostname + '/files/';
	
	// select elements
	for (var i = 0; i < types.length; i++) {
		var uc_types = [types[i].toUpperCase(), types[i]];
		for (var j = 0; j < uc_types.length; j++) {
			var filters = [':has(img)', '.video'];
			for (var k = 0; k < filters.length; k++) {
				c = $.merge(c, $('a[href$=.' + uc_types[j] + ']' + filters[k]).get());
			}
		}
	}
	c = $.unique(c); // important! we do not want the same object selected twice
	
	// setup flowplayer objects
	for (var i = 0; i < c.length; i++) {
		var href = c[i].href,
			id = 'video_' + n++,
			streaming = false,
			w = c[i].offsetWidth;
		c[i].style.display = 'block'; // Firefox 2
		c[i].style.display = 'inline-block'; // Firefox 2 ignores inline-block
		var h = c[i].offsetHeight;
		
		if (href.indexOf(urlprefix) === 0 && streaming_types[(href.toLowerCase().match(/\.([^\.]+$)/) || [])[1]]) {
			// setup streaming
			href = '/stream/' + href.substr(urlprefix.length);
			streaming = true;
		}
		
		// replace container
		$(c[i]).replaceWith('<span class="video_box" id="' + id + '_box"><a class="video" href="' + href + '" id="' + id + '"' + ($(c[i]).attr('title') ? ' title="' + $(c[i]).attr('title') + '"' : '') + '>' + $(c[i]).html() + '</a><span class="controls_box" id="' + id + '_controls_box"><span class="controls" id="' + id + '_controls"></span></span></span>');
		
		// insert flowplayer into container
		flowplayer_insert(id, '/player/flowplayer-3.1.5.swf', w, h, streaming);
	}
});

