// JavaScript Document
window.onload = function(){
	var imgs = document.images;
	precargadas = true;
	for (var i = 0, total = imgs.length; i < total; i ++)
	precargadas = (precargadas && imgs[i].complete);
		if(precargadas){
			$(".body").fadeIn("slow");
	}
		
		
		//Cargo los Parámtros de la pagina Actual
		var myURL = parseURL(document.location.href );
		title = str_replace("-"," ", substr(myURL.file,0,-5));
		$(document).attr("title", title);
		
		//cargamos menu
		$(".menu ul li a").each(function(){
			if($(this).attr("href")==myURL.file){
				$(this).addClass("selected");
			}
		});	
	
	
	if($('.body div.img img')!=null || $('.body_right div img')!="undefined" ){
		$('.body div.img img').hover(function() { //mouse in
			$(this).animate({"opacity": 0.7}, "fast");
		}, function() { //mouse out
			$(this).animate({"opacity": 1.0}, "fast");
		});
	}
    $("form").validate();
		
	$(".scroll").click(function(event){
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 500);
	});  
}



function parseURL(url) {
	/*myURL.file;     // = 'index.html'
	myURL.hash;     // = 'top'
	myURL.host;     // = 'abc.com'
	myURL.query;    // = '?id=255&m=hello'
	myURL.params;   // = Object = { id: 255, m: hello }
	myURL.path;     // = '/dir/index.html'
	myURL.segments; // = Array = ['dir', 'index.html']
	myURL.port;     // = '8080'
	myURL.protocol; // = 'http'
	myURL.surce; //http://www.misite.com*/
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tp:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}

function SendContacto(){
	$.ajax({
		url: "sections/mail.php",
		type: "POST",
		data: "nombre=" + $('#nombre').val() + "&email=" + $('#email').val() + "&msj="+ $('#msj').val(),
		error: function(objeto, quepaso, otroobj){
			alert(quepaso)
		},
		success: function(){
			$("#confirmation").removeClass("AdvError");
			$("#confirmation").addClass("AdvSuccess");
			$("#confirmation").html("Su mensaje a sido enviado con &eacute;xito!");
			$("#confirmation").fadeIn("slow");
			} 
	});
}

function Login(){
	$.ajax({
		url: "sections/clientLogin.php",
		type: "POST",
		data: "user=" + $('#user').val() + "&pass=" + $('#pass').val(),
		beforeSend : function(){
			$(".loading").animate({"opacity": 0.95}, "fast");
			$(".loading div").css({"opacity": "1.0"});
			$(".loading").fadeIn("slow");
			$(".loadingMesg").html("Enviando Solicitud de Acceso...");
			$(".loadingMesg").fadeIn("slow");
			$("#confirmation").fadeOut("slow");
			
		},
		error: function(objeto, quepaso, otroobj){
			alert(quepaso)
		},
		success: function(error){
			if(error){
				alert(error);
				$(".loadingMesg").fadeOut("slow");
				$(".loading").fadeOut("slow");
				$("#confirmation").removeClass("AdvSuccess");
				$("#confirmation").addClass("AdvError");
				$("#confirmation").html("Se ha producido un error  \" No existe el Usuario \",  por favor, intentelo nuevamente");
				$("#confirmation").fadeIn("slow");	
			}
			else{
				$(".loading").fadeIn("slow");
				$(".loadingMesg").html("Solicitud de Acceso aceptada, redireccionando...<br/><br/> Si su navegador no lo redirecciona, haga click <a href='Area-Clientes.html'>AQUI</a>");
				$(".loadingMesg").fadeIn("slow");
			}
			} 
	});
}
