$(function(){

	$('.raffle').showInfoPanels();
	
	$('#slider').slider();

	$('.end_date').each(function(){
		var countdowndate = new Date ( $(this).attr('year'), $(this).attr('month') - 1, $(this).attr('day'), $(this).attr('hour'), $(this).attr('minute'), 0 );
	$(this).countdown({until: countdowndate, compact: true, description: ''});
	});
});

$.fn.showInfoPanels = function() {
	return this.each(function(){
		var thumb = $('>.thumb',this);
		$(this).bind('mouseenter',function(){
			thumb.animate({opacity:0.01},'fast');  // instead of fading fully out leave <a> to be clicked on
		}).bind('mouseleave',function(){
			thumb.animate({opacity:1},'fast');
		});
	});
}

$.fn.slider = function() {
	return this.each(function(){
		var nav = $('#nav');
		var slides = $('#slides');
		var slideWidth = $('li:first',slides).width();
		$('#widget_list>li:first').clone().showInfoPanels().appendTo($('#widget_list'));
		var listWidth = slideWidth*$('#widget_list>li').length;
		var timer;
		var animationSpeed = 500;
		var rotateSpeed;
		var urlSpeed = window.location.search.match(/speed=[0-9]*/);
		urlSpeed!= null ? rotateSpeed = urlSpeed[0].substr(6)*1000 : rotateSpeed = 5000;
		var scrollPosition = 0;
		slides.scrollLeft(scrollPosition);
		
		var slideLeft = function(){
		
			if (scrollPosition - slideWidth > 0 - slideWidth) {
				scrollPosition -= slideWidth;
				slides.animate({
					scrollLeft: scrollPosition
				},{queue: false, duration: animationSpeed });
			} else {
				scrollPosition = listWidth - slideWidth;
				slides.scrollLeft(scrollPosition);
				slideLeft();
			}
			return false;
		}
		
		var slideRight = function(){
			if (scrollPosition < listWidth - slideWidth) {
				scrollPosition += slideWidth;
				slides.animate({
					scrollLeft: scrollPosition
				},{ queue: false, duration: animationSpeed });
			} else {
				scrollPosition = 0;
				slides.scrollLeft(scrollPosition);
				slideRight();
			}
			return false;
		}
		
		// widen the box to let the list items float in a row
		$('#widget_list').each(function(){
			$(this).width(listWidth+'px');
			if ($.browser.msie && $.browser.version <= 7) document.body.className += ''; /* :hack: force IE6 to redraw the screen */
		});
		
		var nextButtonSrc, prevButtonSrc;
		if ($('body').is('.large')) {
			nextButtonSrc = "/images/widget_right.gif";
			prevButtonSrc = "/images/widget_left.gif";
		} else {
			nextButtonSrc = "/images/widget/right_arrow.png";
			prevButtonSrc = "/images/widget/left_arrow.png";
		}
		
		var nextButton = $('<button class="next"><img src="'+nextButtonSrc+'" alt="Next" /></button>').click(function(){
			clearInterval(timer);
			slideRight();
		}).insertAfter('#slides');
		var prevButton = $('<button class="prev"><img src="'+prevButtonSrc+'" alt="Back" /></button>').click(function(){
			clearInterval(timer);
			slideLeft();
		}).insertAfter('#slides');

		timer = setInterval(slideRight,rotateSpeed);
		
		slides.hover(function(){
			clearInterval(timer);
		},function(){
			timer = setInterval(slideRight,rotateSpeed);
		});
		
	});
};


