//window.onunload = function() {};



$(document).ready( function() {	initScripts(); } );


function initScripts() {
	
	$pagina = 1;
	
	$('#galeria_paginador a').click ( function() {
		
		/*var elementClick = $(this).attr("href");
		var destination = $(elementClick).offset().top;
		$("#galeria_previews_contents").animate({ scrollTop: destination}, 1000 );
		*/
		
		if ((!($(this).attr('href') == '#prev')) && (!($(this).attr('href') == '#next'))) {
			
			$pagina = $(this).attr('href').replace('#p', '');
			
			$('#galeria_previews_contents').scrollTo($(this).attr('href'), 1500, {easing:'easeInOutQuad'} );
			
			$('#galeria_paginador a').css('font-weight', 'normal');
			
			$(this).css('font-weight', 'bold');
			
			$pagina = parseInt($pagina);
			
			return false;
			
		} else if ($(this).attr('href') == '#prev') {
			
			if ($pagina > 1) {
				
				$pagina--;
				$pagina_ref = '#p' + $pagina;
				
				$('#galeria_previews_contents').scrollTo($pagina_ref, 1500, {easing:'easeInOutQuad'} );
				
				$('#galeria_paginador a').css('font-weight', 'normal');
				
				$("#galeria_paginador a[href='" + $pagina_ref + "']").css('font-weight', 'bold');
				
				return false;
				
			}
			
		} else if ($(this).attr('href') == '#next') {
			
			if ($pagina < 9) {
				
				$pagina++;
				$pagina_ref = '#p' + $pagina;
				
				$('#galeria_previews_contents').scrollTo($pagina_ref, 1500, {easing:'easeInOutQuad'} );
				
				$('#galeria_paginador a').css('font-weight', 'normal');
				
				$("a[href='"+$pagina_ref+"']").css('font-weight', 'bold');
				
				return false;
				
			}
			
		} 
		
		return false;
		
	})
	
	$('#menu a').click ( function() {
		
		$.scrollTo( '#biocontacto', 1500, {easing:'easeInOutQuad'} );
		
	})
	
	$('#galeria_previews a').click ( function() {
		
		var img = new Image();
        $('#cuadro img').load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $('#cuadro img').hide();
            $('#cuadro img').fadeIn();
        }).error(function () {
            // notify the user that the image could not be loaded
        }).attr('src', $(this).attr('href'));
		
		$this_pic = $(this).attr('href').replace('vistas/', '');
		
		$('#cuadro').attr('href', "cuadros/" + $this_pic);
		
		$('#pintura h2').text($(this).attr('title'));
		
		$('#pintura h3').text($(this).children('img').attr('alt'));
		
		return false;
		
	})
	
	
	$('#submit input').live ('click', function() {
		
		if ($("input[name='nombre']").attr('value') == "") {
			
			$("input[name='nombre']").css('border', '1px dotted #e32000');
			$('#alerta_nombre').show('slow');	
			return false;
			
		} else {
			
			$("input[name='nombre']").css('border', '1px dotted #ffffff');
			$('#alerta_nombre').hide('slow');
			
		}
		
		if ($("input[name='email']").attr('value') == "") {
			
			$("input[name='email']").css('border', '1px dotted #e32000');
			$('#alerta_email').show('slow');
			return false;
			
		} else {
			
			$("input[name='email']").css('border', '1px dotted #ffffff');
			$('#alerta_email').hide('slow');
			
		}
		
		if ($("textarea").attr('value') == "") {
			
			$("textarea").css('border', '1px dotted #e32000');
			$('#alerta_mensaje').show('slow');
			return false;
			
		} else {
			
			$("textarea").css('border', '1px dotted #ffffff');
			$('#alerta_mensaje').hide('slow');
			
		}
		
	})
	
	var options = {
		
        target:        '#contacto_contenedor',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#formulario_de_contacto').ajaxForm(options);
	
};

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	
	//$('#contacto_contenedor').html(responseText);
	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.');
} 
