/**
 * 
 * @author jose
 */

jQuery.fn.inputBoxValue = function(defaultValue, options)
{
	settings = jQuery.extend({
		display: "inline",
		type: "text"
	  }, options);
	var firstBox = this.get(0);
	var secondBox = document.createElement("span");
	secondBox.innerHTML = defaultValue;
	secondBox.style.display = "none";
	var classAtt = document.createAttribute("class");
	classAtt.value = firstBox.getAttribute("class") ;
	secondBox.setAttributeNode(classAtt);
	if (firstBox.value.length == 0)
	{
		secondBox.style.display = settings.display;
		firstBox.style.display = "none";
	}
	$(secondBox).click(function()
			{
				firstBox.style.display = settings.display;
				this.style.display = "none";
				firstBox.focus();
			});
	$(firstBox).blur(function()
			{
				if(this.value.length == 0)
				{
					secondBox.style.display = settings.display;
					this.style.display = "none";
				}
			});
	if (firstBox.nextSibling)
		firstBox.parentNode.insertBefore(secondBox, firstBox.nextSibling);
	else
		firstBox.parentNode.appendChild(secondBox);
};

jQuery.fn.resizeImages = function(options)
{
	settings = jQuery.extend({
		width:	100,//200,
		height:	100,//200,
		center: false,
		referer: null,
		hideIfToSmall: true, // false
		force: false,
		display: "block", 
		clickable: false,
		border: 0,
		callback : null
	  }, options);
	
	return this.each(function()
			{
				var height = $(this).height() ? $(this).height() : this.height;
				var width = $(this).width() ? $(this).width() : this.width;
				//alert(width + " " + height);
				if ((width < settings.width || height < settings.height) && !settings.force)
				{
					if (settings.hideIfToSmall)
						this.style.display = "none";
					return (null);
				}
				var parentWidth = $(this).parent().width();
				var parentHeight = $(this).parent().height();
				if (settings.width == 0 && settings.height == 0)
					var size = ndNewsUI.computeOptimalListImageSize(this, parentWidth, parentHeight );
				else
					var size = ndNewsUI.computeOptimalListImageSize(this, settings.width, settings.height);
				if (size != null)
				{
					$(this).height(size.height);
					$(this).width(size.width);
					if (settings.center)
					{
						if (settings.referer == null)
							settings.referer = this.parentNode;
						if (settings.referer == 1)
							settings.referer = this.parentNode.parentNode;
						MediaItem.center(settings.referer, this, settings.center, {border: settings.border});
					}
					if (settings.clickable)
					{
						this.style.cursor = "pointer";
						$(this).click(function(){MediaItem.switchToFullscreen(this);});
					}
					//if no callback function is passed, try change css display & visibility property
					if (settings.callback == null)
					{
						if ($(this).css("display") == "none")
							$(this).css("display", settings.display);
						if ($(this).css("visibility") == "hidden")
							$(this).css("visibility", "visible");
						this.parentNode.style.backgroundImage="none";
					}
					else
						settings.callback();
				}
			});
};

jQuery.fn.imagesBoxes = function(options)
{
	settings = jQuery.extend({
		width:	140,
		height:	100,
		margin: '5px',
		display: "block",
		visibility:	"visible"
		}, options);
	
	return this.each(function()
			{
			$('.articlePicture').css('margin',settings.margin);
			$('.articlePicture').width(settings.width);
			$('.articlePicture').height(settings.height);
			$('.articlePicture').css('visibility',settings.visibility);
			});
};
