var windowWidth  		= '';
var windowHeight 		= '';
var windowRatio 		= '';
var backgroundLowWidth 	= '';
var backgroundLowHeight = '';
var backgroundRatio     = '';
var timer 				= '';
var resizing 			= false;
var iFile				= '';
var isHomePage			= false;
var resizeAllowed		= true;
var disableThumbInfos	= false;
var currentPicture		= 0;
var lastScroll			= {left:0, top:0};

$(document).ready(function() {
    resize(true);

	$('#backgroundImage').click(function() {
   		window.location = "projets/";
   	});
   	
	$('#home').click(function() {
   		window.location = "projets/";
   	});

	var w = $(window).width();    
	var h = $(window).height();    
    $('#home').css('top', h/2-202/2);
    $('#home').css('left', w/2-420/2);
    if (isHomePage) setTimeout(changeImage, 10000);
    
    //PAGE CONTACT
    $('.people').mouseenter(function() {
    	$(this).children('img').show();
    }).mouseleave(function() {
    	$(this).children('img').hide();    	
    });
    
    //PAGE JOB
    $('.jobTitle').click(function() {
    	$('.jobBox').hide();
    	$('.jobTitle').css('margin-bottom', '2px');
    	
    	if ($(this).next('div').is(':visible')) {
    		$(this).next('div').hide();
    	} else {
    		$(this).next('div').show();
    		$(this).css('margin-bottom', '0px');
    		resize(true);
    		$(this).next('div').find('input[type=text]').first().focus();
    	}
    });
    
    //Menu projet
    $('#ProjectMenu ul li').click(function() {
    	if ($(this).hasClass('first')) {
    		$('#ProjectMenu ul li').removeClass('opened');		
    		$(this).addClass('opened');
    		$('.sub').hide();
    		$(this).next('li').show();
	    	$('#ProjectMenu ul li ul li').css('color', '#576238').removeClass('subSel');		
    		$(this).next('.sub').children('ul').children('li').first().addClass('subSel');
	    	
	    	var type = $(this).attr('rel');
	    	
			if (type == 'type') $('.subSel').css('color', '#C0FF00');
			if (type == 'date') $('.subSel').css('color', '#ec028c');
			if (type == 'city') $('.subSel').css('color', '#23c3f3');
			if (type == 'size') $('.subSel').css('color', '#ecd702');
			
			disableThumbInfos = false;
			$('#projectViewer').hide();
			$('#projectMarker').hide();
			
			
	    	$.ajax({
   				type: "GET",
   				url: "/getGallery.php?mode="+type,
   				success: function(data) {
     				$('#projectsContainer').replaceWith(data);
    				$('#projectsContainer').css('height', $('#prj').height()+100);
				    $('#color').css('height', $('.projects').first().height()-25);
	    			if (type == 'type') $('#color').css('background-image', 'url(/images/color_1.png)');
	    			if (type == 'date') $('#color').css('background-image', 'url(/images/color_2.png)');
		    		if (type == 'city') $('#color').css('background-image', 'url(/images/color_3.png)');
		    		if (type == 'size') $('#color').css('background-image', 'url(/images/color_4.png)');
   				}
 			});
    	}
    });
        
    $('#ProjectMenu ul li ul li').click(function() {
    	closeProject();
    	var color = $('.subSel').css('color');
    	$('.subSel').css('color', ''); //'#576238');

    	$('#ProjectMenu ul li ul li').removeClass('subSel');
   		$(this).addClass('subSel');
   		$(this).css('color', color);
    	filterCat($(this).attr('rel'));	
    });
    
    //Afficher les images, etc.
    $('.projectThumb').live('click', function() {
    	disableThumbInfos = true;
    	currentPicture = 0;
    	$('.lastOpened').css('height', $('.lastOpened').height() -480);
    	$('.lastOpened').removeClass('lastOpened');
    	$(this).addClass('lastOpened');
    	//alert($(this).height());
    	//$(this).css('height', $(this).height() +480); //.parents('table').css('opacity', 0.3);
    	$(this).css('height', 102 +480); //.parents('table').css('opacity', 0.3);
    	$('.projects').css('opacity', 0.3);
    	$('.thumbInfos').hide();
	    
	    $('#projectViewer').css('top', $(this).offset().top+102).show();
	    
	    var w = $(this).children('div').children('img').width();
	    $('#projectMarker').css('background', $('.subSel').css('color')).css('width', w).css('top', $(this).offset().top+97).css('left', $(this).position().left+210).show();
 		$('#projectsContainer').css('height', $('#prj').height()+100);
		
		/*setTimeout(function() {
			$('#projectPictureLine').animate({
    			height:$('#img_0').height(),
   			}, 100);}, 500);*/

		var cP = $('#color').position().top;
		var pP = $(this).parents('.projects').position().top+$('#prj').position().top;
		if (cP + pP == 0) {
		    $('#color').css('height', $(this).parents('.projects').height()-25).css('opacity', 0.3);
		}
		lastScroll.left = $(window).scrollLeft();
		lastScroll.top  = $(window).scrollTop();
		
		window.scrollTo(0, $(this).offset().top-25);
				
	    $.ajax({
   		    type: "GET",
   		    url: "/getProject.php?id="+$(this).attr('rel'),
   		    success: function(data) {
     	    	$('#projectViewer').html(data);
     	    	$('.imgMed img').click(function() {
     	    		nextPicture();
     	    	})
     	    	
     	    	$('.imgMed').first().css('opacity', 0).animate({
     	    		opacity:1,
     	    		left: '0'
     	    	}, 300);
     	    	
     	    	$('#site').after($('#projectPicturesBig'));
     	    	$('#site').after($('#projectDescBig'));
     	    	$('.imgBig img').click(function() {
     	    		nextPicture();
     	    	})
     	    	
     	    	$('.imgBig').first().css('opacity', 0).animate({
     	    		opacity:1,
     	    		left: '0'
     	    	}, 300);
     	    	
     	    	$('.close').click(function() {
     	    		closeProject();	
     	    	});
     	    	
     	    	$('.prev').click(function() {
     	    		prevPicture();
     	    	});

     	    	$('.next').click(function() {
     	    		nextPicture();
     	    	});
     	    	
     	    	$('.zoomIn').click(function() {
     	    		zoomIn();
     	    	});
     	    	
     	    	$('.zoomOut').click(function() {
     	    		zoomOut();
     	    	});
     	    	
     	    	$('.informations').click(function() {
     	    		if (!$('#projectDescBig').is(':visible')) zoomIn();
     	    		
     	    		$('#projectPicturesBig').toggle();
     	    		$('#projectDescBig').toggle();
     	    	});
   		    }
 		});
    });
    
    //Projets
    $('#projectsContainer').css('height', $('#prj').height()+100);
    $('#color').css('height', $('.projects').first().height()-25);
    
    $('.thumbContainer').live('mouseover', function() {
    	if (disableThumbInfos) return;
    	$('#projectMarker').css('background', $('.subSel').css('color')).css('top', $(this).offset().top+$(this).height()-5).css('left', $(this).position().left+210).show();
    	
    	$('.thumbInfos').hide();
    	$(this).children('.thumbInfos').css('height', $(this).height()).show();
    }).live('mouseout', function() {
    	if (disableThumbInfos) return;
    	$('.thumbInfos').hide();
    	$('#projectMarker').hide();
    })
});

function closeProject() {
	if ($('.lastOpened').parents('.projects').get(0) == undefined) return;

	$('#projectDescBig').hide();
	$('#projectPicturesBig').hide();
	$('#site').show();	

    disableThumbInfos = false;
    	
    $('.lastOpened').css('height', $('.lastOpened').height() -480);
	//$('.lastOpened').parents('table').css('opacity', 1);
	$('.projects').css('opacity', 1);
	
	var cP = $('#color').position().top;
	var pP = $('.lastOpened').parents('.projects').position().top+$('#prj').position().top;
	if (cP + pP == 0) {
	    $('#color').css('height', $('.lastOpened').parents('.projects').height()-25).css('opacity', 1);
	}	
	
	//$('#color').hide();
   	$('.lastOpened').removeClass('lastOpened');
   	
	$('#projectViewer').empty().hide();
    $('#projectMarker').css('width', '146px').hide();
 	$('#projectsContainer').css('height', $('#prj').height()+100);

	currentPicture = 0;
	window.scrollTo(lastScroll.left, lastScroll.top);
}

function zoomIn() {
	lastScroll.left = $(window).scrollLeft();
	lastScroll.top  = $(window).scrollTop();

	$('#projectPicturesBig').show();
	$('#site').hide();
}

function zoomOut() {
	$('#projectPicturesBig').hide();
	$('#projectDescBig').hide();
	$('#site').show();
	
	window.scrollTo(lastScroll.left, lastScroll.top);
}

function nextPicture() {
	if ($('.imgMed').size() == 1) return;
	$('#img_'+currentPicture).animate({
    	opacity:0,
     	left: '-500'
    }, 500, function() {
    	$(this).hide();
    });

	$('#imgBig_'+currentPicture).animate({
    	opacity:0,
     	left: '-500'
    }, 500, function() {
    	$(this).hide();
    });

	currentPicture++;
	
	if (currentPicture >= $('.imgMed').size()) currentPicture = 0;
	
	/*$('#projectPictureLine').animate({
    	height:$('#img_'+currentPicture).height(),
    }, 500);*/
    
	/*$('#projectPictureLineBig').animate({
    	height:$('#imgBig_'+currentPicture).height(),
    }, 500);*/

	$('#img_'+currentPicture).css('opacity', 0).show().animate({
    	opacity:1,
     	left: '0'
    }, 500);
    
	$('#imgBig_'+currentPicture).css('opacity', 0).show().animate({
    	opacity:1,
     	left: '0'
    }, 500);

    $('.navNum').html((currentPicture+1));  
}

function prevPicture() {
	if ($('.imgMed').size() == 1) return;

	$('#img_'+currentPicture).animate({
    	opacity:0,
     	left: '-500'
    }, 500, function() {
    	$(this).hide();
    });

	$('#imgBig_'+currentPicture).animate({
    	opacity:0,
     	left: '-500'
    }, 500, function() {
    	$(this).hide();
    });

	currentPicture--;
	
	if (currentPicture < 0) currentPicture = $('.imgMed').size()-1;
	
	$('#img_'+currentPicture).css('opacity', 0).show().animate({
    	opacity:1,
     	left: '0'
    }, 500);
    
	$('#imgBig_'+currentPicture).css('opacity', 0).show().animate({
    	opacity:1,
     	left: '0'
    }, 500);
    
    $('.navNum').html((currentPicture+1));  
}

function filterCat(cat) {
	cat = cat.split(",");

	$('#prj').animate({ 
        top: -$('#p_'+cat[1]).position().top
    }, 250 , function() { 
    	$('#prj').css('top', '0px');
    	var elms = $('#p_'+cat[1]).prevAll().toArray();
    	
    	for (var i = elms.length -1; i >= 0; i--) {
    		$('#prj').append(elms[i]);	
    	}
    	$('.projects').hide().show();
    });
    
	$("#color").animate({ 
        height: $('#p_'+cat[1]).height()-25
    }, 250 );
}

$(window).resize(function() {
    resize(false);

	var w = $(window).width();    
	var h = $(window).height();    
    $('#home').css('top', h/2-202/2);
    $('#home').css('left', w/2-420/2);
});

function changeImage() {
	currentFile++;

	if (currentFile == images.length) currentFile = 0;
	
	resizeAllowed = false;
	
	$('#backgroundImageLow').fadeOut('slow');
	$('#backgroundImageHigh').fadeOut('slow', function () {
		$('#backgroundImageLow').attr('src', '/img.php?file='+images[currentFile].file+'&w=1024');
		$('#backgroundImageLow').css('width', images[currentFile].width).css('height', images[currentFile].height);
		$('#backgroundImageHigh').attr('src', '/images/empty.png');
		resize(true);	
	    $('#backgroundImageLow').hide();
	    $('#backgroundImageHigh').hide();
		$('#backgroundImageLow').fadeIn('slow');
		$('#backgroundImageHigh').fadeIn('slow', function () {
			resizeAllowed = true;
		    setTimeout(changeImage, 10000);
		});
	});

}

function resize(doRatio) {
	if (!resizeAllowed && !doRatio) return;
	
    $('#backgroundImage').css('height', '0px');

    windowWidth  = $(window).width();
    windowHeight = $(document).height();

	if (jQuery.browser.msie) windowHeight -= 4;
    windowRatio 				= windowWidth / windowHeight;

    backgroundLowWidth  = $('#backgroundImageLow').width();
    backgroundLowHeight = $('#backgroundImageLow').height();
    if(doRatio) backgroundRatio = backgroundLowWidth / backgroundLowHeight;

    $('#backgroundImage').css('height', windowHeight);

    $('#backgroundImage').css('width', windowWidth);

    if (windowRatio > backgroundRatio) {
    	$('#backgroundImageLow').css('width',    windowWidth);
    	$('#backgroundImageLow').css('height',   (windowWidth/backgroundRatio));
    	$('#backgroundImageHigh').css('width',   windowWidth);
    	$('#backgroundImageHigh').css('height',  (windowWidth/backgroundRatio));
    } else {
    	$('#backgroundImageLow').css('width',   (windowHeight*backgroundRatio));
    	$('#backgroundImageLow').css('height',  windowHeight);
    	$('#backgroundImageHigh').css('width',  (windowHeight*backgroundRatio));
    	$('#backgroundImageHigh').css('height', windowHeight);
    }
    
    if (resizing) $('#backgroundImageHigh').hide();
    
    if(doRatio) updatePicture(windowWidth, windowHeight);
    			    
    resizing = true;
    
    clearTimeout(timer)
    timer = setTimeout(updatePicture,500);
}

function updatePicture() {
    $('#backgroundImageLow').show();
    $('#backgroundImageHigh').show();
    if (windowRatio > backgroundRatio) {
    	$('#backgroundImageHigh').attr('src', "/img.php?file="+images[currentFile].file+"&w="+windowWidth);
    } else {
    	$('#backgroundImageHigh').attr('src', "/img.php?file="+images[currentFile].file+"&h="+windowHeight);
    }
    resizing = false;
}


