/*
 * Absolute position helper
 *
 * Retrieves the absolute (page relative) position and size of any element.
 *	
*/
jQuery.fn.rect = function() {
	o = jQuery(this).get(0);
	width = $(o).width();
	height = $(o).height();
	
	var left = o.offsetLeft || 0;
	var top = o.offsetTop || 0;

	while(o = o.offsetParent) {
		left += o.offsetLeft
		top += o.offsetTop
	}
	
	return { x: left, y: top, w: width, h: height };
};

function inRect(x, y, rect)
{
	if((x > rect.x && x < (rect.x + rect.w)) && (y > rect.y && y < (rect.y + rect.h)))
		return true;
	else
		return false;
}


/*
 * jQuery regex selector
 *
 * Copyright James Padolsey
 *	
*/
jQuery.expr[':'].regex = function(elem, index, match) {
	var matchParams = match[3].split(','),
		validLabels = /^(data|css):/,
		attr = {
			method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr',
			property: matchParams.shift().replace(validLabels,'')
		},
		regexFlags = 'ig';
		regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
	return regex.test(jQuery(elem)[attr.method](attr.property));
};



/*
 * IE opacity fix
 *
 * Fixes the opacity of elements for IE7 & IE6?
 *	
*/
jQuery.fn.fix_opacity = function() {

	return this.each(function(){
		var opacity = $(this).css('opacity');
		//alert($(this).attr('class') + ' ' + opacity);
		opacity = Math.round(opacity * 100);
		$(this).css({filter: 'alpha(opacity='+opacity+')'});
	});

};



/* IE PNG fix multiple filters */
(function ($) {
	if (!$) return;
	$.fn.extend({
		fixPNG: function(sizingMethod, forceBG) {
				if (!($.browser.msie)) return this;
				var emptyimg = "empty.gif"; //Path to empty 1x1px GIF goes here
				sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
				this.each(function() {
						var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
								imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
								src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
						this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
						if (isImg) this.src = emptyimg;
						else this.style.backgroundImage = "url(" + emptyimg + ")";
				});
				return this;
		}
	});
})(jQuery);



/*
 * IE relayout fix
 *
 * Forces relayout of an element (IE6/7)
 *	
*/
jQuery.fn.force_relayout = function() {

	return this.each(function(){
		$(this).width($(this).width()-1);
		$(this).width($(this).width()+1);
	});

};


/*
 * Sliders plugin
 *
 * Brings sliders to life. Takes criteria (criterion groups) selectors as a parameter.
*/
jQuery.fn.sliders = function(options) {
	var settings = jQuery.extend({
		delayIn: 200,
		delayOut: 500,
		delayFix: 150
	}, options);
	
	return this.each(function(){
		// Enumerate criterions	& plug event handlers
		$(this).children().each(function(){
		
			// Apply hover/out fading behavior to criteria
			$(this).hover(function(){
				if($(this).hasClass('criterion-disabled')) // Trigger only if criteria is disabled
				{
					if($(this).is(':animated'))
						$(this).stop().fadeTo(settings.delayIn, 0.85);
					else
						$(this).fadeTo(settings.delayIn, 0.85);
				}
			},
			function(){
				if($(this).hasClass('criterion-disabled')) // ...
				{
					if($(this).is(':animated'))
						$(this).stop().fadeTo(settings.delayOut, 0.55);
					else
						$(this).fadeTo(settings.delayOut, 0.55);
				}
			});
			
			// Handle mouse move/out/click
			$('.slider', this).mousemove(function(e){
				if($(this).parent().hasClass('criterion-disabled')) // Mouse moving in disabled criterion
				{
					var offset = closestOffset(e.pageX - $(this).rect().x - 6);
					var ghost = $(':regex(class, ghost)', this);
					var current = extractOffset(ghost.attr('class'));
					if(current != offset) {
						ghost.removeClass(ghost.attr('class')).addClass('ghost' + offset).fix_opacity();
						
					}
				}
				else // Mouse moving in enabled criterion
				{
					var offset = closestOffset(e.pageX - $(this).rect().x - 6);
					var ghost = $(':regex(class, ghost)', this);
					if(!ghost.is(':animated')) {
						ghost.removeClass(ghost.attr('class')).addClass('ghost' + offset).fix_opacity();
					}
				}
			}).mouseout(function(e){
				var ghost = $(':regex(class, ghost)', this);
				
				var rect = $(this).rect();
				//alert(rect.x + ', ' + (rect.x + rect.w) + ', ' + rect.y + ', ' + (rect.y + rect.h) + ' <- ' + e.pageX + ', ' + e.pageY);
				
				if(!inRect(e.pageX, e.pageY, $(this).rect()))
				{
					if(!ghost.is(':animated')) {
						//alert('yo');
						ghost.removeClass(ghost.attr('class')).addClass('ghost-hidden').fix_opacity();
						//ghost.removeClass(ghost.attr('class')).css({opacity: 0;}).fix_opacity().addClass('ghost-hidden');
					}
				}
				else
				{
					//alert('hum');
				}
			}).click(function(e){
				if($(this).parent().hasClass('criterion-disabled'))
				{
					// Enable criterion
					$(this).parent().removeClass('criterion-disabled').addClass('criterion').fix_opacity();
					$(this).parent().fadeTo(settings.delayFix, 1);
					
					// Remove ghost
					var ghost = $(':regex(class, ghost)', this);
					ghost.removeClass(ghost.attr('class')).addClass('ghost-hidden').fix_opacity();

					// Set regular cursor to the right position
					var cursor = $('.cursor-hidden', this);
					var offset = closestOffset(e.pageX - $(this).rect().x - 6);
					cursor.removeClass('cursor-hidden').addClass('cursor' + offset).fix_opacity();
					
					$('input', this)[0].value = offset;
					
					// Enable OK button
					
					//
					//
					//
					//alert($('#button-ok').attr('class'));
					//
					//
					//
					
					
					
					$('#button-ok').removeClass('disabled');
				}
				else
				{
					var offset = closestOffset(e.pageX - $(this).rect().x - 6);
					var current = $('input', this)[0].value;
					var ghost = $(':regex(class, ghost)', this);
					var cursor = $(':regex(class, cursor)', this);
					
					if(offset == current)
					{
						// Hide cursor and fade criterion to back
						cursor.removeClass(cursor.attr('class')).addClass('cursor-hidden').fix_opacity();
						$(this).parent().fadeTo(settings.delayFix, 0.85);
						$(this).parent().removeClass('criterion').addClass('criterion-disabled').fix_opacity();
						$('input', this)[0].value = 'x';
						
						// Disable OK button if no enabled cursor left
						if($('.criterion').length == 0)
							$('#button-ok').addClass('disabled');
					}
					else
					{
						// Morph ghost to cursor, cursor to ghost, then swap both and restore opacities
						ghost.fadeTo(settings.delayFix, 1);
						cursor.fadeTo(settings.delayFix, 0, function(){
							cursor.removeClass(cursor.attr('class')).addClass('cursor' + offset).css({ opacity: 1 }).fix_opacity();
							ghost.removeClass(ghost.attr('class')).addClass('ghost-hidden').css({ opacity: 0.55 }).fix_opacity();
						});

						$('input', this)[0].value = offset;
					}
				}
			});
			
			// Private helper method: find closest offset in slider			
			function closestOffset(x)
			{
				var offsets = [0, 45, 90, 135, 180];
				var mindist = 1000000;
				var minidx = 0;
				for(var i=0; i<offsets.length; i++) {
					dist = Math.abs(offsets[i] - x);
					if(dist < mindist) {
						mindist = dist;
						minidx = i;
					}
				}
				return minidx;
			}
			
			// Exract offset from class name
			function extractOffset(classe)
			{
				var value = -1;
				if(classe.substr(0, 6) == 'cursor')
					value = classe.substr(6);
				else if (classe.substr(0, 5) == 'ghost')
					value = classe.substr(5);

				return (value == '-hidden') ? -1 : value;
			}
		
		
		}); // end each children
	}); // end each element

};



/*
 * Readmore plugin
 *
 * 
*/
jQuery.fn.readmore = function(options) {
	var settings = jQuery.extend({
		delayUnroll: 2000,
		delayReroll: 500,
		textUnroll: 'Lire la suite',
		textReroll: 'Fermer',
		classUnroll: 'readmore',
		classReroll: 'readless',
		maxLines: 10,
		tolerance: 3
	}, options);
	
	return this.each(function(){
	
		var lineHeight = parseInt($(this).css('line-height'));

		// Clip text box to maxLine
		var totalLines = Math.ceil(parseInt($(this).css('height')) / lineHeight);
		$(this).css({ height: (settings.maxLines * lineHeight) });
		
		if(totalLines > (settings.maxLines + 3))
		{
			// Append read more link
			var textBox = this;
			var link = $('<a></a>').text(settings.textUnroll).addClass(settings.classUnroll).click(function(){
				if($(this).hasClass(settings.classUnroll)) // Unroll
				{
					$(this).addClass(settings.classReroll).removeClass(settings.classUnroll).text(settings.textReroll);
					$(textBox).animate({ height: (totalLines * lineHeight) }, settings.delayUnroll);
				}
				else // Reroll
				{
					$(this).addClass(settings.classUnroll).removeClass(settings.classReroll).text(settings.textUnroll);
					$(textBox).animate({ height: (settings.maxLines * lineHeight) }, settings.delayReroll);
				}
			});
			$(this).after(link);
		}
		
	});
};



/*
 * Front-end behaviors
 *
 * 
*/
jQuery.fn.front = function(options) {
	var settings = jQuery.extend({
		delayOpen: 800,
		delayClose: 400
	}, options);
	
	var results = [];
	var offset = 0;
	
	// Close related content panel
	$('#related').css({ height: 0, opacity: 0 });
	
	// Form buttons behaviors	
	$('#button-ok').click(function(){
	
		if($(this).hasClass('disabled'))
			return false;
			
		results = [];
		offset = 0;
		$('#button-prev').addClass('disabled').force_relayout();
		$('#button-next').addClass('disabled').force_relayout();
		
		$('#help').css({display: 'none'});
		$('#help-results').css({display: 'block'});
		
		$('#logo-client').css({display: 'none'});
		$('#results').css({display: 'none'});
		
		$('#spinner').css({display: 'block'});
		// Fix IE6/7 layout bug: force relayout
		$('#spinner').force_relayout();
		$('#spinner').spinner(true);
		
	
		var form = $('#search')[0];
		$.getJSON(form.action + '?' + $(form).serialize(), null, function(data){
		

			$('.criteria.left').animate({ left: '-20px' }, settings.delayOpen);
			$('.criteria.right').animate({ left: '20px' }, settings.delayOpen);
			$('#content').animate({ opacity: '0.25' }, settings.delayOpen);
			//$('#related').animate({ height: '187px'}, settings.delayOpen * 1.5).animate({ opacity: '1'}, settings.delayOpen * 1);
			
			results = data;

			setTimeout(function(){
				// Load results with AJAX
				
				var content = '';
				max_results = Math.min(3, results[results.primary].length);
				for(i=0; i<max_results; i++) {
					classes = '';
					if(i == 0)
						classes = 'first';
					if(i == (max_results - 1))
						classes = 'last';
						
						
					var h = 144;
					if(results.primary == 'zic')
						h = 91;
						
					var pad = 0;
					if(results.primary == 'zic')
						pad = 30;
					
					//content += '<li class="'+classes+'"><a href="'+ results[results.primary][i].href +'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i].title + '"><img src="'+results[results.primary][i].cover+'" width="91" height="' + h + '" style="margin-top: '+pad+'px;" /></a></li>';
					content += '<li class="'+classes+'"><a href="'+ results[results.primary][i].href +'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i].title + '"><img src="'+results[results.primary][i].cover3+'" width="'+results[results.primary][i]['width']+'" height="'+results[results.primary][i]['height']+'" style="margin-top: '+(Math.round(150 - results[results.primary][i]['height']) / 2)+'px;" /></a></li>';
				}
				$('#results').html(content);
				
				var hasRelated = false;
				
				var hasLeft = false;
				var hasRight = false;
				var hasCenter = false;

				
				// text
				if(results['text'] != undefined) {

					// Create ghost div to get text line count
					var w = parseInt($('#related-center').css('width'));
					var tmp = '<div id="related-ghost" style="width: '+w+'px; position: absolute; top:0px; left: 0px; opacity: 0;">'+results['text'][0].text+'</div>';
					$('body').append(tmp);

					var line_count = parseInt($('#related-ghost').height()) / parseInt($('#related-center').css('line-height'));
					$('#related-ghost').remove();

					$('#related-center').css({ height: line_count * parseInt($('#related-center').css('line-height')) });
					
					$('#related-center').html(results['text'][0].text);
					hasRelated = true;
					hasCenter = true;
				}
				else
					$('#related-center').empty();
				

				if(results.primary == 'book')
				{
					if(results['dvd'] != undefined) {
						//content = '<a href="' + results['dvd'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['dvd'][0].title + '" class="fifiche"><img src="' + results['dvd'][0]['cover'] + '" width="73" height="115"/></a>';
						content = '<a href="' + results['dvd'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['dvd'][0].title + '" class="fifiche"><img src="' + results['dvd'][0]['cover3'] + '" width="'+results['dvd'][0]['width']+'" height="'+results['dvd'][0]['height']+'" /></a>';
						content += '<a href="' + results['dvd'][0]['wok'] + '" class="link-cinewok">' + 'CineWok' + '</a>';
						hasRelated = true;
						hasLeft = true;
					}
					else
						content = '';
					$('#related-left').html(content);
					
					if(results['zic'] != undefined) {
						//content = '<a href="' + results['zic'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['zic'][0].title + '" class="fifiche"><img src="' + results['zic'][0]['cover'] + '" width="73" height="73"/></a>';
						content = '<a href="' + results['zic'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['zic'][0].title + '" class="fifiche"><img src="' + results['zic'][0]['cover3'] + '" width="'+results['zic'][0]['width']+'" height="'+results['zic'][0]['height']+'" /></a>';
						content += '<a href="' + results['zic'][0]['wok'] + '" class="link-musicwok">' + 'MusicWok' + '</a>';
						hasRelated = true;
						hasRight = true;
					}
					else
						content = '';
					$('#related-right').html(content);
				}
				else if(results.primary == 'dvd') {
					if(results['book'] != undefined)
					{
						//content = '<a href="' + results['book'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['book'][0].title + '" class="fifiche"><img src="' + results['book'][0]['cover'] + '" width="73" height="115"/></a>';
						content = '<a href="' + results['book'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['book'][0].title + '" class="fifiche"><img src="' + results['book'][0]['cover3'] + '" width="'+results['book'][0]['width']+'" height="'+results['book'][0]['height']+'" /></a>';
						content += '<a href="' + results['book'][0]['wok'] + '" class="link-bookwok">' + 'BookWok' + '</a>';
						hasRelated = true;
						hasLeft = true;
					}
					else
						content = '';
					$('#related-left').html(content);

					if(results['zic'] != undefined) {
						//content = '<a href="' + results['zic'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['zic'][0].title + '" class="fifiche"><img src="' + results['zic'][0]['cover'] + '" width="73" height="73"/></a>';
						content = '<a href="' + results['zic'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['zic'][0].title + '" class="fifiche"><img src="' + results['zic'][0]['cover3'] + '" width="'+results['zic'][0]['width']+'" height="'+results['zic'][0]['height']+'" /></a>';
						content += '<a href="' + results['zic'][0]['wok'] + '" class="link-musicwok">' + 'MusicWok' + '</a>';
						hasRelated = true;
						hasRight = true;
					}
					else
						content = '';
					$('#related-right').html(content);
				}
				else // zic
				{
					if(results['book'] != undefined) {
						//content = '<a href="' + results['book'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['book'][0].title + '" class="fifiche"><img src="' + results['book'][0]['cover'] + '" width="73" height="115"/></a>';
						content = '<a href="' + results['book'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['book'][0].title + '" class="fifiche"><img src="' + results['book'][0]['cover3'] + '" width="'+results['book'][0]['width']+'" height="'+results['book'][0]['height']+'" /></a>';
						content += '<a href="' + results['book'][0]['wok'] + '" class="link-bookwok">' + 'BookWok' + '</a>';
						hasRelated = true;
						hasLeft = true;
					}
					else
						content = '';
					$('#related-left').html(content);

					if(results['dvd'] != undefined) {
						//content = '<a href="' + results['dvd'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['dvd'][0].title + '" class="fifiche"><img src="' + results['dvd'][0]['cover'] + '" width="73" height="115"/></a>';
						content = '<a href="' + results['dvd'][0].href + '" title="Cliquez pour voir la fiche de ... ' + results['dvd'][0].title + '" class="fifiche"><img src="' + results['dvd'][0]['cover3'] + '" width="'+results['dvd'][0]['width']+'" height="'+results['dvd'][0]['height']+'" /></a>';
						content += '<a href="' + results['dvd'][0]['wok'] + '" class="link-cinewok">' + 'CineWok' + '</a>';
						hasRelated = true;
						hasRight = true;
					}
					else
						content = '';
					$('#related-right').html(content);
				}
				
				var max_height = 0;
				if(hasLeft)
					max_height = Math.max(max_height, parseInt($('#related-left').css('height')));
				if(hasRight)
					max_height = Math.max(max_height, parseInt($('#related-right').css('height')));
				if(hasCenter)
					max_height = Math.max(max_height, parseInt($('#related-center').css('height')));
				
				if(hasRelated)
					$('#related').animate({ height: max_height + 'px'}, settings.delayOpen * 1.5).animate({ opacity: '1'}, settings.delayOpen * 1);
				
				
				// Make covers clickable (trigger popup)
				$('#results li a, a.fifiche').click(function(){
					$('body').popup(true, this.href);
					return false;
				});
				
				
				$('#spinner').spinner(false);
				
							
				
				$('#help').css({ display: 'none' });
				$('#help-results').css({ display: 'block' });
				
				$('#logo-client').css({display: 'none'});
				$('#spinner').css({ display: 'none' });
				$('#results').css({ display: 'block' });
				
				$('#button-reset').removeClass('disabled');
				
				//$('#button-prev').removeClass('disabled');
				
				//alert(results[results.primary].length);
				
				if(results[results.primary].length > max_results)
					$('#button-next').removeClass('disabled').force_relayout();
	
			}, settings.delayOpen * 1.5);

		});
		
		return false;
	});

	
	$('#button-reset').click(function(){

		if($(this).hasClass('disabled'))
			return false;

		$('#related').animate({ opacity: '0'}, settings.delayClose * 1).animate({ height: '0px'}, settings.delayClose * 1.5);
		$('#content').animate({ opacity: '1' }, settings.delayClose);
		$('.criteria.left').animate({ left: '0px' }, settings.delayClose);
		$('.criteria.right').animate({ left: '0px' }, settings.delayClose);

		$(".criterion").each(function(){
			$(this).addClass('criterion-disabled').removeClass('criterion').fadeTo(settings.delayClose, 0.55);
			var cursor = $(':regex(class, cursor)', this);
			cursor.removeClass(cursor.attr('class')).addClass('cursor-hidden');
			$('input', this)[0].value = 'x';
			
		});

		setTimeout(function(){
			// Remove results
			$('#help').css({ display: 'block' });
			$('#help-results').css({ display: 'none' });
			
			$('#logo-client').css({display: 'block'});
			$('#spinner').css({ display: 'none' });
			$('#results').css({ display: 'none' });
			
			$('#button-reset').addClass('disabled');
			$('#button-ok').addClass('disabled');
			
			$('#button-prev').addClass('disabled').force_relayout();
			$('#button-next').addClass('disabled').force_relayout();
		}, settings.delayClose * 1.5);
		
		return false;
	});
	
	$('#button-prev').click(function(){
		if(!$(this).hasClass('disabled'))
		{
			offset -= 3;

			if(offset < 3)
				$('#button-prev').addClass('disabled').force_relayout();
			if(offset + 3 < results[results.primary].length)
				$('#button-next').removeClass('disabled').force_relayout();
	
			var content = '';
			max_results = Math.min(3, results[results.primary].length - offset);
			for(i=0; i<max_results; i++) {
				classes = '';
				if(i == 0)
					classes = 'first';
				if(i == (max_results - 1))
					classes = 'last';
					
					var h = 144;
					if(results.primary == 'zic')
						h = 91;

					var pad = 0;
					if(results.primary == 'zic')
						pad = 30;

					//content += '<li class="'+classes+'"><a href="'+results[results.primary][i+offset].href+'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover+'" width="91" height="' + h + '" style="margin-top: '+pad+'px;" /></a></li>';
					//content += '<li class="'+classes+'"><a href="'+results[results.primary][i+offset].href+'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover2+'" width="91" height="144" /></a></li>';
					content += '<li class="'+classes+'"><a href="'+ results[results.primary][i+offset].href +'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover3+'" width="'+results[results.primary][i+offset]['width']+'" height="'+results[results.primary][i+offset]['height']+'" style="margin-top: '+(Math.round(150 - results[results.primary][i+offset]['height']) / 2)+'px;" /></a></li>';
				}
			$('#results').html(content);
			// Make covers clickable (trigger popup				)
			$('#results li a').click(function(){
				$('body').popup(true, this.href);
				return false;
			});
			
		}
		
		return false;

	});
	$('#button-next').click(function(){
		if(!$(this).hasClass('disabled'))
		{
			
			offset += 3;
			
			if(offset >= 3)
				$('#button-prev').removeClass('disabled').force_relayout();
			if(offset + 3 >= results[results.primary].length)
				$('#button-next').addClass('disabled').force_relayout();
		
			var content = '';
			max_results = Math.min(3, results[results.primary].length - offset);
			for(i=0; i<max_results; i++) {
				classes = '';
				if(i == 0)
					classes = 'first';
				if(i == (max_results - 1))
					classes = 'last';

					var h = 144;
					if(results.primary == 'zic')
						h = 91;

					var pad = 0;
					if(results.primary == 'zic')
						pad = 30;

					//content += '<li class="'+classes+'"><a href="'+results[results.primary][i+offset].href+'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover+'" width="91" height="' + h + '" style="margin-top: '+pad+'px;" /></a></li>';
					//content += '<li class="'+classes+'"><a href="'+results[results.primary][i+offset].href+'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover2+'" width="91" height="144" /></a></li>';
					content += '<li class="'+classes+'"><a href="'+ results[results.primary][i+offset].href +'" title="Cliquez pour voir la fiche de ... ' + results[results.primary][i+offset].title + '"><img src="'+results[results.primary][i+offset].cover3+'" width="'+results[results.primary][i+offset]['width']+'" height="'+results[results.primary][i+offset]['height']+'" style="margin-top: '+(Math.round(150 - results[results.primary][i+offset]['height']) / 2)+'px;" /></a></li>';
			}
			$('#results').html(content);

			// Make covers clickable (trigger popup)
			$('#results li a').click(function(){
				$('body').popup(true, this.href);
				return false;
			});
		
		}

		return false;
	});
	
	
	$('#news .post .richtext').readmore({ maxLines : 4, tolerance: 2, delayUnroll: 1000 });
	
	$('#content').hover(function(){
		if($('#button-reset').length > 0 && !$('#button-reset').hasClass('disabled'))
		{
			if($(this).is(':animated'))
				$(this).stop().fadeTo(settings.delayIn, 1);
			else
				$(this).fadeTo(settings.delayIn, 1);
		}
	},
	function(){
		if($('#button-reset').length > 0 && !$('#button-reset').hasClass('disabled'))
		{
			if($(this).is(':animated'))
				$(this).stop().fadeTo(settings.delayOut, 0.5);
			else
				$(this).fadeTo(settings.delayOut, 0.5);
		}
	});

	/*
	// News fadein/fadeout
	$('#content').hover(function(){
		if($(this).is(':animated'))
			$(this).stop().fadeTo(delayIn, 1);
		else
			$(this).fadeTo(delayIn, 1);
	},
	function(){
		if($(this).is(':animated'))
			$(this).stop().fadeTo(delayOut, 0.25);
		else
			$(this).fadeTo(delayOut, 0.25);
	});
	*/

};





/*
 * Spinner plugin
 *
 * 
*/
jQuery.fn.spinner = function(active, options) {
	var settings = jQuery.extend({
		opacityLow: 0.2,
		opacityHigh: 0.7,
		delayIn: 150,
		delayOut: 400,
		factor: 2.5,
		delta: 300
	}, options);
	
	if(active) {
		return this.each(function(){
			fadeIn(this);
		});
	}
	else {
		return this.each(function(){
			$(this).stop();
			$(this).css({ opacity: 0.05 });
		});
	}
	
	function fadeIn(o)
	{
		$(o).fadeTo((settings.delayIn + (Math.random() * (settings.delta/2 - settings.delta))) * settings.factor, settings.opacityHigh, function(){
			fadeOut(o);
		});
	}
	
	function fadeOut(o)
	{
		$(o).fadeTo((settings.delayOut + (Math.random() * (settings.delta/2 - settings.delta))) * settings.factor, settings.opacityLow, function(){
			fadeIn(o);
		});
	}


}

jQuery.fn.popup = function(active, url, callback, options) {
	var settings = jQuery.extend({
		delayIn: 600,
		delayOut: 200
	}, options);
	
	if(active) {
		return this.each(function(){
		
			$('#popup-overlay').css({ opacity: 0, display: 'block' });
			$('#popup-overlay').fadeTo(settings.delayIn, 0.90, function(){
			
				$.get(url, function(data){
					$('#popup-content').html(data);
					$('#popup-content').css({ opacity: 0, display: 'block' });
					Cufon.refresh();
					$('#popup-content').fadeTo(300, 1);
					
					if(callback != undefined)
						callback();
					
					$('#popup-close').click(function(){
						$('body').popup(false);
						return false;
					});
				});
			});
		});
	}
	else {
		return this.each(function(){
			
			$('#popup-content').fadeTo(settings.delayOut / 2, 0, function(){
				$('#popup-content').empty();
				$('#popup-content').css({ display: 'none' });

				$('#popup-overlay').fadeTo(settings.delayOut / 2, 0, function(){
					$('#popup-overlay').css({ display: 'none' });
				});

			});

			
			/*
			$(this).stop();
			$(this).css({ opacity: 0.05 });
			*/
		});
	}

}
