/* MBAB 2008 */
/* COMMON JAVASCRIPT FUNCTIONS */

/* ENABLE ROLLOVER ON TARGETED IMAGES (ALL BROWSERS) */
function imgRollover(elt) {
    jQuery(elt).each( function() {
		jQuery(this).hover(
			function() {
				jQuery(this).attr("src", jQuery(this).attr("src").replace("-off","-on"));
			},
			function () {
				jQuery(this).attr("src", jQuery(this).attr("src").replace("-on","-off"));
			}
		);
	});
}

/* SAME HEIGHT FOR SAME CLASS ELEMENTS */
function sameHeight(elt) {
	var heightBlockMax=0;
	jQuery(elt).each(function(){ if( jQuery(this).height() > heightBlockMax ) heightBlockMax = jQuery(this).height(); }); // get max height
	if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version) <= "6") {
			jQuery(elt).each(function(){ jQuery(this).css("height",heightBlockMax); }); // assign max height
		} else {
			jQuery(elt).each(function(){ jQuery(this).css("min-height",heightBlockMax); }); // assign max height
		}
	} else {
		jQuery(elt).each(function(){ jQuery(this).css("min-height",heightBlockMax); }); // assign max height
	}
	heightBlockMax=0;
}


/* FORM VALIDATION */
function formValidation(elt,myRules,myText) {
    jQuery(elt).RSV({
		errorFieldClass: "errorField",
		errorTextIntro: myText,
		rules: myRules
	});
}

/* POPULATE INPUTS WITH EXAMPLE TEXT TAKEN FROM INPUT TITLE */
(function(A){A.fn.example=function(E,C){var D=A.isFunction(E);var B=A.extend({},C,{example:E});return this.each(function(){var F=A(this);if(A.metadata){var G=A.extend({},A.fn.example.defaults,F.metadata(),B)}else{var G=A.extend({},A.fn.example.defaults,B)}if(!A.fn.example.boundClassNames[G.className]){A(window).unload(function(){A("."+G.className).val("")});A("form").submit(function(){A(this).find("."+G.className).val("")});A.fn.example.boundClassNames[G.className]=true}if(A.browser.msie&&!F.attr("defaultValue")&&(D||F.val()==G.example)){F.val("")}if(F.val()==""&&this!=document.activeElement){F.addClass(G.className);F.val(D?G.example.call(this):G.example)}F.focus(function(){if(A(this).is("."+G.className)){A(this).val("");A(this).removeClass(G.className)}});F.blur(function(){if(A(this).val()==""){A(this).addClass(G.className);A(this).val(D?G.example.call(this):G.example)}})})};A.fn.example.defaults={className:"example"};A.fn.example.boundClassNames=[]})(jQuery);
function swapValue(elt) {
	jQuery(elt).example(function() {
		return jQuery(this).attr('title');
	});
}

// CHARGER UN SELECT B (selectToUpdate) APRES CHANGE D'UN SELECT (x, y ou z)
function getSelectList(selectVal,urlToRequest,selectToUpdate) {

    // Si il n'y a pas d'identifiant de region
    if(selectVal == "0")
    {
        jQuery(selectToUpdate).removeOption(/./).addOption("0", "--");
    }
    else
    {
        // Définition du fichier de traitement
        var url = urlToRequest+selectVal;
		//jQuery(selectToUpdate).removeOption(/./).addOption("0", "--");
		jQuery(selectToUpdate).removeOption(/./);
		jQuery(selectToUpdate).ajaxAddOption(url, false);

    }
}

/* SHOWING/HIDING DETAILS FORMS FOLLOWING NUMBER CHOSEN IN RELATED SELECT */
function detailForms(elt,elt2,elt3){
	//jQuery(elt).addClass('offLeft');
	jQuery(elt2).each(function() {
		jQuery(this).change(function() {
			//jQuery(elt3+' '+elt).addClass('offLeft');
			var divToShowQuant = jQuery(elt2+' option:selected').val();
			if(divToShowQuant != "99999"){
				jQuery(elt2+"99999").addClass('offLeft');
			}
			
			if(divToShowQuant) {
				if (elt2 == "#childcountProfilePrefsEd") {
					for (i = 0;  i < divToShowQuant;  ++i ) {
	            		var myNum = i+1;
						jQuery(elt2+myNum).removeClass('offLeft');
					}
				}
				else {
					jQuery(elt2+divToShowQuant).removeClass('offLeft');
				}
			}
		});
	});
};

/* MORE CRITERIA AVAILABLE IN FORMS */
function moreCritInForms(elt,elt2) {
    var moreCritText = jQuery(elt).text();
    jQuery(elt).html('<a href="'+elt2+'">'+moreCritText+'</a>');
    jQuery(elt2).addClass('offLeft');
    jQuery(elt+' a').click(function() {
    	jQuery(elt2).toggleClass('offLeft');
		jQuery(this).toggleClass('moreCritOpen');
		return false;
    });
}



/* PROFILE BOX */
function profileBox(elt) {
   	jQuery(elt).append('<p class="moreOptionsLink"><a href="#moreOptions">'+MSG_PLUS_OPTION+'</a></p>');
    jQuery('#moreOptions').addClass('offLeft');
    jQuery('.moreOptionsLink a').click(function() {
    	jQuery('#moreOptions').toggleClass('offLeft');
		jQuery(this).toggleClass('moreOptOpen');
		if (jQuery(this).attr('class') == 'moreOptOpen') {
        	jQuery('.moreOptionsLink a').text(MSG_MOINS_OPTION);
		} else {
        	jQuery('.moreOptionsLink a').text(MSG_PLUS_OPTION);
		}
		return false;
    });
}

/* UPDATE MEMBER STATUS */
function memberStatusUpdate(elt) {
	jQuery(elt).click(function() {
		akedit('/ajax.php?mode=eg1','memberStatus',{
			type : 'text',
			name : 'memberStatusInput',
			submit: MSG_ENREGISTRER
		});//http://amiworks.co.in/talk/akeditable-jquery-inplace-editor/
		return false;
	});
}

/* SUCKERFISH DROPDOWN MENUS */
function mySuperfish(elt) {
	jQuery(elt).superfish({
		pathClass : 'current',
		dropShadows : false,
		delay: 0
	}).find('ul').bgIframe();
	jQuery(elt+' .navItem').each(function() {
		if (jQuery(this).children('a').size() > 0) {
			jQuery(this).hover(
	        	function(){
	        		jQuery(this).children('a').children('img').attr("src", jQuery(this).children('a').children('img').attr("src").replace("-off","-on"));
	        	},
	        	function() {
	        		jQuery(this).children('a').children('img').attr("src", jQuery(this).children('a').children('img').attr("src").replace("-on","-off"));
            	}
			);
		}
	});
}

/* NOTATION IN ADDRESS DETAIL AND COMMENT */
function starRating(elt) {
	jQuery(elt).stars({
	    cancelShow: false,
        oneVoteOnly: true,
	    showTitles: true
	});
}

/* AJAX PAGINATION */
function myAjaxPagination(elt,defaultUrl){

	var containerName = jQuery(elt).attr('id');
	containerName = containerName+'Container';
    jQuery(elt).wrap('<div id="'+containerName+'"></div>');

	function pageload(hash) {
        if(hash.substr(hash.length-3, hash.length) == "php") {
			jQuery(elt+"Container").load(hash.substr(0,hash.length-3) + ".php "+elt,'',function(){
				jQuery(elt+'Container').fadeIn('slow');
				jQuery('#load').fadeOut('normal');
				tb_init('a.thickbox, area.thickbox, input.thickbox');
			});
		}
        else if(hash.substr(hash.length-3, hash.length) == "htm") {
			jQuery(elt+"Container").load(hash.substr(0,hash.length-3) + ".htm "+elt,'',function(){
				jQuery(elt+'Container').fadeIn('slow');
				jQuery('#load').fadeOut('normal');
				tb_init('a.thickbox, area.thickbox, input.thickbox');
			 });
		}
		/*else if(hash) {
			jQuery(elt+"Container").load(hash + ".html "+elt,'',function(){
				jQuery(elt+'Container').fadeIn('slow');
				jQuery('#load').fadeOut('normal');
			});
		}*/
		else if(hash) {
			jQuery(elt+"Container").load(hash + " "+elt,'',function(){
				jQuery(elt+'Container').fadeIn('slow');
				jQuery('#load').fadeOut('normal');
				tb_init('a.thickbox, area.thickbox, input.thickbox');
			});
		}
		else {
			jQuery(elt+"Container").load(defaultUrl+' '+elt, '', function(){}); //default
		}
	}
	jQuery.historyInit(pageload);
	jQuery(elt+' .pagin a').livequery('click', function(event) {
        var hash = jQuery(this).attr('href');
		hash = hash.replace(/^.*#/, '');
        if (hash.substr(hash.length-3, hash.length) == "php") {
			hash = hash.substr(0, hash.length-4) + "php";
		}
		else if (hash.substr(hash.length-3, hash.length) == "htm") {
			hash = hash.substr(0, hash.length-4) + "htm";
		}
        /*else {
			hash = hash.substr(0,hash.length-5);
		}*/
		jQuery(elt+'Container').fadeOut('normal',function(){jQuery.historyLoad(hash)});
		jQuery('#load').remove();
		jQuery(elt+'Outside').append('<span id="load">'+MSG_CHARGEMENT+'</span>');
		jQuery('#load').fadeIn('normal');
		return false;

	});
}


/* AJAX MAJ CONTENU DU GUIDE */
function myAjaxMAJ(hash,elt){

	var containerName = jQuery(elt).attr('id');
	containerName = containerName+'Container';
    jQuery(elt).wrap('<div id="'+containerName+'"></div>');

	function pageload(hash) {
		if(hash) {
			jQuery(elt+"Container").load(hash + " "+ elt,'',function(){
				jQuery(elt+"Container").fadeIn('slow');
				jQuery('#load').fadeOut('normal');
				tb_init('a.thickbox, area.thickbox, input.thickbox');
			});
		}
	}

	jQuery.historyInit(pageload);

	jQuery(elt+"Container").fadeOut('normal',function(){jQuery.historyLoad(hash)});
	jQuery('#load').remove();
	jQuery(elt+'Outside').append('<span id="load">'+MSG_CHARGEMENT+'</span>');
	jQuery('#load').fadeIn('normal');

}

function griser(bool){
	if(bool){
		jQuery("body").prepend('<div id="griser"></div>');
		jQuery("#griser").css("position","absolute");
		jQuery("#griser").css("margin","0");
		jQuery("#griser").css("left","0");
		jQuery("#griser").css("z-index","1001");
		jQuery("#griser").css("opacity","0.7");
		jQuery("#griser").css("width",document.body.clientWidth);
		jQuery("#griser").css("height",document.body.clientHeight);
		jQuery("#griser").css("background-color","#000000");
		
		jQuery("#pubContainer object").css("z-index","1");
	}else{
		jQuery("#griser").fadeOut("speed");
	}
}



jQuery(document).ready(function() {

    /* ADD hasJS CLASS TO BODY FOR ABS/REL POSITIONNING */
	jQuery("body").addClass("hasJS");

    /* HIDE QUICK LINKS */
	jQuery('#quickAccess').addClass('offLeft');

    /* ENABLE ROLLOVER ON TARGETED IMAGES (ALL BROWSERS) */
    if (jQuery('.swapImg').size() > 0) { initImgRollOver = imgRollover(".swapImg"); }

	/* POPULATE INPUTS WITH EXAMPLE TEXT TAKEN FROM INPUT TITLE */
	if (jQuery('.swapValue').size() > 0) { initSwapValue = swapValue('.swapValue'); }

    /* SUCKERFISH DROPDOWN MENUS */
    if (jQuery('ul#mainNav').size() > 0) { initMainNav = mySuperfish("ul#mainNav"); }

    /* AUTO-GROWING TEXTAREAS */
    if (jQuery('.autoGrowing').size() > 0) { initAutoGrowingTextareas = jQuery(".autoGrowing").growfield(); }

	/* PROFILE BOX */
	if (jQuery('#miniProfile').size() > 0) { initProfileBox = profileBox('#miniProfile'); }

	/* MEMBER STATUS UPDATE */
	if (jQuery('.memberInfo h3 a').size() > 0) { initMemberStatusUpdate = memberStatusUpdate('.memberInfo h3 a.statusLink'); }
	
	if (jQuery('.printLink').size() > 0) { 
		jQuery('.printLink').each(function(){ jQuery(this).click(function() {window.print();}) });
	}
});


/* END */