//NAMESPACES
INDAGANDA = {};
INDAGANDA.general = {};
INDAGANDA.modules = {};
INDAGANDA.contact = {};
INDAGANDA.services = {};
INDAGANDA.news = {};
INDAGANDA.bios = {};
INDAGANDA.trans = {};
INDAGANDA.quotes = {};
INDAGANDA.keywords = {};
INDAGANDA.texts = {};

//READY
$(document).ready(function(){
	$("#input_user_name").bind("keydown", function(e) {
		if (e.keyCode == 13) {
			INDAGANDA.general.login();
		}
	});
	$("#input_password").bind("keydown", function(e) {
		if (e.keyCode == 13) {
			INDAGANDA.general.login();
		}
	});
	$("input, li.validated").blur(function() {
		 $(this).validate.init(this);
	});
	$("textarea, li.validated").blur(function() {
		 $(this).validate.init(this);
	});
	$(".form li").not(".exempt").mouseover(function() {
		$(this).addClass("selected");
	});
	$(".form li").not(".exempt").mouseout(function() {
		$(this).removeClass("selected");
	});
	if($('#countdown').size()) {
		countdown = setInterval("countdown_timer()", 1000);
	}
});

countdown_timer = function() {
	var actual_time = parseInt($('#countdown').text(),10);
	actual_time--;
	$('#countdown').text(actual_time);
	if(actual_time == 0) {
		clearInterval(countdown);
		var result = confirm('¿Quieres visitar nuestro proyecto-estrella www.indagando.tv?');
		if(result) {
			window.location.href = 'http://www.indagando.tv/';
		}
	}
};

//FUNCTIONS
INDAGANDA.general.msg = function (obj) {
	$("#mensaje").fadeIn("fast");
	$("#mensaje_caja").hide();
	if(obj.src) {
		$.ajax({
			type: "GET",
			url: obj.src,
			success: function(data){
				$("#mensaje_cont").html(data)
			},
			error: function(data) {
				alert('error from msg:' + data);
			}
		});
	} else {
		$("#mensaje_cont").html(obj.html);
	}
	$("#mensaje_caja").show("slow");
	
	if(obj.time > 0) {
		setTimeout('$("#mensaje").fadeOut("slow")', obj.time);
	}
	if(obj.width) {
		$("#mensaje_caja").attr("width", obj.width+"px" );
	} else {
		$("#mensaje_caja").attr("width", "300px" );
	}
	if(obj.opacity) {
		$("#mensaje_caja").attr("style", "opacity:" + obj.opacity + ";" );
		//if(window.console) {console.log("here");}
	}
}

INDAGANDA.general.trim_left = function (value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

INDAGANDA.general.trim_right = function (value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

INDAGANDA.general.trim = function (value) {
	return INDAGANDA.general.trim_left(INDAGANDA.general.trim_right(value));
}
INDAGANDA.general.urlize = function (o) {
	$(".urlize").val(strip_accents(o.value.toLowerCase()));
}
INDAGANDA.general.strip_accents = function(text) {
	text = text.replace(/[áàäâå]/g, 'a');
	text = text.replace(/[éèëê]/g, 'e');
	text = text.replace(/[íìïî]/g, 'i');
	text = text.replace(/[óòöô]/g, 'o');
	text = text.replace(/[úùüû]/g, 'u');
	text = text.replace(/[ýÿ]/g, 'y');
	text = text.replace(/[ñ]/g, 'n');
	text = text.replace(/[ç]/g, 'c');
	text = text.replace(/['"]/g, '');
	text = text.replace(/[^a-zA-Z0-9-]/g, ' ');
	text = text.replace(/\s+/g, '_');
	text = text.replace(/(_)$/g, '');
	text = text.replace(/^(_)/g, '');
	return text;
};

INDAGANDA.general.login = function () {
	if(INDAGANDA.general.trim($("#input_user_name").attr("value")) == "" || INDAGANDA.general.trim($("#input_password").attr("value")) == "") {
		INDAGANDA.general.msg({html:"<b class='aviso'>! Por favor rellena ambos<br /> nombre de usuario y password</b>", time:3500});
	} else {
		//post details to motors/do_login.php and msg the return data
		dataStr = "action=log_in&user_name=" + $("#input_user_name").attr("value") + "&password=" + $("#input_password").attr("value");
		if($("#recordarme:checked").length > 0) {
			dataStr +=  "&remember=true";
		}
		$.ajax({
			type: "POST",
			url: "/motors/do_login.php",
			data: dataStr,
			success: function(data){
				INDAGANDA.general.msg({time:3000, html:data });
			},
			error: function(data) {
				alert('error:' + data);
			}
		});
	}
};

INDAGANDA.general.logout = function () {
	$.ajax({
		type: "POST",
		url: "/motors/do_login.php",
		data: "action=log_out",
		success: function(data){
			INDAGANDA.general.msg({time:3000, html:data });
		}
	});
};

INDAGANDA.general.close_message = function () {
	$("#mensaje").fadeOut("fast");
	if($.browser.msie) $("object").show();
};

INDAGANDA.contact.send = function() {
	if($("img[@src=/imagenes/error.gif]").length > 0) {
		INDAGANDA.general.msg({html:"<span class='aviso'>Por favor - corregir los fallos!</span>", time:2500})
	} else {
		if($("#username").attr("value") == "" || $("#email").attr("value") == "" || $("#mensajeA").attr("value") == "") {
			INDAGANDA.general.msg({html:"<span class='aviso'>Por favor - rellena los campos!</span>", time:2500});
		} else {
			INDAGANDA.general.msg({html:"Enviando data... espere por favor."});
			dataStr = $("#contact_form").serialize();
			$.ajax({
				type: "POST",
				url: "/motors/enviar_correo.php",
				data: dataStr,
				success: function(data){
					setTimeout("INDAGANDA.general.msg(" + data + ")", 3000);
				},
				error: function(data) {
					alert('error:' + data);
				}
			});
		}
	}
};

INDAGANDA.general.translate = function(el) {
	if(el.value !== "") {
		window.open("http://translate.google.com/translate?client=tmpg&hl=en&u=" + escape(window.location) + el.value,"translation","status=1,toolbar=1" );
	};
};

//VALIDACION PARA FORMULARIO DE CONTACTO
$.fn.copyTo = function(to) {
	var to = $(to);
	for ( var i = 1; i < arguments.length; i++ )
		to.set( arguments[i], this.get(0)[ arguments[i] ] );
	return this;
};

new function() {
	$.fn.validate = {
		init: function(o) {
			if(o.name == 'username') { this.username(o); };
			if(o.name == 'password') { this.password(o); };
			if(o.name == 'email') { this.email(o); };
			if(o.name == 'mensajeA') { this.mensajeA(o); };
			if(o.name == 'dob') { this.dob(o); };
		},
		username: function(o) {
			var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
			if (!o.value.match(user)) {
				//doValidate(o);
				if(o.value !=="") {
					doSuccess(o);
				} else {
					doError(o,'No puede ser vacio');
				}
			} else {
				doError(o,'No permite c&aacute;racteres especiales');
			};
		},
		password: function(o) {
			var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
			if (!o.value.match(pass)) {
				//doValidate(o);
				doSuccess(o);
			} else {
				doError(o,'No permite c&aacute;racteres especiales');
			};
		},
		email: function(o) {
			var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (o.value.match(email)) {
				doSuccess(o);
			} else {
				doError(o,'Correo no v&aacute;lido');
			};
		},
		mensajeA: function(o) {
			if($(o).attr("value") !=="") {
				doSuccess(o);
			} else {
				doError(o,'No puede ser vacio');
			};
		},
		dob: function(o) {
			var dob  = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
			if (o.value.match(dob)) {
				doSuccess(o);
			} else {
				doError(o,'Fecha no valido');
			};
		}
	};

	function doSuccess(o) {
		$('#' + o.id + '_img').html('<img src="/imagenes/accept.gif" border="0" style="float:left;" />');
		$('#' + o.id + '_li').removeClass("error");
		$('#' + o.id + '_msg').html("");
		$('#' + o.id + '_li').addClass("success");
	}

	function doError(o,m) {
		$('#' + o.id + '_img').html('<img src="/imagenes/error.gif" border="0" style="float:left;" />');
		$('#' + o.id + '_li').addClass("error");
		$('#' + o.id + '_msg').html(m);
		$('#' + o.id + '_li').removeClass("success");
	}

};
$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="/imagenes/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
		$('#' + this.get(0).id + '_img').html('<img src="/imagenes/accept.gif" border="0" style="float:left;" />');
		$(m.error).removeClass("error");
		$(m.error).addClass("success");
		$('#' + this.get(0).id + '_msg').html("");
	} else {
		$('#' + this.get(0).id + '_img').html('<img src="/imagenes/error.gif" border="0" style="float:left;" />');
		$(m.error).addClass("error");
		$(m.error).removeClass("success");
		$('#' + this.get(0).id + '_msg').html("The passwords don't match, please try again");
	};
};

INDAGANDA.general.playVideoDefinir = function(src) {
	var str = '<object width="360" height="280">';
	str += '<param name="movie" value="'+src+'"></param>';
	str += '<param name="allowFullScreen" value="true"></param>';
	str += '<param name="allowscriptaccess" value="always"></param>';
	str += '<embed id="vidsrc2" src="'+src+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="280"></embed></object>';
		
	$("#videoDefinir").html(str);
	//$("#vidsrc1").attr("value", src);
	//$("#vidsrc2").attr("src", src);
}



