/* Per skojar till det och gör en jQuery-plugin */ 

$.fn.hints = function() {
	
	$(this).focus(function() {
		var hint = $(this).attr('title');
		
		if ($(this).val() == hint)
		{
			$(this).val('');
			$(this).removeClass('hint-active');
		}
	});
	
	$(this).blur(function() {
		var hint = $(this).attr('title');
		if ($(this).val() == '')
		{
			$(this).val(hint);
			$(this).addClass('hint-active');
		}
		else
		{
			$(this).removeClass('hint-active');
		}
	});
	
	$(this).trigger('blur');
	
	
};

$.fn.fancy_radiobuttons = function() {
	
	var $_elements = $(this).find('input[type=checkbox],input[type=radio]');
	
	var func = function func($elem) {		
		if ($elem.is(':checked'))
		{
			$elem.parent().css({ opacity : 1.0 });
			$elem.parent().css({ 'font-weight' : 'bold' } );
		}
		else
		{
			$elem.parent().css({ opacity : 0.5 });
			$elem.parent().css({ 'font-weight' : 'normal' } );
		}
	};

	$_elements.each(function() {
		$(this).hide();
		func($(this));

	})
	.live('click', function() {			
		$_elements.each(function() {
			func($(this));
		});
	});	
};


$.fn.overlay = function(method) {
	
	method = method || {};
		
	if ( typeof(method) == 'object' )
	{
		className = method.className || 'jquery-overlay';
		
		var $div = $('<div class="jqo '+className+'"></div>');

		$(this).css('position', 'relative');

		$div.css({
			position: 'absolute',
			top: 0,
			right: 0,
			bottom: 0,
			left: 0		
		});

		$(this).append($div);
	}
	else if ( method == 'remove' )
	{
		
		$(this).find('.jqo').fadeOut(300, function() {
			$(this).remove();
		});
	}
};

(function($) {
	
	var ids = [],
		elements = {},
		timer = 0;
	
	var ajaxFunction = function() {				
		$.ajax({
			url: '/globals/getimages.php',
			type: 'POST',
			dataType: 'json',
			data: { images: ids.join(',') }, 
			success: function(data) {
				for (var i = 0; i < data.length; i++)
				{
					var affected = elements[data[i]],
						id = data[i];
					
					
					for (var j = 0; j < affected.length; j++)
					{
						$.fn.updateimages.func(affected[j], id);
					}
					ids.remove(id);
				}
				

				if (ids.length)
				{
					clearTimeout(timer);
					timer = setTimeout(function() {						
						ajaxFunction();
					}, 3000);			
				}
			},
			error: function(req,err) {
				if (EXIGUS.debug)
				{
					alert(req.responseText);
				}
			}
			});
	};
	
	$.fn.updateimages = function(options) {

		var $images = $(this).find('.generating');
				
		if ( ! $(this).data('updateimages') )
		{
			$(this).data('updateimages', true);
			$.fn.updateimages.func = options.func;
		}

		if ($images.length > 0)
		{
			for (var i = 0; i < $images.length; i++)
			{
				var match = $($images[i]).attr('class').match(/\{id:(.*?)\}/);
				
				if (match && match[1])
				{
					ids.pushUnique(match[1]);
					if (elements[match[1]] == undefined)
					{
						elements[match[1]] = [];
					}					
					elements[match[1]].push($images[i]);
				}
			}
			
			clearTimeout(timer);
			timer = setTimeout(function() {						
				ajaxFunction();
			}, 3000);
		}		
		return this;
	};
	
	
})(jQuery);



(function($) {
	
	$.fn.progress = function(options) {
		
		var $inner = $(this).find('.progress-bar-inner');
		
		$(this).addClass('progress-bar');
		
		if ( ! $inner.length )
		{						
			$(this).append('<div class="progress-bar-inner"></div>');
			$inner = $(this).find('.progress-bar-inner');
		}
		
		if ( options.value == 0 )
		{
			$inner.css({
				width: 0
			});
		}
		else
		{
			$inner.stop(false,false).animate({
				width: options.value + '%'
			}, 200);			
		}
		
		
	};
	
	
})(jQuery);




$(document).ready(function() {

/*	setTimeout(function() {
		updateImages();
	}, 3000);*/
	
});

