String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

var globalSettings = {
		resourceModalWidth:'700',
		resourceModalHeight:'600'
}
$(document).ready(function()
{
	$('#login-button').toggle(function()
	{
		$('#login-form').fadeIn();
		return false;
	}, function()
	{
		$('#login-form').fadeOut();
		return false;
	});

	$('body').click(function(e)
	{
		if ($('#login-form').is(':visible'))
		{
			if (!$(e.target).parents('#login-form').length && !$(e.target).is('#login-form')) $('#login-button').trigger('click');
		}
	});
	

		/*$('a.modalResource').colorbox({
			iframe: true,
			
			fixedWidth: globalSettings.resourceModalWidth,
			fixedHeight: globalSettings.resourceModalHeight
			});*/

});

var flower_bubble_instance = 0;

function globalJS_startLoading()
{
	if (!flower_bubble_instance)
	{
		$.getScript('/jscript/flower_bubble.js', function()
		{
			flower_bubble_instance = new flower_bubble(
			{
				base_obj: $('body'),
				base_dir: '/images',
				background:
				{
					css: 'white',
					opacity: 0.78
				},
				bubble:
				{
					image: 'bubble.png',
					width: 130,
					height: 98
				},
				flower:
				{
					image: 'flower.gif',
					width: 32,
					height: 32
				}
			});

			flower_bubble_instance.enable();
		});
	}
	else
	{
		flower_bubble_instance.enable();
	}
}

function globalJS_stopLoading()
{
	flower_bubble_instance.disable();
}

function globalJS_getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for (var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function globalJS_chooseResource(id)
{
}

function globalJs_wym_receiveInLineResourceIdAndAdd(ris_id, ris_data)
{
	$.fn.colorbox.close();
	var placeHolder = globalJs_createResourcePlaceHolder(ris_data);
	document.__wym.insert(placeHolder);

}

function globalJS_wym_addExistingInlineResource(wym)
{
	document.__wym = wym;
	globalJS_chooseResource = globalJs_wym_receiveInLineResourceIdAndAdd;
	$.fn.colorbox.settings.bgOpacity = '0.9';
	$('<a />').colorbox(
	{
		fixedWidth: '80%',
		fixedHeight: 550,
		iframe: true,
		href: "/webservice/resourceSelector.php?modalView=1",
		open: true
	});
	return false;
}

function globalJs_createResourcePlaceHolder(res_record)
{
	var resourceType = res_record.SX_resource_types_ID_resource_types_resourceType;
	var resourceId = res_record.ID_resources;
	var icon = "/images/resourceIcons/icon_" + resourceType + ".png"
	var linkText = res_record.title.replace('"', "'");
	var replacestr = "<img class=\"resourceHolder " + resourceType + " idResource_" + resourceId + "\"  src=\"" + icon + "\" alt=\"" + resourceType + " -- " + linkText + "\" title=\"" + resourceType + " -- " + linkText + "\" resourceId=\"" + resourceId + "\" resourceText=\"" + linkText + "\">";
	//alert(replacestr);
	return replacestr;
}

function globalJS_wym_addExternalImage(wym)
{
	document.__wym = wym;
	$('<a />').colorbox(
	{
		fixedWidth: '50%',
		fixedHeight: 420,
		iframe: true,
		href: "/webservice/addExternalImage.php",
		open: true
	});
	return false;

}

function globalJS_wym_addLink(wym)
{
	document.__wym = wym;
	$('<a />').colorbox(
	{
		fixedWidth: '50%',
		fixedHeight: 350,
		iframe: true,
		href: "/webservice/addLink.php",
		open: true
	});
	return false;

}

function globalJS_wym_addTrattoDa(wym)
{
	document.__wym = wym;
	$('<a />').colorbox(
	{
		fixedWidth: '50%',
		fixedHeight: 350,
		iframe: true,
		href: "/webservice/addTrattoDa.php",
		open: true
	});
	return false;
}

function globalJS_wym_addLinksUtili(wym)
{
	document.__wym = wym;
	$('<a />').colorbox(
	{
		fixedWidth: '50%',
		fixedHeight: 350,
		iframe: true,
		href: "/webservice/addLinksUtili.php",
		open: true
	});
	return false;
}

function globalJS_wym_addContentBox(wym)
{
	document.__wym = wym;
	$('<a />').colorbox(
	{
		fixedWidth: '50%',
		fixedHeight: 300,
		iframe: true,
		href: "/webservice/addContentBox.php",
		open: true
	});
	return false;
}

function globalJS_wym_addTVPlayer(wym)
{
	wym.insert( '<img alt="WEB TV PLAYER" src="/images/mogulus.png">' );

}
function globalJS_addModerationComment(label, url)
{
	$('<a title="' + label + '" />').colorbox(
	{
		inline: true,
		fixedWidth: '50%',
		fixedHeight: 350,
		href: "#moderationCommentForm",
		open: true
	});
	$('#moderationCommentForm').fadeIn();

	var old = $.fn.colorbox.close;
	$.fn.colorbox.close = function()
	{
		$('#moderationCommentForm').hide('fast', old);
	};

	$('#moderationCommentForm .cancel').click(function() {
		$.fn.colorbox.close();
		return false;
	});

	$('#moderationCommentForm form').attr('action', url).submit(function() {
		if (!$('#f_users_comment').val().length)
			return false;
		return true;
	});

	return false;
}

