function tabs(object) {
	$(".tab_on").attr("class", "tab");
	object.attr("class", "tab_on");
}

function toCart(id) {
	url = base_url + '/' + id + ',do-koszyka.html';
	$.get(url, { ajax: "true" }, function(data) {
		split = data.split("|");
		if (split[0] == 'error') {
			$.prompt(split[1]);
		} else {
			$("#price").text(split[0]);
			$("#qty").text(split[1]);
		}
	});
	
	return false;
}

function toStorage(id) {
	url = base_url + '/' + id + ',do-przechowalni.html';
	$.get(url, { ajax: "true" }, function(data) {
		$("#storage_count").text(data);
	});
	
	return false;
}

function getDescription(id, name) {
	url = base_url + '/' + 'produkt,opis,'+id+','+name+'.html';
	$.get(url, { ajax: "true" }, function(data) {
		$("#content").html(data);
		$(".tab, .tab_on").click(function() { tabs($(this)); });
		$("#photo").fancybox();
		$(".element").each(function(i) { if (i == 0 || i % 2 == 0) { $(this).addClass("dark"); } });
	});
	
	return false;
}

function getGallery(id, name) {
	url = base_url + '/' + 'produkt,galeria,'+id+','+name+'.html';
	$.get(url, { ajax: "true" }, function(data) {
		$("#content").html(data);
		$(".tab, .tab_on").click(function() { tabs($(this)); });
		$(".photo").fancybox();
	});
	
	return false;
}

function getOpinions(id, name) {
	url = base_url + '/' + 'produkt,opinie,'+id+','+name+'.html';
	$.get(url, { ajax: "true" }, function(data) {
		$("#content").html(data);
		$(".tab, .tab_on").click(function() { tabs($(this)); });
		$("#photo").fancybox();
	});
	
	return false;
}

function setShipping(id, price) {
	url = base_url + '/' + id + ',rodzaj-dostawy.html';
	$.get(url, 'price='+price, function(data) {
		split = data.split('|');
		$("#shipping_price").text(split[0]);
		$("#sum_price").text(split[1]);
	}); 
}

function setPayment(id) {
	url = base_url + '/' + id + ',rodzaj-platnosci.html';
	$.get(url);
}

function setDiscount() {
	var code = $('#discount_code').val();
	if (code.length > 0) {
		url = base_url + '/' + code + ',sprawdz-rabat.html';
		$.get(url, function(data) {
			split = data.split('|');
			if (split[0] == 'error') {
				$.prompt(split[1]);
			}
			if (split[0] == 'ok') {
				window.location.href = base_url + '/' + code + ',dodaj-rabat.html';
			}
		});
	} else {
		$.prompt('Wprowadź kod rabatowy');
	}
}

function toBuy(qty, ship, pay) {
	url = base_url + '/' + 'dane-zamowienia.html';
	
	if (qty == 0) { $.prompt('W twoim koszyku nie ma żadnych produktów'); }
	if (ship == 0) { $.prompt('Musisz wybrać sposób dostawy'); }
	if (pay == 0) { $.prompt('Musisz wybrać sposób płatności'); }
	
	if (qty > 0 && ship > 0 && pay != 0) {
		window.location.href = url;
	}
}

function sendOrder() {
	$(".error").hide();
	
	var error = 0;
	$("input.required").each(function() {
		var name = $(this).attr('name');
		if ($(this).val() == '') {
			$("#"+name).slideDown('slow');
			error++;
		}
	});
	
	if (error == 0) {
		$("#send_order").submit();
	} else {
		return false;
	}
}

function saveNewsletter() {
	var mailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var mail = $("#news_mail").val();
	
	if (mail.search(mailRegEx) == -1) {
		$.prompt('Podany adres e-mail jest nieprawidłowy');
	} else {
		$.get(base_url + '/' + 'newsletter,zapisz.html', 'mail='+mail, function(data) {
			split = data.split('|');
			if (split[0] == 'OK') {
				$.prompt(split[1]);
				$("#news_mail").val($("#news_mail").attr('title')); 
			} else {
				$.prompt(split[1]);
			}
		});
	}
}

function searchEngine() {
	var value = $("#search").val();
	var title = $("#search").attr('title');
	
	if (value == title || value == "") {
		$.prompt('Musisz wpisać frazę do wyszukania');
		return false;
	} else {
		var url = encodeURIComponent(value);
		var search = url.replace(/%20/gi, "+");
		window.location.href = base_url + '/' + 'wyszukaj.html?phrase='+search;
		return false;
	}
}

function sendForm() {
	var mail = $("#mail").val();
	var thema = $("#thema").val();
	var content = $("#content").val();
	var error = 0;
	var message = "";
	
	if (mail.length == 0) { 
		error++; message += "Musisz podać adres e-mail<br />"; 
	} else {
		var mailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (!mailPattern.test(mail)) {
			error++; message += "Nieprawidłowy adres e-mail<br />";
		}
	}
	if (thema.length == 0) { error++; message += "Wiadomość musi posiadać temat<br />"; }
	if (content.length == 0) { error++; message += "Wiadomość musi posiadać treść<br />"; }
	
	if (error > 0) {
		$.prompt(message);
	} else {
		data = "mail="+mail+"&thema="+thema+"&content="+content;
		
		$.post(base_url + '/' + 'kontakt,wyslij.html', data, function(data) {
			if (data == 'ok') {
				$("#mail").val("");
				$("#thema").val("");
				$("#content").val("");
				
				$("#form").slideUp("slow");
				$("#sended").slideDown("slow");
			} else {
				$.prompt('Wystąpił błąd podczas wysyłania wiadomości<br />Spróbuj ponownie.');
			}
		});
	}
	
	return false;
}

$(document).ready(function() {
	$("#news_btn").click(function() {
		saveNewsletter();
	});
	
	var inpt = $("input:text, textarea, input:password");
    inpt.each(function(){
        if(this.value == '') this.value = this.title;
    }).focus(function(){
        if(this.value == this.title) this.value = '';
    }).blur(function(){
        if(this.value == '') this.value = this.title;
    });
});