$(document).ready(function() {

    var searchBox = $("#formEmail");
    var searchBoxDefault = $("#formEmail").attr("value");

    searchBox.focus(function(e){$(this).addClass("active");});
    searchBox.blur(function(e){$(this).removeClass("active");});

    searchBox.focus(function(){
        if($(this).attr("value") == searchBoxDefault) $(this).attr("value", "");
    });
    searchBox.blur(function(){
        if($(this).attr("value") == "") $(this).attr("value", searchBoxDefault);
    });
    
    $('#formDispo').validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: {
				required: "",
				email: ""
			}
		}
	});
    
    var options = {
        target:         null,
        dataType:       'text',
        success:        showResponse,
        error:          showError,
        beforeSubmit:   validate
    }
    $('#formDispo').ajaxForm(options);
    
    $(document).ajaxStart(function(){ 
        //$("#formDispoButton").attr("disabled", true);
        $('#formDispo :input').attr("disabled", true);
		$('#ajaxIndicator').show(); 
	}).ajaxStop(function(){
        $('#formDispo :input').removeAttr('disabled');
		$('#ajaxIndicator').hide();
	});
	
    $('img[id^=thumbnail]').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	$('img[id^=thumbnail]').click(
		function(){
			$("img[id^=thumbnail].current").removeClass('current');
			$(this).addClass('current');
			var selectedThumbnailNumber = $(this).attr('id').charAt('thumbnail'.length);
			
			var currentImage = $("img[id^=mainPicture].current");
			var currentImageNumber = currentImage.attr('id').charAt("mainPicture".length);
			currentImage.removeClass('current');
			
			$("img#mainPicture"+selectedThumbnailNumber).addClass('current');
		}
	);
	
});

function validate(formData, jqForm, options) {
    var isFormValid = $("#formDispo").valid();
    $("#formEmail").blur();
    return isFormValid;
}

function showResponse(responseText, statusText, xhr, $form)  {
    $("#output").children().remove();
    var paragraphItem = document.createElement('p');
    paragraphItem.innerHTML = responseText;
    $("#output").append(paragraphItem);
}

function showError(xhr, ajaxOptions, thrownError){
    //alert(xhr.status + ' - ' + thrownError);
}

