function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js'];c=b.join(' ');h.className+=' '+c;return c;};css_browser_selector(navigator.userAgent);

var rtime, timeout, delta;

(function($) {
    $.fn.preload = function(options) {
        var opts    = $.extend({}, $.fn.preload.defaults, options);
        o           = $.meta ? $.extend({}, opts, this.data()) : opts;
        var c       = this.length,
            l       = 0;
        return this.each(function() {
            var $i  = $(this);
            $('<img/>').load(function(i){
                ++l;
                if(l == c) o.onComplete();
            }).attr('src',$i.attr('original'));  
        });
    };
    $.fn.preload.defaults = {
        onComplete  : function(){return false;}
    };
})(jQuery);

$(document).ready(function() {
	
	timeout = false;
	delta = 200;
	$(window).resize(function() {
	    rtime = new Date();
	    if (timeout === false) {
	        timeout = true;
	        setTimeout(resizeend, delta);
	    }
	});
	
	sizeElement();
		
	$('#navigation_top').click(function() {
		var scrollTop = parseInt($(window).scrollTop());
		var height = $(window).height();
		var page = Math.ceil(scrollTop / height);		
		if(page==0) {
			$(this).fadeOut();
		}
		$('#navigation_bottom').fadeIn();
		$('html,body').animate({scrollTop: (page-1)*height}, 'slow');
	});
	
	$(window).scroll(function(e) { 
		var scrollTop = parseInt($(window).scrollTop());
		var height = $(window).height();
		var page = Math.ceil(scrollTop / height);
		if(scrollTop>0) {
			$('#navigation_top').fadeIn();
		} else {
			$('#navigation_top').fadeOut();
		}
		page = Math.floor(scrollTop / height);
		if(page<4) {
			$('#navigation_bottom').fadeIn();
		} else {
			$('#navigation_bottom').fadeOut();
		}
		$("#menu .actif").removeClass('actif');
		switch(page) {
			case 0: $("#menu .accueil").addClass('actif');break;
			case 1: $("#menu .solutions").addClass('actif');break;
			case 2: $("#menu .actualites").addClass('actif');break;
			case 3: $("#menu .realisations").addClass('actif');break;
			case 4: $("#menu .contact").addClass('actif');break;
		}
	});
	
	$('#navigation_bottom').click(function() {
		var scrollTop = parseInt($(window).scrollTop());
		var height = $(window).height();
		var page = Math.floor(scrollTop / height);
		if(page==3) {
			$(this).fadeOut();
		}
		$('#navigation_top').fadeIn();
		$('html,body').animate({scrollTop: (page+1)*height}, 'slow');
	});
	
	$("#menu li").each(function() {
		$(this).click(function() {
			var page = $(this).attr('page');
			$('html,body').animate({scrollTop: page*$(window).height()}, 'slow');
		});
	});
	
	$("#menu").hover(function() {
		$(this).animate({top:0}, {queue:false});
	}, function() {
		$(this).animate({top:'-605px'}, {queue:false});
	});

	var height = $(window).height() - $(".description").height() - parseInt($(".description").css('margin-top')) - parseInt($(".description").css('margin-bottom')) - 100;
	
	var nombre = Math.floor(height / 213)*3;

//	$.post('/?type=611', function(data) {
//		$("#actualites").prepend(data);
//	});
//	$.post('/zend/actualite/lastnews?pid=6', function(data) {
//		if(data.code==0) {
//			$("#actualites").prepend(data.content);			
//		}
//	}, "json");
	
	window.sessionStorage.setItem('nombre', nombre);
	window.sessionStorage.setItem('tag', 0);
//	$.post('/index.php?type=610', {
//		n: nombre,
//		p: 0,
//		t: 0
//	}, function(data) {
//		var height = nombre/3*213;
//		$("#realisations").append($(data));
//		$(".list").height(height);
//		$('.projet').each(function(i) {
//			$(this).css('top', height);
//			
//			$(this).css('left', (310*(i%3))+'px');
//			$(this).fadeIn(i*100, function() {
//				var top = 213*Math.floor(i/3);
//				$(this).animate({top: top+'px'});
//			});
//		});
//		var page = $(".scrollbar .bouton").size();
//		$(".scrollbar").css('padding-top', (nombre/6*(213-(page*8)))+'px');
//		$('.bouton').each(function() {
//			$(this).click(function() {
//				$('.click').removeClass('click');
//				$(this).addClass('click');				
//				var page = parseInt($(this).attr('page'));				
//				var tag = window.sessionStorage.getItem('tag');
//				var nombre = window.sessionStorage.getItem('nombre');
//				$(".projet").each(function() {
//					$(this).addClass('old');
//				});
//				$.post('/?type=610', {
//					n: nombre,
//					p: page,
//					t: tag
//				}, function(data) {
//					var height = $('.list').height();
//					$list = $(data).find('.list');					
//					$(".projet").each(function(i) {	
//						$(this).animate({top: '-='+height}, function() {
//							$(this).remove();
//						});
//					});
//					$list.find('.projet').each(function(i) {
//						$('.list').append($(this));
//						$(this).css('top', height);
//						
//						$(this).css('left', (310*(i%3))+'px');
//						$(this).fadeIn(i*100, function() {
//							var top = 213*Math.floor(i/3);
//							$(this).animate({top: top+'px'});
//						});
//					});
//				});					
//			});
//		});
//		$(".navigation li").each(function() {
//			$(this).click(function() {
//				$('.navigation .actif').removeClass('actif');
//				$(this).addClass('actif');
//				var tag = parseInt($(this).attr('uid'));
//				window.sessionStorage.setItem('tag', tag);
//				var nombre = window.sessionStorage.getItem('nombre');
//				$.post('/?type=610', {
//					n: nombre,
//					p: 0,
//					t: tag
//				}, function(data) {
//					var height = $('.list').height();
//					$list = $(data).find('.list');	
//					$(".projet").each(function(i) {	
//						$(this).animate({top: '-='+height}, function() {
//							$(this).remove();
//						});
//					});
//					$(".scrollbar").empty().append($(data).find('.scrollbar').contents());
//					$('.bouton').each(function() {
//						$(this).click(function() {
//							$('.click').removeClass('click');
//							$(this).addClass('click');				
//							var page = parseInt($(this).attr('page'));				
//							var tag = window.sessionStorage.getItem('tag');
//							var nombre = window.sessionStorage.getItem('nombre');
//							$(".projet").each(function() {
//								$(this).addClass('old');
//							});
//							$.post('/?type=610', {
//								n: nombre,
//								p: page,
//								t: tag
//							}, function(data) {
//								var height = $('.list').height();
//								$list = $(data).find('.list');					
//								$(".projet").each(function(i) {	
//									$(this).animate({top: '-='+height}, function() {
//										$(this).remove();
//									});
//								});
//								$list.find('.projet').each(function(i) {
//									$('.list').append($(this));
//									$(this).css('top', height);
//									
//									$(this).css('left', (310*(i%3))+'px');
//									$(this).fadeIn(i*100, function() {
//										var top = 213*Math.floor(i/3);
//										$(this).animate({top: top+'px'});
//									});
//								});
//							});					
//						});
//					});
//					$list.find('.projet').each(function(i) {
//						$('.list').append($(this));
//						$(this).css('top', height);
//						
//						$(this).css('left', (310*(i%3))+'px');
//						$(this).fadeIn(i*100, function() {
//							var top = 213*Math.floor(i/3);
//							$(this).animate({top: top+'px'});
//						});
//					});
//				});		
//			});
//		});
//	});
});

function resizeend() {
    if (new Date() - rtime < delta) {
        setTimeout(resizeend, delta);
    } else {
    	timeout = false;
    	sizeElement();
    }               
}

function sizeElement() {
	var largeur = (($(window).width()-880)/2)-($("#menu").width())-10;
	if(largeur<0) {
		$(".description").width(880+largeur);		
		$(".description p").width(600+largeur);		
		$(".description").css('margin-left', Math.abs(largeur)+'px');		
	} else {
		$(".description").width(880);		
		$(".description p").width(600);		
		$(".description").css('margin-left', '0px');
	}
	
	$("#page_accueil").height($(window).height());
	$("#page_solutions").height($(window).height());
	$("#page_actualites").height($(window).height());
	$("#page_realisations").height($(window).height());
	$("#page_contact").height($(window).height());
	var topBottom = $(window).height()-20-$('#navigation_bottom').height();
	$('#navigation_bottom').css('top', topBottom);
	var marginTop = ($(window).height()-$("#accueil").height()-parseInt($("#accueil").css('padding-top')))/2;
	$("#accueil").css('margin-top', marginTop);
	marginTop = ($(window).height()-$("#contact").height())/2;
	$("#contact").css('margin-top', marginTop);
	marginTop = ($(window).height()-$("#solutions").height())/2;
	$("#solutions").css('margin-top', marginTop);
	marginTop = ($(window).height()-$("#actualites").height())/2;
	$("#actualites").css('margin-top', marginTop);
} 

function initImage() {	
	var w = $(window).width()-40;
	$('#global').width(w);
	w = $("#entete").width()-60;
	$('.image_fond').width(w);

	$tf_bg_img = $('.image_fond').find('img');
	var src = $(".fiche:first-child .image").attr('original');
	$tf_bg_img.attr('src', src);
    var dim = getImageDim($tf_bg_img);
    //set the returned values and show the image
    $tf_bg_img.css({
        width   : dim.width,
        height  : dim.height,
        left    : dim.left,
        top     : dim.top
    }).fadeIn();
    	
	$(".information").attr('ref', $(".fiche:first-child").attr('id'));
	$(".information .titre").text($(".fiche:first-child .titre").text());
	$(".information .description").text($(".fiche:first-child .description").text());
}

function refreshImage(src) {
	$tmp = $tf_bg_img;
	var w = $(window).width()-40;
	$('#global').width(w);
	
	$tf_bg_img = $('<img/>');
	$tf_bg_img.attr('src', src).addClass('image');
	w = $("#entete").width()-60;
	$('.image_fond').width(w).prepend($tf_bg_img);
	$('<img/>').load(function() {
		var dim	= getImageDim($tf_bg_img);
		$tf_bg_img.css({
			width	: dim.width,
			height	: dim.height,
			left	: dim.left,
			top		: dim.top
		}).show();
		$tmp.fadeOut(function() {
			$(this).remove();
		});
//		$tmp.animate({left: '-'+$('.image_fond').width()}, 500, function() {
//			$(this).remove();
//		});
	}).attr('src', src);
}

function getImageDim($img){
	var w_w	= $('.image_fond').width(),
		w_h	= $('.image_fond').height(),
		r_w	= w_h / w_w,
		i_w	= $img.width(),
		i_h	= $img.height(),
		r_i	= i_h / i_w,
		new_w,new_h,
		new_left,new_top;
	
	if(r_w > r_i){
		new_h	= w_h;
		new_w	= w_h / r_i;
	} else {
		new_h	= w_w * r_i;
		new_w	= w_w;
	}
	
	return {
		width	: new_w + 'px',
		height	: new_h + 'px',
		left	: (w_w - new_w) / 2 + 'px',
		top		: (w_h - new_h) / 2 + 'px'
	};
}

