
var quotes = [
    { q: 'Acceuil excellent avec des prestations sans pareil, que du bonheur. Grand merci', c: 'M et Mme Jaillard, Festernay' },
    { q: 'Que du bonheur, que dire de plus. Acceuil sympa et en plus Claire nous a commandé du soleil.', c: 'Monique et Romain Munsch' },
    { q: 'Très bonne ambiance et surtout bon acceuil. Je suis bretonne, mais je reviens içi', c: 'M et Mme Moisan' },
    { q: 'Très agréable séjour et charmant acceuil. La chambre a beaucoup de charme.', c: 'M et Mme Dumesnil' }
];

var pics = [
'cbre1_0_frm.png', 'cbre1_1_frm.png', 'chambre3_en_suite_stdf.png', 'chambre2_2_stdf.png', 'chambre3_stairs_1_stdf.png',
'chambre2_3_stdf.png', 'garden_1_stdf.png', 'garden_2_stdf.png',
'chambre3_1_stdf.png', 'chambre3_2_stdf.png', 'landing_stdf.png',
'chambre3_4_stdf.png', 'stairs_stdf.png', 'chambre3_5_stdf.png', 'sde1_frm.png' 
];


$(function() {
    showQuotes( 99 );
    
    if( $("#blimage").length )
    {
        $("#blimg").css( 'opacity', 0 );
        rotateImages( 99 );
    }
    
    $(".blank").attr( "title", "This field cannot be blank" );
    $(".fill").attr( "title", "This field should be filled in" );
 
    $("#contactform").submit( checkAndAjax );

    $("#galthumbs a").click( function() {
        var $this    = $(this),
            img      = new Image,
            src      = $this.attr( 'href' ),
            $chrome  = $("#picchrome"),
            $loading = $("#loading"),
            $tray    = $this.parent(),
            pos      = $tray.position();
            
        $("#cover").fadeTo( 100, 0.6 );
        $loading.css( {top: (pos.top + 200), left: (pos.left + 430)} ).show();

        img.src = src;

        img.onload = function() {
            var width  = img.width  + 12,
                height = img.height + 21;

            $loading.hide();
                
            $chrome.css( 
                { width: width, 
                  height: height, 
                  top: 50,
                  left: pos.left + ($tray.innerWidth() - width) / 2
                } )
                .html( '<img src="' + src + '" alt="' + src + '" />' )
                .fadeTo( 600, 1 );
        };

        return  false;            
    });
    
    $("#cover,#picchrome,#picchrome img").live( 'click', function() {
        $("#picchrome").fadeOut( 600 );
        $("#cover").hide();
    });
});


function showQuotes( index )
{
    if( index >= quotes.length )
        index = 0;

    var theOne = quotes[index];
    
    $("#quotes").fadeTo( 600, 0, function () {
        $("#qq").html( theOne.q );
        $("#qcite").text( theOne.c );
        $("#quotes").fadeTo( 600, 1 );
    });
    
    setTimeout( 'showQuotes(' + (index + 1) + ')', 6000 );
}


function rotateImages( index )
{
    var blimg   = new Image,
        trimg   = new Image,
        nPics   = pics.length,
        first   = (index == 99);
        
    if( index >= nPics )
        index = 0;
        
    blimg.src = 'graphics/slideshow/' + pics[index];
    trimg.src = 'graphics/slideshow/' + pics[(index + 3) % nPics];
    
    blimg.onload = function() {
        if( !first )
            changePicture( $("#blimage"), blimg.src );
        setTimeout( function() { changePicture( $("#trimage"), trimg.src ); }, 5000 );
        setTimeout( 'rotateImages( ' + (index + 1) + ')', 10000 );
    };
}


function changePicture( $place, name ) 
{
    $place.fadeTo( 600, 0, function() {
        $place.attr( 'src', name ).fadeTo( 600, 1 );
    });        
}


function checkAndAjax()
{
    var frm = document.forms[0];
        
    if( frm.cname.value === '' || (frm.mail.value === '' && frm.telephone.value === '') )
    {
        alert( 'You must enter your name and an email address or telephone number.' );
        return  false;
    }
    
    $.post( "contact.php?a=1", $("#contactform").serialize(), function( data ) {
//        alert( 'data: "' + data + '"' );
    
        $intro = $("#intro");
        
        $intro.css( 'font-size', '150%' );
        
        if( data.substr( 0, 2 ) == "OK" )
            $intro.text( "Thank you. We will be in touch soon" );
        else            
            $intro.html( "Something has gone wrong with sending requests.<br>Please try again later." ).css( 'color', '#ff2020' );
    });

    return  false;  // Submit has been done
}

