// JavaScript Document

$(document).ready(function() {
   
	$(".tyre").mouseover(function() {
			$(this).css("background-image", "url('../images/bg_over.gif')");	
			$(".tyre_name", this).css("color", "#fff");
			Cufon.now();
	 }).mouseout(function() {
			 $(this).css("background-image", "url('../images/bb.gif')");	
	 });


	$(".addtocart").click(function(event) {
		var gumaid = $(this).attr('href');
		var parent = $(this).parent();
		
		$.post('/u-korpu', { id: gumaid}, function(data) {
			$("#cartCount").html(data);
			$("#confirmation").show('blind', {}, 500);
			
			setTimeout(function() {
				$("#confirmation").hide('blind', {}, 500)
			}, 2000);

		});
		event.preventDefault();
	});
			
	bindHandlers();
	
});

function bindHandlers() {
	$(".qty").keydown(bindKeyDown);
	$(".qty").keyup(bindKeyUp);
	$(".qty").blur(bindFocusLost);
	
	$(".removeFromCart").click(function removeFromCart(event) {
		gumaid = $(this).attr('href');
		$(this).parent().append('<img src="/images/loader.gif" style="left:40px;top:2px;" class="loading" alt="" />');
		$.post('/izbaci-iz-korpe', { id: gumaid}, function(data) {
			$("#korpa").html(data);
			bindHandlers();
			var num = parseInt($("#cartCount").html());
			$("#cartCount").html(num-1);
		});
		event.preventDefault();
	});
	
	$("#clearCart").click(function(event) {
		$.post('/isprazni-korpu', null, function(data) {
			$("#korpa").html(data);
			bindHandlers();
			$("#cartCount").html('0');
		});
		event.preventDefault();
	});
	
	$("#cartSubmit").click(function(event) {
		window.location = '/poruci';
	});
	
}



function bindKeyDown(e) {
	var key = e.charCode || e.keyCode || 0;
	// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
	return (
		key == 8 || 
		key == 9 ||
		key == 13 ||
		key == 46 ||
		(key >= 37 && key <= 40) ||
		(key >= 48 && key <= 57) ||
		(key >= 96 && key <= 105));
}

function bindKeyUp(e) {
	var key = e.charCode || e.keyCode || 0;
	if (key == 13) {
		var id = $(this).prev().val();
		var qty = $(this).val();
		$(this).parent().append('<img src="/images/loader.gif" style="left:40px;top:5px;" class="loading" alt="" />');
		$.post('/izmeni-kolicinu', { id: id, qty: qty}, function(data) {
			$("#korpa").html(data);
			bindHandlers();
		});
	}
}

function bindFocusLost(e) {	
	var id = $(this).prev().val();
	var qty = $(this).val();
	$(this).parent().append('<img src="/images/loader.gif" style="left:40px;top:5px;" class="loading" alt="" />');
	$.post('/izmeni-kolicinu', { id: id, qty: qty}, function(data) {
		$("#korpa").html(data);
		bindHandlers();
	});
}


function slideSwitch() {
    var $active = $('#flash IMG.active');

    if ( $active.length == 0 ) $active = $('#flash IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#flash IMG:first');
	
	$next.css('display', 'block');
	
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2000, function() {
            $active.removeClass('active last-active');
        });
}

/*$(function() {
    setInterval( "slideSwitch()", 7000 );
});*/

