// JavaScript Document
var movingInsideImageContainerId = 'slider_images_cropped';
var imageClass = 'slider_page';
var current = 0;
var speed = 1000;
var boxHeight = 240;
var boxWidth = 796;
var timer;
var activated = false;
var userClick = false;
var right = false;
var currentClick = 0;
var animClick;
var clickReceived;
var bubbleTarget;
var loop = false;
var breakHere;
var offset1=0;
var offset2=0;
var gapDiff = 0;
var insertPadding;
var moveBoxOverOnLast;
var growContainer;

j(document).ready(function() {
	j("#prev").click(function(){ goPrev(true, currentClick); return false; });
	j("#next").click(function(){ goNext(true, currentClick); return false; });
	//if(!activated){timer = setInterval(function(){ goNext(false, currentClick); }, 5000);}
	j('#'+movingInsideImageContainerId).css('width',(j('#'+movingInsideImageContainerId+' .'+imageClass).length * boxWidth)+1000+'px');
	j('.slider_page:last').css('text-align','left');
	//j('.slider_page:last a:first').css('padding-left','30px');
});

function goPrev(userClick, animClick) {
	unlightbox()
	//if(userClick){ clearInterval(timer); }
	right = false;
	j('#bannerNav a:eq('+(current+1)+')').removeClass('active');
	if(current != 0) {
		current --;
		j('#prev').attr('class','active');
		j('#next').attr('class','active');
		if(current == 0) {
			j('#prev').attr('class','disabled');
			j('#next').attr('class','active');
		}
	}else {
		if(loop){
			current = j('#'+movingInsideImageContainerId+' .'+imageClass).length - 1;
			j('#prev').attr('class','active');
			j('#next').attr('class','disabled');
		}
	}
	j('#bannerNav a:eq('+(current+1)+')').addClass('active');
	if(current == j('#'+movingInsideImageContainerId+' .'+imageClass).length - 2){
		j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current)+') a:eq('+breakHere+')').animate({paddingRight:8+"px"}, speed, function(){ callback(animClick); });
		j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current)+')').animate({width:boxWidth+"px"}, speed, function(){ callback(animClick); });
	}
	j('#'+movingInsideImageContainerId).animate({left:-(current * boxWidth) +"px"}, speed, function(){ callback(animClick); });
	currentClick++;
}

function goNext(userClick, animClick){
	unlightbox()
	//if(userClick){ clearInterval(timer); }
	right = true;
	j('#bannerNav a:eq('+(current+1)+')').removeClass('active');
	j(current).removeClass('active');
	if(current >= j('#'+movingInsideImageContainerId+' .'+imageClass).length - 1) {
		if(loop){
			current = 0;
			j('#prev').attr('class','disabled');
			j('#next').attr('class','active');
		}else{
			return false
		}
	}else {
		current++;
		j('#prev').attr('class','active');
		j('#next').attr('class','active');
		if(current == (j('#'+movingInsideImageContainerId+' .'+imageClass).length - 1)) {
			j('#prev').attr('class','active');
			j('#next').attr('class','disabled');
		}
	}
	j('#bannerNav a:eq('+(current+1)+')').addClass('active');
	//if last page is small remove white space
	if(current == j('#'+movingInsideImageContainerId+' .'+imageClass).length - 1){
	
		//width of last page images
		var totalWidth = 0;
		for(var i=0; i<j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+current+') img').length; i++){
			var target = document.getElementById(movingInsideImageContainerId).getElementsByTagName('div')[current].getElementsByTagName('img')[i];
			totalWidth += target.width + 28;
		}
		
		//width of 2nd last page images
		var totalPrevWidth=0;
		for(var i=0; i<j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current-1)+') img').length; i++){
			var target = document.getElementById(movingInsideImageContainerId).getElementsByTagName('div')[current-1].getElementsByTagName('img')[i];
			totalPrevWidth += target.width + 28;
		}
		
		//white space on edge of 2nd last page
		var prevGap = (boxWidth - totalPrevWidth) / 2;
		
		//find element to break off
		var breakAdder=0;
		breakHere = 'false';
		var endPadding = 0;
		growContainer = boxWidth;
		offset1=0;
		for(var i=0; i<j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current-1)+') img').length; i++){
			var target = document.getElementById(movingInsideImageContainerId).getElementsByTagName('div')[current-1].getElementsByTagName('img')[i];
			breakAdder += target.width + 28;
			if(breakHere != 'false') {
				endPadding+=target.width + 28;
			}else{
				offset1 += target.width + 28
			}
			if(((prevGap/2) + breakAdder >= totalWidth) && breakHere == 'false'){
				breakHere = i;
			}
		}
		offset2 = endPadding;
		endPadding = (boxWidth - (endPadding + totalWidth)) / 2;
		if(endPadding > prevGap){
			gapDiff = endPadding - prevGap
			growContainer += gapDiff;
		}
		offset2 += endPadding;
		offset1 += endPadding;
		insertPadding = prevGap+8+18+gapDiff;
		moveBoxOverOnLast = totalWidth + endPadding + gapDiff
		
		//fix padding
		//j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+current+') a:eq(0)').css('padding-left','0');
		
		//execute animation
		j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current-1)+') a:eq('+breakHere+')').animate({paddingRight:insertPadding+"px"}, speed, function(){ callback(animClick); });
		j('#'+movingInsideImageContainerId+' .'+imageClass+':eq('+(current-1)+')').animate({width:growContainer+"px"}, speed, function(){ callback(animClick); });
		j('#'+movingInsideImageContainerId).animate({left:-((current-1) * boxWidth + moveBoxOverOnLast) +"px"}, speed, function(){ callback(animClick); });
	}else{
		j('#'+movingInsideImageContainerId).animate({left:-(current * boxWidth) +"px"}, speed, function(){ callback(animClick); });
	}
	
	currentClick++;
}

function callback(clickReceived){
	if(clickReceived == currentClick-1) {
	}
}