$(function(){
	
	$('.btn-back').click(function(){
		history.back();
		return false;
	});
		
	//Tooltip
	$('#contactForm .error').tooltip();
	$('.TTip').tooltip({showURL: false, track: true, delay: 350, left: -200, extraClass: "item-image", bodyHandler: function() { return $('#'+$(this).attr("rel")).html(); }});
	
	//rating
	//$('input.rating-star').rating();
	
	//colorbox
	$('a[rel*=colorbox]').colorbox();
	$('#colorbox-regulamin, .promo-regulamin').colorbox({width:"800", height:"600", iframe:true});
	$('.promo-regulamin').colorbox({width:"70%", height:"80%", iframe:true});
	$('.OrderDetails').colorbox({ innerWidth: 780, height:"90%", iframe:true});
	
	//pajinate
/*	$('#comments-list').pajinate({
		items_per_page: 3,
		nav_panel_id: '#comments-pagination'
	});
*/
	//close info
	$('#close-info').click(function(){
		$(this).parent().hide(200);		
		return false;
	});
	
	//promocje
	$('#show-prom').click(function(){
		$('#prom-description').toggle(200);
		if($(this).html() == 'Pokaż opis promocji')
			$(this).html('Ukryj opis promocji');
		else
			$(this).html('Pokaż opis promocji');
			
		return false;
	});
	
	// BOF KOSZYK
	
	$('#container .tocart').click(function() {
		var ILOSC = $('#Ilosc-szt').val();
		
		if (ILOSC > 0)
			$(this).attr('href',$(this).attr('href')+'?ilosc='+ILOSC);
		
		return true;
	});
	
	$('.cart-item-delete').click(function() {
		var r = confirm("Czy na pewno usunąć ten produkt z koszyka?");
		if (r == true)
			window.location = $(this).attr('href');
		else
			return false;
	});
	
	$('#wyczysc-koszyk').click(function() {
		var r = confirm("Czy na pewno usunąć wszystkie produkty z koszyka?");
		if (r == true)
			window.location = $(this).attr('href');
		else
			return false;
	});
	
	$('.ToggleDiv').click(function() {
		$('#'+$(this).attr('rel')).slideToggle(400);
		return false;
	});
	
	$('.WybierzPlatnosc').click(function() {
		$('#overlay-max').show(0);
		$.ajax({url: './wybierz_platnosc,'+$(this).attr('rel')+'.html', success: function() {
			window.location.href = './koszyk.html';
		}});
	});
	
	$('.WybierzDostawe').click(function() {
		$('#overlay-max').show(0);
		$.ajax({url: './wybierz_dostawe,'+$(this).attr('rel')+'.html', success: function() {
			window.location.href = './koszyk.html';
		}});
	});
	
	// EOF KOSZYK
});

function OnFocusInput(id, msg)
{
	text = $('#'+id).val();
	
	if(text == msg)
		$('#'+id).val('');
	
	$('#'+id).removeClass('error');
}

function OnBlurInput(id, msg)
{
	text = $('#'+id).val();
	
	if(text == '')
		$('#'+id).val(msg);
}

function OnFocusTextarea(id, msg)
{
	text = $('#'+id).html();
	
	if(text == msg)
		$('#'+id).html('');
}

function OnBlurTextarea(id, msg)
{
	text = $('#'+id).html();
	
	if(text == '')
		$('#'+id).html(msg);
	
	$('#'+id).removeClass('error');
}

function nowy_token() {
	var id = new Math.random(9999);
	document.getElementById("token").src = "captcha.jpg?id=" + id + "";
	document.getElementById("captcha").value = "";
}


function DodajDoZakladek(bookmarkurl,bookmarktitle){
	if (navigator.appName=='Netscape')
		window.sidebar.addPanel(bookmarktitle,bookmarkurl, '');
	else if (navigator.appName=='Microsoft Internet Explorer')
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
} 


function show_confirm(url)
{
	var r=confirm("Czy na pewno usunąć?");
	if (r==true)
		window.location=url;
	else
			return false;
}


function potwierdz(url,msg)
{
	var r=confirm(msg);
	if (r==true)
		window.location=url;
	else
			return false;
}


function dodajDoKoszyka(url)
{
	var result=prompt('Podaj ilość sztuk:');
	var Wzor = /^[0-9]+$/;
	
	if (Wzor.test(result))
		window.location=url+result;
}


function gotoURL(url)
{
	window.location=url;
}



/*
function checkForm()
{
	var brakuje_danych=false;
	var formularz=document.forms['kontakt'];
	var napis="";
	if (formularz.ImieNazwisko.value == "")
	{
		napis += "- imie i nazwisko<br />"
			brakuje_danych=true;
	}
	
	if (formularz.Telefon.value == "")
	{
		napis += "- telefon<br />"
		brakuje_danych=true;
	}
	
	var WzorMaila = /^[0-9a-zA-Z_.-]+@+[0-9a-zA-Z.-]+\.[a-zA-Z]{2,3}$/;
	if (!WzorMaila.test(formularz.Email.value))
	{
	    napis += "- e-mail (np. imienazwisko@nnn.pl)<br />"
	    brakuje_danych=true;
	}
	
	if (formularz.Temat.value == "")
	{
		napis += "- temat<br />"
			brakuje_danych=true;
	}
	
	var WzorTresci = /^.{10,}$/;
	if (!WzorTresci.test(formularz.Tresc.value))
	{
		napis += "- treść (min. 10 znaków)<br />"
    	brakuje_danych=true;
	}
	
	if (!formularz.Preferowana.value)
	{
		napis += "- wybrać preferowaną formę kontaktu<br />"
			brakuje_danych=true;
	}
	
	if (!brakuje_danych)
		return true; 
	else
	{ 
		Sexy.alert('<h1>W formularzu pojawiły się błędy.</h1><br/><p>Proszę poprawnie wypełnić następujące pola<br />' + napis);
		scroll(0,0);
		return false;
	}
}
*/

function changeLang(parm)
{
	var new_language = "/"+parm+"/";
	var address = window.location.href;
	var current = new RegExp("/../","i");
	var new_address = address.replace(current, new_language);

	window.location.href = new_address;
}


function przelicz(flag) {
	
	var rabat = (100-document.getElementById('rabat').value)/100;
	var kwota = document.getElementById('all2').value
	
	if (flag==1)
	{
		document.getElementById('all').value=kwota * rabat;
		document.getElementById('RAZEM').value=kwota * rabat;
	}
	else if (flag==2)
	{
		document.getElementById('all').value=kwota;
		document.getElementById('RAZEM').value=kwota;
	}
	document.getElementById('link').href='realizacja_zamowienia.html?payment='+flag;
}


function expand(div)
{
	var Block = document.getElementById('tab'+div);
	if (Block.style.display=='none') 
		Block.style.display='block';
	else 
		Block.style.display='none';

	var Link = document.getElementById('link'+div);
	
	if (Link.className=='link-closed')
		Link.className='link-opened';
	else
		Link.className='link-closed'; 
}


function validateEmail(id)
{
	document.getElementById(id).className = 'kom-text';
	
	var Wzor1 = /^[0-9a-zA-Z_.-]+@+[0-9a-zA-Z.-]+\.[a-zA-Z]{2,3}$/;
	if (!Wzor1.test(document.getElementById(id).value)) 
		document.getElementById(id).className += ' error-comm';
}


function validateKomentarz(id)
{
	document.getElementById(id).className = 'kom-text';
	
	var Wzor1 = /^.{5,}/;
	if (!Wzor1.test(document.getElementById(id).value)) 
		document.getElementById(id).className += ' error-comm';
}


function validateNick(id)
{
	document.getElementById(id).className = 'kom-text';
	
	var Wzor1 = /^.+/;
	if (!Wzor1.test(document.getElementById(id).value)) 
		document.getElementById(id).className += ' error-comm';
}


function checkCommentForm()
{
	var brakuje_danych1 = false;
	var napis1 = "";
  
	document.getElementById('nick-comm').className = 'kom-text';
	document.getElementById('email-comm').className = 'kom-text';
	document.getElementById('komentarz-comm').className = 'kom-text';
	
	if (document.getElementById('nick-comm').value == "")
	{
		napis1 += "- nick\n";
		brakuje_danych1=true;
		document.getElementById('nick-comm').className += ' error-comm';
	}
	
	var Wzor1 = /^[0-9a-zA-Z_.-]+@+[0-9a-zA-Z.-]+\.[a-zA-Z]{2,3}$/;
	
	if (!Wzor1.test(document.getElementById('email-comm').value)) 
	{
		napis1 += "- e-mail (np. imienazwisko@nnn.pl)\n";
		brakuje_danych1=true;
		document.getElementById('email-comm').className += ' error-comm';
	}
	
	if ( document.getElementById('komentarz-comm').value.length < 5 )
	{
		napis1 += "- treść komentarza (min. 5 znaków)\n";
		brakuje_danych1=true;
		document.getElementById('komentarz-comm').className += ' error-comm';
	}
	
	if (brakuje_danych1)
		var flag = 2;
	else
		var flag = 1;
	
	DodajKomentarz(flag);
	return false;
}


function PopUP(url, width, height, scrollbars) {
	var Win=window.open(url,"ToDo",'width=' + width + ',height=' + height + ',resizable=no,scrollbars=' + scrollbars + ',menubar=no' );

}
