$(document).ready(function(){

    if($('#Page_Accueil').size())
	setInterval('SlideSwitch();', 5000);

    GoogleMap.Setup(46.800772, -71.169572);

    $('#SubNavigation li a').each(function(){
        $(this).attr('rel', $(this).width());
    });

    $('#SubNavigation li a').hover(function(){
        $(this).animate({'padding-left': 14, 'width': parseInt($(this).attr('rel')) + 5}, 400);
    }, function(){
        $(this).animate({'padding-left': 9, 'width': $(this).attr('rel')}, 200);
    });

    $('#EspacePhotos').click(function(){
        document.location = 'Club/Parcours/#Photos';
    });

    var CurrentLocation = document.location.toString();
    if(CurrentLocation.match('#')){
	var Action = CurrentLocation.split('#')[1];
        if(Action == 'Photos')
            Photos.Show();
    }

    $('#CommentsTextArea').focus(function(){
       if($(this).val() == 'Explications et commentaires concernant votre demande de réservation')
           $(this).val(null);
    });

    var LastWidth = $('#Navigation').innerWidth();
    $('#NavigationList > li:lt('+($('#NavigationList > li').size() - 1)+')').each(function(){
        LastWidth -= $(this).outerWidth() + 1;
    });

    $('#NavigationList > li:last-child').css('width', LastWidth + 6);

});

$(window).bind('hashchange', function(){

    var CurrentLocation = document.location.toString();
    if(CurrentLocation.match('#')){
	var Action = CurrentLocation.split('#')[1];
        if(Action == 'Photos')
            Photos.Show();
    }

});

Photos = {
    Current:1,
    Total: 0,
    Show:function(){
        Photos.Current = 1;
        Photos.Total = $('.ParcoursContainer').size();
        Photos.Change();
    },
    Previous:function(){
        Photos.Current--;
        if(Photos.Current < 1)
            Photos.Current = 1;
        Photos.Change();
    },
    Next:function(){
        Photos.Current++;
        if(Photos.Current > Photos.Total)
            Photos.Current = Photos.Total;
        Photos.Change();
    },
    Change:function(){
        $.colorbox({
            html: $('#ParcoursNumero'+Photos.Current).html() + $('#ParcoursControls').html(),
            height: 660,
            width: 775,
            onClosed: function(){
                var CurrentLocation = document.location.toString();
                if(CurrentLocation.match('#')){
                    var Location = CurrentLocation.split('#')[0];
                    document.location = Location+'#';
                }
            }
        });
        $('#CurrentPhoto').html(Photos.Current);
        $('#TotalPhoto').html(Photos.Total);
    },
    Close:function(){
        $.colorbox.close();
    }
}

function SlideSwitch(){
    var Active = $('#Slides img.Active');
    
    if(Active.length == 0) Active = $('#Slides img:last');

    var Next = Active.next().length ? Active.next()
        : $('#Slides img:first');

    Active.addClass('LastActive');

    Next.css({opacity: 0.0})
        .addClass('Active')
        .animate({opacity: 1.0}, 3000, function(){
            Active.removeClass('Active LastActive');
        });
}

GoogleMap = {
  Map:null,
  Setup:function(Latitude, Longitude){

    if($('#GoogleMap').size()){
      var Coordinates = new google.maps.LatLng(Latitude, Longitude);
      var Options = {
        zoom: 14,
        center: Coordinates,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };

      GoogleMap.Map = new google.maps.Map(document.getElementById('GoogleMap'), Options);

      var Marker = new google.maps.Marker({
          position: Coordinates,
          map: GoogleMap.Map,
          title: 'Contact'
      });
    }

  }
}
