/********************************************************************************/
/***** CUFON SCRIPTS ***********************************************************/
/******************************************************************************/

Cufon.replace('.section-left h1');
Cufon.replace('.section-right h2');
Cufon.replace('.viewing strong');
Cufon.replace('.shifter-horizontal .container a em');


/********************************************************************************/
/***** IMAGE CHANGER SCRIPTS ***************************************************/
/******************************************************************************/


/*

$(function() {
	var container	= $('.product-image-list');
	var images  	= $('.product-image-list .product');
	var links		= $('.shifter-horizontal .thumb a');
	var bullets 	= $('.viewing span');
	var count		= bullets.length;
	var active		= 0;
	
	function imageChanger(i) {
		if(active == (count - 1)) {
			var nextStep = 0;
		} else {
			var nextStep = (active + 1);
		}
		
		$(container).append($(images[active]));
		$(images[i]).show();
		$(bullets).removeClass('selected');
		$(bullets[i]).addClass('selected');
		
		$(images[active]).fadeOut(500, function() {
			
			$(container).append($(images[i]));
			active = nextStep;
			imageChanger(nextStep);
		});
		
	}
	imageChanger(1);

});
*/

var startDelay 	= 4000;
var timeFade	= 1500;
var timeActive	= 6000;
var activeImg	= 0;
var timerStart;
var transition 	= 'off';

$(function() {
	timerStart = setTimeout('imageRotator(1)', startDelay);
	
	var clickableBullets = $('.viewing span');
	$(clickableBullets).each(function(i) {
		$(this).click(function(e) {
			imageRotator(i);
		});
	});
});

/***** GET THE FUNCTIONS STARTED *****/

function imageRotator(id) {
	if(transition == 'off') {
		transition = 'on';
		
		clearTimeout(timerStart);

		var imageList = $('.product-image-list').find('div.product');
		
		if((id) == imageList.length) {
			var nextStep = 0;
		} else {
			var nextStep = id;
		}
		
		var linkList 	= $('#rotate_nav').find('a');
		for(var i=0; i<linkList.length; i++) {
			$(linkList[i]).attr({'class':''});
		}
		var bullets 	= $('.viewing span');
		$(bullets).removeClass('selected');
		$(bullets[nextStep]).addClass('selected');
		
		var imageActive	= $(imageList[activeImg]);
		var imageNext	= $(imageList[nextStep]);
		
		$(imageActive).css({'zIndex':'100'});
		$(imageNext).css({'zIndex':'1'});
		
		$(imageActive).fadeOut(timeFade).add(
			$(imageNext).fadeIn(timeFade, function() {
				var nextStep2 = nextStep + 1;
				activeImg = nextStep;
				transition = 'off';
				timerStart = setTimeout('imageRotator(' + nextStep2 + ')', timeActive);
			})
		);
	}
}

/********************************************************************************/
/***** SHIFTER SCRIPTS *********************************************************/
/******************************************************************************/

$(function() {
	new shifter('shifter1','horizontal');
});

function shifter(element,axis) {
	var splitIt = $('#' + element).find('li');
	var transition		= 'false';

// Get the links
	var linkPrev = $(splitIt[0]).find('a');
	var linkNext = $(splitIt[2]).find('a');
	
// Get the thumbs and container
	var container		= $(splitIt[1]).children();
	var layoutItems 	= $(container).parent().width();
	var thumbsList		= $(container).children();
	var count			= thumbsList.length;
	
	var layoutTotal		= $(thumbsList[0]).width() * thumbsList.length;
	$(container).css({ 'width': layoutTotal + 'px' });
	var step			= $(thumbsList[0]).width();
	var layoutVisible	= $(splitIt[1]).width();
	
// Specify the settings variable
	var layoutHidden	= layoutTotal - layoutVisible;
	var thumbsVisible	= Math.floor(layoutVisible / step);
	var thumbsHidden	= Math.ceil((layoutHidden / step)-1);
	var thumb			= (layoutTotal / count) * thumbsHidden;
	
	if(layoutTotal > layoutItems) {
		$(linkPrev).unbind('click').click(function() {
			this.blur();
			if(transition == 'false') {
				$(linkNext).show();
				transition 	= 'true';
				var position	= parseInt($(container).css('left'));
				if(position < 0) {
					var newPos = position + (step*5);
				} else {
					var newPos = 0;
				}
				$(container).animate({ left: newPos + 'px' }, 700, function() {
					transition = 'false';
					var position	= parseInt($(container).css('left'));
					if(position < 0) {
						$(linkPrev).show();
					} else {
						$(linkPrev).hide();
					}
				});
			}
			return false;
		});
		$(linkNext).show().unbind('click').click(function() {
			this.blur();
			if(transition == 'false') {
				$(linkPrev).show();
				transition 	= 'true';
				var position	= parseInt($(container).css('left'));
				
				if(position > (thumb / 5 * -1)) {
					var newPos = position - (step*5);
				} else {
					var newPos = 0;
				}
				$(container).animate({ left: newPos + 'px' }, 700, function() {
					transition = 'false';
					var position	= parseInt($(container).css('left'));
					if(position > (thumb / 5 * -1)) {
						$(linkNext).show();
					} else {
						$(linkNext).hide();
					}
				});
			}
			return false;
		});
	} else {
		$(linkPrev).hide();
		$(linkNext).hide();
	}
}
