// JavaScript Document
$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 560;
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');
  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
	if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }					   
	// Using multiple unit types within one animation.
	$("#reg").click(function(){
		if($("#block").css('top') != '-200px'){
			$("#block").animate({ 
			top: "0px" 
			}, 1500 );
			var cssObj = {
				'background-image' : 'url(img/back-form.png)'
			}
			$("#block").css(cssObj);	
		}
		var cssObj1 = {
				'display' : 'none'
		}
		var cssObj2 = {
				'display' : 'block'
		}
		$("#formLog").css(cssObj1);	
		$("#formReg").css(cssObj2);
	});
	
	$("#log").click(function(){
		if($("#block").css('top') != '-200px'){
			$("#block").animate({ 
			top: "0px" 
			}, 1500 );
			var cssObj = {
				'background-image' : 'url(img/back-form.png)'
			}
			$("#block").css(cssObj);	
		}
		var cssObj1 = {
				'display' : 'none'
		}
		var cssObj2 = {
				'display' : 'block'
		}
		$("#formReg").css(cssObj1);	
		$("#formLog").css(cssObj2);
	});
	$("#cancelarReg").click(function(){
		$("#block").animate({ 
			top: "-220px" 
			}, 1500 );
			var cssObj = {
				'background-image' : 'none'
			}
			$("#block").css(cssObj);	
		});	
	$("#cancelarLog").click(function(){
		$("#block").animate({ 
			top: "-220px" 
			}, 1500 );
			var cssObj = {
				'background-image' : 'none'
			}
			$("#block").css(cssObj);	
		});	
	$("#enviarReg").click(function(){
		var theForm= document.registro;
		var valida=true;
		if(theForm.regName.value == ''  || !isText50(theForm.regName)){
			theForm.regName.style.backgroundColor='#ededed';
			valida= false;
		}else{
			theForm.regName.style.backgroundColor='#cf7575';
		}
		
		if(theForm.regMail.value=='' || !isEmail(theForm.regMail)){
			theForm.regMail.style.backgroundColor='#ededed';
			valida= false;
		}else{
			theForm.regMail.style.backgroundColor='#cf7575';
		}
	
		if(theForm.regtel.value=='' || !isTel(theForm.regtel)){
			theForm.regtel.style.backgroundColor='#ededed';
			valida= false;
		}else{
			theForm.regtel.style.backgroundColor='#cf7575';
		}
	
		if(valida == true){
			$("#regError").fadeOut("slow");
			$("#formReg").fadeOut("slow",function (){$("#send").fadeIn("slow");} );
			setTimeout('sendRegistro()', 4000);
		}else{
			$("#regError").fadeIn("slow");  			
		}
	});	
	$("#mapImg").click(function(){
		$("#mapa").fadeIn("slow",function (){initialize();}); 
	});		
	$("#btCerrar").click(function(){
		$("#mapa").fadeOut("slow"); 
	});	
	$("#linkOferta").click(function(){
									
		$("#oferta").fadeIn("slow");
		//Obtengo tamaņo de la oferta
		var hOferta=document.getElementById("contenidoOferta").scrollHeight;
		//obtengo tamaņo del body
		var hBody=document.getElementsByTagName("body")[0].scrollHeight;
		document.getElementById("oferta").style.top=(hBody-hOferta)/2+'px';
	});	
	$("#btCerrarOferta").click(function(){
		 
	});	
	$("#enviarLog").click(function(){						   
		var theForm= document.ingreso;
		var valida=true;
		if(theForm.user_name.value == ''){
			theForm.user_name.style.backgroundColor='#ededed';
			valida= false;
		}else{
			theForm.user_name.style.backgroundColor='#cf7575';
		}
		
		if(theForm.password.value==''){
			theForm.password.style.backgroundColor='#ededed';
			valida= false;
		}else{
			theForm.password.style.backgroundColor='#cf7575';
		}
	
		if(valida == true){
			theForm.login.click();
			theForm.user_name.value="";
			theForm.password.value="";
			$("#block").animate({ 
				top: "-220px" 
				}, 1500 );
				var cssObj = {
					'background-image' : 'none'
				}
			$("#block").css(cssObj);
		}else{
			$("#logError").fadeIn("slow");  			
		}
	});	
	
});


function sendForm(theForm){
	var valida=true;
	if(theForm.name.value == ''  || !isText50(theForm.name)){
		theForm.name.style.backgroundColor='#fcf7cf';
		valida= false;
	}
	if(theForm.apellido.value=='' || !isText50(theForm.apellido)){
		theForm.apellido.style.backgroundColor='#fcf7cf';
		valida= false;
	}
	if(theForm.mail.value=='' || !isEmail(theForm.mail)){
		theForm.mail.style.backgroundColor='#fcf7cf';
		valida= false;
	}
	
	if(theForm.tel.value=='' || !isTel(theForm.tel)){
		theForm.tel.style.backgroundColor='#fcf7cf';
		valida= false;
	}
	
	if(theForm.comentario.value == ''){
		theForm.comentario.style.backgroundColor='#fcf7cf';
		valida= false;
	}
	if(valida == true){
		$("#error").fadeOut("slow");
		$("#infoForm").fadeOut("slow",function (){$("#ajax").fadeIn("slow");} );
		setTimeout('generateAjax()', 4000);
	}else{
		$("#defaultMsg").fadeOut("slow",function (){$("#error").fadeIn("slow");} );  			
	}
	return;
}

function generateAjax(){
	var theForm= document.contact;
	var peticion= $.ajax({
		url:'mail.php',
		type:'POST',
		async:false,
		data:'nombre='+theForm.name.value+'&apellido='+theForm.apellido.value+'&mail='+theForm.mail.value+'&tel='+theForm.tel.value+'&text='+theForm.comentario.value,
		dataType: 'response.text',
		success: function (respuesta){ response(respuesta);},
		error:function(){ alert('Se ha producido un error'); }
	});
}
function sendRegistro(){
	var theForm= document.registro;
	var peticion= $.ajax({
		url:'registro.php',
		type:'POST',
		async:false,
		data:'nombre='+theForm.regName.value+'&mail='+theForm.regMail.value+'&tel='+theForm.regtel.value,
		dataType: 'response.text',
		success: function (respuesta){responseReg(respuesta);},
		error:function(){ alert('Se ha producido un error'); }
	});
}
function response(rslt){
	if(rslt==''){
		var cssObj = {
				'padding' : '170px 0'
			}
		$("#ajax").css(cssObj);
		document.getElementById('ajax').innerHTML='<img src="img/ic-error.jpg" style="width:47px;height:48px;"/><br /><span>Ha surgido un error en la tranferencia de datos.<br>Por favor, intentelo nuevamente m&aacute;s tarde .</span>'
	}else{
		var cssObj = {
				'padding' : '160px 0'
			}
		$("#ajax").css(cssObj);
		document.getElementById('ajax').innerHTML='<img src="img/ic-ok.jpg" style="width:53px;height:53px;"/><br /><span>Sus datos han sidos tranferidos de manera correcta.<br>Muchas gracias por contactarse con nosotros.<br />Le responderemos a la brevedad.</span>'
	}
	return;
}
function responseReg(rslt){
	var cssObj = {
				'margin' : '0px'
			}
		$("#send").css(cssObj);	
	if(rslt==''){
		//error
		document.getElementById('send').innerHTML='<img src="img/err-reg.jpg" style="margin:60px auto 10px 80px;" /><br />Los datos no se han podido transferir...';
		setTimeout('cerrarBotones()', 2000);
	}else{
		document.getElementById('send').innerHTML='<img src="img/ok-reg.jpg" style="margin:60px auto 10px 80px;" /><br />Los datos se han transferido de manera correcta.';
		setTimeout('cerrarBotones()', 2000);
	}
	return;
}

function cerrarBotones(){
	
	$("#block").animate({ 
		top: "-220px" 
		}, 1500 );
	var cssObj = {
		'background-image' : 'none'
	}
	$("#block").css(cssObj);
	document.getElementById('send').style.display='none';
	document.registro.style.display='block';
	document.registro.regName.value='';
	document.registro.regMail.value='';
	document.registro.regtel.value='';
	
}
function initialize() {
	if (GBrowserIsCompatible()) {
		//http://maps.google.com.ar/maps?f=q&source=s_q&hl=es&geocode=&q=capitan+juan+de+san+martin+1484&sll=-38.341656,-63.28125&sspn=35.559855,79.013672&ie=UTF8&hq=&hnear=Capit%C3%A1n+Juan+de+San+Martin+1484,+Boulogne,+Buenos+Aires&z=16
		
		var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(-34.494828,-58.574492), 16);
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var icono = new GIcon();
		icono.image = 'img/ic-maps.png';
		icono.shadow = 'img/ic-mapsSombra.png';
		icono.size = new GSize(77,57);
		icono.shadowSize = new GSize(99,53);
		icono.iconAnchor = new GPoint(23,60);
		icono.infoWindowAnchor = new GPoint(30,60);
		var iconoPoint = new GLatLng(-34.494828,-58.574492);
		function createMarker(point,icon,textoHtml){
			markerOptions = { icon:icon };
			var marker = new GMarker(point, markerOptions);
			if(textoHtml){
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(textoHtml);
				});
			}
			return marker;
		}
		map.addOverlay(createMarker(iconoPoint,icono,"<strong>RGA | computaci&oacute;n</strong><br />Capit&aacute;n Juan de San Martin 1484. Boulogne<br />Buenos Aires. Argentina."));
		map.addControl(new GSmallMapControl());//Crea un control para hacer desplazamientos en cuatro direcciones
		//map.addControl(new GMapTypeControl());//Crea un control de tipo de mapa estandar
		//map.addControl(new GOverviewMapControl());//Crea una vista genreal de minimapa 
	}
}

