//jquery.elais.js
(function($) {
	var loglevels = [ 'info', 'log', 'debug', 'trace', 'warn' ],
		_getLogFunction = function(level) {
			if ('console' in window && level in console) {
				return console[level];
			}
			else return function() {};
		};
		
	for (i in loglevels) {
		var l = loglevels[i];
		$[l] = _getLogFunction(l);
	}

	$.fn.getObject = function() {
		var r = null;
		var o = this[0];
		if (o && o.nodeName == "OBJECT") {
			if ('SetVariable' in o) {
				r = o;
			} else {
				var n = this.children('object')[0];
				if (n) {
					r = n;
				}
			}
		}
		return r;
	};
})(jQuery)

$(function() {
	if ($.fn.pngFix)
		$(document).pngFix();

	// Load the fancybox stuff
	if ($.fn.fancybox) {
		$.info('Found the fancybox plugin');

		var simpleFancyOptions = {
			// 'zoomSpeedIn': 400,
			// 'zoomSpeedOut': 400,
			'overlayOpacity' : 0.4,
			'overlayShow' : true
		},

			$photogallery = $("a.photogallery");
			galleries = new Array();
			
		$photogallery.each(function() {
			var rel = this.rel;
			if (!(galleries[rel])) {
				galleries[rel] = new Object();
				var $g = $("a.photogallery[rel='" + rel + "']");
				$g.fancybox(simpleFancyOptions);
			}
		});
	}

	// Load the boxy stuff
	if ($.fn.boxy) {
		//$.info('Found the boxy plugin');
		$('.boxy').boxy( {
			modal : true,
			draggable : true
		});
	}

	// Make the some links to have the same effect as the submit buttons
	$("a[href='#submit']").click(function() {
		var $form;

		if (this.rel) {
			$form = $('#' + this.rel);
		}
	
		if (!($form) || $form.size() == 0) {
			$form = $(this).parents('form')
		}

		$form.submit();
		return false;
	});

	// The send to friend form submit stuff
	$('form#send2friend_form').submit(function() {
		var form = this,
			$form = $(form),
			url = $form.attr('action') + '?_r=' + Math.random(),
			postbody = $form.serialize();
		
		$.post(url, postbody, function(data) {
			var boxy = Boxy.get(form);
			if (boxy)
				boxy.toggle();
		});

		return false;
	});

	// Open print popup:
	$('a.print').click(function() {
		if (this.href) {
			$.log('Opening the print page (' + this.href + ') to a popup');

			var f = 'status=0,toolbar=0,location=0,menubar=0,directories=0';
			f += ',resizable=1,scrollbars=1';
			f += ',width=930';
			f += ',height=500';

			var printWindow = window.open(this.href, 'elaisprintwindow', f);
			printWindow.focus();
		}

		return false;
	});

	var player = $('#elais_video_player').getObject();
	if (player && player != null) {
		$.info('Found big video player: ' + player);

		var boxy = new Boxy($('#big_video_player'), {
			modal : true,
			closeText : '[x]',
			show : false,
			title : 'Video'
		}),

		playurl = function(url) {
			var index = url.indexOf('#video=');
			if (index >= 0)
				url = url.substring(index + 7);
			try {
				$.log('Trying to play video from url: ' + url);
				player.loadvideo(url);
			} catch (e) {
				$.warn(e + ', retring in 300ms...');
				setTimeout(function() {
					player.loadvideo(url);
				}, 300);
			}
		},

		$loadvideoBtns = $("a.loadvideo").click(function() {
			if (this.title)
				boxy.setTitle(this.title);

			boxy.toggle();
			playurl(this.rel);
			return false;
		});
		
		if (window.location.hash.toString().indexOf('playvideo') >= 0) {
			$.log('window.hash: ' + window.location.hash + ', Hafta start a video');
			//Click the button for loading the last video:
			$loadvideoBtns.eq($loadvideoBtns.size() - 1).click();
		}
	}
});