/**
 * @author pablo
 */
$.extend({
	stop: function(e){
		if (!e) e = window.event;
		if (e.preventDefault) e.preventDefault();
		if (e.stopPropagation) e.stopPropagation();
	}
});

$.generateId = function() {
	return arguments.callee.prefix + arguments.callee.count++;
};
$.generateId.prefix = 'jq$';
$.generateId.count = 0;

$.fn.generateId = function() {
	return this.each(function() {
	this.id = $.generateId();
	});
};

$.ajaxSetup({
	complete: function(request, settings){
		if( $('session_expired_bye',request.responseXML).size() == 1 ){
			window.location = $('session_expired_bye',this).text();
		}
	}
 });

function changeLanguage(language){
	$.ajax({
			type: 'POST',
			async: false,
			url: '/settings/system/updateLanguage',
			data: {lang:language},
			dataType: 'xml',
			success: function(responseXML){
				try{
					var result = $('result', responseXML).get(0);
					resultText = $(result).text();
					if(resultText == 'SUCCESS' ){
						window.window.location.reload( false );
					}else{
						error = $('error', responseXML).get(0);
						errorText = $(error).text(); 
						alert(errorText);
					}
				}catch(e){
					//unexpectederror
					
				}
			}
		});
}

$(document).ready(function(){
	//google.load("language","1");
	$('#googleSugestionButton').click(
		
		function(){
			enText = $("#i18nKEY").val();
			google.language.translate(enText, "en", "es",function(translated){$("#i18nTEXTSP").val(translated.translation);});
			google.language.translate(enText, "en", "pt",function(translated){$("#i18nTEXTPT").val(translated.translation);});
			
		});
	if(logedUsername == 'pablo'){
	$('i18n\\:text').mousedown(function(e) {
	var evt = e;
	$(this).mouseup(function(e) {
		var srcElement = $(this);
		$(this).unbind('mouseup');
	if (evt.button == 2 || evt.leftButton == true) {
	//$('i18n\\:text').dblclick(function(){
			var i18nID = $(this).attr('id');
			var i18nKEY = $(this).attr('key');
			var i18nTEXT =$(this).text();
			$("#i18nID").val(i18nID);
			$("#i18nKEY").val(i18nKEY);
			$("#i18nIDText").text(i18nID);
			$("#i18nKEYText").text(i18nKEY);
			if(i18nID !=''){
				$.ajax({
					async:false,
					url:'/system/translate/getActualTranslation',
					data:{i18nID:i18nID},
					type: "POST",
					success: function(responseXML){
						$("#i18nTEXTSP").val($('sp',responseXML).text());
						$("#i18nTEXTPT").val($('pt',responseXML).text());
					}
				});
			}else{
				$("#i18nTEXTSP").val(i18nTEXT);
				$("#i18nTEXTPT").val(i18nTEXT);
			}
			
			
			$("#i18n").dialog({
				resizable: false,
				width: 400,
				modal: true,
				overlay: {
					backgroundColor: '#000',
					opacity: 0.5
				},
				close: function(){$("#i18n").dialog("destroy");},
				buttons: {
					"Ok": function(){
						$(this.parentNode).css('display','none');
						var response = $.ajax({
							async:false,
							url:'/system/translate/translate',
							data:$('#i18nForm').serialize(),
							type: "POST",
							success: function(responseXML){
							try{
								var result = $('result', responseXML).get(0);
								resultText = $(result).text();
								if(resultText == 'SUCCESS' ){
									window.location.reload(true);
								}else{
									error = $('error', responseXML).get(0);
									errorText = $(error).text(); 
									alert(errorText);
								}
							}catch(e){
								//unexpectederror
							}
						},
						dataType:"xml"});
					$("#i18n").dialog("destroy");
				},
				"Cancel": function(){
					$(this).dialog("destroy");
				}
			}
		});
	}});});
	}
	$('#changeLanguageButton').click(function(event){$('#languageMenuDiv').toggle(); event.preventDefault();});
});

$.fn.addErrorMsg = function (errorMsg){
	if(typeof(errorMsg)== 'undefined' || errorMsg == '') return 
	if( $(this).next(".formFieldErrorMsg").size() != 1){
		$(this).after('<p class="formFieldErrorMsg" style="margin-top:0px;color:red; font-weight:bolder" ><img style="float:left" class="ui-icon" src="/images/icons/bullet_error.png"/>'+errorMsg+'</p>');
		
	}
};

$.fn.removeErrorMsg = function (){
	if ($(this).next(".formFieldErrorMsg").size() == 1) {
		$(this).next(".formFieldErrorMsg").remove();
	}
};

function validorInit(form){
	$(form).submit(function(e){
		var result;
		$('.required,.email,.number', this).each(function(){
			$(this).blur()
		});
		if($('.input-error', this).size() > 0){
			$.stop(e);
			return false;
		}
		return true;
	});
	
	$('.required,.email,.number,.alfaNumeric', form).blur(function(){
		$(this).removeClass('input-error');
		$(this).removeErrorMsg()
		
	});
	$('.required').bind('blur', function(e){
		if ($(this).val() == '') {
			$(this).addClass('input-error');
			$(this).addErrorMsg($(this).attr('errorMsg'));
			$.stop(e);
			return false;
		}
	});
	$('.email', form).bind('blur', function(e){
		if ($(this).val() != '') {
			if (!/^[A-Za-z0-9][A-Za-z0-9_.-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_]+[.A-za-z]+$/.test($(this).val())) {
				$(this).addClass('input-error');
				$(this).addErrorMsg($(this).attr('errorMsg'));
				$.stop(e);
				return false;
			}
		}
	});
	
	$('.password',form).bind('blur',function(e){
		if ($(this).val() != '') {
			if (!/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/.test($(this).val())) {
				$(this).addClass('input-error');
				$(this).addErrorMsg($(this).attr('errorMsg'));
				$.stop(e);
				return false;
			}
		}
	});
	
	$('.alfaNumeric').bind('blur',function(e){
		if ($(this).val() != '') {
			if (!/^[A-Za-z0-9\s-\.]*$/.test($(this).val())) {
				$(this).addClass('input-error');
				$(this).addErrorMsg($(this).attr('errorMsg'));
				$.stop(e);
				return false;
			}
		}
		
	});

	$('.number', form).bind('blur', function(e){
		if ($(this).val() != '') {
			if (isNaN($(this).val())) {
				$(this).addClass('input-error');
				$(this).addErrorMsg($(this).attr('errorMsg'));
				$.stop(e);
				return false;
			}
		}
	});
	
}

function confirmAction(title, content,confirmButtonText,confirmFunction){
	$("#confirmDialog").attr('title',title) ;
	$("#confirmDialog").append(content);
	var objButtons = {}
	objButtons[confirmButtonText] = function() {confirmFunction();$(this).dialog('close');$("#confirmDialog").empty();$(this).dialog( 'destroy');};
	objButtons[__('Cancel')] = function() {$(this).dialog('close');$("#confirmDialog").empty();$(this).dialog( 'destroy');};
	$("#confirmDialog").dialog({
		draggable:false,
		closeOnEscape:true,
		title:title,
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: objButtons
	});
}
