$(document).ready(carga)

function carga(){
	
	//$('body').fadeIn('slow');
	
	$(".tabs li").hover(rollOverImagenes, rollOutImagenes);
	$('body').fadeIn('slow')
	$(window).resize(calculaAlto);
	calculaAlto();

	
	$('#fichaObra img').click(function(){
		adonde= '#'+($(this).prev().html());
		$('#fotosObras').css('opacity','0')
		document.location=adonde;
		$('#fotosObras').animate({opacity:'1'},{duration: 500})
		})
		
	
	// PIE PARA IE Internet Explorer hace cagadas con los botones de abajo, entocnes hago el contenido del ul de nuevo
	// No direcciona a ningun lado xq no tiene link. Podría hacer que direcciones mandandolo a index.php?seccion=4
	// y en el index.php agregar una linea de GET[seccion] pero no vale la pena.
	
	// El hoverscroll de jquery no funciona en IE porque no toma los valores de ALTO que le asigna en la línea 448 
	// de jquery.hoverscroll.js. Por lo tanto, elimino este "chiche" para IE y le pongo un scroll común y silvestre
		
	if($.browser.msie){
		$('#listadoObras').parent().css('height', '370px')
		$('#listadoObras').parent().css('overflow', 'auto')
		$('#listadoObras').css('overflow', 'auto')
		$('#pie .tabs').removeClass('tabs');
		$('#pie .overs').css('margin-right','20px');
		$('#pie ul').html('<ul style="float:right; margin:5px;"><li><img src="img/imago_industrial_32x32.png" width="20" height="20" align="right" title="Coronato Noblía - Estudio de Diseño Industrial" /><img src="img/imago_arquitectura_32x32.png" width="20" height="20" align="right" title="Coronato Fazzini - Estudio de Arquitectura" /><img src="img/imago_herreria_32x32.png" width="20" height="20" align="right" title="Coronato&Cia S.A. - Herrería de obra" /></li>');
		
		
		} else {
			$('head').append('<script type="text/javascript" src="js/jquery.hoverscroll.js"></script>');
			}
			
		// los mapas de google se ven "centrados" distintos en chrome que en el resto FIX GOOGLE MAPS
		var userAgent = navigator.userAgent.toLowerCase(); 
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 

		//if(!$.browser.chrome){

			mapa = "http://maps.google.com/maps/ms?msa=0&amp;msid=204514969352773213805.0004a7326444efceea386&amp;ie=UTF8&amp;ll=-34.856000,-56.216460&amp;spn=0.006163,0.027938&amp;t=p&amp;z=15&amp;output=embed"
		//} else {
		//	mapa = "http://maps.google.com/maps/ms?msa=0&amp;msid=204514969352773213805.0004a7326444efceea386&amp;ie=UTF8&amp;ll=-34.853996,-56.229486&amp;spn=0.012326,0.018239;&amp;t=p&amp;z=15&amp;output=embed"
		//}
		
	    $('#googlemap').html('<iframe width="650" height="175" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src='+mapa+'></iframe><br />');

		
		
		
		// FIN FIX GOOGLE MAPS
	
    

	$('#pie img:eq(0)').hover(opacity100, opacity80)
	$('#pie img:eq(0)').animate({opacity:'0.3'},{duration: 500})
	$('#pie img:eq(1)').hover(opacity100, opacity80)
	$('#pie img:eq(1)').animate({opacity:'0.3'},{duration: 500})
	$('#pie img:eq(2)').load(opacity100);

	vacia();

// Envio y validacion del formulario
$("form").submit(function() {

	valida=	true;
	error=$('input[type=submit]').parent().prev();
	
	// si alguno de los 3 primeros campos no fue modificado no valida
	$('input[type=text]').each(
	function(){
		if($(this).val()==$(this).attr('nombre')&&$(this).parent().index()!==1){
					valida =false;
					$(this).css('color','red');
					error.html('Complete todos los campos resaltados')

		} else {
				$(this).css('color','grey');
		}
		}
	);
	
	// Chequeo que hayan @ y . en el mail hecho por mi...
	/*
	if (valida && ($('#email').val().indexOf('@')<3 || $('#email').val().indexOf('.')<$('#email').val().indexOf('@')+3)){
		error.html('Verifique su e-mail');
		$('#email').css('color','red');
		valida =false;
	}
	*/
	
	// validación POSTA del mail
	validaMail = IsValidEmail($('#email').val())
	function IsValidEmail(email) {
			var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			return filter.test(email);
	}
	if(valida && !validaMail){valida=false;error.html('Verifique su e-mail');	$('#email').css('color','red');}
	// fin  validación POSTA del mail
	
	// si la Introducción es vacía no valida
	if($('#motivo').html()==$('#motivo').attr('nombre')){
					valida =false;
					$('#motivo').css('color','red');

	}
	
	
	if (valida){error.html('Enviando Mensaje');}
	
	return valida;	

}); // termina submit


} // termina la CARGA
	
	function vacia() {
	
	$('input[type=text]').focus(function(){if($(this).val()==$(this).attr('nombre'))$(this).val(''); $(this).css('color','grey'); 
		})
	
	$('input[type=text]').blur(function(){
		if($(this).val()=='')$(this).val($(this).attr('nombre'));
			}
		)
		
	$('textarea').focus(function(){if($(this).val()==$(this).attr('nombre'))$(this).html(''); $(this).css('color','grey'); 
		})
	
	$('textarea').blur(function(){
		if($(this).val()=='')$(this).html($(this).attr('nombre'));
			}
		)
	
	$('input[type=reset]').click(function(){
			
		if($(this).val()=='Cambiar'){
			$(this).val('Borrar ');
			$('input[type=submit]').css('display','block')
			$('textarea').html('Motivo de Consulta');
		}else if ($(this).val()=='Borrar '){
			
		window.location='index.php?re-envio';

			
			

			}
			}
		)
		
	}
	
function manda(){
	
	nombre=$('#nombre').val();
	motivo=$('#motivo').val();
	email=$('#email').val();
	telefono=$('#telefono').val();
	empresa=$('#empresa').val();
	window.location='index.php?nombre='+nombre+'&email='+email+'&telefono='+telefono+'&motivo='+motivo+'&empresa='+empresa;
	}

	
function opacity100(){
	$(this).parent().parent().parent().parent().children('.overs').html($(this).attr('title'))
	$(this).animate({opacity:'1'},{duration: 500})
	$('#pie img:eq(2)').animate({opacity:'0.3'},{duration: 500})
	}
	
function opacity80(){
	$(this).parent().parent().parent().parent().children('.overs').html($('#pie img:eq(2)').attr('title'))
	$(this).animate({opacity:'0.3'},{duration: 500})
	$('#pie img:eq(2)').animate({opacity:'1'},{duration: 500})

	}

function calculaAlto(){
		
	/* creo un array con las reglas de estilos.css 
	como IE lee .rules y el resto del mundo lee .cssRules si uno es vacío chapa el otro */
	var rules = document.styleSheets[0].rules || document.styleSheets[0].cssRules;
	
	/* tomo el alto disponible */
	altoTotal=$(window).height();
	//alert(altoTotal)
	margenSuperior=(altoTotal-637)/2;
	if(margenSuperior<0){margenSuperior=0}
	/* asigno los valotes calculados a #contenedor y #cabezal respectivamente */
	rules[2].style.marginTop=margenSuperior+"px";	
	
	}

function rollOverImagenes(){

$(this).append('<div id="cajaSolapa"><div id="solapa" class="rounded-corners"></div></div>');


$("#cajaSolapa", this).stop().animate({top:"0px"},{
											queue:false,
											duration:200, 
											//complete:function() {aparece()}
									  });

	}
	
function rollOutImagenes(){
	
	$("#cajaSolapa", this).remove();

		}

function writeSWFCode(swfSrc,swfWidth,swfHeight,wmode) {
		writeString = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"100%\" height=\""+swfHeight+"\">\n";
		writeString += "  <param name=\"movie\" value=\""+swfSrc+"\" />\n";
		if(wmode!=null) {
			writeString += "<param name=\"wmode\" value=\""+wmode+"\" />\n";
		}
		writeString += "  <param name=\"quality\" value=\"high\" />\n";
		writeString += "  <param name=\"BGCOLOR\" value=\"#006c76\">\n";
		writeString += "  <param name=\"showmenu\" value=\"false\" />\n";
		writeString += "<param name=\"scale\" value=\"noscale\">";
		writeString += "<embed src=\""+swfSrc+"\" quality=\"high\" showmenu=\"false\" ";
		if(wmode!=null) {
			writeString += "wmode=\""+wmode+"\" ";
		}
		writeString += "pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+swfWidth+"\" height=\""+swfHeight+"\"></embed>\n";
		writeString += "</object>\n";
		document.write(writeString);
		delete writeString;
	}





function cambiaCaracteres(mensaje){
	codhtml=["&Aacute;","&aacute;","&Eacute;","&eacute;","&Iacute;","&iacute;","&Oacute;","&oacute;","&Uacute;","&uacute;","&Ntilde;","&ntilde;","&lt;","&gt;","&amp;","&itilde;","&nbsp;","&quot;","&apos;"];
	
	textoingresado=["Á","á","É","é","Í","í","Ó","ó","Ú","ú","Ñ","ñ","<",">","&"]
	
	codunicode=["\u00C1","\u00E1","\u00C9","\u00E9","\u00CD","\u00ED","\u00D3","\u00F3","\u00DA","\u00FA","\u00D1","\u00F1","\u003C","\u003E","\u0026","\u00ED","\u00A0","\u0022","\u0027"];	
	
	for(j=0;j<=codhtml.length;j++){	
		if(mensaje.indexOf(codhtml[j]>=0)) {
			for(p=0;p<=mensaje.length;p++){
			mensaje=mensaje.replace(codhtml[j],codunicode[j]);
			}
			}
		if(mensaje.indexOf(textoingresado[j]>=0)) {
			for(p=0;p<=mensaje.length;p++){
			mensaje=mensaje.replace(textoingresado[j],codunicode[j]);
			}
			}
	}	
	return mensaje;
}
