function rgb2hex(rgb){
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 return "#" +
  ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
  ("0" + parseInt(rgb[3],10).toString(16)).slice(-2);
}
function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name){
			return unescape(y);
		}
	}
}
function setTitle(){
	$('.tytul_inner').css({'position' : 'absolute'});
	szerokoscTresci = $('.tytul_inner').outerWidth();
	$('.tytul_inner').css({
		'position' : 'static',
		'width' : szerokoscTresci+'px'
	});
}
function scrollTitle(){
	szerokoscBoxu = $('.tytul_strony').innerWidth();
	szerokoscTresci = $('.tytul_inner').outerWidth();
	pozycja = $('.tytul_strony').scrollLeft();
	
	var czas = Math.abs(szerokoscTresci - szerokoscBoxu)*30;
	
	nowyScroll = 0;
	if(pozycja == 0)
		nowyScroll = szerokoscTresci - szerokoscBoxu;
		
	$('.tytul_strony').animate(
		{scrollLeft : nowyScroll},
		czas,
		'swing',
		function(){
			setTimeout("scrollTitle()",2000);
		}
	)
}

function wyswietlKomunikaty(){
	$.ajax({
		url: BASE + 'ajax/komunikaty.php',
		data: "ajax=1",
		cache: false,
		success: function(data) {
			$('#komunikaty').hide('fast', function(){
				$('#komunikaty').html(data);
			}).show('fast');
		},
		error: function(){
			$('#komunikaty').hide('fast', function(){
				$('#komunikaty').html(data);
			});
		}
	});
}

/*
 * SCROLL
 */
var szerokosc = 124;
var autoScroll = 1;
var autoScrollTimer = null;
function automatycznyScroll(){
	//$('.tresc_strony').append($('#scroll_srodek').scrollLeft());
	$('#scroll_srodek').scrollLeft($('#scroll_srodek').scrollLeft()+(autoScroll*1));
	
	if($('#scroll_srodek').scrollLeft() == 0 && autoScroll == -1){
		autoScroll = 1;
		autoScrollTimer = setTimeout(automatycznyScroll,2000);
		return;
	}
	
	if($('#scroll_srodek').scrollLeft() == $('.baner_produkt').length * szerokosc - $('#scroll_srodek').innerWidth() && autoScroll == 1){
		autoScroll = -1;
		autoScrollTimer = setTimeout(automatycznyScroll,2000);
		return;
	}
	autoScrollTimer = setTimeout(automatycznyScroll,20);
}

var animacja = true;
var animacjaBoks = true;

$(document).ready(function(){
	$("div[flash]").each(function() {
		var box = $(this);
		var flash = $.flash.create({
			swf: box.attr('flash'),
			//height: 400,
			width: box.css('width')
		});
		box.html(flash);
	});
	
	$('input[type="checkbox"],input[type="radio"]').css('border', '0');
	
	setTitle();
	scrollTitle();
	$('input[wzorzec], textarea[wzorzec]').trigger('keyup');
	
	$('[rel="lightbox"]').lightBox();
	
	
	/*
	 * SCROLL
	 */
	var szerokosc = 124;
	$('#baner_przewijany_produkty').css('width', $('.baner_produkt').length*szerokosc+'px');
	$('#scroll_lewo').click(function(){
		if(animacja){
			animacja = false;
			clearTimeout(autoScrollTimer);
			autoScroll = -1;
			$('#scroll_srodek').stop(true).animate({
				'scrollLeft' : '-='+szerokosc*5
			},1000,'swing',function(){
				animacja = true;
				automatycznyScroll();
			});
		}
	});
	$('#scroll_prawo').click(function(){
		if(animacja){
			animacja = false;
			clearTimeout(autoScrollTimer);
			autoScroll = 1;
			$('#scroll_srodek').stop(true).animate({
				'scrollLeft' : '+='+szerokosc*5
			},1000,'swing',function(){
				animacja = true;
				automatycznyScroll();
			});
		}
	});
	$('#scroll_srodek').mouseenter(function(){
		$('#scroll_srodek').stop(true);
		clearTimeout(autoScrollTimer);
		animacja = true;
	}).mouseleave(function(){
		automatycznyScroll();
		animacja = true;
	})
	automatycznyScroll();
	
 

	$('.boksScroll .scroll').each(function(){
		var ilosc = $(this).find('.boksScrollProdukt').length;
		//$(this).css('position','absolute');
		var szerokosc = ilosc*120;
		$(this).css({
			'position':'static',
			'width': szerokosc+'px'
		});
	})
	
	$('.boksScroll').each(function(){
		var rodzic = $(this);
		var dziecko = rodzic.find('.srodek').first();
		var scrollWidth = dziecko.find('.scroll').first().width();
		if((scrollWidth - 120) <= dziecko.scrollLeft()){
			rodzic.find('.prawo').fadeTo(500, 0.2);
		}else{
			rodzic.find('.prawo').fadeTo(500, 1);
		}
		if(dziecko.scrollLeft() <= 0){
			rodzic.find('.lewo').fadeTo(500, 0.2);
		}else{
			rodzic.find('.lewo').fadeTo(500, 1);
		}
	})
	
	
	$('.boksScroll .lewo').click(function() {
		var rodzic = $(this).closest('.boksScroll');
		var dziecko = rodzic.find('.srodek').first();
		if (animacjaBoks) {
			var scroll = dziecko.scrollLeft();
			animacjaBoks = false;
			dziecko.animate(
				{
					scrollLeft: scroll-120
				}, 
				500, 
				'swing',
				function() {
					animacjaBoks = true;
					var scrollWidth = dziecko.find('.scroll').first().width();
					if((scrollWidth - 120) <= dziecko.scrollLeft()){
						rodzic.find('.prawo').fadeTo(500, 0.2);
					}else{
						rodzic.find('.prawo').fadeTo(500, 1);
					}
					if(dziecko.scrollLeft() <= 0){
						rodzic.find('.lewo').fadeTo(500, 0.2);
					}else{
						rodzic.find('.lewo').fadeTo(500, 1);
					}
					
				}
			);
		}
	});
	
	$('.boksScroll .prawo').click(function() {
		var rodzic = $(this).closest('.boksScroll');
		var dziecko = rodzic.find('.srodek').first();
		if (animacjaBoks) {
			var scroll = dziecko.scrollLeft();
			animacjaBoks = false;
			dziecko.animate(
				{
					scrollLeft: scroll+120
				}, 
				500, 
				'swing',
				function() {
					animacjaBoks = true;
					var scrollWidth = dziecko.find('.scroll').first().width();
					if((scrollWidth - 120) <= dziecko.scrollLeft()){
						rodzic.find('.prawo').fadeTo(500, 0.2);
					}else{
						rodzic.find('.prawo').fadeTo(500, 1);
					}
					if(dziecko.scrollLeft() <= 0){
						rodzic.find('.lewo').fadeTo(500, 0.2);
					}else{
						rodzic.find('.lewo').fadeTo(500, 1);
					}
					
				}
			);
		}
	}); 
	
	$('.boksKategoria a').mouseover(function() {
		$(this).addClass('boksKategoriaHover');
	});
	
	$('.boksKategoria a').mouseout(function() {
		$(this).removeClass('boksKategoriaHover');
	});
	
	$('#select_adresy').change(function() {
		$('#adres').html('<img src="obrazki/loading.gif" style="margin: 20px;">');
		$.ajax({
			url: BASE + 'ajax/adres.php',
			data: "ajax=1&id="+$(this).val(),
			cache: false,
			success: function(data) {
				$('#adres').html(data);
				$('input[wzorzec]').trigger('keyup');
			}
		});
	}); 
	
	$('#eservice_form').delay(5000).queue(function() {
		$(this).submit();
	});
	
	$('#przypomnienie_hasla').submit(function(e){
		e.preventDefault();
		var pole = $(this).find('[name="email"]');
		var regex  = new RegExp(pole.attr('wzorzec'));
		if(regex.test(pole.val())) {
			var email = pole.val();
			var old = $('#przypomnienie_hasla').html();
			$('#przypomnienie_hasla').html('<img src="obrazki/loading.gif" style="margin: 20px;">');
			$.ajax({
				type: 'POST',
				url: BASE + 'ajax/przypomnienie.php',
				data: "ajax=1&email="+pole.val(),
				cache: false,
				success: function(data) {
					$('#przypomnienie_hasla').html(old);
					wyswietlKomunikaty();
				},
				error: function(){
					$('#przypomnienie_hasla').html(old);
				}
			});
		} else {
			alert('Podana wartość nie jest poprawnym adresem email.');
		}
		$('input[wzorzec]').trigger('keyup');
	})
	
	$('#formularz_kontaktowy').submit(function(e){
		var imie = $(this).find('[name="imie"]').first();
		var nazwisko = $(this).find('[name="nazwisko"]').first();	
		var telefon = $(this).find('[name="telefon"]').first();
		var email = $(this).find('[name="email"]').first();
		if(($.trim(telefon.val()) == '' && $.trim(email.val()) == '') && ($.trim(imie.val()) == '' || $.trim(nazwisko.val()) == '')){
			alert('Pola imie i nazwisko nie mogą być puste oraz przynajmniej jedno z pól, telefon lub email, powinny zostać wypełnione');
			e.preventDefault();
		}else{
			if($.trim(imie.val()) == '' || $.trim(nazwisko.val()) == ''){
				alert('Pola imie i nazwisko nie mogą być puste.');
				e.preventDefault();
			}else{
				if($.trim(telefon.val()) == '' && $.trim(email.val()) == ''){
					alert('Pola telefon i email nie mogą być puste. Przynajmniej jedno z tych pól powinno być wypełnione.');
					e.preventDefault();
				}
			}
		}
	})
})
$(window).load(function(){
	$('.baner_produkt').each(function(){
		var wysokosc = 120;
		var obrazek = $(this).find('img').first();
		if(wysokosc > obrazek.height()){
			obrazek.animate(
				{
					'margin-top' : ((wysokosc - obrazek.height()) / 2)+'px'
				}, 
				500
			);
		}
	})
	
	$('.boksScrollFoto').each(function(){
		var wysokosc = 150;
		var obrazek = $(this).find('img').first();
		//alert(orazek.attr('src'));
		if(wysokosc > obrazek.height()){
			obrazek.animate(
				{
					'margin-top' : ((wysokosc - obrazek.height()) / 2)+'px'
				}, 
				500
			);
		};
	})	
	$('.info_box').delay('15000').hide('slow');
})


$('.walidacja_form').live('submit', function(e){
	var poprawne = 1;
	$(this).find('[wzorzec]').each(function(){
		var regex  = new RegExp($(this).attr('wzorzec'));
		var dlugosc = true;
		if($(this).attr('wymagane') == 1){
			if($(this).val().length < 1){
				dlugosc = false;
			}
		}
		if(regex.test($(this).attr('value')) && dlugosc) {
			poprawne *= 1;
		} else {
			poprawne *= 0;
		}
	});
	//if($('.koszyk_produkt_nieparzysty, .koszyk_produkt_parzysty').length > 0) {
	//	poprawne *= 1;
	//} else {
	//	poprawne *= 0;
	//}
	if(poprawne == 0){
		alert('Prosze wypełnić wszystkie wymagane pola poprawnymi danymi.\nPola oznaczone czerwonym tłem są wypełnione niepoprawnie');
		e.preventDefault();
	}
})



var poprawne = '#AAFFAA';
var bledne = '#FFAAAA';
$('input[wzorzec], textarea[wzorzec]').live('keyup', function() { 
	var regex  = new RegExp($(this).attr('wzorzec'));
	var dlugosc = true;
	if($(this).attr('wymagane') == 1){
		if($(this).val().length < 1){
			dlugosc = false;
		}
	}
	if(regex.test($(this).attr('value')) && dlugosc) {
		$(this).css('background-color', poprawne);
	} else {
		$(this).css('background-color', bledne);
	}
}).live('change', function(){
	$(this).trigger('keyup');
});

$('#koszyk_id_adresy').live('change', function() {
	$('.koszykAdres').html('<img src="obrazki/loading.gif" style="margin: 20px;">');
	$.ajax({
		url: BASE + 'ajax/koszyk_adres.php',
		data: "ajax=1&id="+$(this).val(),
		cache: false,
		success: function(data) {
			$('.koszykAdres').html(data);
			$('input[wzorzec]').trigger('keyup');
		}
	});
}); 

/*
 * 
 */
var mysz_nad_ile_na_strone = false;
$('.ile_na_strone').live('click',function(e){
	$('.ile_na_strone_lista').toggle();
}).live('mouseenter',function(){
	mysz_nad_ile_na_strone = true;
}).live('mouseleave',function(){
	mysz_nad_ile_na_strone = false;
});
$('.ile_na_strone_lista').live('mouseenter',function(){
	mysz_nad_ile_na_strone = true;
}).live('mouseleave',function(){
	mysz_nad_ile_na_strone = false;
})
$('html').live('click',function(){
	if(!mysz_nad_ile_na_strone){
 		$('.ile_na_strone_lista').hide();
 	}
});

var mysz_nad_kolejnosc = false;
$('.kolejnosc_produktow').live('click',function(e){
	$('.kolejnosc_produktow_lista').toggle();
}).live('mouseenter',function(){
	mysz_nad_kolejnosc = true;
}).live('mouseleave',function(){
	mysz_nad_kolejnosc = false;
});
$('.kolejnosc_produktow_lista').live('mouseenter',function(){
	mysz_nad_kolejnosc = true;
}).live('mouseleave',function(){
	mysz_nad_kolejnosc = false;
})
$('html').live('click',function(){
	if(!mysz_nad_kolejnosc){
 		$('.kolejnosc_produktow_lista').hide();
 	}
});

$('.historia_wiersz').live('click', function(){
	var adres = BASE+'ajax/pokaz_zamowienie.php?ajax=1&id='+$(this).attr('id_zamowienia')+'&token='+$(this).attr('token');
	window.open(adres, 'Zamowienie', 'width=800, height=600, resizable=no, toolbar=no, location=yes, directories=no, status=no, menubar=no, scrollbars=yes');
});

//wyszukiwarka zaawansowana - start
function sprawdz_wyszukiwarke_kategorii() {
	if($('#wyszukiwarka_podkategoria').length > 0) {
		$('#wyszukiwarka_podkategoria').attr('name', 'kategoria');
		$('#wyszukiwarka_kategoria').removeAttr('name');
	} else {
		$('#wyszukiwarka_kategoria').attr('name', 'kategoria');
	}
}

$('#wyszukiwarka_kategoria').live('change', function() {
	$.ajax({
		url: BASE + 'ajax/wyszukiwarka_podkategoria.php',
		data: "ajax=1&seo="+$(this).val(),
		cache: false,
		success: function(data) {
			$('#podkategoriaWyszukiwarki').html(data);
			sprawdz_wyszukiwarke_kategorii();
		}
	});
})
//wyszukiwarka zaawansowana - end

/*
 * opinie - start
 */
$('#dodajOpinieButton').live('click', function() {
	$.ajax({
		url: BASE + 'ajax/opinie_dodaj.php',
		data: $('#dodajOpinieForm').serializeArray(),
		type: 'POST',
		cache: false,
		success: function(data) {
			$('#opinie').hide('fast');
			$.ajax({
				url: BASE + 'ajax/opinie.php',
				data: $('#dodajOpinieForm').serializeArray(),
				cache: false,
				success: function(data) {
					$('#opinie').html(data);
					$('#opinie').slideDown();
				}
			});
		}
	});
});
/*
 * opinie - koniec
 */
