function $(obj) {
	return (typeof obj == "string") ? document.getElementById(obj) : obj;
}
window.onload = function() {
	// obsluga menu bocznego
	if ($('sub_menu')) {
		var heads = $('sub_menu').getElementsByTagName('a');
		for (var i=0;i<heads.length;i++) {
			if ($(heads[i]).id) {
				$(heads[i]).onclick = function() {
					var uls = $('sub_menu').getElementsByTagName('ul');
					var id = this.id.split('_');
					var img = this.getElementsByTagName('img');
					if ($('k_' + id[1]).className == 'main') {
						$('k_' + id[1]).className = 'main none';
						$(img[0]).src = '/img/collapse.gif';
					} else {
						for (j = 0; j < uls.length; j++) {
							if ($(uls[j]).id) {
								$(uls[j]).className = 'main none';
							} else {
								var img = $(uls[j]).getElementsByTagName('img');
								if (img.length == 1) {
									$(img[0]).src = '/img/collapse.gif';
								}
							}
						}
						$('k_' + id[1]).className = 'main';
						image = $('kh_' + id[1]).getElementsByTagName('img');
						if(image.length == 1)
							image[0].src = '/img/expand.gif';
					}
					this.blur();
					return false;
				}
			}
		}
	}
}

window.onload = startList;
// Obsluga miniaturek
function switchThumb(prodId, imgName, obj)
{
	$('photo_big').src = '/img/produkt/' + prodId + '/180x240,' + imgName;
	var hrefs = $('prod-photos').getElementsByTagName('a');
	$(hrefs[0]).href = '/img/produkt/' + prodId + '/750x500,' + imgName;
	$(obj).blur();	
}
function switchPrice(obj, nazwa)
{
	for(var j=0;j<obj.length;j++) {
		document.getElementById(obj[j].value).style.display='none';
	}
	$(obj.options[obj.selectedIndex].value).style.display='inline';
	
	$("pid").value=obj.options[obj.selectedIndex].id;
	
	if(obj.options[obj.selectedIndex].lang=='0.00')
		$("koszyk_form").style.display='none';
	else
		$("koszyk_form").style.display='inline';
	//document.getElementById("produkt_nazwa").innerHTML=obj.options[obj.selectedIndex].label;
	$("produkt_nazwa").innerHTML=obj.options[obj.selectedIndex].title;
}

function setPrice(obj)
{
	$("pid").value=obj.options[obj.selectedIndex].id;
	if(obj.options[obj.selectedIndex].lang=='0.00')
		$("koszyk_form").style.display='none';
	else
		$("koszyk_form").style.display='inline';
	$("produkt_nazwa").innerHTML=obj.options[obj.selectedIndex].title;
}



// Obsluga zakladek w szczegolach
function switchTab(id, obj)
{
	for (i=0;i<8;i++) {
		if ($('tab_' + i)) {
			$('tab_' + i).className = 'tab none';
		}
	}
	$('tab_'+id).className = 'tab';
	var hrefs = $('tabs').getElementsByTagName('a');
	for (i=0;i<hrefs.length;i++) {
		$(hrefs[i]).className = '';
	}
	var lnk = $('tab_'+id).getElementsByTagName('a');
	$('tb_'+id).className = 'akt';
	$(obj).className = 'akt';
	$(obj).blur();
}

// Sprawdzenie czy element jest w tablicy
function in_array(item, obj)
{
	for(var i=0;i<obj.length;i++) {
		if(obj[i] == item) {
			return true
		}
	}
	return false;
}

// Sprawdzenie naciskanych klawiszy 
function checkMoneyKeys(e)
{
	var a = [8,9,13,37,39,35,36,46];
	var unicode = e.charCode ? e.charCode : e.keyCode;
	if(!in_array(unicode,a)) {
		if(unicode == 46) {
			var reg = /\./;
			if(reg.test(this) || this == '') {
				return false;
			}
		} else if (unicode < 48 || unicode > 57) {
			return false;
		}
	}
}





// Pokazanie warstwy z koszykiem
function showBasket()
{
	var o = $('koszyk_head'), r = o.offsetLeft;
	while (o = o.offsetParent) r += o.offsetLeft;
	$('koszyk_menu').style.left = r + 'px';
	var o = $('koszyk_head'), r = o.offsetTop;
	while (o = o.offsetParent) r += o.offsetTop;
	$('koszyk_menu').style.top = (r + 24) + 'px';
	if ($('koszyk_menu').style.display == 'block') {
		$('koszyk_menu').style.display = 'none';
		$('koszyk_img').src = '/img/koszyk_arr.gif';
		if ($('sort_box')) {
			$('sort_box').style.visibility = 'visible';
		}
	} else {
		$('koszyk_menu').style.display = 'block';
		$('koszyk_img').src = '/img/koszyk_arr_up.gif';
		if ($('sort_box')) {
			$('sort_box').style.visibility = 'hidden';
		}
	}
	/*window.setTimeout(function() {
		document.body.onclick = function() {
			$('koszyk_menu').style.display = 'none';
			$('koszyk_img').src = '/img/koszyk_arr.gif';
			document.body.onclick = function() { };
		}
	},100);
	$('koszyk_menu').onclick = function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}*/
	return false;
}

// Pokazanie warstwy z porownywarka
function showCompare()
{
	var o=$('porownaj_head'),r=o.offsetLeft;
	while(o=o.offsetParent)r+=o.offsetLeft;
	$('porownaj_menu').style.left = r + 'px';
	var o=$('porownaj_head'),r=o.offsetTop;
	while(o=o.offsetParent)r+=o.offsetTop;
	$('porownaj_menu').style.top = (r + 24) +'px';
	if ($('porownaj_menu').style.display == 'block') {
		$('porownaj_menu').style.display = 'none';
		$('porownaj_img').src = '/img/porownaj_arr.gif';
	} else {
		$('porownaj_menu').style.display = 'block';
		$('porownaj_img').src = '/img/porownaj_arr_up.gif';
	}
	/*window.setTimeout(function() {
	}	document.body.onclick = function() {
			$('porownaj_menu').style.display = 'none';
			$('porownaj_img').src = '/img/porownaj_arr.gif';
			document.body.onclick = function() { };
		}
	},100);
	$('porownaj_menu').onclick = function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}*/
	return false;
}

// Otwarcie nowego okna
function openWin(url, width, height, left, top)
{
	if (typeof left == "undefined") {
		if (document.compatMode.toLowerCase().indexOf('back') == -1) {
			var screenWidth = document.documentElement.clientWidth;
		} else {
			var screenWidth = document.body.clientWidth;
		}
		var left = Math.round((screenWidth-width)/2);
	}
	if (typeof top == "undefined") {
		if (document.compatMode.toLowerCase().indexOf('back') == -1) {
			var screenHeight = document.documentElement.clientHeight;
		} else {
			var screenHeight = document.body.clientHeight;
		}
		var top = Math.round((screenHeight-height)/2);
	}
	window.open(url,'','width='+width+',height='+height+',left='+left+',top='+top+',scrollbars=yes,menubar=no,toolbar=no');
}
function copyDown()
{
	$('nazwa_firmy').value = $('dostawa_nazwisko').value+ " "+$('dostawa_imie').value;
	$('miejscowosc').value = $('dostawa_miejscowosc').value;
	$('kod_pocztowy').value = $('dostawa_kod_pocztowy').value;
	$('ulica').value = $('dostawa_ulica').value;
	$('nr_domu').value = $('dostawa_nr_domu').value;
	$('nr_lokalu').value = $('dostawa_nr_lokalu').value;
	return false;
}

function zmienAdresDostawy(id) 
{
	self.location.href="/zamowienie/krok2/dostawa/"+id;
}


function rozwin_zwin(id){
	
	if($(id).className == 'none'){
		$(id).className = '';
		$('opinie_nag').innerHTML="Dodaj opinię";
	} else {
		$(id).className = 'none';
		$('opinie_nag').innerHTML="Dodane opinie";
	}
}

function blinkFlash() {
	blink = window.setInterval(function (){
			if ($('flash').className == '') {
				$('flash').className = "blink";
			} else {
				$('flash').className = "";
			}
		},
	 	200
	);
	var stop = window.setTimeout('clearInterval(blink);$("flash").className = ""', 1600);
}

function showHideDed(id) {
	var trs = $('tab_4').getElementsByTagName('tr');
	for (i=0;i<trs.length;i++) {
		if (trs[i].className == 'none ded_'+id) {
			trs[i].className = 'ded_'+id;
		} else if (trs[i].className == 'ded_'+id) {
			trs[i].className = 'none ded_'+id;
		}
	}
}

function productPrint(url,typ) {
	// usuniecie #costam z url
	url = url.replace(/#{1}[0-9a-z]+/i,"");
	// ustawienie cechy cenowej jesli istnieje
	var cechaCenowa = document.getElementById("ceny");
	if (cechaCenowa) {
		url = url+'/cechaCenowa/'+cechaCenowa.value;
	}
	// dodanie typu do url'a <domyslnie print> 
	if (typeof typ == 'undefined') {
		url = url+'/print/1';
		window.open(url,'productPrint','width='+770+',height='+600+',left='+100+',top='+100+',scrollbars=yes,menubar=no,toolbar=no');	
	} else {
		window.location.href = url+'/pdf/1';
	}
	
}

function setCechaCenowa(){
	var cechaCenowa = document.getElementById("ceny");
	if (cechaCenowa) {
		$('cechaCenowa').value = cechaCenowa.value;
	}
}
function fvEnable(show){
	if (show) {
		$('fv-block').style.display = '';
		$('nazwa_firmy').disabled = false;
		$('miejscowosc').disabled = false;
		$('kod_pocztowy').disabled = false;
		$('ulica').disabled = false;
		$('nr_domu').disabled = false;
		$('nr_lokalu').disabled = false;
		$('nip').disabled = false;
		if ($('przepiszButton'))
			$('przepiszButton').style.display = '';
	} else {
		$('fv-block').style.display = 'none';
		$('nazwa_firmy').disabled = true;
		$('miejscowosc').disabled = true;
		$('kod_pocztowy').disabled = true;
		$('ulica').disabled = true;
		$('nr_domu').disabled = true;
		$('nr_lokalu').disabled = true;
		$('nip').disabled = true;
		if ($('przepiszButton'))
			$('przepiszButton').style.display = 'none';
	}
}

function startList() {
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById("menu");
		if ( navRoot ) {
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}
