$(document).ready(function() {
	// initialise menu
    $('#header_menu > ul > li > ul').addClass('ssmenu');
	$('#header_menu > ul > li > a').mouseover(function() {
	    if (!$('#cache').get(0)) {
            $('<div id="cache">').mouseover(function() {
                $('.ssmenu').hide();
                $(this).hide();
            }).prependTo('body');
	    }
	    $('.ssmenu').hide();
	    $('#cache').show();
	    var pa = $(this).offset();
	    var ul = $('> ul', $(this).parent()).get(0);
	    $(ul).css({
	        left : pa.left + $(this).width() / 2 - $(ul).width() / 2,
	        top : pa.top + 20
	    }).show();
	});
	// image resize
	$('img.billet_vignette').each(function() {
		initImgSize($(this));
	});

	majSize();
	$(window).resize(function(){
		majSize();
	});

});

//Cette fonction active le hover sur les billet et le lien vers l'article
function initBlog() {
	$('div.bloc_3 .billets .billet').each(function(index){
		if($(this).attr('data_init') != 'ok') {
			if (iPhoneDetect()) {
				$(this).click(function(event) {
					var myElements = $(this).children();
					var monBillet = $(myElements[0]);
					var url = monBillet.attr('data');
	                window.location=url;
	            });
			} else {
				$(this).mouseenter(function(event) {
	                $(this).css('cursor','pointer');
	                $(this).find('.infosMessage').fadeIn();
	              });
				$(this).mouseleave(function(event) {
	                $(this).find('.infosMessage').fadeOut();
	                $(this).css('cursor','auto');
	              });
				var myElements = $(this).children();
				var monBillet = $(myElements[0]);
				var url = monBillet.attr('data');
				$(this).click(function(event) {
	                window.location=url;
	            });
			}
			$(this).attr('data_init','ok');
		}
	});
}

// Initialise une image pour qu'elle scale au resize sans depasser sa largeur max
function initImgSize(img) {
	var imgSrc = img.attr('src');
    var newImg = new Image();
    newImg.src = imgSrc; // this must be done AFTER setting onload
    newImg.onload = function() {
	    var height = newImg.height;
	    var width = newImg.width;
	    /*         'max-width' : width + 'px',*/
	    img.css({
    		'width' : '100%'
    	});
    };
}

function urlencode(str) {
    return escape(str.replace(/%/g, '%25').replace(/\+/g, '%2B')).replace(/%25/g, '%');
}
function recherche() {
    /*var saisie = prompt("Votre recherche :", "Mots a chercher");
    if (saisie!=null) {
        location.href='?todo=search&search='+urlencode(saisie);
    }*/
	$.colorbox({
        href			: 'ajax/front/formulaire.htm?todo=recherche',
        width			: '300px',
        transition      : 'none',
        speed			: 0,
        opacity			: 0.4
    });
}
function identification() {
    /*var saisie = prompt("Mot de passe :", "");
    if (saisie!=null) {
        location.href='?todo=connect&mdp='+urlencode(saisie);
    }*/
    $.colorbox({
        href			: 'ajax/front/formulaire.htm?todo=identification',
        width			: '300px',
        transition      : 'none',
        speed			: 0,
        opacity			: 0.4
    });
}

function iPhoneDetect() {
    return ((navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/Android/i))) && (!(navigator.userAgent.match(/iPad/i)));
}

function majSize() {
	if (window.innerWidth) {
		var larg = window.innerWidth -60;
	} else if (document.body && document.body.offsetWidth) {
		var larg = document.body.offsetWidth -60;
	}
	//var larg = ((document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth)-60;
	if (larg>=1680) {larg=1680;}
    if (larg>1280 && larg<1680) {larg=1280;}
    var colonne2=0;
    if (larg>768) {colonne2=235;}
    if (larg>992) {colonne2=460;$('.derniers_billets').css('width','220px');}
	if (iPhoneDetect() || larg<1280){larg = larg+24;}
   $('.content').css('width',larg+"px");

   $('.groupe_1').css('width',(larg-colonne2-20)+"px");
   if(iPhoneDetect()){
	   $('.groupe_1, .groupe_2').css({
			'float'		: 'none',
			'margin'	: '0 auto'
	   });
		$('.groupe_2, .groupe_2 .bloc').css('width','100%');
		//$('.bloc, div.liens, div.archives, div.tags, div.qui_suis_je, div.derniers_billets').css('float','none');
		$('div#liens a, div#archives a, div.tags a, div.qui_suis_je a, div.derniers_billets a').css({
     	   'padding-top' : '7px',
     	   'padding-bottom' : '7px'
     	   });
 	    $('.derniers_billets').css('width','100%');
   }

   $('.contour_billet_vignette').css('width',(larg-colonne2-40)+"px");
   $('.billet_vignette').css('width',(larg-colonne2-40)+"px");
   $('.ombre_vignette_billet').css('width',(larg-colonne2-20)+"px");
   switch(larg) {
   		case 1280 :
	   		$('.content').css({
	     		'margin':'0 auto',
	     		'float' :'none'
	        });
	   		regularSize();
   		break;
   		case 1680 :
	   		$('.derniers_billets').css('width','360px');
	   		$('div.qui_suis_je .reseaux_sociaux').css('margin-left','12px');
	   		$('div.qui_suis_je .reseaux_sociaux a.picto').css({'margin-right':'5px'});
   		break;
   		default :
		   	$('.content').css({
		  		'margin':'0px',
		  		'float' :'left'
		    });
   			regularSize();
   		break;
   }
   initBlog();
}

function regularSize() {
	if(!iPhoneDetect()) {
		$('.groupe_2').css({
			'width' : '',
			'margin-left' : ''
		});
		$('.bloc').css({
			'width' 		: '',
			'margin-right'	: ''
		});
	}
	$('div.qui_suis_je .reseaux_sociaux').css('margin-left','');
	$('div.qui_suis_je .reseaux_sociaux a.picto').css({'margin-right':''});
}
