$(function(){
	// menu
	var interval1;
	var interval2;
	var interval3;
	var interval4;
	var index;
	
	$('.sous-categorie:eq(0), #sub2').hover(
		function(){
			clearInterval(interval4);
			var pos = $(this).position();
			var width = $(this).width();
			index = $(this).index();
			
			$('#sub' + index).css({
				'left': ((pos.left - (135 - (width * .5))) - 2) + 'px'
			}).fadeIn(300);
		},
		function(){
			interval4 = setTimeout(function(){ $('#sub2').fadeOut(100); }, 200);
		}
	);
	
	$('.sous-categorie:eq(1), #sub4').hover(
		function(){
			clearInterval(interval1);
			var pos = $(this).position();
			var width = $(this).width();
			index = $(this).index();
			
			$('#sub' + index).css({
				'left': ((pos.left - (135 - (width * .5))) - 2) + 'px'
			}).fadeIn(300);
		},
		function(){
			interval1 = setTimeout(function(){ $('#sub4').fadeOut(100); }, 200);
		}
	);
	
	$('.sous-categorie:eq(2), #sub6').hover(
		function(){
			clearInterval(interval2);
			var pos = $(this).position();
			var width = $(this).width();
			index = $(this).index();
			
			if($(this).is('.sous-categorie'))
			{
				$('#sub' + index).css({
					'left': ((pos.left - (135 - (width * .5))) - 2) + 'px'
				}).fadeIn(300);
			}
		},
		function(){
			interval2 = setTimeout(function(){ $('#sub6').fadeOut(100); }, 200);
		}
	);
	
	$('.sous-categorie:eq(3), #sub7').hover(
		function(){
			clearInterval(interval3);
			var pos = $(this).position();
			var width = $(this).width();
			index = $(this).index();
			
			$('#sub' + index).css({
				'left': ((pos.left - (135 - (width * .5))) - 2) + 'px'
			}).fadeIn(300);
		},
		function(){
			interval3 = setTimeout(function(){ $('#sub7').fadeOut(100); }, 200);
		}
	);
	
	// trending topics
	$('#first-button-more a').hover(
		function(){
			var height = $('#tags-wrapper').height();
			var space = $('#tags-wrapper').css('top');
			space = space.replace(/px/i, '');
			var perc = (space / height) * 100;
			
			$('#tags-wrapper').animate({
				'top':  0
			}, 10000 - (100 * (100 + perc)), 'linear');
		},
		function(){
			$('#tags-wrapper').stop();
		}
	);
	
	$('#second-button-more a').hover(
		function(){
			var height = $('#tags-wrapper').height();
			var space = $('#tags-wrapper').css('top');
			space = space.replace(/px/i, '');
			var perc = (space / height) * 100;

			$('#tags-wrapper').animate({
				'top':  -(height - 180)
			}, 10000 - (100 * (100 + perc)), 'linear');
		},
		function(){
			$('#tags-wrapper').stop();
		}
	);
	
	$('#first-button-more a, #second-button-more a').click(function(){
		return false;
	});
	
	
	// rollover articles
	/*var req;
	
	$('#latest-articles .post').mouseover(function(){
		if(req != null) req.abort();
		$('#article-over-wrapper').show();
		$('#article-over-container').hide();
		$('#preloader').show();
		
		var articlePos = $(this).position();
		var url = $(this).find('.entry-title a').attr('href');
		var color = $(this).find('.color').text();
		
		$('#article-over-container').css('border', color + ' solid 5px');
		$('#indic').css({
			'height': $(this).height() - 45,
			'top': articlePos.top - 13,
			'background': color,
			'opacity': '0.9'
		});
		
		$('#article-over-wrapper').bind('click', function(){
			window.location = url;
		});
		
		req = $.ajax({
			url: url,
			success: function(data){
				$('#preloader').hide();
				var content = $(data).find('#content');
				$(content).find('#comments, .entry-utility').remove();
				$('#article-over-container').empty().append(content).show();
				var pos = $('#content').position();
			}
		})
	});
		
	$('#indic').mouseout(function(){
		$('#article-over-wrapper').hide();
		$('#article-over-container').empty().hide();
	});*/
	
	// job
	$("a[rel^='prettyPhoto']").prettyPhoto();
	$('.menu a:eq(2)').click(function(){
		$.prettyPhoto.open('http://www.bethe1.com/fr/offres-d-emploi-beauty?iframe=true&width=1020&height=500', 'Beauty Jobs', '');
		return false;
	});
	
	
	
	// ads
	displayAds('/banners/en/top/data/top.xml', '#ad-top', [650, 90]);
	displayAds('/banners/en/bottom/data/bottom.xml', '#ad-bottom', [120, 600]);
	
	
	function displayAds(pXMLUrl, pContainer, pFlashSize)
	{
		// load the xml file
		$.ajax({
			url: pXMLUrl,
			dataType: 'xml',
			success: function(data){
				var banners = $(data).find('banner');
				var today = new Date();
				var bannersToDisplay = [];

				$(banners).each(function(i){
					// parse dates
					var bannerDateBegin = $(this).find('dateDebut').text();
					var dayBegin = bannerDateBegin.substr(0, 2);
					var monthBegin = bannerDateBegin.substr(3, 2) - 1;
					var yearBegin = bannerDateBegin.substr(6);

					var bannerDateEnd = $(this).find('dateFin').text();
					var dayEnd = bannerDateEnd.substr(0, 2);
					var monthEnd = bannerDateEnd.substr(3, 2) - 1;
					var yearEnd = bannerDateEnd.substr(6);

					var fBeginDate = new Date();
					fBeginDate.setFullYear(yearBegin, monthBegin, dayBegin);

					var fEndDate = new Date();
					fEndDate.setFullYear(yearEnd, monthEnd, dayEnd);

					// if the banner needs to be displayed
					if(today > fBeginDate && today < fEndDate)
					{
						bannersToDisplay.push($(this));
					}
				});

				// if there is a banner to display
				var num = bannersToDisplay.length;
				var container = $(pContainer);
				var current = 0;

				// if there is a banner to display
				if(num > 0) displayBanner();

				function displayBanner()
				{
					var src = $(bannersToDisplay[current]).find('image').text();
					var url = $(bannersToDisplay[current]).find('lien').text();
					var timeout = $(bannersToDisplay[current]).find('tempsAffichage').text() * 1000;
					var reg = new RegExp('.swf');
					if(reg.test(src)) // if the banner is flash
					{
						$(container).append('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + pFlashSize[0] + '" height="' + pFlashSize[1] + '" id="banner-top" align="top"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + src + '" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="' + src + '" quality="high" bgcolor="#ffffff" width="' + pFlashSize[0] + '" height="' + pFlashSize[1] + '" name="noname" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
					}
					else // if the banner is an image
					{
						$(container).append('<a href="' + url + '"><img src="' + src + '" alt=""></a>');
					}

					if(num > 1) setTimeout(function(){ dispose(); }, timeout);
				}

				function dispose()
				{
					$(container).empty();
					if(current + 1 == num) current = 0;
					else current++;

					displayBanner();
				}
			}
		});
	}
});
