$(function(){
    $('.nojs').hide();
    $('.onlyjs').show();
    
    function goto(url) {
    	$('#wrapper').scrollTo(url, 800, {easing: 'easeOutBack'});
    	$('div.selected').removeClass('selected');
    	$(url).addClass('selected');
    }
    
    $('ul.sf-menu li a').each(function(){
    	var href_origin = $(this).attr('href');
    	var href = href_origin;
    	href = href.replace('/', '');
		$(this).attr('href', '#pg_'+href);
    });
    
    $('ul.sf-menu li a').click(function(e) {
        if (this.pathname == window.location.pathname) {
            goto($(this).attr('href'));
            
        } else {
        	url = $(this).attr('href');
        	$(location).attr('href', 'http://'+window.location.hostname+'/?goto='+encodeURIComponent(url));
        }
        e.preventDefault();
    });
    
    var id_en_cours = 0;
    var url_en_cours = '';
    
    $('a.click_2_fiche').click(function(){
    	var id = $(this).parents('.row_boat').attr('id');
    	id = id.split('b')[1];
    	id_en_cours = id;
    	
    	url_en_cours = window.location;
    	pageTracker._trackPageview('/'+$(this).attr('href'));
    	
    	$('#fiche_detaillee_'+id).find('a.back_2_list').attr('href', url_en_cours);
    	var height = $('#fiche_detaillee_'+id).height();

    	$('#fiche_detaillee_'+id).css({height: '1px', opacity: 0}).show();
    	$('#boats_list, .top_title').hide();

        $('#content_list3').animate({right: '-1000px'}, 'normal', function(){
        	$('#fiche_detaillee_'+id).animate({right: 0, opacity: 1, height: height}, 'fast', 'easeOutBack');
        	$('.pages_list_content').hide();
        });
        return false;
    });
    
    $('a.back_2_list').click(function(){
    	pageTracker._trackPageview(url_en_cours);
    	$('#fiche_detaillee_'+id_en_cours).animate({right: '-1000px'}, 'normal', function(){
    		$('#fiche_detaillee_'+id_en_cours).hide();
    		$('#boats_list, .top_title').show();
    		$('#content_list3').animate({right: 0, opacity: 1}, 'normal', 'easeOutBack', function() {
    			$('.pages_list_content').show();
    		});
    	});
    	return false;
    });
    
    $('a.show_boat_text').click(function(){
    	if ($('.content_text').is(':hidden'))
    		$('.content_text').css('opacity', 0.8).fadeIn('slow').show();
    		
    	$('ul.icons_slider a.selected').removeClass('selected');
    	$(this).addClass('selected');
    	$('.box_list').animate({left: '0px'}, 'slow', 'easeOutBack');
    	return false;
    });
    
    $('a.show_boat_buttons').click(function(){
    	if ($('.content_text').is(':hidden'))
    		$('.content_text').css('opacity', 0.8).fadeIn('slow').show();
    	
    	$('ul.icons_slider a.selected').removeClass('selected');
    	$(this).addClass('selected');
    	$('.box_list').animate({left: '-600px'}, 'slow', 'easeOutBack');
    	return false;
    });
    
    $('a.show_boat_pics').click(function(){
    	if ($('.content_text').is(':visible')) {
    		$('.content_text').fadeOut().hide();
    	}
    	
    	$('ul.icons_slider a.selected').removeClass('selected');
    	$(this).addClass('selected');
    	$('.content_text').hide();
    	return false;
    });
    
    $('form#sort_by select#sortby_field').change(function() {
    	$(location).attr('href', $('#form_url').val()+'__sb='+$(this).val());
    });
});
