//var nbrGal = 0;


Cufon.replace('#pages a, #projects > li > a', { fontFamily: 'FreightSmall', hover: true });
Cufon.replace('#projects > li > ul > li > a', { fontFamily: 'FreightBig', hover: true });
Cufon.replace('.post h2, .category h1, .post h1, .post h3', { fontFamily: 'FreightBig' });

$(document).ready(function(){

	$('body').addClass('js');
	
	hover('#header h2 a');
	
	initMenu();
	
	initGallery();
	
	$("a[href$='.swf']").attr('rel', 'gallery').each(function(){
    var elmt = $(this);
    var img = elmt.find('img');
    var attr = img.attr("class").slice("-");
    vars = attr.split("-");
	elmt.fancybox({
		padding: 0,
		margin: 0,
		opacity: true,
		cyclic: false,
		scrolling: 'no',
		centerOnScroll: true,
		hideOnContentClick: true,
		overlayOpacity: 0.9,
		overlayColor: '#FFFFFF',
		titlePosition: 'over',
		width: parseInt(vars[1]),
		height: parseInt(vars[2]),
		type: 'swf'	
	});
   });       
	
	$('.gallery ul a img').parent('a').attr('rel', 'gallery').fancybox({
		padding: 0,
		margin: 0,
		opacity: true,
		cyclic: false,
		scrolling: 'no',
		centerOnScroll: true,
		hideOnContentClick: true,
		overlayOpacity: 0.9,
		overlayColor: '#FFFFFF',
		titlePosition: 'over'		
	});       
	
	$('.fancybox').attr('rel', 'gallery').fancybox({
		padding: 0,
		margin: 0,
		opacity: true,
		cyclic: false,
		scrolling: 'no',
		centerOnScroll: true,
		hideOnContentClick: true,
		overlayOpacity: 0.9,
		overlayColor: '#FFFFFF',
		titlePosition: 'over'		
	});
	
	initHome();          

});




function hover(emlt){
	$('span', emlt).hide();
	$(emlt).hover(function(){
		$('span', this).stop().fadeTo(250, 1);
	}, function(){
		$('span', this).stop().fadeTo(250, 0);
	});
}


function initMenu(){
	$('#menu #projects li:not(.current) ul').hide(); 
	$('#menu #projects > li > a').click(function(){
		return false;
	});
	$('#menu #projects > li > a').hover(function(){
		var elmt = $(this);
		elmt.addClass("actif");
		$('#menu #projects ul').each(function(){
			var ul = $(this);
			ul.prev("a").not(elmt).removeClass("actif");
			ul.stop().fadeTo(250, 0, function(){$(this).hide();});
		}); 
		elmt.next('ul').stop().show().fadeTo(250, 1);
		Cufon.refresh('#pages a, #projects > li > a', { fontFamily: 'FreightSmall', hover: true });
		Cufon.refresh('#projects > li > ul > a', { fontFamily: 'FreightBig', hover: true });
		return false;
	}, function(){
	
	});
}

function initGallery(){
	$('.single #content .content ul, .category .post .content ul, .page:not(.liens) .post .content ul').each(function(){
		if($('li', this).length >= $('li img', this).length){
			$(this).gallery();
		}
	});
}







function initHome(){
	$('.home #content table').each(function(){
		var table = $(this);
		table.find('td').each(function(){
			var td = $(this);
			var link = td.find('a');
			var img = td.find('img');
			link.append('<span class="title">'+link.attr('rel')+'</span>');
			var title = link.find('.title');
			Cufon.replace(title, { fontFamily: 'FreightBig', softHyphens: false });
			title.hide();
			title.css({
				'height': td.height()-25+'px',
				'width': td.width()-25+'px',
				'padding': '10px'
			});
			link.hover(function(){
				title.stop().fadeTo(150,1);
			}, function(){
				title.stop().fadeTo(150,0);
			});
		});
	});
}









// manage galleries
(function($){
	$.fn.extend({
		gallery: function() {
			this.each(function() {
			
				//alert("caca");
			
				//nbrGal++;
				
				var elmt = $(this);
				elmt.wrap('<div class="gallery"><div class="container" /></div>');
				var container = elmt.parent('.container');  
				var gallery = container.parent('.gallery');
				
				
				var _w = 0;
				//alert(elmt.children('li').eq(0).text().length);
				if(elmt.children('li').eq(0).text().length < 2){
					var description = '';
					var count = elmt.children('li').length-1;	
				} else {                                    
					var first = elmt.children('li').eq(0);
					var description = first.html();
					first.remove();
					var count = elmt.children('li').length-1;                              
				} 
				var _liW = 0;
				
				elmt.find('li').each(function(){
					var li = $(this);
					$('a img', li).parent('a').attr('rel', 'gal');
					if(parseInt(li.width()) > _w){       
						_w = parseInt(li.width());
					}
				});	
				elmt.find('li').width(_w);
				elmt.width((_w*(count+1))+'px');
				
				container.before('<p class="description">'+description+'</p>');
				
				if(elmt.width() > 600 && count > 1){
				
					container.before('<p class="navigation"><a href="#" class="prev">&lsaquo;</a> <span class="current">1</span> / '+(count+1)+' <a href="#" class="next">&rsaquo;</a></p>');
					
					var prev = gallery.find('.prev');
					var next = gallery.find('.next');
					var state = $('.navigation .current', gallery);
					var current = 0;
					
					prev.click(function(){
						if(current > 0){
							current--;	
							state.text(current+1);	
							elmt.stop().animate({
								left: (_w * current)/-1
							}, 250);
						}
						return false;
					});
					
					next.click(function(){
						if(current < count){
							current++; 	
							state.text(current+1);
							elmt.stop().animate({
								left: (_w * current)/-1
							}, 350);
						}
						return false;
					});
			}
				
			});
		}
	});
})(jQuery);

