function videoPlayer(event, ui) {
	$f("a.player", root_url+"media/ade/flowplayer.commercial-3.0.7.swf", {  
		key: '@498e1400316cf51fcec',
		plugins: { 
				controls: { 
					// location of the plugin 
					url: root_url+'media/ade/flowplayer.controls-3.0.4.swf', 
					height: 30,
					// visible buttons
					all: false,
					volume: true,
					time: true,
					mute: true,
					play: true,
					scrubber: true,
					fullscreen: true,
					// player controls display properties such as size, location and opacity 
					autoHide: 'always', 
					hideDelay: 3500
				} 
			},		
			clip: { 
				baseUrl: '/media/ade/videos/', 
				// play M4V with Flash 9.115+ versions - otherwise play FLV 
				autoPlay:  	true,
				autoBuffering: 	true,
				bufferLength: 3,
				scaling: 'fit'
			}     
		}
	);
}


function videoDialog(video, title) {
	var id = "videoPlayer";
	if($('#'+id).length < 1) {
		$('body').append('<div id="'+id+'" style="display: none;"></div>');
	}
	$('#'+id).html('<a href="'+video+'" class="player" style="display:block;width:760px;height:550px;"></a>');
	$('#'+id).dialog({
		close: function() {
			$('#'+id).dialog('destroy');
			$('#'+id).html('');
		},
		height: 600,
		modal: true,
		open: videoPlayer,
		title: title,
		width: 800
	});
}

function inlineVideo($anchor) {
	
	var videoFile = $anchor.attr('href');
	$f($anchor.attr('id'), root_url+"media/ade/flowplayer.commercial-3.0.7.swf", {
		key: '@498e1400316cf51fcec',
		plugins: { 
			controls: { 
				// location of the plugin 
				url: root_url+'media/ade/flowplayer.controls-3.0.4.swf', 
				height: 30,
				// visible buttons
				all: false,
				volume: false,
				time: false,
				mute: true,
				play: true,
				scrubber: true,
				fullscreen: true,
				// player controls display properties such as size, location and opacity 
				autoHide: 'always', 
				hideDelay: 2000
			} 
		},
		clip: { 
			autoPlay:  	false,
			autoBuffering: 	false,
			bufferLength: 3,
			scaling: 'fit'
		},
		playlist: [
		       	{url: videoFile, autoPlay: true}
		]
	});
}

/**
 * Palaute
 */
FeedbackDialog = $.klass({
	initialize: function(obj) {
		this.obj = obj;
		this.form = $('#feedbackForm');
		this.obj.click($.proxy(this.clicked, this));
		$('#feedbackSend').click($.proxy(this.send, this));
	},
	clicked: function(e) {
		e.preventDefault();
		e.currentTarget.blur();
		this.show();
	},
	feedbackSentCallback: function(data) {
		$('#feedbackDone').html(data['response']).show();
		if(data['success'] == true) {
			var dialogDiv = this.dialogDiv;
			setTimeout("$('#feedbackDialog').dialog('close'); $('#feedbackSend').button('enable');", 3000);
		} else {
			$('#feedbackSend').button('enable');
		}
	},
	onClose: function(e) {
		// Blurrataan, että tooltip menee piiloon $('.tooltip').hide() <- no go, ei näytä uudelleen
		this.form.find('input, textarea').blur();
		$('#feedbackSend').button('enable');
		$('#feedbackDone').html('');
	},
	clearInputs: function() {
		$('#feedbackForm').find('input,textarea').each(function(){
			$(this).val('');
		});
	},
	send: function(e) {
		e.preventDefault();
		// estä tuplaklikkaus
		$('#feedbackSend').button('disable');
		
		$.post(this.form.attr('action'), {
			url: $('#feedbackUrl').val(),
			email: $('#feedbackEmail').val(),
			title: $('#feedbackTitle').val(),
			message: $('#feedbackMessage').val()
		}, $.proxy(this.feedbackSentCallback, this), "json");
	},
	show: function() {
		this.dialogDiv = $('#feedbackDialog');
		this.dialogDiv.dialog({
			resizable: true,
			height: 'auto',
			width: 500,
			minWidth: 400,
			modal: true,
			title: this.obj.attr('title'),
			close: $.proxy(this.onClose, this)
		});
		$('#feedbackEmail').focus().focus();
	}
});


$(function(){
	
	$('.scrollable').scrollable({
		circular: true
	});
	
	$('a.player').each(function(){
		inlineVideo($(this));
	});
	
	if($('a.demoreel').length) {
		$f("demoreel", root_url+"media/ade/flowplayer.commercial-3.0.7.swf", {  
			key: '@498e1400316cf51fcec',
			plugins: { 
				controls: { 
					// location of the plugin 
					url: root_url+'media/ade/flowplayer.controls-3.0.4.swf', 
					height: 30,
					// visible buttons
					all: false,
					volume: false,
					time: false,
					mute: true,
					play: true,
					scrubber: true,
					fullscreen: true,
					// player controls display properties such as size, location and opacity 
					autoHide: 'always', 
					hideDelay: 2000
				} 
			},
			clip: { 
				autoPlay:  	true,
				autoBuffering: 	false,
				bufferLength: 3,
				scaling: 'fit'
			},
			playlist: [
			       	{url: root_url+'media/ade/videos/demoreel2011.mp4'}
			]
		});
	}
	
	$('.nav li').hover(function(){
		$(this).find('ul').show();
	}, function(){
		$(this).find('ul').hide();
	});
	
	if($('a.fancy').length > 0) {
		$('a.fancy').fancybox({titlePosition: 'inside', titleShow: 'true'});
	}
	
	new FeedbackDialog($('#showFeedbackForm'));
	
});
