 $(document).ready(function(){
 	var back = $('#back');
 	if (back) {
 		//
 		// Resizer
 		//
 		(function() {
		 	var onResize = function() {
			 	var back = $('#back');
				var width = back.width();
				var height = back.height();
				var ratio = width/height;
	
				var imgs = $('img', back);
				imgs.each(function() {
					var img = $(this);
				
					var w = img.width(),
					    h = img.height();
					var r = w/h;
					
					if (r>ratio) {
						var size = (height * r);
					
						img.css('width', size);
						img.css('height', height);
						img.css('top', 0);
						img.css('left', 0.5*(width - size));
					} else {
						var size = (width / r);
					
						img.css('width', width);
						img.css('height', size);
						img.css('top', 0.5*(height - size));
						img.css('left', 0);
					}
				});
		 	};
		 
			$(window).resize(function () {
				onResize();
		    });
		    
		    $(window).load(function () {
			    onResize();
		    });
	    })();
	    
	    
	    //
	    // Rotator
	    //
	    (function() {
		    var imgs = $('img', back);
		    if (imgs.length > 1) {
			    var position = 0;
		
			    var i = 0;
				imgs.each(function() {
					if (i++ != position) {
						$(this).hide();
					}
				});
				
				
				var timer = setInterval(function() {
					$(imgs[position]).fadeOut(1000);
					
					position = (position + 1) % imgs.length;
					
					$(imgs[position]).fadeIn(1000);
				}, 4000);
			}
		})();
    }
    
    
	//
	// Menu
	//
	(function() {
		var nav1 = $('#navigation > li');
		var actif = null;
		
		nav1.each(function() {
			var ul = $('ul', this).first();
			var head = $(this).children('a').first();
			
			if (ul.length > 0) {
				if (!head.hasClass('actif')) {
					ul.hide();
				} else {
					actif = ul;
				}
				
				head.click(function() {
					if (actif != ul) {
						if (actif) {
							actif.slideUp();
						}
						
						ul.slideDown();
						actif = ul;
					}
					return false;
				});
			}
		});
	})();
	
	
	
	//
	// Slider
	//
	(function() {
		var slider = $('#slider');
		if (slider.length > 0) {
			var lock = false;
			var position = 0;
			var next = $('#slider-next');
			var prev = $('#slider-prev');
			var children = slider.children('li');
			var current = $(children.first());

			current.css('left', 0);

			if (children.length <= 1) {
				next.css('visibility', 'hidden');
				prev.css('visibility', 'hidden');
			}

			var sliderFx = function() {
				var nextSlide = $(children[position]);
			
				//current.fadeOut(500);
				current.hide();
				nextSlide.css({
					'display': 'block',
					'left': 200,
					'opacity': 0
				});
				nextSlide.animate({'left': 0, 'opacity': 1}, 500, function() {
					lock = false;
				});
					
				current = nextSlide;
				
				if (position == 0) {
					prev.css('visibility', 'hidden');
					next.css('visibility', 'visible');
				} else if (position == (children.length - 1)) {
					prev.css('visibility', 'visible');
					next.css('visibility', 'hidden');
				} else {
					prev.css('visibility', 'visible');
					next.css('visibility', 'visible');
				}
			};
			
			prev.click(function() {
				if (!lock) {
					lock = true;
					position = (position - 1);
					if (position < 0) {
						position = (children.length - 1);
					}
					sliderFx();
				}
				return false;
			});
			
			next.click(function() {
				if (!lock) {
					lock = true;
					position = (position + 1) % children.length;
					sliderFx();
				}
				return false;
			});
			
			for (i=1, l=children.length; i<l; i++) {
				$(children[i]).hide();
			}
		}
	})();
	
	
	
	//
	// Slider
	//
	(function() {
		var portfolio = $('#portfolio');
		if (portfolio.length > 0) {
			var ul = $(portfolio.children('ul').first());
		
			var size = {
				x: ul.width(),
				y: ul.height()
			};
		
		 	var onResize = function() {
				var height = portfolio.height() - 30;
				var width = height * size.x / size.y;
				
				ul.css('height', height);
				ul.css('width', width);
		 	};
		 
			$(window).resize(function () {
				onResize();
		    });
		    
		    /*$(window).load(function () {
			    onResize();
		    });*/
		    
		    $('#portfolio img').load(function() {
		    	onResize();
		    });
		    
		    onResize();
		}
	})();
	
	
	
	
	//
	// Booking
	//
	(function() {
	
		var updateDepot = function() {
			if ($('#shoot_13').prop("checked")) {
				$('#depot').hide();
			} else {
				$('#depot').show();
			}		
		};
	
	
		var book = $('#frmBook');
		if (book.length > 0) {
			$('#types input').click(updateDepot);
			
			updateDepot();
		}
	})();
 });
 
