var steps = $(".gallery-item").size();
var currStep = 1;

var currScrollPos = 0;
var galleryHeight;
var imgArr = [];
galleryHeight = $(".gallery").css('width').replace("px","");

// budowa layoutu
var totalImgs = 0;
totalImgs = $('.gallery-item').size();
$('.gallery').width((totalImgs * 127) + 25);
$('.text-wrapper').prepend('<div id=img-preview><img class=currImg src="' + $('.gallery-item a').eq(0).attr('href') + '" /></div>');
$('#img-preview').append('<label></label><a id="btn-right"></a><a id="btn-left"></a>');
$(".gallery-row").replaceWith( $('.gallery-row').contents() );
//tablica linków
$('.gallery a').each(function(){
	imgArr.push($(this).attr('href'));
//	console.log($(this).attr('href'));
})
/*

$('.gallery a').click(function(){
    var currSrc = "";
    currSrc = $(this).attr('href');
    currLabel = $(this).find('img').attr('alt');
	$('#img-preview label').empty();
	$('#img-preview label').append(currLabel);
	
    var img = new Image();
    $(img).load(function(){
        $(this).hide();
        $('#img-preview').append(this);
		$('.currImg').remove();
		$('#img-preview img').addClass('currImg');
        $(this).fadeIn();
    }).error(function(){
        // notify the user that the image could not be loaded
    }).attr('src', currSrc);
    
    
    return false;
})*/
$('.gallery a').click(function(){
	swapImg(this);
	return false;
	});
function swapImg(o){
	var currSrc = "";
    currSrc = $(o).attr('href');
    currLabel = $(o).find('img').attr('alt');
	$('#img-preview label').empty();
	$('#img-preview label').append(currLabel);
	
    var img = new Image();
    $(img).load(function(){
        $(this).hide();
        $('#img-preview').append(this);
		$('.currImg').remove();
		$('#img-preview img').addClass('currImg');
        $(this).fadeIn();
    }).error(function(){
        // notify the user that the image could not be loaded
    }).attr('src', currSrc);
}
$('a#btn-right').click(function(){
	var imgPath = $('.currImg').attr('src');
	for(var i = 0; i< imgArr.length; i++){
		if((imgPath == imgArr[i]) && (i < imgArr.length)){
			swapImg($('.gallery a').eq(i+1));
		}
	}
});
$('a#btn-left').click(function(){
	var imgPath = $('.currImg').attr('src');
	for(var i = 0; i< imgArr.length; i++){
		if((imgPath == imgArr[i]) && (i > 0)){
			swapImg($('.gallery a').eq(i-1));
		}
	}
});

$('a#scroll-right').addClass('active');
$('a#btn-right').addClass('active');
$("a#scroll-right, a#btn-right").click(function(){
	if(currStep<(steps-3)){
			//console.log(galleryHeight);
	currScrollPos = currScrollPos - 126;
    $(".gallery").animate({
        'margin-left': currScrollPos
    }, 200);
    //console.log('currPos:'+currScrollPos);
	currStep++;
	}else{
		$("a#scroll-right").removeClass('active');
		$("a#btn-right").removeClass('active');
	}
	$("a#scroll-left").addClass('active');
	$("a#btn-left").addClass('active');

})
$("a#scroll-left, a#btn-left").click(function(){
    if (currStep > 1) {
        currScrollPos = currScrollPos + 126;
        $(".gallery").animate({
            'margin-left': currScrollPos
        }, 200);
		currStep--;
    }else{
		$("a#scroll-left").removeClass('active');
		$("a#btn-left").removeClass('active');
	}
	$("a#scroll-right").addClass('active');
	$("a#btn-right").addClass('active');
    
    
})


