function lastPostFunc(){

	nxt = parseInt($('#sc').text());
	frx = parseInt($('#fr').text());
	typ = $('#st').text();
	svr = $('#sv').text();

	if(nxt < frx){
		$.post('/sljedeca', { open: nxt, type: typ, vars: svr },
			function(data){
				$('#more').append(data);
			}
		);
		$('#sc').text(nxt + 1);
	} else $('#show-more').hide();
}

$(window).scroll(
	function(){
		$('#right-column').animate( { top: ($(window).scrollTop() - 16) + 'px' }, { queue: false, duration: 500 } );
		frt = parseInt($('#fr').text());
		nxf = parseInt($('#sc').text());
		if(frt > 5){
			if(nxf < frt){
				if($(window).scrollTop() == $(document).height() - $(window).height()){
					lastPostFunc();
				}
			} else $('#show-more').hide();
		}
	}
);

$(document).ready(function() {

	// hide modalbox on click within window

	$('html').click(
		function(){
			$('.modal, .modal-background').hide();
		}
	);

	// mouseover actions for voting

	$('.heart li a').live('mouseover mouseout',
		function(event) {
			if (event.type == 'mouseover') {
				id = $(this).parents('.item').attr('id');
				score = $(this).attr('rel');
				$('#' + id + ' .heart li a').each(function(){
					if($(this).attr('rel') <= score){
						$(this).addClass('hover');
					}
				});
			} else {
				$('.heart li a').removeClass('hover');
			}
		}
	);

	// click actions for voting

	$('.heart li a').live('click',
		function(){
			id = $(this).parents('.item').attr('id');
			score = $(this).attr('rel');
			$.post('/ocijeni', { shirtId: id, star: score },
				function(data){
					$('#' + id + ' .modal').html(data).show();
					$('#' + id + ' .modal-background').removeClass('zoom-background').css({ 'background-color': '#003661', 'opacity': 0.8 }).show();
				}
			);
			return false;
		}
	);

	// click actions for zooming

	$('.zoom').live('click',
		function(){
			id = $(this).parents('.item').attr('id');
			parts = id.split('-');
			if(parts[2] == 3) boja = '#000';
			else if(parts[2] == 7) boja = "#e5e5e5";
			else boja = "#fff";
			data = '<img src="/zoom/' + parts[1] + '" alt="Zoom" />';
			$('#' + id + ' .modal').html(data).show();
			$('#' + id + ' .modal-background').addClass('zoom-background').css({ 'background-color': boja, 'opacity': 1 }).show();
			return false;
		}
	);

	// update basket

	$('.size-s, .size-m, .size-l, .size-xl, .size-xxl').live('click',
		function(){
			id = $(this).parents('.item').attr('id');
			size = $(this).attr('rel');
			$.post('/kupi', { count: 1, shirtId: id, sizeId: size },
				function(data){
					$('#' + id + ' .modal').html(data).show();
					$('#' + id + ' .modal-background').removeClass('zoom-background').css({ 'background-color': '#003661', 'opacity': 0.8 }).show();
				}
			);
			$('#right-column .side-nav-block').slideUp();
			$('#side-nav-2').load('/obnovi-kosaricu').slideDown();
			return false;
		}
	);

	$('.size-s-dim, .size-m-dim, .size-l-dim, .size-xl-dim, .size-xxl-dim').live('click',
		function(){
			id = $(this).parents('.item').attr('id');
			data = '<h3>Greška</h3><p>Majica nije dostupna u odabranoj veličini.</p>';
			$('#' + id + ' .modal').html(data).show();
			$('#' + id + ' .modal-background').removeClass('zoom-background').css({ 'background-color': '#003661', 'opacity': 0.8 }).show();
			return false;
		}
	);

	$('.badge').live('click',
		function(){
			id = $(this).parent().attr('id');
			$('#' + id + ' .size li a').animate({ 'opacity': 0 }, 100).animate({ 'opacity': 1 }, 100).animate({ 'opacity': 0 }, 100).animate({ 'opacity': 1 }, 100);
			$('#' + id + ' .size .description').css({ 'font-weight': 'bold', 'color': '#c00' });
			return false;
		}
	);

});
