$(function() {
	var switchTo5x=false;
	stLight.options({publisher:'ee28d08b-b160-4b6c-9f70-5c6100e30f2c'});
	setupAudioPlayer();
	setupPopupPlayer();
	setupMapSprite();
	setupMapLinks();
	setupPopupVideoPlayer();
	
	$("a.login-box").attr("href", "/membres/box-login");
	$("a.login-box").fancybox();
	
	$(".qaanswer").addClass("qahide");

	$(".showhide").click(showAnswer);
	$("#accordion").accordion({ collapsible: false, autoHeight: false, navigation: true });
	$("#accordion").bind('accordionchange', function(event, ui) {
		$.scrollTo( ui.newHeader, 500 );
	});
	$("#site-search").submit(function(){
    	if ($("#keywords-fake").val()) {
    		var the_alert = $("#keywords-fake").val();
        	var replaceChars = accent_fold($("#keywords-fake").val());
	        $("#site-search #keywords").val(replaceChars);
	        return true;
	    }
	    else { alert("Entrez des mots clés");}
	    return false;
	});
	$("#questions-search").submit(function(){
    	if ($("#qkeywords-fake").val()) {
        	var replaceChars = accent_fold($("#qkeywords-fake").val());
	        $("#questions-search #keywords").val(replaceChars);
	        return true;
	    }
	    else { alert("Entrez des mots clés");}
	    return false;
	}); 

	$(".product-img a").fancybox();
	$(".newspage-img a").fancybox({
		'titlePosition'	: 'inside'
	});
	$(".gallery-img a").fancybox({
		'titlePosition'	: 'inside'
	});
	
	$('.clearField').clearField();

});

function setupAudioPlayer() {
    $('a.panel-player').each(function(i) {
        var $this = $(this);
        var width = $(this).width();
        var autostart = 'false';
        if (location.href.indexOf('popup-player') !== -1) {
        	autostart='true';
        }
        width = (width < 180) ? '180' : '' + width;
        $this.replaceWith("<div class='panel-player' id='panel-player-" + i + "'></div>");
			jwplayer('panel-player-' + i).setup({
				'id': 'playerID',
				'width': width,
				'height': '24',
				'autostart': autostart,
				'skin': '/js/mylibs/simple.zip',
				'controlbar': 'top',
				'backcolor': '502211',
			    'frontcolor': 'ffffff',
			    'lightcolor': 'a18e87',
			    'screencolor': '502211',
			    'icons': 'false',
				'file': $this.attr('href'),
			    'modes': [
			        {type: 'flash', src: '/js/mylibs/player.swf'},
			        {type: 'download'}
			    ]
			});
    });
};

function setupPopupPlayer() {
    $('a.popup-player').click(function() {
        var newwindow = window.open(this.href,'',
            'resizable=1,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,width=515,height=250');
        newwindow.focus();
        return false;
    });
};

function setupPopupVideoPlayer() {
    $('a.popup-video').click(function() {
        var newwindow = window.open(this.href,'',
            'resizable=1,toolbar=0,location=0,status=0,menubar=0,scrollbars=1,width=515,height=470');
        newwindow.focus();
        return false;
    });
};

function openComments(url){
	window.opener.location=url
}

function setupMapSprite() {
	var $themap = $('#map-hover');
	$('#map-links #map-united-states-of-america').mouseover(function(){
			$themap.addClass('map-hover-united-states-of-america');
		}).mouseout(function(){
			$themap.removeClass('map-hover-united-states-of-america');
		});
	$('#map-links #map-haiti').mouseover(function(){
			$themap.addClass('map-hover-haiti');
		}).mouseout(function(){
			$themap.removeClass('map-hover-haiti');
		});
	$('#map-links #map-belgium').mouseover(function(){
			$themap.addClass('map-hover-belgium');
		}).mouseout(function(){
			$themap.removeClass('map-hover-belgium');
		});
	$('#map-links #map-tchad').mouseover(function(){
			$themap.addClass('map-hover-tchad');
		}).mouseout(function(){
			$themap.removeClass('map-hover-tchad');
		});
	$('#map-links #map-mali').mouseover(function(){
			$themap.addClass('map-hover-mali');
		}).mouseout(function(){
			$themap.removeClass('map-hover-mali');
		});
	$('#map-links #map-guinea').mouseover(function(){
			$themap.addClass('map-hover-guinea');
		}).mouseout(function(){
			$themap.removeClass('map-hover-guinea');
		});
	$('#map-links #map-burkina-faso').mouseover(function(){
			$themap.addClass('map-hover-burkina-faso');
		}).mouseout(function(){
			$themap.removeClass('map-hover-burkina-faso');
		});
	$('#map-links #map-benin').mouseover(function(){
			$themap.addClass('map-hover-benin');
		}).mouseout(function(){
			$themap.removeClass('map-hover-benin');
		});
	$('#map-links #map-central-african-republic').mouseover(function(){
			$themap.addClass('map-hover-central-african-republic');
		}).mouseout(function(){
			$themap.removeClass('map-hover-central-african-republic');
		});
	$('#map-links #map-cameroun').mouseover(function(){
			$themap.addClass('map-hover-cameroun');
		}).mouseout(function(){
			$themap.removeClass('map-hover-cameroun');
		});
	$('#map-links #map-gabon').mouseover(function(){
			$themap.addClass('map-hover-gabon');
		}).mouseout(function(){
			$themap.removeClass('map-hover-gabon');
		});
	$('#map-links #map-congo-rdc').mouseover(function(){
			$themap.addClass('map-hover-congo-rdc');
		}).mouseout(function(){
			$themap.removeClass('map-hover-congo-rdc');
		});
	$('#map-links #map-madagascar').mouseover(function(){
			$themap.addClass('map-hover-madagascar');
		}).mouseout(function(){
			$themap.removeClass('map-hover-madagascar');
		});
	$('#map-links #map-swaziland').mouseover(function(){
			$themap.addClass('map-hover-swaziland');
		}).mouseout(function(){
			$themap.removeClass('map-hover-swaziland');
		});
	$('#map-links #map-south-africa').mouseover(function(){
			$themap.addClass('map-hover-south-africa');
		}).mouseout(function(){
			$themap.removeClass('map-hover-south-africa');
		});
};

function setupMapLinks () {
		$('div.station-country:gt(0)').hide();
		var $links = $('.map-link');
		var $widgets = $('#station-listing>div.station-country');
		$links.live('click', function(){
      		if ( $widgets.filter(':animated').length === 0 ){
          		var $el = $(this).attr("href");
          		$widgets.filter(':visible').fadeOut(200, function(){
              		$widgets.filter( $el ).fadeIn(200);
          		})
      		}
      		$('html, body').animate({scrollTop:0}, 500);
		});
}

function showAnswer() {
	$(this).find('span').text($(this).find('span').text() == 'exposer +' ? 'cacher -' : 'exposer +');
	$(this).next().slideToggle('fast');
	$.scrollTo( this, 500 );
	return false;
}

var accent_map = {'À':'&Agrave;','Á':'&Aacute;','Â':'&Acirc;','Ã':'&Atilde;','Ä':'&Auml;','Å':'&Aring;','Æ':'&AElig;','Ç':'&Ccedil;','È':'&Egrave;','É':'&Eacute;','Ê':'&Ecirc;','Ë':'&Euml;','Ì':'&Igrave;','Í':'&Iacute;','Î':'&Icirc;','Ï':'&Iuml;','Ñ':'&Ntilde;','Ò':'&Ograve;','Ó':'&Oacute;','Ô':'&Ocirc;','Õ':'&Otilde;','Ö':'&Ouml;','Ù':'&Ugrave;','Ú':'&Uacute;','Û':'&Ucirc;','Ü':'&Uuml;','Ý':'&Yacute;','à':'&agrave;','á':'&aacute;','â':'&acirc;','ã':'&atilde;','ä':'&auml;','å':'&aring;','æ':'&aelig;','ç':'&ccedil;','è':'&egrave;','é':'&eacute;','ê':'&ecirc;','ë':'&euml;','ì':'&igrave;','í':'&iacute;','î':'&icirc;','ï':'&iuml;','ð':'&eth;','ñ':'&ntilde;','ò':'&ograve;','ó':'&oacute;','ô':'&ocirc;','õ':'&otilde;','ö':'&ouml;','ù':'&ugrave;','ú':'&uacute;','û':'&ucirc;','ü':'&uuml;','ý':'&yacute;','ÿ':'&yuml;',"'":'&rsquo;','‘':'&lsquo;','’':'&rsquo;'};
function accent_fold (s) {
  if (!s) { return ''; }
  var ret = '';
  for (var i = 0; i < s.length; i++) {
    ret += accent_map[s.charAt(i)] || s.charAt(i);
  }
  return ret;
};

function validateAmount(amount){
	if(amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
		return true;
	}else{
		alert('Vous devez entrer un don valide.');
		amount.focus();
		return false;
	}
}
