jQuery(document).ready(function($) {

	function formatPrice(num) {
		var p = num.toFixed(2).split(".");
    		return p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        		return  num + (i && !(i % 3) ? " " : "") + acc;
		}, "");
	}

	if(typeof reference!='undefined') {
		$.each(reference,function(index,value) {
			$('<a>').attr('href',value.url).append($('<div>').append($('<img>').attr('src',value.img))).appendTo('#flash');
		});
	}

	$('#flash').cycle();

	jQuery.cookie('sleva',$.ajax({url: "/zahradni-nabytek/slevy/",async: false}).responseText,{path: '/'});

	jQuery('li.cena:not(a)').each(function(index) {
		cena = parseInt(jQuery(this).children('strong').text().replace(/\s+/g,""));
		znacka = jQuery(this).attr('class').match(/znacka-(\S+)/);
		dph = 1.20;
		sleva = 1;
		dis = jQuery.parseJSON(jQuery.cookie('sleva'));

		$.each(dis, function(key, val) {
			if(val.brand.toLowerCase()==znacka[1].toLowerCase()) {
				sleva = 1-(val.discount/100);
			}
		});

		value = cena*sleva;

		if(sleva!=1) {
	                string = "Po slevě: ";
			string += "<strong price=\""+Math.round(value)+"\">"+formatPrice(Math.round(value))+"</strong>";
                        string += " Kč ";
                        string += "(bez DPH: "+formatPrice(Math.round(value/dph))+" Kč)";		
		jQuery(this).parent().append($('<li>').attr('class','discount').html(string));
		}
		else {
                        string = "<strong price=\""+Math.round(value)+"\"></strong>";
		}

	});

});

