/**
Name: Javeline - envero.org's internal JS framework
Version: 0.3
**/

// FUNCTIONS & MODULES
javeline = function() {

	// propiedades generales
  	var UID = new Number(0);

	// funciones
  	function centerOnViewport( element ) {

		$('html').css('height', '100%');
		$('html').css('width', '100%');

		// tomo la altura de los elementos
		viewPortHeight = $('html').innerHeight();
		viewPortWidth = $('html').innerWidth();

		loginHeight = element.innerHeight();
		loginWidth = element.innerWidth();

		newLoginTop = (viewPortHeight - loginHeight) / 2;
		newLoginLeft = (viewPortWidth - loginWidth) / 2;

		// setea la nueva posicion
		element.css('position', 'absolute');
		element.css('top', newLoginTop);
		element.css('left', newLoginLeft);

	}

	function equalHeights( theClasses ) {
		// console.log('igualando alturas...' );

		if ( theClasses == '' || theClasses == undefined ) { theClasses = 'equal-height'; }
			//console.log( theClasses );
			//console.log( $('.' + theClasses) );

		actualHeight = 0;

		// recorre los elementos y determina el de mayor altura
		$('.' + theClasses).each( function() {
				//console.log( $(this).attr('id') + ' ' + $(this).outerHeight(true) );

			// determina la mayor altura de elementos
			if ( actualHeight < $(this).outerHeight(true) ) { actualHeight = $(this).outerHeight(true); }
				//console.log( actualHeight );
		});

		// aplica la mayor altura a todos los elementos
		$('.' + theClasses).height( actualHeight );

	}

	function rememberInputText( element ) {
		// guardo el valor original
		element.data('originalText', element.val() );
		// si al tomar el foco el texto es el original lo limpio
		element.focus( function() {
			if ( element.val() == element.data('originalText') ) { element.val(''); }
		});
		// si al perder el foco no se ha escrito nada, vuelve el valor original
		element.blur( function() {
			if ( element.val() == '' ) { element.val( element.data('originalText') ); }
		});

	}

	function styleAllButtons() {
		$('input[type=submit].button').each(function(){
			UID++;
			var btn = $(this);
			var btnForm = btn.parents('form'); //console.log( btnForm );
			fgColor = '';
			bgColor = '';
			// creo un array con las clases del botón para aplicarlos luego como propiedades
			btnClasses = btn.attr('class').split(' ');
			jQuery.each( btnClasses, function() {
				if ( this.substr(0,6) == 'front-' ) {
					fgColor = ' ' + this.substr(6);
				}
				if ( this.substr(0,4) == 'bkg-' ) {
					bgColor = ' btn-bkg-' + this.substr(4);
				}
			});

			btn.css('display', 'none');
			btn.before('<a href="#btn_'+UID+'" class="btn' + fgColor + bgColor + '" id="btn_'+UID+'" name="btn_'+UID+'"><span>'+btn.attr('value')+'</span></a>');
		  	$('#btn_'+UID).bind('click', function(){ btn.closest('form').submit(); return false; });
		});
	}

  return {
	// esto es para que los métodos puedan ser accedidos desde afuera del nameSpace
	centerOnViewport:centerOnViewport,
	equalHeights:equalHeights,
	rememberInputText:rememberInputText,
	styleAllButtons:styleAllButtons
  }

}();
