
$(document).ready(function() {
	var buff, temp, temp2;

	// kupowanie
	$("a.ajax_kup_teraz").click(function() {
		ajax($(this).attr('href')+'&ajax=true', 1);
		return false;
	});

	// usuwanie wielu
	$("a.ajax_usun_zaznaczone").click(function() {
		array = new Array();
		buff = $("input.ajax_produkt").map(function() {
			return $(this);
		});
		jQuery.each(buff, function(index, value) {
			if (value.attr('checked')) {
				temp = new Array(value.attr('value'), document.getElementById('ajax_produkt_wersja_'+value.attr('value')).value);
				$('div.ajax_koszyk_'+value.attr('value')).remove();
				array.push(temp.join(','));
			}
		});
		array = array.join(';');
		ajax($(this).attr('href')+array+'&ajax=true', 3);
		return false;
	});

	// usuwanie
	$("a.ajax_usun").click(function() {
		buff = $(this).attr('href');
		temp = buff;
		temp = temp.split('usun=');
		temp = temp[1].split(',');
		$("div.ajax_koszyk_"+temp[0]).remove();
		ajax(buff+'&ajax=true', 2);
		return false;
	});
});

function ajax(uri, func) {
	$.ajax({
		type: 'GET',
		url: uri,
		async: true,
		cache: false,
		timeout: 10000,
		dataType: "text",
		success: function(msg) {

			eval(msg);

			for (var klucz in koszyk) {
				if (klucz == 'produkty') {

					$('.ajax_koszyk_produkty').empty();
					temp = '';
					temp2 = '';
					var i = 0;
					for (var klucz2 in koszyk[klucz]) {
						temp += '<div class="twoj_koszyk0 ajax_koszyk_'+koszyk[klucz][klucz2]['id']+'">';
						temp += '<a class="send" title="'+koszyk[klucz][klucz2]['nazwa_pl']+'" href="'+koszyk[klucz][klucz2]['link']+'">'+koszyk[klucz][klucz2]['nazwa_pl']+'</a>';
						if (koszyk[klucz][klucz2]['wybrana_wersja_nazwa']) temp += ' ('+koszyk[klucz][klucz2]['wybrana_wersja_nazwa']+')';
						temp += ' x '+koszyk[klucz][klucz2]['ile'];
						temp += '<br /><br />';
						temp += '<span>Cena: '+koszyk[klucz][klucz2]['cena_cal']+' PLN</span>';
						temp += '<input class="ajax_produkt" name="id_produktu" value="'+koszyk[klucz][klucz2]['id']+'" type="checkbox" />';
						temp += '<input id="ajax_produkt_wersja_'+koszyk[klucz][klucz2]['id']+'" value="'+koszyk[klucz][klucz2]['wybrana_wersja']+'" type="hidden" />';
						temp += ' <a class="ajax_usun" href="'+base+'koszyk/usun='+koszyk[klucz][klucz2]['id']+','+koszyk[klucz][klucz2]['wybrana_wersja']+'" title="Usuń">Usuń</a>';
						temp += '</div>';

						temp2 += '<div class="box_produkt ajax_koszyk_'+koszyk[klucz][klucz2]['id']+'">';
						temp2 += '<div class="box_produkt_foto">';
						temp2 += '<a title="'+koszyk[klucz][klucz2]['nazwa_pl']+'" rel="nofollow" href="'+koszyk[klucz][klucz2]['link']+'"><img src="'+base+'fotki/'+koszyk[klucz][klucz2]['nazwa']+','+koszyk[klucz][klucz2]['fotka']+',121x91.jpg" alt="'+koszyk[klucz][klucz2]['nazwa_pl']+'" /></a>';
						temp2 += '</div>';
						temp2 += '<div class="box_produkt_txt">';
						temp2 += '<table cellpadding="0" cellspacing="0" style="width: 350px">';
						temp2 += '<tr>';
						temp2 += '<td class="box_produkty_naglowek">';
						temp2 += '<a title="'+koszyk[klucz][klucz2]['nazwa_pl']+'" href="'+koszyk[klucz][klucz2]['link']+'">'+koszyk[klucz][klucz2]['nazwa_pl']+'</a>';
						if (koszyk[klucz][klucz2]['wybrana_wersja_nazwa']) temp2 += ' ('+koszyk[klucz][klucz2]['wybrana_wersja_nazwa']+')';
						temp2 += ' x '+koszyk[klucz][klucz2]['ile'];
						temp2 += '</td>';
						temp2 += '</tr><tr>';
						temp2 += '<td class="box_produkty_tresc">';
						if (koszyk[klucz][klucz2]['wersja'] != 'Default') {
							temp2 += '<select name="wersja_'+koszyk[klucz][klucz2]['id']+'_'+koszyk[klucz][klucz2]['wybrana_wersja']+'">';
							for (var klucz3 in koszyk[klucz][klucz2]['wersje']) {
								temp2 += '<option value="'+klucz3+'"';
								if (klucz3 == koszyk[klucz][klucz2]['wybrana_wersja']) temp2 += ' selected="selected"';
								temp2 += '>'+koszyk[klucz][klucz2]['wersje'][klucz3]+'</option>';
							}
							temp2 += '</select>';
						} else {
							temp2 += '<input type="hidden" name="wersja_'+koszyk[klucz][klucz2]['id']+'_'+koszyk[klucz][klucz2]['wybrana_wersja']+'" value="'+koszyk[klucz][klucz2]['wybrana_wersja']+'" />';
						}
						temp2 += '</td>';
						temp2 += '</tr><tr>';
						temp2 += '<td>';
						temp2 += '<table cellpadding="0" cellspacing="0" style="width: 350px">';
						temp2 += '<tr>';
						temp2 += '<td class="box_produkty_cena">';
						temp2 += 'Cena: <span class="gold">'+koszyk[klucz][klucz2]['cena_cal']+' PLN</span>';
						temp2 += '</td>';
						temp2 += '<td align="right">';
						temp2 += '<input name="ilosc_'+koszyk[klucz][klucz2]['id']+'_'+koszyk[klucz][klucz2]['wybrana_wersja']+'" style="width: 40px" type="text" value="'+koszyk[klucz][klucz2]['ile']+'" />';
						temp2 += '</td>';
						temp2 += '<td align="right" style="width:111px">';
						temp2 += '<input type="image" src="'+base2+'images/zmien.jpg" /> ';
						temp2 += '<a class="ajax_usun" href="'+base+'koszyk/usun='+koszyk[klucz][klucz2]['id']+','+koszyk[klucz][klucz2]['wybrana_wersja']+'" title="Usuń"><img src="'+base2+'images/usun.jpg" alt="Usuń" /></a>';
						temp2 += '</td>';
						temp2 += '</tr>';
						temp2 += '</table>';
						temp2 += '</td>';
						temp2 += '</tr>';
						temp2 += '</table>';
						temp2 += '</div>';
						temp2 += '</div>';

						++i;
					}
					$('.ajax_koszyk_produkty').html(temp);
					$('.ajax_koszyk_produkty2').html(temp2);
					if (!i) {
						$('.ajax_koszyk_produkty').html('<p>Twój koszyk jest aktualnie pusty.<br />Aby dokonać zakupu dodaj produkty do koszyka.</p>');
						$('.ajax_koszyk_produkty2').html('Twój koszyk jest aktualnie pusty.<br />Aby dokonać zakupu dodaj produkty do koszyka.');
					}

					// usuwanie
					$("a.ajax_usun").click(function() {
						buff = $(this).attr('href');
						temp = buff;
						temp = temp.split('usun=');
						temp = temp[1].split(',');
						$("div.ajax_koszyk_"+temp[0]).remove();
						ajax(buff+'&ajax=true', 2);
						return false;
					});

				} else $('.ajax_load_'+klucz).html(koszyk[klucz]);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			alert('AJAX - '+' - '+XMLHttpRequest+' - '+textStatus+' - '+errorThrown);
		}
	});
}

