var bayerjardin =
{
	namespace : function(name)
    {
	    if ( !name || !name.length )
	    {
			return null;
	    }
	
	    var current = window;
	    var names = name.split(".");
	    for ( var i = 0 ; i < names.length ; i++ )
	    {
	        current[names[i]] = current[names[i]] || {};
	        current = current[names[i]];
	    }
	
	    return current;
    },
    
    toElement : function(e)
    {
	    if ( typeof e == "string" )
	    {
			return document.getElementById(e);
	    }

		return e;
    },

	preloadImage : function(src)
	{
		new Image().src = src;
	},
	
	checkFields : function(form, fieldNames)
	{
		form = EL(form);
		var invalidFields = new Array();
		
		for ( var i = 0 ; i < fieldNames.length ; i++ )
		{
			var f = form[fieldNames[i]];
			if ( f.type == 'select-one' )
			{
				if ( !f.options[f.selectedIndex].value.trim() )
				{
					invalidFields.push(f);
				}
			}
			else if ( !f.value.trim() )
			{
				invalidFields.push(f);
			}
		}
		
		if ( invalidFields.length > 0 )
		{
			var msg = 'Les champs suivants sont obligatoires : \n';
			for ( var i = 0 ; i < invalidFields.length ; i++ ) 
			{
				msg += '\t' + invalidFields[i].title + '\n';
			}
			
			alert(msg);
			return false;
		}
		
		return true;
	},
	
	toggleBlocUsage : function(bloc, id)
	{
		bloc = EL(bloc);
		if ( !bloc )
		{
			return;
		}
		
		var classes = bloc.className.split(' ');
		bloc.className = classes[0] + " " + classes[1];
		if ( classes.length < 3 )
		{
			bloc.className += " open";
			id.className = "bt_bloc_close";
		}
		else {
			id.className = "bt_bloc";
		}
	}
};

var EL = bayerjardin.toElement;

String.prototype.trim = function()
{
	return this.replace(/^(\s*)([\W\w]*)(\b\s*$)/, '$2');
};

bayerjardin.namespace('bayerjardin.diapo');
bayerjardin.diapo = 
{
	images 	: new Array(),
	index	: 0,
	
	next : function()
	{
		if ( this.index < (this.images.length - 1) )
		{
			EL('diapoImage').src = this.images[++this.index];
		}
		
		this.toggleControlers();
	},
	
	previous : function()
	{
		if ( this.index > 0 )
		{
			EL('diapoImage').src = this.images[--this.index];
		}
		
		this.toggleControlers();
	},
	
	add : function(src)
	{
		this.images.push(src);
		bayerjardin.preloadImage(src);
	}, 
	
	toggleControlers : function()
	{
		if ( this.index == 0 )
		{
			EL('diapoPreviousControler').style.display = 'none';
		}
		else
		{
			EL('diapoPreviousControler').style.display = 'block';
		}
		
		if ( this.index == (this.images.length - 1) )
		{
			EL('diapoNextControler').style.display = 'none';
		}
		else
		{
			EL('diapoNextControler').style.display = 'block';
		}
	}
};