/* global vars for browser detection, if needed */
var explorer = navigator.userAgent.indexOf("MSIE") != -1;
var explorer6 = navigator.userAgent.indexOf("MSIE 6.0") != -1;
var explorer7 = navigator.userAgent.indexOf("MSIE 7.0") != -1;
var explorer8 = navigator.userAgent.indexOf("MSIE 8.0") != -1;
var firefox = navigator.userAgent.indexOf("Firefox") != -1;
var safari = navigator.userAgent.indexOf("Safari") != -1;
var opera = navigator.userAgent.indexOf("Opera") != -1;
var chrome = navigator.userAgent.indexOf("Chrome") != -1;

/* jQuery add-ons & plugins */
$.fn.toggleFade = function(settings) {
    if (settings == undefined) {
        settings = { speedIn: 'slow' };
    }
    settings = jQuery.extend(
				{
				    speedIn: "normal",
				    speedOut: settings.speedIn
				}, settings
			);
    return this.each(function() {
        var isHidden = jQuery(this).is(":hidden");
        jQuery(this)[isHidden ? "fadeIn" : "fadeOut"](isHidden ? settings.speedIn : settings.speedOut);
    });
};

$.fn.delay = function(time, func) {
    this.each(function() {
        setTimeout(func, time);
    });
    return this;
};

$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

$.fn.alternateRowColors = function() {
    $('tbody tr:odd', this).removeClass('even').addClass('odd');
    $('tbody tr:even', this).removeClass('odd').addClass('even');
    return this;
};

/* functions */
function returnFlash(id, path, width, height, flashvars) {
    var myFlash = '<object id="' + id + '" width="' + width + '" height="' + height + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">';
    myFlash = myFlash + '<param name="allowScriptAccess" value="always" />';
    myFlash = myFlash + '<param name="movie" value="' + path + '" />';
    myFlash = myFlash + '<param name="menu" value="false" />';
    myFlash = myFlash + '<param name="allowFullScreen" value="true" />';
    myFlash = myFlash + '<param name="flashvars" value="' + flashvars + '">';
    myFlash = myFlash + '<param name="quality" value="high" />';
    myFlash = myFlash + '<param name="scale" value="noscale" />';
    myFlash = myFlash + '<param name="bgcolor" value="#ffffff" />';
    myFlash = myFlash + '<param name="wmode" value="transparent" />';
    myFlash = myFlash + '<embed name="' + id + '" width="' + width + '" height="' + height + '" src="' + path + '" flashvars="' + flashvars + '" wmode="transparent" menu="false" quality="high" scale="noscale" allowFullScreen="true" bgcolor="#ffffff" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
    return myFlash;
}

function setPage(el) {
    setHover('.nav li');
    var minH = '0';
    if (el == 'home') {
        minH = '605';
        if (($(window).height() - 126) > minH) {
            minH = $(window).height() - 126;
        }
        $('.bgHash').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        $('.bgImage').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        $('.wrapContent').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        $('.step').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        $('.wrapFooter').css({ 'bottom': '0' });
        //$(window).resize(function() {
        // if(($(window).height() - 126) > minH) {
        //     minH = $(window).height() - 126;
        // }
        //  $('.wrapContent').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        //  $('.step').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
        //  $('.wrapFooter').css({ 'bottom': '0' });
        //});
        $('.step').find('a').click(function() {
            var classActive = $(this).parents().hasClass('active');
            if (classActive == false) {
                var divStep = $('div.step');
                $(this).closest('div', divStep).click();
                return false;
            }
        });
        $('.step').live('mouseover', function() {
            if (!$(this).hasClass('active')) {
                $(this).animate({ 'padding-right': '10px', 'opacity': 0.8 }, { duration: 200 });
            }
        });
        $('.step').live('mouseout', function() {
            if (!$(this).hasClass('active')) {
                $(this).animate({ 'padding-right': '0', 'opacity': 0.85 }, { duration: 200 });
            }
        });
        $('.step').live('click', function() {

            if (!$(this).hasClass('active')) {
                //Changed by Yulia
                // debugger;
                $('.home').find('div.active').animate({ 'width': '150px', 'opacity': 0.85 }).removeClass('active');
                $(this).animate({ 'width': '687px', 'padding-right': '0', 'opacity': 0.95 }).addClass('active');
            }

            var s_pageState = "";
            if ($(this).hasClass('step1')) {
                s_pageState = "Membre";
            } else if ($(this).hasClass('step2')) {
                s_pageState = "Candidat";
            } else if ($(this).hasClass('step3')) {
                s_pageState = "Etudiant";
            } else if ($(this).hasClass('step4')) {
                s_pageState = "Public";
            }

            s_trackPageView(false, s_pageState);
        });
        $('.home').find('.step').css({ 'opacity': 0.85 });
        $('.home').find('div.active').css({ 'width': '687px', 'padding-right': '0', 'opacity': 0.95 });
    } else {
        $('.wrapMain').append($('.wrapContent .wrapFooter'));
        //$('.wrapContent').css({ 'height': $('body').height() - 126, 'min-height': $(window).height() - 126 + 'px' });
    }
}

function setHover(el) {
    $(el).hover(function() {
        $(this).find('ul.accessAlt').removeClass('accessAlt').addClass('active');
    }, function() {
        $(this).find('ul.active').removeClass('active').addClass('accessAlt');
    });
}

var setCarousel = '';
function startSlideshow(settings, el) {
    if (settings == 'init') {
        if ($('.carousel').length > 0) {
            if ($('.carousel .item').length > 1) {
                $('.carousel .item').each(function() {
                    $('.carousel .nav').append('<li><a class="' + $(this).attr('class').replace('item ', '') + '" href="">' + $(this).attr('class').replace('item item', '') + '</a>');
                });
            }
            $('.carousel .item').first().addClass('active').fadeIn(800);
            $('.carousel .nav li').first().find('a').addClass('active');
            $('.carousel .nav li a').click(function() {
                return false;
            });
            $('.carousel .nav li a').hover(function() {
                startSlideshow('stop', $(this));
            }, function() {
                startSlideshow('start');
            });
            startSlideshow('start');
        }
    } else if (settings == 'anim') {
        if ($('.carousel .item.active').next('.item').length > 0) {
            $('.carousel .item.active').fadeOut(800).removeClass('active').next('.item').addClass('active').delay(900, function() {
                $('.carousel .nav li a.active').removeClass('active').parent().next('li').find('a').addClass('active');
                $('.carousel .item.active').fadeIn(800);
            });
        } else {
            $('.carousel .item.active').fadeOut(800).removeClass('active');
            $('.carousel .item').first().addClass('active').delay(900, function() {
                $('.carousel .nav li a.active').removeClass('active');
                $('.carousel .nav li').first().find('a').addClass('active');
                $('.carousel .item.active').fadeIn(800);
            });
        }
    } else if (settings == 'start') {
        if (setCarousel == '') {
            setCarousel = window.setInterval("startSlideshow('anim');", 5000);
        } else {
            startSlideshow('stop');
        }
    } else if (settings == 'stop') {
        window.clearInterval(setCarousel);
        setCarousel = '';
        $('.carousel .nav li a.active').removeClass('active');
        $(el).addClass('active');
        $('.carousel .item').removeClass('active').hide();
        $('.' + $(el).attr('class').replace(' active', '').replace('item ', '')).addClass('active').show();
    }
}

function openShareWindow(name, baseUrl, patternStr, w, h) {
    var d = document;
    var e = encodeURIComponent;
    var s = baseUrl + patternStr.replace("{URL}", e(d.location.href)).replace("{TITLE}", e(d.title));
    var o = function() {
        window.open(s, name, 'toolbar=0,status=0,resizable=1,width=' + w + ',height=' + h);
    };
    firefox ? setTimeout(o, 0) : o();
    return false;
}

function setLanguage(language) {
    $.cookie('oiq_language', language);

    switch ($.cookie('oiq_language')) {
        case 'fr':
            $.prettyPhoto.changePage('next');
            break;

        case 'en':
            $.prettyPhoto.changePage('previous');
            break;

        default:
            $.prettyPhoto.changePage('next');
    }
}

function putImageCaption() {
    $('.pageContent img').each(function() {
        if ($(this).attr('alt') != '' && !$('.ms-consoleframe').length > 0) {
            var imgWidth = $(this).attr('width');
            if (imgWidth == 656) {
                $(this).wrap('<div class="caption" style="width:' + imgWidth + 'px; padding:0 0 10px 0;" />');
            } else {
                $(this).wrap('<div class="caption" style="width:' + imgWidth + 'px; padding:0 10px 10px 0;" />');
            }
            $(this).after('<p>' + $(this).attr('alt') + '</p>')
        }
    });
}

//cheminement

	function changeStepActive(step) {
	//alert('new file');
		if ((step=="step5")||(step=="step6")||(step=="step7")||(step=="step8")||(step=="step9")||(step=="step10")){
			var stepDiv = $('#imgProcedureSteps').parent();
			stepDiv.hide(1000);
			stepDiv = $('#imgProcedureSteps2').parent();
			stepDiv.show(1000)
		} else {
			var stepDiv = $('#imgProcedureSteps').parent();
		}
		
		var alreadySwitch = false;

		if(stepDiv.hasClass('step0')){stepDiv.removeClass('step0');}
		if(stepDiv.hasClass('step1')){stepDiv.removeClass('step1');}
		if(stepDiv.hasClass('step2')){stepDiv.removeClass('step2');}
		if(stepDiv.hasClass('step3')){stepDiv.removeClass('step3');}
		if(stepDiv.hasClass('step4')){stepDiv.removeClass('step4');}
		if(stepDiv.hasClass('step5')){stepDiv.removeClass('step5');}
		if(stepDiv.hasClass('step6')){stepDiv.removeClass('step6');}
		if(stepDiv.hasClass('step7')){stepDiv.removeClass('step7');}
		if(stepDiv.hasClass('step8')){stepDiv.removeClass('step8');}
		if(stepDiv.hasClass('step9')){stepDiv.removeClass('step9');}
		if(stepDiv.hasClass('step10')){stepDiv.removeClass('step10');}
		
		stepDiv.addClass(step)
		
		clearSteps()
		
		switch(step)
		{
		case 'step1':
		
		  	$('.procedure .col2 .step1').show(500)
		  	break;
		case 'step2':
			$('.procedure .col2 .step2').show(500)
		  	break;
		case 'step3':
			$('.procedure .col2 .step3').show(500)
		  	break;
		case 'step4':
			$('.procedure .col2 .step4').show(500)
		  	break;
		case 'step5':
			$('.procedure .col2 .step5').show(500)
			$('.procedure .ingenieur').show(500);
		  	break;
		case 'step6':
			$('.procedure .col2 .step6').show(500)
		  	break;
		case 'step7':
			$('.procedure .col2 .step7').show(500)
		  	break;
		case 'step8':
			$('.procedure .col2 .step8').show(500)
		  	break;
		case 'step9':
			$('.procedure .col2 .step9').show(500)
		  	break;
		case 'step10':
			$('.procedure .col2 .step10').show(500)
		  	break;
		default:
		  	$('.procedure .col2 .step0').show(500)
		}
	}
	
	
	function setProcedure() {
		var stepDiv = $('#imgProcedureSteps2').parent();
		stepDiv.hide()
		
		$('.procedure .col2 .step1').hide();
		$('.procedure .col2 .step2').hide();
		$('.procedure .col2 .step3').hide();
		$('.procedure .col2 .step4').hide();
		$('.procedure .col2 .step5').hide();
		$('.procedure .col2 .step6').hide();
		$('.procedure .col2 .step7').hide();
		$('.procedure .col2 .step8').hide();
		$('.procedure .col2 .step9').hide();
		$('.procedure .col2 .step10').hide();
		$('.procedure .ingenieur').hide();
	}
	
	function clearSteps() {
		$('.procedure .col2 .step0').hide(500)
		$('.procedure .col2 .step1').hide(500)
		$('.procedure .col2 .step2').hide(500)
		$('.procedure .col2 .step3').hide(500)
		$('.procedure .col2 .step4').hide(500)
		$('.procedure .col2 .step5').hide(500)
		$('.procedure .col2 .step6').hide(500)
		$('.procedure .col2 .step7').hide(500)
		$('.procedure .col2 .step8').hide(500)
		$('.procedure .col2 .step9').hide(500)
		$('.procedure .col2 .step10').hide(500)
	}
	

/* on DOM ready events */
$(function() {
    /* tabs creation */
    $('.subNav ul ul ul').each(function() {
        $('body').find('h1').before('<div class="tabs"><ul class="tabs">' + $(this).html() + '</ul></div>');
        $(this).remove();
    });

    /* fix tabs */
    /* Remplacer par du code server-side (DS)
    if ($('.tabs').length > 0) {
    document.title = document.title.replace(/B_R/g, ' ');
    var f = $('.wrapContent');
    f.html(f.html().replace(/B_R/g, '&nbsp;'));
    $('ul.tabs li div a').each(function() {
    var t = $(this).html();
    t = t.replace('&nbsp;', '<br />');
    $(this).html(t);
    });
    }
    */
    if (!$('.txtSearch').val() == "") { $('.lblSearch').css('display', 'none') }

    if ((explorer7) || (explorer8)) {
        $('#ctl00_PlaceHolderMain_txtPrenomUtilisationIllegale').css({ 'position': 'absolute', 'top': '0', 'left': '110px' });
        $('#ctl00_PlaceHolderMain_txtNomUtilisationIllegale').css({ 'position': 'absolute', 'top': '0', 'left': '305px' });
        $('#ctl00_PlaceHolderMain_txtPrenomPlaignant').css({ 'position': 'absolute', 'top': '0', 'left': '110px' });
        $('#ctl00_PlaceHolderMain_txtNomPlaignant').css({ 'position': 'absolute', 'top': '0', 'left': '305px' });
		$('ul.stepList li a').css({'line-height':'34px'});
    }
	
	if (($('.home').length > 0) && (explorer6)) {
			window.location = "/fr/Pages/accueil-ie6.aspx";
	} else if ($('.home').length > 0) {
		$linkFR = $('#linkFR').val(); 
		$linkEN = $('#linkEN').val(); 

            $welcomeVideoCookie = $('#welcomeVideoCookie').val(); 

        if ($welcomeVideoCookie!= null){
		       // alert("oiq visit1");

		    if (($.cookie($welcomeVideoCookie) == null) && ($linkFR != null) && ($linkEN != null)) {
		        //alert("oiq visit2");
			    $('.videoFR').attr('href', $linkFR);
			    $('.videoEN').attr('href', $linkEN);
    					

			    $.cookie($welcomeVideoCookie, 'visited');             
			    var pathURL = window.location.pathname;          
			    var newPatURL = pathURL + '#!prettyPhoto[mixed]/0/';           
			    window.location.replace(newPatURL);
		    }
		}else{
		
			    //alert("welcomeVideoCookie is null");

		}
	}
	
    if ($('.home').length > 0) {
        $(window).resize(function() {
            minH = '605';
            if (($(window).height() - 126) > minH) {
                minH = $(window).height() - 126;
            }
            $('.bgHash').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
            $('.bgImage').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
            $('.wrapContent').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
            $('.step').css({ 'height': $('body').height() - 126, 'min-height': minH + 'px' });
            $('.wrapFooter').css({ 'bottom': '0' });
        });
    }

    $('body').removeClass('noJs');
    $('html').addClass($('html').attr('lang'));
    $('.ext').each(function() {
        $(this).attr('target', '_blank');
    });

    $('.communiques .infoList li').first().addClass('first');

    /*$('.wrapNav').css({ 'top': $(window).height() + $(window).scrollTop() - 65 });
    $(window).resize(function() {
    $('.wrapNav').css({ 'top': $(window).height() + $(window).scrollTop() - 65 });
    });
    $(window).scroll(function() {
    $('.wrapNav').css({ 'top': $(window).height() + $(window).scrollTop() - 65 });
    });*/

    setPage($('.wrapMain').attr('class').replace('wrapMain ', ''));
    startSlideshow('init');

    /* contentBox corner generation */
    $('.contentBox').each(function() {
        $(this).append('<div class="leftTop"></div><div class="rightTop"></div><div class="leftBottom"></div><div class="rightBottom"></div>');
    });

    /* errorMsg corner generation */
    $('div.errorMsg').each(function() {
        $(this).append('<div class="leftTop"></div><div class="rightTop"></div><div class="leftBottom"></div><div class="rightBottom"></div><div class="warning"></div>');
    });

    /* landing pages title change */
    $('.landing .col2 > .article').first().find('h2').css({ 'font-size': '2.6667em', 'line-height': '1.0833em' });
    $('.landing .news .item').last().css({ 'margin-right': '0' });

    /* header // search box management */
    if ($('.wrapHeader .frmSearch .txtSearch').val() != '') {
        $(this).prev('label').hide();
    }
    $('.wrapHeader .frmSearch .lblSearch').click(function() {
        $(this).hide();
        $('.wrapHeader .frmSearch .txtSearch').focus();
    });
    $('.wrapHeader .frmSearch .txtSearch').focus(function() {
        $('.wrapHeader .frmSearch .txtSearch').animate({ 'width': '397px' }, { duration: 100 }).delay(200, function() {
            if (!explorer) {
                $('.wrapHeader .frmSearch .onFocus').fadeIn(200);
            } else {
                $('.wrapHeader .frmSearch .onFocus').show();
            }
            $('.wrapHeader').css('z-index', 10);
        });
    });
    $('.wrapHeader .frmSearch').hover(function() {

    }, function() {
        $(this).delay(400, function() {
            $('.wrapHeader .frmSearch .onFocus').hide();
            $('.wrapHeader .frmSearch .txtSearch').blur().animate({ 'width': '130px' }, { duration: 100 });
            $('.wrapHeader').css('z-index', 0);
        });
    });
    $('.wrapHeader .frmSearch .txtSearch').blur(function() {
        if ($(this).val() == '') {
            $(this).prev('label').show();
        }
    });
    if ($('.drpSection').length > 0) {
        $('#rdoSection0').attr('checked', 'checked');
    }

    /* nav management */
    $('.wrapNav ul li ul.accessAlt').append("<span></span>");
    $('.wrapNav ul li').hover(function() {
        $('.wrapNav .subWrap').addClass('active');
        $(this).addClass('active').find('ul.accessAlt').removeClass('accessAlt').addClass('active');
        $('.wrapNav .active ul li a').css({ 'opacity': 0.4 });
        $('.wrapNav .active ul li.active a, .wrapNav .active ul li.active li a').css({ 'opacity': 1 });
    }, function() {
        if (!$(this).parents('li').length > 0) {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active').find('ul.active').removeClass('active').addClass('accessAlt');
                $('.wrapNav .active ul li a').css({ 'opacity': 1 });
                $('.wrapNav .subWrap').removeClass('active');
            }
        }
    });
    $('.subNav li.active ul li').first().addClass('first');
    $('.subNav li.active ul li').last().addClass('last');
    $('.subNav li.active ul li ul li').first().addClass('first');
    $('.subNav li.active ul li ul li').last().addClass('last');

    /* item list pagination */
    $('.itemList').each(function() {
        var c = $(this).find('.list ul li').size();
        if (c <= 5) {
            $(this).css({ 'padding-bottom': '0' }).find('.arrows').addClass('accessAlt');
        } else {
            $(this).find('.arrows a.next').addClass('active');
            var l = $(this).find('.list ul li');
            for (var i = 0; i < l.length; i += 5) {
                l.slice(i, i + 5)
					 .wrapAll('<div></div>');
            }
        }
        $(this).find('span.total').text($(this).find('.list ul div').size());
        $(this).find('.list ul div').first().addClass('active').show();
        /*$(this).find('.list').animate({'height':$(this).find('.list ul div').first().height()+'px'});*/
    });
    $('.itemList .arrows a').live('click', function() {
        var p = $(this).parents('.itemList');
        if ($(this).hasClass('prev')) {
            if ($(this).parents('.itemList').find('.list ul div.active').prev('div').length > 0) {
                /*problem on IE is on the line below*/
                $(this).parents('.itemList').find('.list ul div.active').removeClass('active').fadeOut(400).prev('div').addClass('active').delay(400, function() {
                    $(p).find('.list').animate({ 'height': $(p).find('.list ul div.active').height() + 'px' }, 400).delay(400, function() {
                        $('.list ul div.active').fadeIn(400);
                    });
                });
                $(this).parents('.itemList').find('a.next').addClass('active');
                $(this).parents('.itemList').find('span.current').text((eval($(this).parents('.itemList').find('span.current').text()) - 1));
                if ($(this).parents('.itemList').find('.list ul div.active').prev('div').length <= 0) {
                    $(this).parents('.itemList').find('a.prev').removeClass('active');
                }
            }
        } else if ($(this).hasClass('next')) {
            if ($(this).parents('.itemList').find('.list ul div.active').next('div').length > 0) {
                /*problem on IE is on the line below*/
                $(this).parents('.itemList').find('.list ul div.active').removeClass('active').fadeOut(400).next('div').addClass('active').delay(400, function() {
                    $(p).find('.list').animate({ 'height': $(p).find('.list ul div.active').height() + 'px' }, 400).delay(400, function() {
                        $('.list ul div.active').fadeIn(400);
                    });
                });
                $(this).parents('.itemList').find('a.prev').addClass('active');
                $(this).parents('.itemList').find('span.current').text((eval($(this).parents('.itemList').find('span.current').text()) + 1));
                if ($(this).parents('.itemList').find('.list ul div.active').next('div').length <= 0) {
                    $(this).parents('.itemList').find('a.next').removeClass('active');
                }
            }
        }
        return false;
    });
    $('.itemList.eventsCalendar').each(function() {
        $(this).find('li:nth-child(2n)').addClass('even');
    });

    if ((chrome) || (explorer7) || (explorer8)) { setTimeout("putImageCaption()", 1000); }

    /* add caption to content images */
    if ((!chrome) && (!explorer7) && (!explorer8)) {
        $('.pageContent img').each(function() {
            if ($(this).attr('alt') != '' && !$('.ms-consoleframe').length > 0) {
                var imgWidth = $(this).attr('width');
                if (imgWidth == 656) {
                    $(this).wrap('<div class="caption" style="width:' + imgWidth + 'px; padding:0 0 10px 0;" />');
                } else {
                    $(this).wrap('<div class="caption" style="width:' + imgWidth + 'px; padding:0 10px 10px 0;" />');
                }
                $(this).after('<p>' + $(this).attr('alt') + '</p>')
            }
        });
    }

    /* faq */
    $('.faq h2 a').live('click', function() {
        $(this).toggleClass('expand').toggleClass('collapse');
        $(this).parents('h2').next('.groupContent').toggleClass('accessAlt');
        if ($(this).attr('class').indexOf('collapse') > -1) {
            if (lang == 'EN')
                $(this).html('Close<span></span>');
            else
                $(this).html('Fermer<span></span>');
        } else {
            if (lang == 'EN')
                $(this).html('Open<span></span>');
            else
                $(this).html('Ouvrir<span></span>');
        }
        return false;
    });

    var faqURL = document.location.toString();
    if (faqURL.match('#')) {
        var faqAnchor = faqURL.split('#')[1];
        $('.faq h2 a[name="' + faqAnchor + '"]').each(function() {
            $(this).trigger('click');
        });
    }

    /* share */
    $('.share .tw').bind('click', function() {
        return openShareWindow('twShareWindow',
				'http://twitter.com/share?',
				'url={URL}&text={TITLE}',
				550, 450);
    });
    $('.share .fb').bind('click', function() {
        return openShareWindow('fbShareWindow',

				'http://www.facebook.com/sharer.php?',
				'src=bm&v=4&i=1292620118&u={URL}&t={TITLE}',
				626, 436);
    });

    /* sort tables */
    var myTextExtraction = function(node) {
        return node.childNodes[0].innerHTML;
    }
    $('table.sortable').each(function() {
        $(this).tablesorter({ textExtraction: myTextExtraction });
        $(this).alternateRowColors($(this));
    });
    $('table.sortable th').live('click', function() {
        var p = $(this).parents('table').attr('id');
        setTimeout("$('#" + p + "').alternateRowColors($('#" + p + "'));", 15);
    });

    /* fix breadcrumb */
    $('span.breadcrumb').find('span').first().remove();
    $('span.breadcrumb').find('span').first().remove();
    $('span.breadcrumb').find('span').first().remove();
    $('span.breadcrumb').find('span').first().remove();
    $('span.breadcrumb').find('span').each(function() {
        if ($(this).html() == '') {
            $(this).remove();
        } else {
            var t = $(this).text();
            if (t.length >= 20) {
                t = t.substr(0, 20) + '...';
            }
            $(this).find('a').attr('title', $(this).text()).text(t);
        }
    });

    /* news list */
    $('.video, .audio').each(function() {f
        $(this).find('.img a').append('<span></span>');
    });

    /* custom dropdowns */
    var zIndex = 50;
    $('.drp').each(function() {
        var id = $(this).attr('id');
        var css = $(this).attr('class');
        var t = $('#' + id + ' option:selected').text();
        if (t.length >= 38) {
            t = t.substr(0, 38) + '...';
        }
        $(this).addClass('accessAlt');
        $(this).before('<div class="drpCustom ' + css + '"><div class="current"><span>' + t + '</span></div><ul id="new_' + id + '" class="drpList accessAlt"></ul></div>');
        $(this).find('option').each(function() {
            if ($(this).text() == $(this).parents('select').find('option:selected').text()) {
                $('#new_' + id).append('<li class="active"><input type="radio" name="' + id + '" checked="checked"><a href="" option="' + $(this).val() + '" title="' + $(this).text() + '" rel="' + $(this).parents("select").attr("id") + '">' + $(this).text() + '</a></li>');
            } else {
                $('#new_' + id).append('<li><input type="radio" name="' + id + '"><a href="" option="' + $(this).val() + '" title="' + $(this).text() + '" rel="' + $(this).parents("select").attr("id") + '">' + $(this).text() + '</a></li>');
            }
        });
        $(this).parents('.row').css({ 'z-index': zIndex });
        zIndex = zIndex - 1;
    });
    $('.drpCustom .current').live('click', function() {
        $(this).parents('.drpCustom').addClass('drpOpened').find('ul.accessAlt').removeClass('accessAlt');
    });
    $('.drpCustom').hover(function() {
        // nothing to do on hover, triggered on click
    }, function() {
        $(this).removeClass('drpOpened').find('ul').addClass('accessAlt');
    });
    $('.drpList li a').live('click', function() {
        var t = $(this).text();
        if (t.length >= 22) {
            t = t.substr(0, 22) + '...';
        }
        $('#' + $(this).attr('rel')).val($(this).attr('option')).change();
        $(this).prev('input[type=radio]').attr('checked', 'checked');
        $(this).parents('ul').find('li.active').removeClass('active');
        $(this).parent('li').addClass('active');
        $(this).parents('.drpCustom').find('.current span').text(t);
        return false;
    });

    $('.drpList li input[type=radio]').live('click', function() {
        $(this).next('a').click();
        $(this).parents('ul').addClass('accessAlt');
    });

    $('.drpList li a').live('mouseup', function() {
        if (!$(this).parents('li').hasClass('datePicker')) {
            $(this).parents('.drpList').addClass('accessAlt');
        }
    });

    /* move the date picker inside the dropdown drpPeriod */
    $('.filterCalendar').each(function() {
        var el = $(this);
        $('.drpPeriod .drpList li').last().addClass('datePicker').find('a').after(el);
        $("input[id ^= 'txtFilterPeriod']").datePicker({ startDate: '01/01/1980' });
        $('.drpPeriod .drpList li .filterCalendar').find('.dp-choose-date').css({ 'text-indent': '-999em', 'text-align': 'left' }).each(function() {
            $(this).after('<div class="clearer"><!-- --></div>');
        });
    });
    $('#btnFilterSearch').each(function() {
        var el = $(this);
        $('.group select').last().after(el);
    });

    /* transform some buttons */
    $('.filters .group input[type=button]').each(function() {
        $(this).addClass('accessAlt');
        $(this).after('<a class="btSearch" href="#" onclick="$(this).prev(\'input[type=button]\').click(); return false;"><span>' + $(this).attr('value') + '</span></a>');
    });
    
    /*cheminement*/
    
    
		setProcedure();
	
	$('.procedure .candidat a').live('click', function() {
		if ( $('.procedure .candidat .step').is(':hidden')){
			$('.procedure .junior .step').hide(500);
			$('.procedure .ingenieur').hide(500);
			$('.procedure .candidat .step').show(500);
			clearSteps();
			$('.procedure .col2 .step0').show(500);
		}
	});
	
	$('.procedure .junior a').live('click', function() {
		if ( $('.procedure .junior .step').is(':hidden')){
			$('.procedure .candidat .step').hide(500);
			$('.procedure .ingenieur').show(500);
			$('.procedure .junior .step').show(500);
			clearSteps();
			$('.procedure .col2 .step5').show(500);
		}
	});

    /* obtenir un permis */
    $(window).load(function() {
        if ($('.phInteractiveTool').length > 0) {
            if ($('html').attr('lang') == 'fr') {
                $.ajax({
                    url: '/fr/pages/interactiveTool_fr.aspx',
                    dataType: 'html',
                    async: false,
                    success: function(data) {
                        var content = $(data).find('.tool');
                        $('.phInteractiveTool').append(content);
                        $('.phInteractiveTool').find('.intro').addClass('lastViewed');
                    }
                });
            } else if ($('html').attr('lang') == 'en') {
                $.ajax({
                    url: '/fr/pages/interactiveTool_en.aspx',
                    dataType: 'html',
                    async: false,
                    success: function(data) {
                        var content = $(data).find('.tool');
                        $('.phInteractiveTool').append(content);
                        $('.phInteractiveTool').find('.intro').addClass('lastViewed');
                    }
                });
            }
        }
    });
    $('.permis .menu li:even').each(function() {
        $(this).css({ 'width': '328px' });
    });
    $('.permis .tool .btStartOver').live('click', function() {
        $('.lastViewed').removeClass('lastViewed');
        $(this).parent('.step').fadeOut(400).delay(800, function() {
            $('.permis .tool .contentBox').animate({ 'height': $('.permis .tool .intro').css('height') }, { duration: 400 }).delay(600, function() {
                $('.permis .tool .intro').addClass('lastViewed').fadeIn(400);
            });
        });
        return false;
    });
    $('.permis .tool .btPrevious').live('click', function() {
        var el = $('.lastViewed');
        if ($(this).parents('.step').prev('.step').length > 0 && !($(this).parents('.step').next('.step').length > 0)) {
            $(this).parent('.step').fadeOut(400).delay(800, function() {
                $('.permis .tool .contentBox').animate({ 'height': $(el).css('height') }, { duration: 400 }).delay(600, function() {
                    $(el).fadeIn(400);
                });
            });
        } else {
            $(this).parent('.step').fadeOut(400).removeClass('lastViewed').delay(800, function() {
                $('.permis .tool .contentBox').animate({ 'height': $(el).prev('.step').css('height') }, { duration: 400 }).delay(600, function() {
                    $(el).prev('.step').addClass('lastViewed').fadeIn(400);
                });
            });
        }
        return false;
    });
    $('.permis .tool .btNext').live('click', function() {
        var goNext = '0';
        var option;
        var link;
        var text;
        if ($(this).parents('.step').find('.frmTool').length > 0) {
            if ($(this).parents('.step').find('.frmTool input[type=radio]:checked').length > 0) {
                if ($(this).parents('.step').find('.frmTool input[type=radio]:checked').val() == '') {
                    goNext = '1';
                } else {
                    goNext = '2';
                    link = $(this).parents('.step').find('.frmTool input[type=radio]:checked').val();
                    text = $(this).parents('.step').find('.frmTool input[type=radio]:checked').attr('rel');
                    option = $(this).parents('.step').find('.frmTool input[type=radio]:checked').val();
                }
            } else {
                //alert('Error message');
            }
        } else {
            goNext = '1';
        }
        if (goNext == '1') {
            var el = $(this).parents('.step').next('.step');
            $(this).parent('.step').removeClass('lastViewed').fadeOut(400).delay(800, function() {
                $('.permis .tool .contentBox').animate({ 'height': $(el).css('height') }, { duration: 400 }).delay(600, function() {
                    $(el).addClass('lastViewed').fadeIn(400);
                });
            });
        } else if (goNext == '2') {
            var el = $(this).parents('.contentBox').find('.step').last();
            $(this).parent('.step').fadeOut(400).delay(800, function() {
                $('.permis .tool .contentBox').animate({ 'height': $(el).css('height') }, { duration: 400 }).delay(600, function() {
                    $(el).find('a.myLink').attr('href', link).html(text);
                    $(el).fadeIn(400);
                });
            });
        }
        return false;
    });
});
