// JavaScript Document
// Framework : jQuery 1.6.4
// @author : Romain Couturier - © http://www.neko-studio.fr/
// @Mail : romain.couturier@neko-studio.fr
//

var header_display = true;
var photo_gallery = 1;
var pos_photo_gal = 1;


$(document).ready( function() {
	
	var gallery_photos_selec = $('#gallery-photos').attr("title");
	var body_width = $(document).width();
	var body_height = $(document).height();
	
	$("#overlay").css({"width" : body_width, "height" : body_height});
	$('#pos-header, #container').css("display", "block");
	
	$(window).load( function() {
		$("#overlay").fadeOut('slow', 'swing');
	});	
	
	$('h1').click( function() {
		window.location.href = "/home";
	});

	
	$('#header-hide').click( function() {
		
		if (header_display) {
			$('#global-main').fadeOut('fast', 'swing');
			
			$('h1').fadeOut('fast', 'swing', function() {
				$('#gallery').css('z-index', '5');
				$('#gallery').animate({
				    marginTop: "30px"
				  }, 1000, function() {
					  
					  $('#header-hide').css("background", "url('http://www.martinbruno.fr/images/header-arrow-display.png') no-repeat");
					  header_display = false;
				  });
			});
		}
		else {
			$('#gallery').animate({
			    marginTop: "100px"
			  }, 1000, function() {
				  
				  $('#gallery').css('z-index', '1');
				  $('#header-hide').css("background", "url('http://www.martinbruno.fr/images/header-arrow-hide.png') no-repeat");
				  header_display = true;
				  
				  $('#global-main, h1').fadeIn('fast', 'swing');
			});
		}
		
	});
	
	$('#link-news').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-news-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-news.png");
	});
	
	$('#link-about').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-about-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-about.png");
	});
	
	$('#link-work').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-work-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-work.png");
	});
	
	$('#link-clients').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-clients-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-clients.png");
	});
	
	$('#link-videos').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-videos-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-videos.png");
	});
	
	$('#link-contact').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-contact-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-contact.png");
	});
	
	$('#link-links').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-links-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/main-item-links.png");
	});
	
	$('#groundfloor').hover (
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/home-logo-rollover.png");
			},
			
			function() {
				$(this).find('img').attr("src", "http://www.martinbruno.fr/images/home-logo.png");
	});
	
	$('#groundfloor').click( function() {
		$('h1').fadeIn('fast', 'swing');
		$('#pos-header').fadeIn('fast','swing');
		$(this).fadeOut('fast', 'swing', function() {
			$('#middle').fadeIn('fast', 'swing', function() {
				$('#top-left').fadeIn('fast', 'swing', function() {
					$('#bottom-right').fadeIn('fast', 'swing', function() {
						$('#middle-left').fadeIn('fast', 'swing', function() {
							$('#middle').fadeIn('fast', 'swing', function() {
								$('#top-right').fadeIn('fast', 'swing', function() {
									$('#bottom-left').fadeIn('fast', 'swing');
								});
							});
						});
					});
				});
			});
		});
	});
	
	$('#top-left').click( function() {
		window.location.href = "/home/page-work";
	});
	$('#bottom-left').click( function() {
		window.location.href = "/home/page-about";
	});
	$('#middle-left').click( function() {
		window.location.href = "/home/page-links";
	});
	$('#middle').click( function() {
		window.location.href = "/home/page-clients";
	});
	$('#top-right').click( function() {
		window.location.href = "http://newmartinbruno.tumblr.com/";
	});
	$('#bottom-right').click( function() {
		window.location.href = "/home/page-videos";
	});
	
	// Galleries RollOver and RollOut
	$('.gallerie-block').mouseover( function() {
		var src = "http://www.martinbruno.fr/pictures/icons/rollover-"+$(this).find('a').find('img').attr('id')+".png";
		$(this).find('a').find('img').attr('src', src);
	});
	$('.gallerie-block').mouseout( function() {
		var src = "http://www.martinbruno.fr/pictures/icons/"+$(this).find('a').find('img').attr('id')+".png";
		$(this).find('a').find('img').attr('src', src);
	});
	
	// Photos RollOver and RollOut
	$('.photo-block').mouseover( function() {
		var src = "http://www.martinbruno.fr/pictures/thumb/"+$(this).find('a').find('img').attr('id')+"-rollover.png";
		$(this).find('a').find('img').attr('src', src);
	});
	$('.photo-block').mouseout( function() {
		var src = "http://www.martinbruno.fr/pictures/thumb/"+$(this).find('a').find('img').attr('id')+".png";
		$(this).find('a').find('img').attr('src', src);
	});
	
	
	$('#photo-roll').hover(
			function() {
				$(this).fadeTo('fast', 1, 'swing');
			},
			function() {
				$(this).fadeTo('fast', 0.5, 'swing');
	});
	
	
	$('#photo-overlay').click ( function() {
		
		var nbr_photos = $('#photos-carrousel').find('li').length;
		
		photo_gallery++;
		pos_photo_gal++;
		
		id_gallery = "photo"+pos_photo_gal;
		
		if (pos_photo_gal <= nbr_photos) {
			$('#photo-previous').css("visibility", "visible");
			//alert('');
			$('#photo-infos, #photo-roll').fadeOut('fast','swing');
			$('#photo-block').find('#photo-block-content').fadeOut('fast', 'swing', function() {
				$('#photo-block').find('#photo-block-zoom').fadeOut('fast', 'swing');
				$(this).remove();
				$('#photo-previous').hide();
				$('#photo-next').hide();
				$('#photo-infos').empty();
				
				$('#loading').fadeIn('fast', 'swing', function() {
					
					$.ajax({
						type: "GET",
						url: "/xml/gallery-photo-view.php",
						data: "id_gallery="+gallery_photos_selec,
						dataType: "xml",
						complete: function(data, status) {
						//alert(status);
						var link = data.responseXML;
	
						var title_photo = $(link).find(id_gallery).find('title').text();
						var url_photo = $(link).find(id_gallery).find('url').text();
						var caption_photo = $(link).find(id_gallery).find('caption').text();
						var type_photo = $(link).find(id_gallery).find('type').text();
						var full_url_photo = "http://www.martinbruno.fr/pictures/zoom/"+url_photo+"-zoom";

						var content_box = "<img id='photo-block-content' src='http://www.martinbruno.fr/pictures/"+url_photo+".jpg' style='display: none;' />";
						var content_caption = "<p id='photo-caption'>"+caption_photo+"</p>";
						var content_nbr = "<p id='photo-nbr'>"+pos_photo_gal+" /"+nbr_photos+"</p>";
							
						$(content_nbr).appendTo('#photo-infos');
						$(content_caption).appendTo('#photo-infos');
						$(content_box).appendTo('#lightme');
						document.getElementById('lightme').setAttribute('href', full_url_photo);
						//$('#loading').after(content_box);
					
						$('#photo-block-content').load( function() {
							
							var height_content = $('#photo-block-content').height();							
							$('#photo-overlay').css({'width' : 796, 'height' : height_content});
							
							if (type_photo == 'diptyque') {
								
								$('#photo-overlay').css({'width' : 0, 'height' : 0});
								
								$(this).bind('click', function() {
									$('#photo-overlay').css({'width' : 796, 'height' : 590});
								});
								
							}
								
							$('#loading').fadeOut('fast', 'swing', function() {
									
								$('#photo-block-content, #photo-infos, #photo-roll').fadeIn('fast', 'swing');
								$('#photo-previous').show();
								$('#photo-next').show();
								
							});
						});
					}});
				});
			});
		}
		else {
			//photo_gallery = $('#first-picture').attr("title");
		}
	});

	
	$('#photos-carrousel').find('li').click ( function() {
		//alert('');
		
		$('#photo-previous').css("visibility", "visible");
		pos_photo_gal = $(this).attr('id');
		var id_photo = $(this).attr('title');
	
		photo_gallery = id_photo;
		var nbr_photos = $('#photos-carrousel').find('li').length;
		$('#photo-infos, #photo-roll').fadeOut('fast', 'swing');
		$('#photo-block').find('#photo-block-content').fadeOut('fast', 'swing', function() {
			
			$('#photo-block').find('#photo-block-zoom').fadeOut('fast', 'swing');
			$(this).remove();
			$('#photo-previous').hide();
			$('#photo-next').hide();
			$('#photo-infos').empty();
			
			$('#loading').fadeIn('fast', 'swing', function() {
				
				$.ajax({
					type: "GET",
					url: "/xml/photo-view.php",
					data: "id_photo="+id_photo,
					dataType: "xml",
					complete: function(data, status) {
					
					var link = data.responseXML;
					
					$(link).find('photo').each(function() {
						
						var title_photo = $(this).find('title').text();
						var url_photo = $(this).find('url').text();
						var caption_photo = $(this).find('caption').text();
						var type_photo = $(this).find('type').text();
						var full_url_photo = "http://www.martinbruno.fr/pictures/zoom/"+url_photo+"-zoom";
						
						var content_box = "<img id='photo-block-content' src='http://www.martinbruno.fr/pictures/"+url_photo+".jpg' style='display: none;' />";
						var content_caption = "<p id='photo-caption'>"+caption_photo+"</p>";
						var content_nbr = "<p id='photo-nbr'>"+pos_photo_gal+" /"+nbr_photos+"</p>";
						
						$(content_nbr).appendTo('#photo-infos');
						$(content_caption).appendTo('#photo-infos');
						
						$(content_box).appendTo('#lightme');
						document.getElementById('lightme').setAttribute('href', full_url_photo);
						
						$('#photo-block-content').load( function() {
							
							var height_content = $('#photo-block-content').height();							
							$('#photo-overlay').css({'width' : 796, 'height' : height_content});
							
							if (type_photo == 'diptyque') {
								
								$('#photo-overlay').css({'width' : 0, 'height' : 0});
																
								$(this).bind('click', function() {
									$('#photo-overlay').css({'width' : 796, 'height' : 590});
								});
								
							}
							
							$('#loading').fadeOut('fast', 'swing', function() {
								
								$('#photo-block-content, #photo-infos, #photo-roll').fadeIn('fast', 'swing');
								$('#photo-previous').show();
								$('#photo-next').show();
							});
						});
					});
				}});
			});
		});
	});
	
	
	$('#photo-next').click ( function() {
		
		var nbr_photos = $('#photos-carrousel').find('li').length;
		
		photo_gallery++;
		pos_photo_gal++;
		
		id_gallery = "photo"+pos_photo_gal;
		
		if (pos_photo_gal <= nbr_photos) {
			$('#photo-previous').css("visibility", "visible");
			//alert('');
			$('#photo-infos, #photo-roll').fadeOut('fast','swing');
			$('#photo-block').find('#photo-block-content').fadeOut('fast', 'swing', function() {
				$('#photo-block').find('#photo-block-zoom').fadeOut('fast', 'swing');
				$(this).remove();
				$('#photo-previous').hide();
				$('#photo-next').hide();
				$('#photo-infos').empty();
				
				$('#loading').fadeIn('fast', 'swing', function() {
					
					$.ajax({
						type: "GET",
						url: "/xml/gallery-photo-view.php",
						data: "id_gallery="+gallery_photos_selec,
						dataType: "xml",
						complete: function(data, status) {
						//alert(status);
						var link = data.responseXML;
	
						var title_photo = $(link).find(id_gallery).find('title').text();
						var url_photo = $(link).find(id_gallery).find('url').text();
						var caption_photo = $(link).find(id_gallery).find('caption').text();
						var type_photo = $(link).find(id_gallery).find('type').text();
						var full_url_photo = "http://www.martinbruno.fr/pictures/zoom/"+url_photo+"-zoom";						
							
						var content_box = "<img id='photo-block-content' src='http://www.martinbruno.fr/pictures/"+url_photo+".jpg' style='display: none;' />";
						var content_caption = "<p id='photo-caption'>"+caption_photo+"</p>";
						var content_nbr = "<p id='photo-nbr'>"+pos_photo_gal+" /"+nbr_photos+"</p>";
							
						$(content_nbr).appendTo('#photo-infos');
						$(content_caption).appendTo('#photo-infos');
						document.getElementById('lightme').setAttribute('href', full_url_photo);
							
						$(content_box).appendTo('#lightme');
												
						$('#photo-block-content').load( function() {
							
							var height_content = $('#photo-block-content').height();							
							$('#photo-overlay').css({'width' : 796, 'height' : height_content});
								
							if (type_photo == 'diptyque') {
								
								$('#photo-overlay').css({'width' : 0, 'height' : 0});
								
								$(this).bind('click', function() {
									$('#photo-overlay').css({'width' : 796, 'height' : 590});
								});
								
							}
								
							$('#loading').fadeOut('fast', 'swing', function() {
									
								$('#photo-block-content, #photo-infos, #photo-roll').fadeIn('fast', 'swing');
								$('#photo-previous').show();
								$('#photo-next').show();
									
							});
						});
					}});
				});
			});
		}
		else {
			//photo_gallery = $('#first-picture').attr("title");
		}
	});
	
	$('#photo-previous').click ( function() {
		//alert('');
		var nbr_photos_real = $('#photos-carrousel').find('li').length;
		
		photo_gallery--;
		pos_photo_gal--;
		
		id_gallery = "photo"+pos_photo_gal;
		
		if (pos_photo_gal > 0) {
			
			$('#photo-infos, #photo-roll').fadeOut('fast','swing');
			$('#photo-block').find('#photo-block-content').fadeOut('fast', 'swing', function() {
				
				$('#photo-block').find('#photo-block-zoom').fadeOut('fast', 'swing');
				$(this).remove();
				$('#photo-previous').hide();
				$('#photo-next').hide();
				$('#photo-infos').empty();
				
				$('#loading').fadeIn('fast', 'swing', function() {
					
					$.ajax({
						type: "GET",
						url: "/xml/gallery-photo-view.php",
						data: "id_gallery="+gallery_photos_selec,
						dataType: "xml",
						complete: function(data, status) {
						//alert(status);
						var link = data.responseXML;
						
						var title_photo = $(link).find(id_gallery).find('title').text();
						var url_photo = $(link).find(id_gallery).find('url').text();
						var caption_photo = $(link).find(id_gallery).find('caption').text();
						var type_photo = $(link).find(id_gallery).find('type').text();
						var full_url_photo = "http://www.martinbruno.fr/pictures/zoom/"+url_photo+"-zoom";
							
						var content_box = "<img id='photo-block-content' src='http://www.martinbruno.fr/pictures/"+url_photo+".jpg' style='display: none;' />";
						var content_caption = "<p id='photo-caption'>"+caption_photo+"</p>";
						var content_nbr = "<p id='photo-nbr'>"+pos_photo_gal+" /"+nbr_photos_real+"</p>";
						
						$(content_nbr).appendTo('#photo-infos');
						$(content_caption).appendTo('#photo-infos');
							
						document.getElementById('lightme').setAttribute('href', full_url_photo);
						$(content_box).appendTo('#lightme');
							
						$('#photo-block-content').load( function() {
							
							var height_content = $('#photo-block-content').height();							
							$('#photo-overlay').css({'width' : 796, 'height' : height_content});
							
							if (type_photo == 'diptyque') {
								
								$('#photo-overlay').css({'width' : 0, 'height' : 0});
								
								$(this).bind('click', function() {
									$('#photo-overlay').css({'width' : 796, 'height' : 590});
								});
								
							}

							$('#loading').fadeOut('fast', 'swing', function() {
									
								$('#photo-block-content, #photo-infos, #photo-roll').fadeIn('fast', 'swing');
								$('#photo-previous').show();
								$('#photo-next').show();
									
							});
						});

					}});
				});
			});
		}
		else {
			pos_photo_gal = 1;
			photo_gallery++;
			$('#photo-previous').css("visibility", "hidden");
		}
	});
	
	
});


