$(function() { // abreviatura del $(document).ready(function(){...});
	
	h = $(window).height() - (($('div#columna').position().top||0) + $('div#footer').outerHeight(true));
	//alert($('div#principal').position().top);
	// $('div.portada').height(h);
	
	//var original = $('fieldset').html();

	/*
	var enviar = function () {
		$('fieldset').html('<p id="avis" style="font-family: arial; font-size: 13px">Usuari<br />o contrasenya<br />incorrectes</p>')
		$('#avis').fadeOut('slow', function(){
			$('fieldset').html(original);
			$('form#acces').bind('submit', enviar);
		});
		return false;
	};
	*/

	//$('form#acces').bind('submit', enviar);
	
	$('.cajaBanner').jcaption({
		//wrapperElement: 'div',
		captionElement: 'p',
		//Animate on hover over the image
		animate: true,
		copyStyle: false,
		//Show Animation
		show: {height: 'show'},
		showDuration: 200,
		//Hide Animation
		hide: {height: 'hide'},
		hideDuration: 200, 
		autoWidth: false,
		autoHeight: false
	});
	
	var $dialog = $('<div></div>')
	.html('<div style="Margin-top: 25px">En construcció.</div>')
	.dialog({
		autoOpen: false,
		modal: true,
		title: 'Més informació'
	});

	$('.mes_info').click(function() {
		$dialog.dialog('open');
		return false;
	});
	
	$('label.boto_envia').hover( 
		function () {
			$(this).find('input#submit').css({ 'background-position': 'right -18px !important' });	
			//$(this).find('input#submit').fadeOut();
		},
		function () {
			//$(this).find('input#submit').fadeIn();
			$(this).find('input#submit').css({ 'background-position': 'right 0px !important' });
		}
	);
	
	/*
	var original = 	$('form#acces input#usuari').val();
	var original2 =	$('form#acces input#contrasenya').val();
	*/
	
	$('form#acces input.casella').click(function() {
		//if(($(this).val())==($(this)[0].defaultValue)) { // furguem en el DOM per a que només canvie si segueix el valor per defecte i no si hem posat un usari i contrasenya nostres
		$(this).val("");
	});
	
	$('form#acces').submit(function() {
		$.ajax({
			type: "GET",
			url: $('form#acces').attr("action"),
			data: { action: '1',
					usuari: $.md5($('form#acces input#usuari').val()),
					contrasenya: $.md5($('form#acces input#contrasenya').val()) },
			beforeSend: function() { $('div.ajax_indi').html('<img src="imatges/ajax-loader-form.gif" />'); },
			complete: function() { $('div.ajax_indi').html(''); },
			error: function(msg){
				alert('AJAX error!');
			},
			success: function(msg){
				/*
				var wait = setInterval(function() {				 			   
			    	clearInterval(wait);
			    	alert(msg);		
			    }, 1000); 
			    */
				//setTimeout("function(){alert(msg)}",2000);
				alert(msg);
			    //return false;
			}
		});
		return false;	
	});
	
	$('div.cat_mostra_2').click(
		/*
		function() {
			$(this).parent().find('div.desc').slidedown('slow');
		}
		*/
		function () {
			
			if($(this).parent().find('div.desc').hasClass('open')) {					
				$('+ div.desc', this).slideUp('slow', function () {
					$(this).parent().find('span img').attr('src', "imatges/btn_arrow_down.png");
					$(this).removeClass('open'); 
				} );
				$('div.intern.visible').fadeOut('fast', function () {
					$('div.intern.visible').removeClass('visible');
					$('div#intern_buit').fadeIn('fast', function () { 
						$(this).addClass('visible'); 
					});
				});
			}
			else {											
				$(this).parent().parent().find('div.open').each( 
					function () {		
						$(this).slideUp('slow', function() {
							$(this).parent().find('span img').attr('src', "imatges/btn_arrow_down.png");
							$(this).removeClass('open'); 
						});
					}
				);		
				aobrir = $(this).attr('id');
				$('+ div.desc', this).slideDown('slow');
				$('div.intern.visible').fadeOut('fast', function () {
					$('div.intern.visible').removeClass('visible');
					$('div#intern_' + aobrir).fadeIn('fast', function () { 
						$(this).addClass('visible'); 
					});
				});
				$(this).parent().find('span img').attr('src', "imatges/btn_arrow_up.png");
				$(this).parent().find('div.desc').addClass('open'); 
			}	
		}
	);
	
	$('p.baixa_news a').click( function () {
		$('form#newsletter_off').slideToggle('fast');
		return false;
	});
	
	/*
	$('div.cat_mostra_2').click(
			
			function () {
				
				if($(this).parent().find('div.desc').hasClass('open')) {					
					$('+ div.desc', this).slideUp('slow', function () {
						$('span img', this).attr('src', "imatges/btn_arrow_down.png");
						$(this).removeClass('open'); 
					});
				}
				else {											
					$(this).parent().parent().find('div.open').each( 
						function () {		
							$(this).slideUp('slow');
							$(this).find('span img').attr(src, "imatges/btn_arrow_down.png");
							$(this).removeClass('open'); 
						}
					);
					$(this).parent().find('div.desc').addClass('open'); 
					$('+ div.desc', this).slideDown('slow', function () {
						$(this).find('span img').attr('src', "imatges/btn_arrow_up.png");
					});						
				}	
			});
	*/
	// Bannerslide:
	
	/*
	if (nMax > 2) {
		setTimeout(slide, transicion);
	}
	*/
	$('input#envia_contacte').click(function () {
		//adreca = '/contacte.php';
		//alert(adreca);
		$.post(
			"/contacte.php",
			{
				"accio": "envia_coment",
				"nom": $('input#cont_nom').val(),
				"email": $('input#cont_email').val(),
				"adreca": $('input#cont_add').val(),
				"poblacio": $('input#cont_city').val(),
				"telefon": $('input#cont_phone').val(),
				"comentaris": $('textarea#cont_comments').val()
			},
			function (retorn) {
				alert(retorn);
			}
		);
		return false;
	});
});


/*
 * <iframe width="950" height="200" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" 
 * src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=ca&amp;geocode=&amp;q=C%2FLes+fonts,+7+-+46711+Miramar+(Val%C3%A8ncia)+&amp;
 * sll=37.0625,-95.677068
 * sspn=36.368578,79.013672&
 * amp;ie=UTF8&amp;hq=&amp;hnear=Carrer+de+les+Fonts,+7,+46711+Miramar,+Val%C3%A8ncia,+Comunitat+Valenciana,+Espanya&amp;t=h&amp;
 * ll=38.963413,-0.159988
 * spn=0.026695,0.16325&amp;z=13&amp;iwloc=A&amp;output=embed"></iframe><br /><small>
 * <a href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=ca&amp;geocode=&amp;q=C%2FLes+fonts,+7+-+46711+Miramar+(Val%C3%A8ncia)+&amp;sll=37.0625,-95.677068&amp;sspn=36.368578,79.013672&amp;ie=UTF8&amp;hq=&amp;hnear=Carrer+de+les+Fonts,+7,+46711+Miramar,+Val%C3%A8ncia,+Comunitat+Valenciana,+Espanya&amp;t=h&amp;ll=38.963413,-0.159988&amp;spn=0.026695,0.16325&amp;z=13&amp;iwloc=A" style="color:#0000FF;text-align:left">Mostra un mapa més gran</a></small>
 * 
 */


function inicialitza_mapa() {
    var latlng = new google.maps.LatLng(38.963413,-0.159988); // paràmetre ll
    var globuslatlng = new google.maps.LatLng(38.958858,-0.123596);
    var myOptions = {
      zoom: 13,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    
    var contentString = '<div id="content">'+
    '<h2>Saformedia</h1>'+
    '<div id="bodyContent">'+
    '<p>C/Les Fonts, 7<br/>46711 Miramar (València)<br />Tel.: 627828037</p>'+
    '</div>'+
    '</div>';

	var infowindow = new google.maps.InfoWindow({
	    content: contentString
	});
	
	infowindow.setContent(contentString);
	infowindow.setPosition(globuslatlng);

	infowindow.open(map);

}
