var isie6 = ($.browser.msie && $.browser.version.substr(0,1)<7);
var isie = $.browser.msie;

$(document).ready(function () {
    png_fix_all();
    setupSlideshow();
    setupChauff();
    checkScroll();
    //setupTours();
});

var isanim = false;
function setupChauff() {
	
	if(! $('body').is('.chauff')) return;
	$("#slideshow .slide").width($("#slideshow .slides").outerWidth() - $("#slideshow .scroll-pane").outerWidth() + 20 + 'px');

	$(window).bind("resize", function() {
		$("#slideshow .slide").width($("#slideshow .slides").outerWidth() - $("#slideshow .scroll-pane").outerWidth() + 20 + 'px');
	});

	$("#slideshow .btn-up, #slideshow .btn-down").click(function () {
	    if (isanim) return;
	    isanim = true;
	    var it = $("#slideshow .items");
	    var t = parseInt(it.css('top'));
	    var lim = it.outerHeight();
	    var scroll = it.find("li").outerHeight(true);

	    var dir = $(this).is('.btn-down') ? 1 : -1;
	    if (dir < 0) {
	        scroll *= Math.min(((it.find("li").length + (t - 18) / scroll) - 3), 3);
	    }
	    else {
	        scroll *= Math.min(Math.abs((t - 18) / scroll), 3);
	    }
	    var newTop = ((dir > 0) ? ((dir * t >= 0) ? 0 : 1) : (t > scroll - lim ? 1 : 0)) * (dir * scroll) + t + 'px';
	    //alert(it.css('top') + ' ' + newTop);
	    //alert(t > scroll - lim);
	    it.animate({
	        top: newTop
	    }, 800, function () { isanim = false; checkScroll(); });
	});
	// Thumbnails small screen fix :P
	if($('body').width()<1100) {
		$('ul.thumbs img').width('80px');
		$('ul.thumbs').css('margin-left', '-750px');
    };
}

function checkScroll() {
    var it = $("#slideshow .items");
    var t = parseInt(it.css('top'));
    var lim = it.outerHeight();
    var scroll = it.find("li").outerHeight(true);
    scroll *= Math.min(((it.find("li").length + (t - 18) / scroll) - 3), 3);
    var newTop = scroll + t + 'px';
    //alert('scroll up:' + (t < 0) + ' scroll down:' + (it.css('top') != newTop));
    if (t < 0) {
        $(".btn-down").show();
    }
    else {
        $(".btn-down").hide();
    }
    if ((it.css('top') != newTop)) {
        $(".btn-up").show();
    }
    else {
        $(".btn-up").hide();
    }
}

var sl_isanim = 0;
function switchSlide(obj) {
	if(sl_isanim) return;
	var nb = $(obj).parent().prevAll().length + 1;
	if(true) {
		if(true) {
			sl_isanim = 2;
			$('#slideshow .slides .slide:visible').fadeOut(function() {sl_isanim--;
				if(!sl_isanim)
					$('#slideshow').animate({height: ($('#slideshow .slides .slide:visible').outerHeight() - 20 + 'px')}, 400);
			});
			$('#slideshow .slides .sl-'+nb).fadeIn(function() {sl_isanim--;
				if(!sl_isanim)
					$('#slideshow').animate({height: ($('#slideshow .slides .slide:visible').outerHeight() - 20 + 'px')}, 400);
			});
			if(isie) {
				
			}
			$('#slideshow .sl-nav .btn-sl').removeClass('act');
			$('#slideshow .sl-nav .sl-'+nb).addClass('act');		
		} else {
			$('#slideshow .slides .slide:visible').hide();
			$('#slideshow .slides .sl-'+nb).show();
		}	
	}
}

function setupSlideshow() {
	//if(!$('body').is('home')) return;
	$('#slideshow img').load(function(){
		$('#slideshow').height($('#slideshow .slides .slide:visible').outerHeight() - 20 + 'px');
	});
	$("#slideshow a.btn-sl").click(function() { switchSlide(this); });
	$("#slideshow a.btn-next").click(function() {
		var nb = $("#slideshow .slide:visible").prevAll().length + 1;
		nb++; nb%=$("#slideshow .slide").length + 1; // change number of slides here (total slides + 1) !!!
		switchSlide($('#slideshow .sl-nav .sl-'+ nb));
	});
	$("#slideshow a.btn-prev").click(function() {
		var total = $("#slideshow .slide").length + 1;
		var nb = $("#slideshow .slide:visible").prevAll().length + 1;
		nb--; if(nb<=0) nb = total - 1; nb%=total; // change number of slides here (total slides + 1) !!!
		switchSlide($('#slideshow .sl-nav .sl-'+ nb));
	});
}

function setupTours() {
    $("#main.tour .scrollable").scrollable({ size : 17, disabledClass: 'hiddenElem' });
    $('#main.tour #slideshow2 .items img').click(function () { // other way of if is main.tours .. :P
        $('#main.tour #slideshow2 .slide img').attr('src', $(this).attr('longdesc'));
        $('#descr').html($(this).attr('longdesc2'));
    });
}

function png_fix_all() {
	if(isie6) {
		DD_belatedPNG.fix('.pngfix');
	}
}