var imgs;
var totalWidth = 0;
var index = 0;
var count = 0;
var margin = 225;
var leftEdge = 0;
var rightSide;

$(document).ready(function() {
 var height = 0;
 imgs = $('#slider .image-img');
 $(imgs).each(function(i){					
	//$(this).parents(".image-node").css({'width':parseInt(this.width)});
	});
count = imgs.length;
rightSide = count*-(745+margin)
$(".count-number").html(count); 
$(".count-number").show();
first();
//slide();
})

function first(){
	var text = $(imgs[index]).parents(".image-node").children(".image-teaser").html();
	$("#slider-caption").html(text); 
	$("#slider-caption-wrapper").fadeIn(1200);
	$(".current-number").html(1); 
	$(".current-number").show();

}

function next(){
	$("#slider-caption-wrapper").hide();
	if(index >= count-1){
		index = 0;
		leftEdge = 0;
		slide();
		return true;
	};
		
	if (imgs[index].width != 745){
		var width = 745;
	}
		
	leftEdge -= parseInt(width)+margin;
	index++;
	slide();
	return true;
}
function prev(){
//	alert("leftEdge: " +leftEdge + " | index: " + index + " | count: "+count);
	$("#slider-caption-wrapper").hide();
	if (index < 1 && leftEdge < 1){
		if (count != 1){
			index = count;
			leftEdge = rightSide;
			slide();
		} else {
			index = 0;
			slide();
			return true;
		}
	}
	
	index--;

	if (imgs[index].width != 745){
		var width = 745;
	}

	leftEdge += parseInt(width)+margin;
	slide();
	return true;
}

function slide(){
//	alert("index: " + index);
//	alert("leftEdge: " + leftEdge);
	var text = $(imgs[index]).parents(".image-node").children(".image-teaser").html();
//	$("#slider-caption").css({'width':parseInt(imgs[index].width)});
	$(".current-number").html(index+1); 
	$(".current-number").show();
	$("#slider-caption").html(text); 
	$('#slider-inner').animate({'left':leftEdge+170},520);
	$("#slider-caption-wrapper").fadeIn(1200);
}