function urlencode (str) {
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
                                                                    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

jQuery(document).ready(function() {
    if ($('.fiche-td .fiche-titre-modele').length || $('body.actus').length) {
        var uri = window.location.href;
        var encodedUri = urlencode(uri);
    
        var defaults = {
            version:    '2.0.1',
            login:      'terredecafe',
            apiKey:     'R_ec702157325d3fc2dcae8dfd97284480',
            history:    '0',
            longUrl:    encodedUri
        };
        
        var socialServices = {
            facebook : {
                description : 'Partager sur Facebook',
                url : 'http://www.facebook.com/share.php?u={u}&t={t}'
            },
            twitter : {
                description : 'Partager sur Twitter',
                url : 'http://twitter.com/home?status=A voir sur Sofida.fr : {t} {u}'
            }
        }
        
        if ($('body.actus').length) {
            socialServices['twitter']['url'] = 'http://twitter.com/home?status=Actualité Sofida.fr : {t} {u}';
        }
        
        // Build the URL to query
        var daurl = "http://api.bit.ly/shorten?"
                +"version="+defaults.version
                +"&longUrl="+defaults.longUrl
                +"&login="+defaults.login
                +"&apiKey="+defaults.apiKey
                +"&history="+defaults.history
                +"&format=json&callback=?";
    
        // Utilize the bit.ly API
        $.getJSON(daurl, function(data){
            var shortUrl = data.results[uri].shortUrl;
            
            var t = '';    
            if ($('.fiche-td .fiche-titre-modele').length) {
                t += $.trim($('.fiche-td .fiche-titre-modele').eq(0).text());
                t += ' '+$.trim($('.fiche-td .fiche-descriptif-modele').eq(0).text());
            } else if ($('body.actus').length) {
                t += $.trim($('body.actus #chapo h1').eq(0).text());
            }
            t = urlencode(t);

            var htmlShare = '<div id="share">';
                htmlShare += '<ul>'
                
            for (service in socialServices) {
                htmlShare += '<li class="'+service+'">';
                htmlShare += '<a target="_blank" href="'+socialServices[service].url.replace('{u}', shortUrl).replace('{t}', t)+'">';
                htmlShare += socialServices[service].description;
                htmlShare += '</a>';
                htmlShare += '</li>';
            }
            htmlShare += '</ul>';
            htmlShare += '</div>';
            
            if ($('.vo-fiche-produit .fiche-penser').length) {
                $(htmlShare).insertBefore('.vo-fiche-produit .fiche-penser');
            } else if ($('body.actus #chapo').length) {
                $(htmlShare).prependTo('body.actus #chapo');
            }
            
        });
    }
    
    jQuery('.recrutementTbl tbody').each(function() {
    	var $this = jQuery(this);
    	$this.attr('data-facebox',$this.find('a').attr('href')).css('cursor','pointer');
    	$this.click(function() {
    		jQuery.facebox({ ajax: $this.attr('data-facebox') })
    	})
	})
	
	$('body').append('<img class="loader" style="position:fixed;z-index:9999;top:-9999px;left:50%;margin-left:-35px;margin-top:-35px;" src="/images/ajax-loader.gif" alt="chargement en cours" />');
	
	$(':input.error').focus(function() {
		$(this).removeClass('error');
	});
	
	jQuery('form').submit(function() {
		if (!$('.error').length) {
			$('.loader').css('top','50%');
			setTimeout(function() {
			    $('.loader').css('top','-99999px');
			}, 30000);
		}
	});
});

