// Au chargement de chaque page


function formatUrlHash(hashValue) {
	var urlCourante = window.location.href.indexOf("/videos-surprises");

	if (urlCourante >= 0) {
		var domainUrl = window.location.href.substr(0,urlCourante-2);
	} else {
		var domainUrl = 'http://www.mistercredito.com';
	}

	
	var matchHach = hashValue.indexOf("#");
	if (matchHach >= 0) {
		var playHach = hashValue.substr(matchHach+1,250);
		urlVideo = domainUrl+'videos-surprises/flv/'+playHach+'.flv';
	} else {
		var urlVideo = '';
	}
	return urlVideo;
}



$(document).ready(function() {

	if ($('#videos-surprises').length) {
		var listVideos= '';
		$('.liste-videos').find('a:even').each(function(i,item) {
			if (i==0)
			listVideos += $(this).attr('href')
			else
			listVideos += '|'+$(this).attr('href')
		})
		var fo = new FlashObject("lecteur-video.swf", "sendmovie", "374", "310", 6, "#f2b227");
		
		if (window.location.hash) {
			fo.addVariable('nomfichier', formatUrlHash(window.location.hash));
		} else {
			fo.addVariable('nomfichier', $('.liste-videos').find('a').eq(0).attr('href'));
		}

		fo.addVariable('listeFichiers', listVideos);
		fo.addVariable('hexaCouleurBase', '0xf2b21e');
		fo.addVariable('hexaCouleurDesc', '0xf2b21e');
		fo.addVariable('langvar', '<?php echo LANG ?>');
		fo.addVariable('newvar', 'false');
		fo.addVariable('movieid', 'sendmovie');
		fo.addParam('menu', 'false');
		fo.addParam('wmode', 'transparent');
		fo.addParam('scale', 'exactfit');
		fo.addParam('swLiveConnect', 'true');		
		
		if (!window.location.hash) {
			$('#video').html('<div id="content-illustr"><a href="#" id="activ_vid" title=""><img class="libre withoutThumb" src="videosurprises-encours.jpg" id="tv-illustr" alt="Cliquer pour afficher le Lecteur vidéo" /></a></div>');
		} else {
			fo.write("video");
		}
	}
	
	
	$('.liste-videos').find('a').each(function() {
		var linkHref = $(this).attr('href');
		var matchUrl = linkHref.indexOf("flv/");
		if (matchUrl >= 0) {
			var hachValue = linkHref.substr(matchUrl+4,250);
			var matchUrl2 = hachValue.indexOf(".flv");
			if (matchUrl2 >= 0) {
				hachValue = hachValue.substr(0,matchUrl2);
			} else {
				var hachValue = '';
			}
		}
		else {
			hachValue = '';
		}
		$(this).attr('href','#'+hachValue)
		
	})
	
	
$('#activ_vid').click(function(){ fo.write("video"); return false; })
	if ($('.liste-videos').length) {
		$('.liste-videos').find('a').click(function() {
			var linkHach = $(this).attr('href');
			var urlCible = formatUrlHash(linkHach)

			fo.addVariable('nomfichier', urlCible);
			if ($('#activ_vid').length) {
				fo.write("video");
			}
			play(urlCible);
		})
	}

	if (isEligibleScrollBar()) $('#contenu-scroll').jScrollPane({showArrows:true, animateTo:true, scrollbarWidth: 15, arrowSize: 16, scrollbarWidth:15, scrollbarMargin :0, wheelSpeed :10});
	$("#liste-images a").each(function(i, monLien) {
		$(monLien).click(function() { return false; });
		$(monLien).mouseover(function() {
			$('#img_click').attr({'src': monLien.href}).css('opacity', 0.1).stop().fadeTo("slow",1.0);
			$('#img_desc p').css('opacity', 0.1).stop().fadeTo("slow",1.0);
			var nom_pays = $(monLien).find('img').attr('alt').split(/:/)[1] || ''
			var img = '<img src="../images/drapeaux/' + monLien.className + '.gif" alt="' + nom_pays + '" style="border: 1px solid #ccc" />';
			$('#img_desc p').html((monLien.className != '') ? img : '');
		});
	});
});

function isEligibleScrollBar() {  
  if (BrowserDetect.browser == 'Explorer' && Number(BrowserDetect.version) < 6) return false;
  if (BrowserDetect.browser == 'Safari' && Number(BrowserDetect.version) < 420) return false;
  return true;
}

function updateScrollBar() {
  if (isEligibleScrollBar()) $('#contenu-scroll').jScrollPane({showArrows:true, scrollbarWidth: 15, arrowSize: 16});	
}


// Calcule le total des produits sur la boutique
function calculateCartTotals() {
	var bigtotal = 0;
	$('#contenu_panier tbody tr.product').each(function(i, elem) {
		var price = $(elem).find('.prix_unitaire span').text().replace(',', '.');
		var quantity = $(elem).find('.count input').val();
		quantity = /^[0-9]+$/.test(quantity) ? parseInt(quantity, 10) : 0;
		var total = parseFloat(price, 10) * quantity;
		bigtotal += total;
		
		// màj champs
		$(elem).find('.count input')[0].size = String(quantity).length;
		$(elem).find('.prix span').text(formatNr(total));
	});
	$('#total').text(formatNr(bigtotal) + ' &euro;');
}

// Formate un prix au format français
function formatNr(val) {
	val = parseFloat(val, 10).toFixed(2);
	return String(val).replace('.', ',');
}

// Printf avec des numéros
// Exemple : nprintf("Bli {0}, Bla {1}", "foo", "spam") => "Bli foo, Bla spam"
function nprintf() { 
  var num = arguments.length; 
  var oStr = arguments[0];   
  for (var i = 1; i < num; i++) { 
    var pattern = "\\{" + (i-1) + "\\}"; 
    var re = new RegExp(pattern, "g"); 
    oStr = oStr.replace(re, arguments[i]); 
  } 
  return oStr; 
}

// Traduction gettext, a besoin de l'array window.locales[] pour fonctionner
function gettext(text) {
	//var lang = document.getElementsByTagName('html')[0].lang || 'fr';
	return window.locales && window.locales[text] ? window.locales[text] : text;
}
var _ = gettext;
