/**
 * vide le champ $sInputId s'il contient toujours la valeur par défaut $sDefaultValue
**/
function clearInput($sInputId, $sDefaultValue)
{
    if ($input = document.getElementById($sInputId)) {
        if ($input.value == $sDefaultValue) {
            $input.value = '';
        }
    }
}


/**
 * récupère tous les élements ayant pour class $sClassName
**/
function getElementsByClassName($sClassName)
{
    var $sTestClass = new RegExp($sClassName);

    var $aElements = document.getElementsByTagName('*');
	
	var $aReturnElements = new Array();
	
	var $iLength = $aElements.length;
	
	for ($i = 0; $i < $iLength ; $i++) {
	    	    
		if ($sTestClass.test($aElements[$i].className)) {
		      
			$aReturnElements.push($aElements[$i]);
		}
	}
	
	return $aReturnElements;
}


var ns6= document.getElementById && !document.all;

function restrictinput(maxlength,e,placeholder){
	if (window.event && event.srcElement.value.length >= maxlength)
		return false;
	else
		if (e.target && e.target == eval(placeholder) && e.target.value.length >= maxlength){
			var pressedkey=/[a-zA-Z0-9\.\,\/]/ ;
			if (pressedkey.test(String.fromCharCode(e.which))) e.stopPropagation();
		}
}

function countlimit(maxlength,e,placeholder){
	var theform =placeholder;
	var lengthleft = maxlength-theform.value.length;
	var placeholderobj = document.all ? document.all[placeholder] : document.getElementById(placeholder);
	if (window.event || e.target&&e.target == eval(placeholder)){
		if (lengthleft<0)
			theform.value = theform.value.substring(0,maxlength);
	}
}

function displaylimit(thename, theid, thelimit){
	var theform=(theid!="")? document.getElementById(theid) : thename


	if (document.all || navigator.userAgent.indexOf('Safari') != -1 || navigator.userAgent.indexOf('MSIE 7') != -1) {
		eval(theform).onkeypress=function() {
			return restrictinput(thelimit,event,theform)
		}

		eval(theform).onkeyup=function(){
			countlimit(thelimit,event,theform)
		}

	}
	else if (ns6){
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
		document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
	}
}




jQuery(document).ready(function() {

    jQuery('#participant_iThemeId').change(function() {
        //jQuery('#participant_iMarathonIdoption[@value=all]').attr("selected", "selected");
        jQuery("#participant_iMarathonId").get(0).selectedIndex = 0;
        jQuery("#participant_iMarathonId_content").html($("#participant_iMarathonId option:selected").text());
        jQuery("#participant_iThemeId_content").html($("#participant_iThemeId option:selected").text());
    });

    jQuery('#participant_iMarathonId').change(function() {
        //jQuery("#participant_iThemeIdoption[@value='all']").attr("selected", "selected");
        jQuery("#participant_iThemeId").get(0).selectedIndex = 0;
        jQuery("#participant_iMarathonId_content").html($("#participant_iMarathonId option:selected").text());
        jQuery("#participant_iThemeId_content").html($("#participant_iThemeId option:selected").text());
    });

    
    function addClickHandlers() {
        // Clic sur le bouton jaime depuis la fiche photo
        jQuery("a.jaime", this).click(function() {
            if ( this.id == "jaime_visionneuse" ) {

                iPhotoId = jQuery("#visionneuse_photo_id").val();
            }else{

                aTemp = this.id.split('_');
//                iPhotoId = jQuery("#likeIt_id").val();
                iPhotoId = aTemp[1];
            }
            jQuery('#formulaire_commentaire').load(jQuery('#likeIt_form').attr('action'), {likeIt_id:iPhotoId}, addClickHandlers);
            jQuery('#conteneur_popup_commentaire').show();
        });
        // Clic sur le bouton facebook depuis la visionneuse
        jQuery("a.facebook_redirect", this).click(function() {
            window.open('/acceder_photo/'+jQuery('#visionneuse_photo_id').val()+'/bFacebook/1');
            return false;
        });
        // Clic sur le bouton twitter depuis la visionneuse
        jQuery("a.twitter_redirect", this).click(function() {
            window.open('/acceder_photo/'+jQuery('#visionneuse_photo_id').val()+'/bTwitter/1');
            return false;
        });
        // Clic sur le bouton "J'aime" depuis la visionneuse
        jQuery("a.jaime_redirect", this).click(function() {
           window.location = '/acceder_photo/'+jQuery('#visionneuse_photo_id').val()+'/bJaime/1';
        });
        // Clic sur le bouton "envoyer à un ami" depuis la visionneuse
        jQuery("a.ami_redirect", this).click(function() {
           window.location = '/acceder_photo/'+jQuery('#visionneuse_photo_id').val()+'/bAmi/1';
        });
        // Clic sur le bouton "signaler un abus" depuis la visionneuse
        jQuery("a.abus_redirect", this).click(function() {
           window.location = '/acceder_photo/'+jQuery('#visionneuse_photo_id').val()+'/bAbus/1';
        });
        // Clic sur le lien "détail" depuis la visionneuse
        jQuery("a.picto_photo_detail", this).click(function() {
           window.location = '/acceder_photo/'+jQuery('#visionneuse_photo_id').val();
        });
    }

    jQuery(document).ready(addClickHandlers);

})
