var currentLeft = 0;
var itemsCount = 0;
var itemWidth = 720;
var pagesCount = 0;
jQuery(document).ready(function(){
	itemsCount = jQuery('div.carousel-img-container').size();
	pagesCount = jQuery('a.carousel-page').size();

	jQuery('div#carousel-images').css('width', (itemsCount*itemWidth)+'px');
	jQuery('div#carousel-images').css('left' , currentLeft);
	jQuery('a#carousel_left').click(function() {
		slideShowRuning = false;
		goLeft();
	});
	jQuery('a#carousel_right').click(function() {
		slideShowRuning = false;
		goRight();
	});
	jQuery('a.carousel-page').click(function(){
		slideShowRuning = false;
		gotoPage(this.innerHTML);
	});
	jQuery('a#carousel_page_right').click(function(){
		for (i=pagesCount; i>0; i--){
			if (jQuery("#carousel_page_"+pagesCount).text()*1+i <= itemsCount){
				for (k=pagesCount; k>0; k--){
					updatePagesValues(k, jQuery("#carousel_page_"+k).text()*1+i);
				}
				updateActivePage();
				break;
			}
		}
	});
	jQuery('a#carousel_page_left').click(function(){
		for (i=pagesCount; i>0; i--){
			if (jQuery("#carousel_page_1").text()-i >= 1){
				for (k=1; k<=pagesCount; k++){
					updatePagesValues(k, jQuery("#carousel_page_"+k).text()-i);
				}
				updateActivePage();
				break;
			}
		}
	});
	lookForCurrentPage();
});

function gotoPage(page){
	var pageLeft = (page-1)*itemWidth;
	if (pageLeft >= 0 && pageLeft <= (itemsCount-1)*itemWidth){
		currentLeft = -pageLeft;
		changeCurrent();
	}
}
function redirectToCategory(value){
	if (value && value != 0){
		window.location = value;
	}
}
function hideFirstImage(){
	jQuery('#carousel-first-image').css("display", "none");
	jQuery('#carousel-images').css("display", "block");
}
function changeCurrent(){
	hideFirstImage();
	jQuery('div#carousel-images').animate({
			left:currentLeft
		},750, 0);	
	lookForCurrentPage();
}
function updateActivePage(){
	jQuery('a.carousel-page').removeClass('active');
	jQuery('a.carousel-page:[title='+((-currentLeft)/itemWidth+1)+']').addClass('active');
	disablePaginationArrows();	
}
function lookForCurrentPage(){
	var page = (-currentLeft)/itemWidth+1;
	var mult = Math.ceil(page/pagesCount)-1;
	var from = pagesCount*mult + 1;
	var to   = pagesCount*(mult + 1);
	if (page >= itemsCount-pagesCount+1){
		from = itemsCount-pagesCount+1;
		to = itemsCount;
	}
	if (page <= pagesCount){
		from = 1;
		to = pagesCount;
	}
	var k = 1;
	for (i=from; i<=to; i++){
		updatePagesValues(k++, i);
	}	
	updateActivePage();
	disableArrows(page);
}
function updatePagesValues(page, value){
	jQuery("#carousel_page_"+page).text(value);
	jQuery("#carousel_page_"+page).attr("title", value);
}
function disableArrows(page){
	if (page == 1){
		jQuery('a#carousel_left').addClass("disabled");
	} else {
		jQuery('a#carousel_left').removeClass("disabled");
	}
	if (page == itemsCount){
		jQuery('a#carousel_right').addClass("disabled");
	} else {
		jQuery('a#carousel_right').removeClass("disabled");
	}
}
function disablePaginationArrows(){
	if (jQuery("#carousel_page_1").text() == 1){
		jQuery('a#carousel_page_left').addClass("disabled");
	} else {
		jQuery('a#carousel_page_left').removeClass("disabled");
	}
	if (jQuery("#carousel_page_"+pagesCount).text() == itemsCount){
		jQuery('a#carousel_page_right').addClass("disabled");
	} else {
		jQuery('a#carousel_page_right').removeClass("disabled");
	}
}
function goLeft(){
	if( currentLeft != 0 ){
		currentLeft += itemWidth;
		changeCurrent();
	}
}

function goRight(){
	if( currentLeft != -((itemsCount-1)*itemWidth) ){
		currentLeft -= itemWidth;
		changeCurrent();
	}else{
		slideShowFinished = true;
	}
}
var isCtrl = false;
$j(window).keydown(function(e) {

    if(e.ctrlKey) isCtrl = true;

    if(e.keyCode == 37 && isCtrl) {
    	goLeft();
    	return false;
    }
    if(e.keyCode == 39 && isCtrl) {
    	goRight();
    	return false;
    }
}).keyup(function(e) {
    if(e.ctrlKey) isCtrl = false;
});

var slideShowRuning = true;
var slideShowFinished = false;
function slideShow(){
	if (slideShowRuning) {
		goRight();
		if (slideShowFinished){
			currentLeft = itemWidth;
			slideShowFinished = false;
		}
	}
}