// JavaScript Document

var SlideTimer;
var SlideCount;
var SlideTo = 0;
var TimerSpeed = 8000;
var TransitionSpeed = 1000;
var OffAlpha = 0.7;
var FadeSpeed = 200;
var SlideWidth = 713;
var SlideHeight = 259;
var IEOffset = 0;

$(document).ready(function() {
	$("#slide_holder img,#slide_holder div#flashcontent").each(function(i) {
		$("#slide_buttons").prepend('<a href="" class="btn_slide" index="' + i +'">'+i+'</a>');
		SlideCount = (i+1);
	});

	if ( $.browser.msie ) {
		if($.browser.version == "7.0") {
			
			SlideCount++;
			$("#slide_buttons").prepend('<a href="" class="btn_slide" index="' + SlideCount +'">'+SlideCount+'</a>');
			IEOffset = 5;
		}
	}

	$(".btn_slide").live("click", function(event) {
		event.preventDefault();
		SlideTo = parseInt($(this).html());
		SliderClick(SlideTo);
	});
	
	$(".btn_slide").live("mouseover", function(event) {
		$(this).stop().fadeTo(FadeSpeed, 1);
	});
	
	$(".btn_slide").live("mouseout", function(event) {
		if (parseInt($(this).attr("index")) != SlideTo) $(this).stop().fadeTo(FadeSpeed, OffAlpha);
	});
	
});

$(window).load(function() {
	// correct the slide width if it's different than defined
	$("#slide_holder img").each(function(i) {
		SlideWidth = $(this).width();
		SlideHeight = $(this).height();
	});
	
	
	$("#gallery").width(SlideWidth);
	$("#gallery").height(SlideHeight);	
	$("#slide_buttons").css("right","20px");
	$("#slide_buttons").css("bottom","0");	
	$("#slide_buttons").width((SlideCount * 16) + "px");	
	if(!$("#flashcontent")) {
		SliderClick(SlideTo);
	}
});

function SliderClick(SlideIndex) {
	clearInterval(SlideTimer);
	SlideTo = SlideIndex;
	SlideSlider(SlideTo);
	SlideTimer = setInterval(NextSlide, TimerSpeed);
}

function NextSlide(SlideIndex) {
	SlideTo++;
	if (SlideTo >= SlideCount) { SlideTo = 0; }
	SlideSlider(SlideTo);
}

function SlideSlider(SlideIndex) {
	//alert((SlideIndex * (SlideWidth + IEOffset)) + "    " + (SlideIndex * SlideWidth));
	$("#slide_holder").stop().animate({
		marginLeft: -(Math.ceil(SlideIndex * (SlideWidth + IEOffset)))
	}, TransitionSpeed);
	$(".btn_slide").each(function(i) {
		if (parseInt($(this).attr("index")) == SlideIndex) { $(this).stop().fadeTo(FadeSpeed, 1); }
		else { $(this).stop().fadeTo(FadeSpeed, OffAlpha); }
	});
}
