var ShareUtils = function() {
	
// INI: Private properties
	var _mailRegExp      = new RegExp(/^([\w\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
	var _shareLayerId 	 = "share";
	var _shareLayer 	 = null;
	var _sendMailFormId  = "sendMailForm";
	var _sendMailForm    = null;
	var _inputAddressId  = "addShareAddressInput";
	var _inputAddress    = null;
	var _addMailButtonId = "addMail";
	var _addMailButton   = null;
	var _contactListId   = "contactsList";
	var _contactList     = null;
	var _that            = this;
	var _delContactClass = ".removeShareContactList";
	var _importServiceId = "#mail_link a";
	var _importService   = null;
// END: Private properties

// INI: Private methods
	function _setupEvents() {
		if (_sendMailForm) {
			jQuery(_sendMailForm).submit(_sendMail);
		}
		if (_addMailButton) {
			jQuery(_addMailButton).click(_addMail);
		}
		if (_inputAddress) {
			jQuery(_inputAddress).keyup(_inputKeyUp);
		}
		try {
			_importService.onclick = function(){
			
				if (jQuery(this).parent().hasClass("select")) {
					jQuery(this).parent().removeClass("select");
					jQuery('#mail_container').addClass('hide');
				}
				else {
					jQuery(this).parent().addClass("select");
					jQuery('#mail_container').removeClass('hide');
				}
				return false;
			};
		} catch(e) {
			
		}
		
		jQuery("#inputWithUrlToCopy").mouseup(function(){
			jQuery(this).select();
		});
		
		jQuery('#shareFormSubmit').click(_sendMail);
	}
	
	function _sendMail(e) {
		if (jQuery(_sendMailForm).find("input[name='destinationEmails']").length === 0) {
			alert("Debes introducir una direccion de correo de destino como mínimo");
		}
		else {
			jQuery.ajax({
				url: jQuery(_sendMailForm).attr("action"),
				type: 'post',
				data: jQuery(_sendMailForm).serialize(),
				dataType: 'xml',
				success: function(xml){
					var error = jQuery('errorCode', xml).text();
					var desc = jQuery('errorMessage', xml).text();
					if (error == "0") {
						if(dodot_lang=="es"){
						    alert("Gracias!");
						}else {
							alert("Obrigado!");
						}
						jQuery("#ModalElementBackground").click();
						// INI: STATS
						wtMeta.tracker("WT.cg_n", "Compartir", "WT.cg_s", "Mail", "WT.aa_event", jQuery("title").text());
						// END: STATS
					}
					else {
						alert("A ocurrido un error:\n" + desc);
						jQuery("#ModalElementBackground").click();
					}
				}
			});
		}
		return false;
	} 
	
	this.addMail = function(val) {
		if ( jQuery(_sendMailForm).find("input[name='destinationEmails'][value="+val+"]").length === 0 ) {
				
			var input = document.createElement("input");
			var li    = document.createElement("li");
			var span  = document.createElement("span");
			var a     = document.createElement("a");
			
			input.type  = "hidden";
			input.name  = "destinationEmails";
			input.value = val;
			_sendMailForm.appendChild(input);
			
			a.href = "#";
			a.className = "removeShareContactList";
			a.innerHTML = "Eliminar";
			li.innerHTML = val;
			
			span.appendChild(a);
			li.appendChild(span);
			
			_contactList.appendChild(li);
			
			jQuery(_delContactClass).unbind("click").click(function(){
			    var li = jQuery(this).parents('li');
			    jQuery(this).remove();
			    var mail = li.text();
			    li.remove();
			    jQuery(_sendMailForm).find("input[value="+mail+"]").remove();
			});
		}
	};
	
	function _addMail(e) {
		_inputAddress.value = jQuery.trim( _inputAddress.value );
		var val =  _inputAddress.value;
		if ( _mailRegExp.test( val) ) {
			_that.addMail(val);	
		}
		else {
			alert("invalid email");
		}
		return false;
	} 
	function _inputKeyUp(e) {
		if (e.keyCode == 13) {
			_addMailButton.click();
		}
	}
// END: Private methods

// INI: Constructor
    
	(function(){
    	// Set main 
		jQuery.root = jQuery.root ? jQuery.root : jQuery(document);
		
		_shareLayer    = document.getElementById(_shareLayerId);
		_shareLayer    = document.getElementById(_shareLayerId);
		_sendMailForm  = document.getElementById(_sendMailFormId);
		_inputAddress  = document.getElementById(_inputAddressId);
		_addMailButton = document.getElementById(_addMailButtonId);
		_contactList   = document.getElementById(_contactListId);
		try {
			_importService = jQuery(_importServiceId).get(0);
		} catch(e) {
			
		}
		_setupEvents();
		
    }());
// END: Constructor

	
	
	
// END: Constructor
};

