// common
$(document).ready(function () {
	if (navigator.userAgent.match(/like Mac OS X/i)) {
			
				$('.mixTest').hide();		
			}
	// dd roundies
	if(typeof(DD_roundies) !== 'undefined') {
		var roundMe = true;
		if(isIE) {roundMe = false;}
		if(roundMe) {
			DD_roundies.addRule('.retailerConnexion .textWrapper', '3px', true);
		}
		
		DD_roundies.addRule('.retailerHome .aside li', '10px', true);
		DD_roundies.addRule('.retailerConnexion .submitField input', '3px', true);
		DD_roundies.addRule('#carrouselContent, #carrousel', '5px', true);
	}
	
   	$('input[placeholder], textarea[placeholder]').placeholder();
	
	$('.dispatch a').click(function() {
		window.location = $(this).attr('href');
	});
	

	$("a.fancybox").fancybox();
	$("a.colorbox").colorbox({
		innerHeight: '95%',
		innerWidth: '80%',
		title : true
	});
	
	// carrousels de chiffres / images
	$('#carrousel').carousel();
	$('.keyfigures #carrousel li a, .dates #carrousel li a, .management #carrousel li a, .press #carrousel li a').live('click', function (e) {
		e.preventDefault();
		var elmt = $(this);
		var id = elmt.attr('href');
		
		$('#carrouselContent .container').hide();
		$(id).show();
		
		elmt.parent().parent().find('.select').removeClass('select');
		elmt.parent().addClass('select');
	});
	
	//on active le container si le carousel bouge
	$('#carrousel').bind('moved', function() {
		$('#carrousel li.first a').click();	
	});
	
	
	catalog.init();
	
	yCufon.init('#header a');
	yCufon.init('#footer .nav a');
	yCufon.init('.catalog .label');
	yCufon.init('.mixTest a span');
	
	Cufon.replace('.baseline h2 span', {
		fontFamily: 'DIN'
	});
	
	yCufon.init('.baseline .smallLetters');
	yCufon.init('.subMenu a');
	yCufon.init('.contactUs a');
	yCufon.init('.push .title', 'DIN', '.push a');
	yCufon.init('.baseline .important', 'Marcelle Script');
	
	Cufon.replace('.bubble .title', {
		fontFamily: 'Marcelle Script',
		lineHeight: '20px'
	});
	
	yCufon.init('.glossary a');
	yCufon.init('#header .baseline');
	
	// int
	yCufon.init('.mainContent h1');
	
	// print
	$('.print').click(function () {
		w.print();
		return false;
	});
	
	// style selection : voix masculine, féminine...
	$('.style a').bind('click', function (e) {
		e.preventDefault();
		var elmt = $(this);
		var li = elmt.parent();
		var ul = li.parent();
		
		$('li.select', ul).toggleClass('select');
		li.toggleClass('select');
	});
	
	// search result hover
	$('.list ul li').live('mouseenter', function () {
		$(this).addClass('hover');
	});
	$('.list ul li').live('mouseleave', function () {
		$(this).removeClass('hover');
	});
	
	$('.list ul li .titleWrapper').live('click', function (e) {
		e.preventDefault();
		var elmt = $(this);
		if(elmt.parents('.musique_select').length > 0) {
			setPreload(elmt.find('.pause'));
		}
		var container = elmt.parent();
		var mainContainer = container.parent().parent();
		var content = $('.content', container);
		var switchTag = false;
		
		if(mainContainer.hasClass('switch')) {
			switchTag = true;
		}
		
		if(switchTag) {
			$('li.select', mainContainer).toggleClass('select');
			container.toggleClass('select');
		} else {
			content.slideToggle();
			var timer = window.setTimeout(function () {
				adjustContainer();
				window.clearTimeout(timer);
			}, 500);
			$('a', elmt).toggleClass('less');
			container.toggleClass('select');
		}
	});
	
	$('#header .volume').click(function () {
		var elmt = $(this);
		elmt = $('img', elmt);
		var src = elmt.attr('src');
		if(src.indexOf('-off.gif') > -1) {
			src = src.replace('-off.gif', '-on.gif');
		} else {
			src = src.replace('-on.gif', '-off.gif');
		}
		elmt.attr('src', src);
		return false;
	});
	
	// masque l'ascenseur horizontal
	W.resize(function () {
		adjustScroll();
	});
	adjustScroll();
	
	// ajustement de la tailler des colonnes
	adjustContainer = function() {
		$('.mainContent > .pngWrapper > .bgWrapper').css('height', 'auto');
		$('#content > .generic > .wrapper > .contentWrapper').css('height', 'auto');
		var contentHeight = $('#content').height();
		
		var centerPaddingBottom, centerPaddingTop = 0;
		
		if(typeof($('.mainContent .bgWrapper').css('padding-bottom')) != 'undefined') {
			centerPaddingBottom = $('.mainContent .bgWrapper').css('padding-bottom').replace('px', '');
		}
		
		if(typeof($('.mainContent .bgWrapper').css('padding-top')) != 'undefined') {
			centerPaddingTop = $('.mainContent .bgWrapper').css('padding-top').replace('px', '');
		}
		
		var totalCenterHeight = contentHeight - centerPaddingTop - centerPaddingBottom;
		$('.retailer .mainContent > .pngWrapper > .bgWrapper').css('height', totalCenterHeight+'px');
		$('.retailer #content > .generic > .wrapper > .contentWrapper').css('height', contentHeight+'px');
		$('#tools').css('position', 'static').css('position', 'absolute');
	}
	
	$(window).load(function() {
		adjustContainer();
	});
	
	function bindEvents() {
		$('#ajax_musique, #ajax_voix, #musique_choix, #ajax_message, #ajax_mixage, #voix_choix').ajaxComplete(function() {
			adjustContainer();
			if (navigator.userAgent.match(/like Mac OS X/i)) {
				$('#header').css('width',$(document).width());


		
			}
		});
	}
	
	bindEvents();
	$(new Image()).load(function() {}).attr('src', 'tpl/img/common/visu/loader.gif');
	
	$('.list .play').live('click', function() {
		setPreload(this);
	});
	
	$('a.play_ref').bind('click',function(){
		$('a.play_ref').removeClass("active");
		$(this).addClass("active");
	});
	
	// MODIFS FDU 20111108
	// POP-UP : Contrôles emails utilisateur pour la connexion
	if($('#formulaire_login').length>0)
	{
		$('.submit',$(this)).bind('click',function(){
			var sLogin = $('#var_login').val();
			// ORANGE
			var sMotif1 = '@orange-ftgroup.com';		
			if(sLogin.search(sMotif1)>0)
			{
				var sMessage = 'Attention ! Votre login est erron&eacute;. Veuillez saisir votre nouvelle adresse mail. Merci. ';
				$.prompt(sMessage);
				return false;
			}
			// NEXTIRAONE
			var sMotif2 = '@nextiraone.fr';
			if(sLogin.search(sMotif2)>0)
			{
				var sMessage = 'Attention ! Votre login est erron&eacute;. Veuillez saisir votre nouvelle adresse mail. Merci.';
				$.prompt(sMessage);
				return false;
			}
			
		});
	}
	
});

var setPreload = function(obj) {
	var elmt = $(obj);
	 if (buzz.isMP3Supported()) {
	
     }else{
		
		if(!elmt.data('preloaded')) {
			elmt.data('preloaded', true);
			elmt.find('img').remove();
			elmt.append('<img class="loading" style="border:0;position:absolute;top:1px;left:1px;width:13px;" src="tpl/img/common/visu/loader.gif" alt="" />');
		}
	 }
};

function loadComplete() {
	$('.list .play img, .list .pause img').remove();
}

var adjustContainer;

var adjustScroll = function () {
	var siteWidth = $('#content').width();
	var wWidth = W.width();
	
	var left = 0;
	var top = w.scrollY;
	w.scroll(left, top);
	
	if(wWidth > siteWidth) {
		$('body').css('overflow-x', 'hidden');
	} else {
		$('body').css('overflow', 'auto');
	}
};

var catalog = {
	init: function () {
		$('.catalog li').each(function () {
			var elmt = $(this);
			var link = $('a', elmt);
			var text = link.text();
			elmt.append('<span class="label">'+text+'</span>');
			elmt.hover(function () {
				var currentElmt = $(this);
				var currentContainer = currentElmt.parent();
				if(!currentElmt.hasClass('item1Select') && !currentElmt.hasClass('item2Select') && !currentElmt.hasClass('item3Select') && !currentElmt.hasClass('item1DefaultSelect')) {
					currentContainer.find('.item1Select .label, .item2Select .label, .item3Select .label, .item1DefaultSelect .label').hide();
				}
				
				$(this).addClass('hover');

			}, function () {
				var currentElmt = $(this);
				var currentContainer = currentElmt.parent();
				currentElmt.removeClass('hover');
				currentContainer.find('.item1Select .label, .item2Select .label, .item3Select .label, .item1DefaultSelect .label').show();
			});
		});
	}
};
$('.catalog li a').live('touchend', function(e) {
    var el = $(this);
    var link = el.attr('href');
    window.location = link;
});

var yCufon = {
	param: {
		face: 'DIN'
	},
	init: function (obj, pFace, hover) {
		var path = obj;
		
		var face = yCufon.param.face;
		if(pFace != undefined) {
			face = pFace;
		}
		
		Cufon.replace(path, {fontFamily:face});
		
		var hoverPath = path;
		if(hover != undefined) {
			hoverPath = hover;
		}
		
		$(hoverPath).each(function () {
			$(this).hover(function () {
				Cufon.refresh(path);
			}, function () {
				var time = window.setTimeout(function () {
					Cufon.refresh(path);
					window.clearTimeout(time);
				}, 1);
			});
		});
	}
};


 ddaccordion.init({
	  headerclass: "titre", //Shared CSS class name of headers group
	  contentclass: "contenu", //Shared CSS class name of contents group
	  revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	  mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	  collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	  defaultexpanded: [1], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
	  onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	  animatedefault: false, //Should contents open by default be animated into view?
	  persiststate: true, //persist state of opened contents within browser session?
	  toggleclass: ["", "contenuOuvert"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	  togglehtml: ["none", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	  animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	  oninit:function(expandedindices){ //custom code to run when headers have initalized
		  //do nothing
	  },
	  onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		  //do nothing
	  }
})
