function updateCompareBox(id, title)
{
	g_title = title;
	g_id = id;
	$.getJSON("compare.php", {action: "addSingleAjax", productId: id} ,function(reply){
		if(reply['code'] == 0)
		{
			$('#compareBox').append("<tr><td>"+g_title+"</td><td><a href='compare.php?action=remove&productId="+g_id+"'>X</a></td></tr>");
		}
		else if(reply['code'] == 1)
		{
			alert(reply['text']);
		}
		//console.log(reply);
	});
}
// Cart
minicart = {
	selector: ".dropcart",

	init: function(){
		$('.cart a.opencart').toggle(function() {
			minicart.open();
		}, function() {
			minicart.close();
		});
	},

	close: function(){
		$(this.selector).slideUp(300);
		$('.dropcart .cart_load').empty();
	  $('.dropcart .added').empty();
	},

	open: function(){
		$(this.selector).slideDown(300);
	}
}
minicart.init();
/* SHOPPING CART BOX_DISPLAY */
// init default vars
var product_prices = [];
var mouse_is_inside = false;
var new_cart = 0;

// get default cart items count
var current_c = $('.cart .opencart b').html();
var current_cart = parseInt(current_c);

// get default cart price sum
if ($('.dropcart .sum .price_only').html() == "Pokličite za ceno!") {
  var previous_price_eur = "0 €";
  $('.dropcart .sum .price_only').empty().html(previous_price_eur);
} else {
  var previous_price_eur = $('.dropcart .sum .price_only').html();
  var previous_p = previous_price_eur.substring(0, previous_price_eur.length-1);
  
  var previous_price = previous_p.replace(".", "");
  var previous_price_dot = parseFloat(previous_price.replace(",", "."));
  product_prices.push(previous_price_dot);
}

function addToCart(id, title, price, quantity, nurlid)
{
	new_cart++;
	// change cart items count
	$('.cart .opencart b').empty().html(current_cart + new_cart); // end

	// prepare added-to-cart product info
	var product_id = id;
	var product_title = title;
	var product_price = price;
	var product_eur = product_price.substring(0, product_price.length-1);
	var product_dot = parseFloat(product_eur.replace(",", "."));
	var product_href = 'izdelek_'+nurlid; // end

	// add clicked product price into array
	product_prices.push(product_dot * quantity); // end

	// sum the array
	var total_price = 0;
	//product_prices.push(product_dot);
	$.each(product_prices, function() {
	  total_price += this;
	}); // end

	var total_price_rounded = Math.round(total_price*100)/100;
	$('.dropcart .sum .price_only').empty();

	var price = parseFloat(total_price_rounded);
	$('.dropcart .sum .price_only').html(" "+price+" €");

	$('.dropcart .cart_load').html('<img src="skins/svet_komunikacij/images/ajax-loader-cart.gif" />');
	$.ajax({
		type: "POST",
		url: "shoppingcart.php?action=add&productId="+product_id+"&pNItems="+quantity,
		success: function(msg){
		  $('.dropcart .cart_load').empty();
		  $('.dropcart .added').html('Izdelek je bil uspešno dodan v košaro!');
		$('.cart_items').append("<div class='row item'><a class='name' href='"+product_href+"' title='"+product_title+"'>"+quantity+"x "+product_title+"</a><span class='price'>cena: "+product_price+"</span></div>");
		if ($('.dropcart').is(":hidden")) {
		  minicart.open();
		}
	  }
	});
}

$(function() {

$('.cat li.active b').toggle(function() {
		$(this).parent().find('ul').slideUp(300);
		$(this).removeClass('up').addClass('down');
	}, function() {
		$(this).parent().find('ul').slideDown(300);
		$(this).addClass('up').removeClass('down')
	});

  $('.bluebox .quan .submit').click(function(e) {
    e.preventDefault();
    var quan = $('.bluebox .quan .kol').val();
    
    new_cart += parseInt(quan);
    // change cart items count
    $('.cart .opencart b').empty().html(current_cart + new_cart); // end  
  });
  
  $('a.tocart').click(function(e) {
    e.preventDefault();
    
    new_cart++;
    // change cart items count
    $('.cart .opencart b').empty().html(current_cart + new_cart); // end
     
    // prepare added-to-cart product info
    var product_id = $(this).attr('id');
    var product_title = $('div.'+product_id+' .name a').html();
    var product_price = $('div.'+product_id+' .price span.right').html();
    var product_eur = product_price.substring(0, product_price.length-1);
    var product_dot = parseFloat(product_eur.replace(",", "."));
    var product_href = $(this).attr('href'); // end
    
    // add clicked product price into array
    product_prices.push(product_dot); // end
    
    // sum the array
    var total_price = 0;
    //product_prices.push(product_dot);
    $.each(product_prices, function() {
      total_price += this;
    }); // end
    
    var total_price_rounded = Math.round(total_price*100)/100;
    $('.dropcart .sum .price_only').empty();
    
    var price = parseFloat(total_price_rounded);
    $('.dropcart .sum .price_only').html(" "+price+" €");
    
    $('.dropcart .cart_load').html('<img src="skins/svet_komunikacij/images/ajax-loader-cart.gif" />');
    $.ajax({
    	type: "POST",
    	url: "shoppingcart.php?action=addone&productId="+product_id+"",
    	success: function(msg){
    	  $('.dropcart .cart_load').empty();
    	  $('.dropcart .added').html('Izdelek je bil uspešno dodan v košaro!');
        $('.cart_items').append("<div class='row item'><a class='name' href='"+product_href+"' title='"+product_title+"'>1x "+product_title+"</a><span class='price'>cena: "+product_price+"</span></div>");
        if ($('.dropcart').is(":hidden")) {
          minicart.open();
        }
      }
    });
  });
  
  /* PRODUCT DISPLAY VARIATIONS */
  $('span.line').click(function(){
    $('#productlist').addClass('vrsticno');
    $('#productlist').removeClass('skatle');
    $.cookie('display', 'line');
  });
  $('span.grid').click(function(){
    $('#productlist').removeClass('vrsticno');
    $('#productlist').addClass('skatle');
    $.cookie('display', 'grid');
  });
  
  var display = $.cookie('display');
  
  if (display == 'line') {
    $('#productlist').addClass('vrsticno');
  } else {
    $('#productlist').addClass('skatle');
  }
  
  // TRADEMARKS
  $(".filters select[name=manufacturer]").change(function(){
      
	  $('#filtersPage').val(0);
	  getFilteredContent();
      
  });
  
  // STOCK
  $(".filters input[name=stock]").click(function(){
	$('#stockMode').val($(this).attr('value'));
	$('#filtersPage').val(0);
    getFilteredContent();
  });

  // PRICE 0 - 50
  $('.filters a.range1').click(function(e) {
    e.preventDefault();
	$('#priceMode').val(1);
	$('#filtersPage').val(0);
    getFilteredContent();
  });
  
  // PRICE 50 - 100
  $('.filters a.range2').click(function(e) {
    e.preventDefault();
    $('#priceMode').val(2);
	$('#filtersPage').val(0);
    getFilteredContent();
  });
  
	// PRICE 50 - ...
	$('.filters a.range3').click(function(e) {
		e.preventDefault();
		$('#priceMode').val(3);
		$('#filtersPage').val(0);
		getFilteredContent();
	});
  
	// RESTORE DEFAULTS
	$('.filters .restore').click(function(e) {
		$('#priceMode').val('null');
		$('#stockMode').val('null');
		$('#filtersPage').val(0);
		getFilteredContent();
	});
  
    var options = {
  	    zoomWidth  : 250,
  	    zoomHeight : 250,
  	    title      : false
  	};

    $(".jqzoom").jqzoom(options);

  	$('.bind').bind('mouseover', function(e) {
      e.preventDefault();

  		//console.log($(this).attr('src'));
  		original = $('#origimagejao')
  		var orig_src = $(original).find('img').attr('src');

  		rep = original.clone();
  		$('.pic .big').append(rep)
  		original.remove();
  		$(rep).attr('href',$(this).attr('src'));	    
  		$(rep).find('img').attr('src',$(this).attr('src'));	

  		// Recall the zoomer to update the page
  	    $(this).attr('src', orig_src);
  	    //$('.jqzoom').unbind();
  	    var options = {
  		    zoomWidth  : 250,
  		    zoomHeight : 250,
  		    title      : false
  		};
          $(rep).jqzoom(options);
  });
});

function filtersPage(page)
{
	$('#filtersPage').val(page);
	getFilteredContent();
}

function getFilteredContent()
{
	$('#gallery').empty();
	$('#gallery').html('<div class="loader"><img src="skins/svet_komunikacij/images/ajax-loader.gif" /></div>');

	var priceMode = $('#priceMode').val();
	var stockMode = $('#stockMode').val();
	var trademark = $('#manufacturer_select').val();
	var page = $('#filtersPage').val();
	var categoryId = $('#categoryId').val();

	$.post( 'filter_svet.php',
			{
				tm: trademark,
				price: priceMode,
				stock: stockMode,
				catid: categoryId,
				page: page
			},
			function(msg){
				if ($(msg).text() == '')
				{
					$("#gallery").html('<div class="loader">Ni izdelkov.</div>');
				}
				else
				{
					$("#gallery").html(msg);
				}
			},
			'html'
		);
}
