var delay = 3000;

//jQuery's noConflict mode
jQuery.noConflict();
//extension de Jquery pour trouver les positions d'un �l�ment.
jQuery.fn.extend({
    findPos : function() {
        var obj = jQuery(this).get(0);
        var curleft = obj.offsetLeft || 0;
        var curtop = obj.offsetTop || 0;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
        return {
            x:curleft,
            y:curtop
        };
    }
});

var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    $_GET[decode(arguments[1])] = decode(arguments[2]);
});


jQuery(document).ready(function(){
    //gestion du menu en 1 clic
    jQuery('#en1Clic .content ul li').hover(e1cOver, e1cOut);

    //gestion des tools
    jQuery('#tools li a').hover(e1cOver, e1cOut);

    //gestion du menu principal
    jQuery('#mainNav ul.first li .subMenu').addClass('styledSubMenu');
    jQuery('#mainNav ul.first li').hover(mainMenuOver,mainMenuOut);

    //gestion du slideshow de home
    homeSlideShowInit();

    //gestion du navigateur de home
    homeNavigatorInit();

    //gestion des documents en home
    homeDocumentsInit();

    momentDeVieInit();
    
    //initialisation des scrollbars
    jQuery('.newsContainer').jScrollPane();
    jQuery('#en1Clic .content').jScrollPane();

    jQuery('.hideMe').each(hideMe);

    jQuery('.emptyMe').focus(emptyMe);
    jQuery('.emptyMe').blur(fullMe);
    
    jQuery('#centerColumn .tt_news h2 a.news img').hover( 
		function(){
				jQuery(this).attr('src','fileadmin/templates/img/rss_survol_rose.jpg');
		},
		function (){
				jQuery(this).attr('src','fileadmin/templates/img/rss.jpg');
		}
    
    );
    jQuery('#centerColumn .tt_news h2 a.events img').hover( 
		function(){
				jQuery(this).attr('src','fileadmin/templates/img/rss_survol_vert.jpg');
		},
		function (){
				jQuery(this).attr('src','fileadmin/templates/img/rss.jpg');
		}
    
    );
    
    eluSlideShowInit();
    
    OnLoadDate();
    
    if ($_GET['email'] != null){
		jQuery('#newsletter .texte').val(($_GET['email']));
	}

    jQuery('.infoImg').next().addClass('infoTextRight');
});


function momentDeVieInit() {
	if(jQuery('#page').hasClass('int')) {
		var content = jQuery('#momentDeVie ul');
		jQuery('#momentDeVie h2').append( jQuery('<img/>').attr('src','fileadmin/templates/img/momentDeVieArrow.gif') );
		content.css('position','absolute');
		content.hide();
		jQuery('#momentDeVie').hover(
			function () { jQuery(this).children('ul').show() },	
			function () { jQuery(this).children('ul').hide() }	
		);
	}
}

/**
 * Gestion du menu en 1 clic
 */
function e1cOver(){
    var srcName = jQuery(this).children('img').attr('src');
    srcName = srcName.replace('off','on');
    jQuery(this).children('img').attr({
        src:srcName
    });
}

function e1cOut(){
    var srcName = jQuery(this).children('img').attr('src');
    srcName = srcName.replace('on','off');
    jQuery(this).children('img').attr({
        src:srcName
    });
}

/**
 * Gestion des documents en home
 */
var HD_itemWidth = 280;
var HD_itemMargin = 5;
var HD_currentPage = 0;
function homeDocumentsInit() {
    var slide = jQuery('.tx-indoc-pi1 .slide');
    slide.addClass('resize');

    //cr�ation des fl�ches
    var leftA = jQuery('<a/>').addClass('leftArrow')
                                          .addClass('arrow');
    leftA.insertBefore(slide);
    var rightA = jQuery('<a/>').addClass('rightArrow')
                                          .addClass('arrow');
    rightA.insertAfter(slide);

    slide.children('.list').css({width: ((HD_itemWidth + HD_itemMargin) * slide.children('.list').children('.item').length)+'px'})

    leftA.click(homeDocumentPreviousPage);
    rightA.click(homeDocumentNextPage);
}

function homeDocumentPreviousPage(){
    if(HD_currentPage > 0){
        var slide = jQuery('.tx-indoc-pi1 .slide .list');

        HD_currentPage--;
        slide.animate({
            marginLeft:-(HD_currentPage * (HD_itemWidth + HD_itemMargin))+'px'
        });
    }
}

function homeDocumentNextPage(){
    var slide = jQuery('.tx-indoc-pi1 .slide .list');

    if(HD_currentPage < (slide.children('.item').length-1)){

        HD_currentPage++;
        slide.animate({
            marginLeft:-(HD_currentPage *(HD_itemWidth + HD_itemMargin))+'px'
        });
    }
}


/**
 * Gestion du slideshow navigateur de home
 */
var HN_slideshowWidth = 694;
var HN_slideshowHeight = 120;
var HN_imgWidth = 140;
var HN_imgMarginRight = 28;
var HN_imgHeight = 98;
var HN_currentPage = 0;
function homeNavigatorInit() {
    var slide = jQuery('.homeNavigator .content .contentContainer');

    slide.css({
        width:HN_slideshowWidth+'px',
        height: HN_slideshowHeight+'px'
    })

    slide.children('.leftArrow').click(homeNavigatorPreviousPage);
    slide.children('.rightArrow').click(homeNavigatorNextPage);

    var cpt = 0;
    slide.children('.slide').children('.container').children('.item').each(function() {
        //on replace en CSS
        if((cpt%4)!=3) {
            jQuery(this).css({
                margin : ' 0 '+HN_imgMarginRight+'px 0 0'
            });
        }
        cpt++;
    });

    slide.children('.slide').css({
        overflow: 'hidden'
    })

    var nbChild = slide.children('.slide').children('.container').children('.item').length;
    slide.children('.slide').children('.container').css({
        width: (HN_imgWidth * nbChild) + (Math.ceil(nbChild/4*3) * HN_imgMarginRight) +'px'
    })
}

function homeNavigatorPreviousPage(){
    if(HN_currentPage > 0){
        var slide = jQuery('.homeNavigator .content .contentContainer .slide .container');

        HN_currentPage--;
        slide.animate({
            marginLeft:-(HN_currentPage*(4*HN_imgWidth+3*HN_imgMarginRight))+'px'
        });
    }
}

function homeNavigatorNextPage(){
    var slide = jQuery('.homeNavigator .content .contentContainer .slide .container');
    if(HN_currentPage < Math.floor((slide.children('.item').length-1)/4)){

        HN_currentPage++;
        slide.animate({
            marginLeft:-(HN_currentPage*(4*HN_imgWidth+3*HN_imgMarginRight))+'px'
        });
    }
}

/**
 * Gestion du slideshow de home
 */
var HS_timeDuration = 3000;
var HS_currentTime = -1;
var HS_slideshowWidth = 694;
var HS_imgWidth = 455;
var HS_imgHeight = 341;
var HS_imgStep = 79;
var HS_marginLeft = 8;
var HS_marginTop = 7;
var HS_interval;
function homeSlideShowInit() {
    var slide = jQuery('.homeSlideShow .content .contentContainer');


    slide.css({
        overflow:'hidden',
        width:HS_slideshowWidth+'px',
        height: HS_imgHeight+'px'
    })

    var cpt = 0;
    var caption;
    var htmlContent;
    HS_imgStep = (HS_slideshowWidth - HS_imgWidth ) / (slide.children('.item').length-1);
    slide.children('.item').children('.container').each(function() {
        htmlContent = jQuery(this).html();

        //on ajoute la banni�re texte
        jQuery(this).append(jQuery('<div/>').attr('class','caption'));
        caption = jQuery(this).children('div');
        caption.append('<h3>'+jQuery(this).children('a').children('img').attr('alt')+'</h3>');
        caption.append('<p/>');
        caption.children('p').html(htmlContent);
        caption.children('p').children('a').html('En savoir plus');

        //on replace en CSS
        jQuery(this).parent().css({
            left: (cpt*HS_imgStep)+'px',
            position: 'absolute'
        })
        cpt++;
    });

    homeSlideTime();
}

function homeSlideTime(newTime) {
    var slide = jQuery('.homeSlideShow .content .contentContainer');
    if(HS_interval) {
        clearInterval(HS_interval);
    }
	
    if(newTime) {
        HS_currentTime = newTime;
    }else {
        HS_currentTime++;
        if(HS_currentTime >= slide.children('.item').length) {
            HS_currentTime = 0;
        }
    }

    var currentStep = 0;
    var cpt = 0;
    slide.children('.item').each(function() {
        jQuery(this).animate({
            left: currentStep+'px'
        })

        if(cpt == HS_currentTime){
            currentStep += HS_imgWidth;
        }else {
            currentStep += HS_imgStep;
        }

        cpt++;
    });

    HS_interval = setInterval("homeSlideTime()",HS_timeDuration);
}

/**
 * Gestion du slideshow des �lus
 */
var ES_slideshowWidth = 675;
var ES_slideshowHeight = 167;
var ES_imgWidth = 108;
var ES_imgMarginRight = 0;
var ES_imgHeight = 167;
var ES_currentPage = 0;

function eluSlideShowInit() {
    var slide = jQuery('.structureSingle .slideShow');
    slide.css({
        width:ES_slideshowWidth+'px',
        height: ES_slideshowHeight+'px'
    })

    slide.children('.leftArrow').click(eluNavigatorPreviousPage);
    slide.children('.rightArrow').click(eluNavigatorNextPage);

    var cpt = 0;
    slide.children('.slide').css({
        overflow: 'hidden'
    })

    var nbChild = slide.children('.slide').children('.container').children('.item').length;
    slide.children('.slide').children('.container').css({
        width: (ES_imgWidth * nbChild) + (Math.ceil(nbChild/6) * ES_imgMarginRight) +'px'
    })
}

function eluNavigatorPreviousPage(){
    if(ES_currentPage > 0){
        var slide = jQuery('.structureSingle .slideShow .slide .container');

        ES_currentPage--;
        slide.animate({
            marginLeft:-(ES_currentPage*(6*ES_imgWidth))+'px'
        });
    }
}

function eluNavigatorNextPage(){
    var slide = jQuery('.structureSingle .slideShow .slide .container');
    if(ES_currentPage < Math.floor((slide.children('.item').length-1)/6)){

        ES_currentPage++;
        slide.animate({
            marginLeft:-(ES_currentPage*(6*ES_imgWidth))+'px'
        });
    }
}

/**
 * Gestion du menu principal
 **/
function mainMenuOver(){
    jQuery(this).children('a').addClass('over');
    jQuery(this).children('.subMenu').show();
}
function mainMenuOut(){
    jQuery(this).children('a').removeClass('over');
    jQuery(this).children('.subMenu').hide();
}


/*****
 ****** Fonction  G�n�rique qui cache les champs
 *****/
function hideMe(){
    jQuery(this).hide();
}



/*****
 ****** Fonctions  G�n�riques qui vide et reremplit les �l�ments de formulaire
 *****/
var tmp_empty='';
function emptyMe(){
    tmp_empty = jQuery(this).val();
    jQuery(this).val('')
}
function fullMe(){
    if(jQuery(this).val()=='')
        jQuery(this).val(tmp_empty);
}

/*****
 ****** Fonctions G�rant la date du jour en Ajax
 *****/
function OnLoadDate(){
	jQuery.ajax({
		type: "GET",
		url: "/fileadmin/templates/ajax/date.php",
		data: "",
		success: function(ret){
			jQuery('.tx-rtmeteofr-pi1 .dateNow').html(ret);
		}
	});
}

