/* FantasyCapture custom js */
var fc = {};

jQuery(document).ready(function(){
	if (jQuery('body#home').length > 0) {	
		setInterval( 'simpleSlideSwitch()', 5000 );
	}
	disableRight();	
	Cufon.replace('h1,h2,h3,h4,h5,h6');
	if ($('ul#slideshow').length) {
		$('#slideshow li a').each(function(idx){
			$(this).data('index', (++idx));
		});
		fc.slideshow.init();
		$('#slideshow').jcarousel({
			scroll: 8,
			initCallback: nextPrev
		});
	}
});

function simpleSlideSwitch()	{
	var active = jQuery('#simple-slideshow li.active');
	if ( active.length == 0 ) {
		active = jQuery('#simple-slideshow li:first');
	}
	var next = active.next().length ? active.next() : jQuery('#simple-slideshow li:first');
	active.addClass('last-active');
	
	next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			active.removeClass('active last-active');
		});
}

function disableRight() {
	jQuery(document).bind('contextmenu', function(e) {
		e.preventDefault();
	})
}

function nextPrev(carousel)	{
	$('#slideshow-prev').bind('click', function(){ 
		var currentSlide = $('#slideshow li.active');			
		var prevSlide = currentSlide.prev().length ? currentSlide.prev() : $('#slideshow li:last');
		var idx = parseInt(prevSlide.attr('jcarouselindex')) - 3;
		prevSlide.find('a').click();
		carousel.scroll(idx);
	});
	$('#slideshow-next').bind('click', function(){ 
		var currentSlide = $('#slideshow li.active');			
		var nextSlide = currentSlide.next().length ? currentSlide.next() : $('#slideshow li:first');
		var idx = parseInt(nextSlide.attr('jcarouselindex')) - 3;
		nextSlide.find('a').click();
		carousel.scroll(idx);
	});
	$('#view-size').bind('click', function() {
		alert('change size!');
	});
	var timer;
	$('#view-mode').bind('click', function() {
		if ( $(this).hasClass('to-slideshow') ) {
			$(this).removeClass('to-slideshow').addClass('to-still').text('View one at a time');
			moveToNext();
			timer = window.setInterval('moveToNext()', 3000);
		} else {
			$(this).removeClass('to-still').addClass('to-slideshow').text('View as slideshow');
			window.clearInterval(timer);
		}
	});
}

function moveToNext() {
	$('#slideshow-next').click();
}

fc.slideshow = {
	init: function(){
		$('#slideshow').wrap('<div id="slideshow-wrap"></div>');
		$('#slideshow-wrap').prepend('<div id="slideshow-image"></div>');
		var slideshowImg = $('#slideshow-image');
		slideshowImg.css('opacity', 0);
		var imageLinks = $('#slideshow li a');
		var src;
		imageLinks.each(function(i){
			var src = $(this).attr('href');
			slideshowImg.append('<img id="image-' + i + '" src="' + src + '"/>');
			$('#slideshow-image img').css('opacity', 0);
			slideshowImg.animate({'opacity': 1},300);
		});
		imageLinks.bind('click', function(e){
			e.preventDefault();
			var idx = $(this).data('index');
			var img = idx-1;
			$(this).parents('li').addClass('active').siblings('.active').removeClass('active');
			slideshowImg.find('img.active').animate({'opacity': 0}, 300).removeClass('active');
			var nextImage = slideshowImg.find('img:eq(' + img + ')');
			var nextImageDom = nextImage.get(0);
			var imageHeight = parseInt(nextImageDom.height);
			var imageWidth = parseInt(nextImageDom.width);
			var top = (800 - imageHeight) / 2;
			var left = (800 - imageWidth) / 2;
			if (imageHeight === 0) { top = 0; }
			if (imageWidth === 0) { left = 0; }
			nextImage.css({
				'top': top,
				'left': left
			});
			nextImage.animate({'opacity': 1}, 300).addClass('active');
		});
		$('#slideshow-image img:first').each(function() {
			if (this.complete || this.readyState == 'complete') {
				imageLinks.filter(':first').click();
			} else {
				$(this).load(function(){ imageLinks.filter(':first').click(); });
			}
		});
		
		slideshowImg.after('<div id="slideshow-prev"></div><div id="slideshow-next"></div>');
		$('#slideshow-wrap').prepend('<div id="slideshow-controls"><span id="view-mode" class="to-slideshow">View as slideshow</span></div>');
	}
};
