jQuery(function($) {
	$("a[rel=gallery]").colorbox({transition:"fade"});
	
	var home_slider_position = 0, slide_img_width = 1060;
	function move_home_slider(new_pos) {
	    home_slider_position = new_pos;
	    
	    var new_left = -1 * home_slider_position * slide_img_width;
	    
	    var active_link = $('#home-slider a:eq(' + new_pos + ')');
	    $('#home-slider .img-holder').animate({'left': new_left + 'px'}, {duration: '10000'});
	    
	    $('#home-slider span.title').html(active_link.find('img').attr('alt'));
	    $('#home-slider a.more').attr('href', active_link.attr('href'));
	    
	}
	$('#home-slider .buttons a').click(function () {
		var new_pos;
	    if ($(this).hasClass('prev') && home_slider_position==0) {
	    	new_pos = $('#home-slider img').length - 1;
	    } else if ($(this).hasClass('next') && home_slider_position==$('#home-slider img').length - 1) {
	    	new_pos = 0;
	    } else if ($(this).hasClass('prev')) {
	    	new_pos = home_slider_position - 1;
	    } else if ($(this).hasClass('next')) {
	    	new_pos = home_slider_position + 1;
	    } else {
	    	// not possible
	    }	
	    move_home_slider(new_pos);
	    reset_home_slider_timeout();
	    return false;
	});
	var home_slider_timeout = null;
	function reset_home_slider_timeout() {
		window.clearTimeout(home_slider_timeout);
		home_slider_timeout = window.setTimeout(function() {
			$('#home-slider .buttons a.next').click();
		}, 10000);
	}
	reset_home_slider_timeout();
	

	$('#nav li.has-child').hover(function() {
		$(this).find('a:eq(0)').addClass('hover');
		$(this).find('ul').show();
		if ($.browser.msie) {
			set_nav_width($(this));
		};
	}, function() {
		$(this).find('a:eq(0)').removeClass('hover');
		$(this).find('ul').hide();
	});
	
	$('.field').focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });
    
    $('.block-toggler .title').hover(function() {
    	$(this).addClass('title-hover');
    }, function() {
    	$(this).removeClass('title-hover');
    });
    
    $('.block-toggler .content:gt(0)').hide();
    
    $('.block-toggler .title:gt(0)').removeClass('title-active');
    
    $('.block-toggler .title').live('click', function() {
    	if ( $(this).is('.title-active') ) {
    		$(this).parents('.block-toggler:eq(0)').find('.content').slideUp();
    		$(this).removeClass('title-active');
    		return;
    	};
    	$('.block-toggler .title').removeClass('title-active');
    	$(this).addClass('title-active');
    	
    	$('.block-toggler .content:visible').slideUp();
    	$(this).parents('.block-toggler:eq(0)').find('.content').slideDown();
    });
    
    function set_nav_width(li) {
		var width = 0;
		$(li).find('ul li').each(function() { 
			new_width = $(this).innerWidth();
			
			if (new_width > width) {
				width = new_width;
			};
			$(this).parents('ul:eq(0)').css('width', width);
		});
	};
	
	$('#participer').live('click', function() { 
 	    $('#participer-box').slideToggle(); 
 	}); 
 	 
 	$('#participer-box .close').live('click', function() { 
 	    $('#participer-box').slideUp(); 
 	    return false; 
 	}); 
 	
 	var collection_slider_pos = 0;
 	
 	function calculate_year_offest (year) {
 		// $('.collection-slider .year-nav .current').width()/2 - 17
 		var offset = (year-1875)*1.53 - 17;
 		return (offset<0)? '0' : (offset>185) ? '185' : offset ;
 	}
 	
 	function move_collection_slider(pos) {
	    collection_slider_pos = pos;
	    // 218 - $('.collection-holder .collection-item').width()
	    var new_left = -1 * pos * 218;
	    $('.collection-slider .collection-holder').animate({'left': new_left + 'px'}, {duration: 'slow'});
	    var current_item = $('.collection-holder .collection-item:eq(' + pos + ')').find('img');
	    
	    $('.collection-slider .nav p').html(current_item.attr('alt'));
	    $('.collection-slider .year-nav .current span').html(current_item.attr('title'));
	    
	    var year_new_left = calculate_year_offest(current_item.attr('title'));
	    $('.collection-slider .year-nav .current').animate({'left': year_new_left + 'px'}, {duration: 'slow'});
 	}
 	
 	$('.collection-slider .buttons a').click(function() {
		var new_pos;
	    if ($(this).hasClass('prev') && collection_slider_pos==0) {
	    	new_pos = $('.collection-holder .collection-item').length - 1;
	    } else if ($(this).hasClass('next') && collection_slider_pos==$('.collection-holder .collection-item').length - 1) {
	    	new_pos = 0;
	    } else if ($(this).hasClass('prev')) {
	    	new_pos = collection_slider_pos - 1;
	    } else if ($(this).hasClass('next')) {
	    	new_pos = collection_slider_pos + 1;
	    }
	    move_collection_slider(new_pos);
	    return false;
	});
	$('.contact form').submit(function() {
		var nonempty = ['cn_message', 'cn_nom', 'cn_email'];
		for(i=0; i<nonempty.length; i++) {
			if ($('.contact form .field[name="' + nonempty[i] + '"]').text() == '' ) {
 				alert('Please fill all required fields!');
				return false;
			}
		}
		
    	if( $('.contact form .field[name="cn_email"]').val() == '' || !$('.contact form .field[name="cn_email"]').val().match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/) ) {
    		alert('Please enter a valid email!');
    		return false;
    	}
		
		return true;
	});
	
});
