$(document).ready(function() {
	var mytimer = null;
	mac_pc();
	if ($('#slideshow').length > 0) slideshow();
});
function mac_pc() {
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$('body').addClass('mac');
	} else {
		$('body').addClass('pc');
	}
}
function slideshow() {
	/* controls */
	$('#slideshow').hover(function() {
		$(this).addClass('hovering');
		$('a#b_prev_slideshow, a#b_next_slideshow').css({'top':'50%'});
		$('a#b_prev_slideshow, a#b_next_slideshow').fadeTo(300,1);
		$('#controls').animate({'bottom':'0px'},{duration:300,queue:false});
	},function() {
		$(this).removeClass('hovering');
		$('a#b_prev_slideshow, a#b_next_slideshow').fadeTo(300,0,function() {
			$(this).css({'top':'-100px'});
		});
		$('#controls').animate({'bottom':'-80px'},{duration:300,queue:false});
	});
	
	
	/* slideshow */
	var total = $('#slideshow img').length;
	var curr = 0;
	var sec = 5000;
	$('#slideshow,.caption:gt(0)').fadeTo(0,0,function() { $(this).css({'display':'none'}); });
	
	function initialize() {
		curr = 0;
		mytimer = window.setInterval(function() { advance_image('next'); },sec);
		$('#counter').html('<span>01</span> of '+total);
		$('#imagebank img:not(:eq(0))').fadeTo(0,0,function() { $(this).css({'display':'none'}); });
		$('#imagebank img:eq(0)').fadeTo(0,1);
	}
	
	$('a#starter').click(function() {
		$('#slideshow').css({'display':'block'}).fadeTo(300,1);
		initialize();
		return false;
	});
	$('a#b_close_slideshow').click(function() {
		$('#slideshow').fadeTo(300,0,function() { $(this).css({'display':'none'}); });
		window.clearInterval(mytimer);
		return false;
	});

	
	$('a#b_prev_slideshow, a#b_next_slideshow').click(function() {
		$('a#b_pause_play').addClass('paused');
		window.clearInterval(mytimer);
		var dir = $(this).html();
		advance_image(dir);
		return false;
	});
	$('a#b_pause_play').click(function() {
		if ($(this).hasClass('paused')) {
			$(this).removeClass('paused');
			mytimer = window.setInterval(function() { advance_image('next'); },sec);
		} else {
			$(this).addClass('paused');
			window.clearInterval(mytimer);
		}
		return false;
	});

	function advance_image(dir) {
		$('#imagebank img:eq('+curr+'),#captions .caption:eq('+curr+')').fadeTo(300,0,function() { $(this).css({'display':'none'}); });
		if (dir == 'next') {
			if (curr < total-1) {
				curr++;
			} else {
				curr = 0;
			}
		} else {
			if (curr > 0) {
				curr--;
			} else {
				curr = total-1;
			}
		}
		$('#imagebank img:eq('+curr+'),#captions .caption:eq('+curr+')').css({'display':'block'});
		$('#imagebank img:eq('+curr+'),#captions .caption:eq('+curr+')').fadeTo(300,1,function() {
			if (jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
		});
		
		// change counter
		var counter_curr = (curr < 10) ? '0'+(curr+1) : (curr+1);
		$('#counter span').text(counter_curr);
	}
}
