/*
 * Pokud neco z tohoto kodu pouzijete, budu rad, kdyz mi napisete, ze to pro vas bylo uzitecne: mzvarik@gmail.com
 */

function $(o){
	return document.getElementById(o);
}

function dokose(o,s,x){
	var i, ks=0, j=0, p=$('lblKosik'), z=$('mnuKosik'), y=(z.getElementsByTagName('a'))[0];
	
	if((i=jsc_find('pform',s))<0){
		jsc_setValue('pform',s+'//1',-1);
		o.className='add2cart2';
	}else if(typeof x=='undefined'){
		jsc_unsetValue('pform',i);
		o.className='add2cart';
	}
	jsc_init('pform');
	for(i=0;i<jsc_arr['pform'].length;i++){
		ks+=parseInt(jsc_arr['pform'][i].split(jsc_glue2)[1]);
		if(ks>0) j++;
	}
	
	
	if(ks<1){
		z.className='nkosikzero nbl';
		y.href='javascript:alert("Váš nákupní košík je prázdný.")';
		p.innerHTML='<span style="color:#555">Váš nákupní košík je prázdný.</span>';
	}else{
		z.className='nkosik nbl';
		y.href='/kosik/';
		p.innerHTML='Váš <a href="/kosik/"><strong>nákupní košík</strong></a> obsahuje '+j+' polož'+((j < 5) ? ((j < 2) ? 'ku' : 'ky')  : 'ek')+(ks != j ? ', '+ks+' ks' : '')+'.';
		
	}
	
	if(typeof x!='undefined') $('vlozeno').style.display='block';
	return false;
}

function getcity(o){
	var s=o.value, p=$('mesto');
	if(s){
		s=s.replace(/[^0-9]/g,'');
		if(s.length==5 && !p.value){
			p.readonly = true;
			p.style.backgroundColor = '#eee';
			setTimeout(function(){p.style.backgroundColor='';p.removeAttribute('readonly');},1000);
			get('?psc='+s,function(r){
				p.style.backgroundColor='';
				p.removeAttribute('readonly');
				if(!p.value) p.value=r;
			});
		}
	}
}

var _g=[];
function get(uri, fx){
	var A;
	if(window.XMLHttpRequest)
		A = new XMLHttpRequest();
	else if(window.ActiveXObject){
		try {
			A = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				A = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!A) return alert('Váš prohlížeč tuto funkci nepodporuje.');
	
	_g[uri]=0;
	
	A.open('GET', uri+'&'+Math.random(), true);
	A.onreadystatechange=function(){
		if(A.readyState==4 && _g[uri] == 0){
			_g[uri]=1;
			if(A.status==200){
				if(fx) fx(A.responseText);
				else if(A.responseText) alert(A.responseText);
			}
		}
	};
	A.send(null);
}


//----------------------

function ttipOn(i, o){
	var oo = $('ttip'+i);
	oo.style.top = (o.offsetTop+o.offsetHeight+10)+'px';
	oo.style.left = (o.offsetLeft-105)+'px';
	oo.style.visibility='visible';
}
function ttipOff(i){
	$('ttip'+i).style.visibility='hidden';
}


var nimg;
var ntimeout;
var nlast;
function nahled(oo,src,w,h){
	if(nlast) nlast.style.background='#fff';
	nlast = oo;
	
	var o=$('nahled');
	if(!nimg){
		nimg=document.createElement('img');
		o.appendChild(nimg);
	}
	var p=findPos(oo);
	o.style.left=(p[0]+28)+'px';
	oo.style.background='#eee';
	o.style.top=(p[1]+oo.offsetHeight)+'px';
	o.style.display='block';
	nimg.src=src;
	nimg.style.width=w+'px';
	nimg.style.height=h+'px';
}
function nahled_hide(){
	if(nlast) nlast.style.background='#fff';
	$('nahled').style.display='none';
}

//----------------------

function findPos(obj){
	var x = y = 0;
	do{
		x += obj.offsetLeft;
		y += obj.offsetTop;
	} while(obj = obj.offsetParent);
	return [x,y];
}

//----------------------

function changeqty(o,i,k){
	QT[i] = (o.value.search(/^-?[0-9]+$/) == 0) ? parseInt(o.value) : 0;
	$('p'+i).innerHTML = pformat(QT[i]*PR[i]);
	jsc_setValue("pform", k+jsc_glue2+QT[i], jsc_find('pform',k));
	recount();
}

function recount(){
	if(prazdnyKosik()) return;
	
	var total=0;
	for(i in QT) total+=QT[i]*PR[i];
	
	if(total > 0){
		//var sleva = Math.round(total*0.07);
		//$('sleva').innerHTML = '-'+pformat(sleva); 
		//total-=sleva;
		total+=D+P;
		$('do').innerHTML = D+P;
	}else{
		//$('sleva').innerHTML = '0';
		$('do').innerHTML = '0';
	}
	
	$('tp2').innerHTML = pformat(total);
}

function pformat(nStr){
	nStr+='';
	x=nStr.split('.');
	x1=x[0];
	x2=x.length>1?'.'+x[1]:'';
	var rgx = /(\d+)(\d{3})/;
	while(rgx.test(x1)) x1=x1.replace(rgx, '$1'+' '+'$2');
	return x1+x2;
}

function plclick(p){
	P=p;
	recount();
}

function dinfo(i,pr,str,dlbl)
{
	$('dolbl').innerHTML=dlbl;
	$('odinfo').innerHTML=str;
	D=pr;
	jsc_setValue('opts',i,0);
	
	var pl3=$('pl3');
	var pl1=$('pl1');
	
	if(i==2){
		// osobne => blokneme dobirku
		$('lpl3').className='';
		pl3.disabled=false;
		if(pl1.checked){
			window.setTimeout(function(){pl3.click()},0);
		}else{
			recount(); // nechceme 2x recount
		}
		$('lpl1').className='disabled';
		pl1.disabled=true;
	} else {
		// dobirkou => blokneme osobne
		$('lpl1').className='';
		pl1.disabled=false;
		if(pl3.checked){
			window.setTimeout(function(){pl1.click()},0);
		}else{
			recount();
		}
		$('lpl3').className='disabled';
		pl3.disabled=true;
	}
}

function ptrdel(o,j,k){
	nahled_hide();
	var delRow = o.parentNode.parentNode;
	var rIndex = delRow.sectionRowIndex;
	var rowArray = new Array(delRow);
	for (var i=0; i<rowArray.length; i++){
		var rIndex = rowArray[i].sectionRowIndex;
		rowArray[i].parentNode.deleteRow(rIndex);
	}
	// jen nastavime na 0, jinak by se zmenilo poradi a neslo by vymazat dalsi produkty => az po refresh
	jsc_unsetValue('pform', jsc_find('pform', k));
	QT[j]=0;
	recount();
	return false;
}

function prazdnyKosik(){
	var len = $('kosik').rows.length-1;
	if(len <= 2){
		$('main').innerHTML = '<div class="msg">Váš nákupní košík je teď prázdný.</div>';
		return true;
	}
	return false;
}

//----------------------

function addEvent(elm, evType, fn, useCapture){
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function removeEvent(elm, evType, fn, useCapture){
	if (elm.removeEventListener){
		elm.removeEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.detachEvent){
		var r = elm.detachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}


var bst=0; // (boolean) scrolltop
var ap=false; // active popup


function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}

function showMyPop(url, title, id, refresh){
	if(!id) id='';
	if(!refresh) refresh=0;
	
	var height = (id == 'z') ? 350 : 200;
	
	ap = id;
	var cr=0;
	var mask = $('popupMask');
	var inner = $('popupInner'+id);
	var wdb = window.document.body;
	
	
	if(!mask){
		mask = document.createElement('div');
		mask.id = "popupMask";
		addEvent(mask,'click',hideMyPop);
		wdb.appendChild(mask);
	}
	
	var fullHeight = getViewportHeight();
	popHeight = fullHeight;
	mask.style.height = popHeight + "px";
	
	if(!inner){
		cr=1;
		inner = document.createElement('div');
		inner.id = "popupInner"+id;
		inner.className = "popupInner";
		addEvent(inner,'click',hideMyPop);
		
		var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
		
		inner2 = document.createElement('div');
		inner2.className = "popupInner2";
		inner2.innerHTML = '<table cellpadding="0" cellspacing="0">'
			+'<tr><td class="popupControls">'
			+'<div></div><span id="popupTitle">'+title+'</span></td></tr>'
			+'<tr><td style="height:100%">'
			+'<iframe scrolling="'+(IE6 ? 'yes' : 'auto')+'" frameborder="0" name="popupFrame'+id+'" id="popupFrame'+id+'"></iframe></td></tr>'
			+'</table>';
		inner.appendChild(inner2);
		wdb.appendChild(inner);
		
		inner.style.height = height+'px';
	}else{
		$('popupTitle').innerHTML=title;
	}
	
	
	addEvent(top.window,'scroll',noscroll);
	
	mask.style.display='block';
	inner.style.display='block';
	
	if(refresh||cr){
		var IFrameDoc;
		var IFrameObj = $('popupFrame'+id);
		if(IFrameObj.contentDocument){
			IFrameDoc = IFrameObj.contentDocument; 
		} else if (IFrameObj.contentWindow){
			IFrameDoc = IFrameObj.contentWindow.document;
		} else if (IFrameObj.document){
			IFrameDoc = IFrameObj.document;
		}
		if(IFrameDoc.location.href != url) IFrameDoc.location.replace(url);
	}
	//---/navigace
	
	var windowh=(window.innerHeight ? window.innerHeight : wdb.offsetHeight);
	
	if(wdb.scrollTop != bst){
		mask.style.top=wdb.scrollTop+'px';
		bst=wdb.scrollTop;
	}
	inner.style.top = (wdb.scrollTop+fullHeight/2-height/2)+'px';
	
	return false;
}

function noscroll(){window.scrollTo(0,bst);}

function hideMyPop(){
	if(ap === false) return;
	id = ap;
	var body = window.document.body;
	removeEvent(top.window,'scroll',noscroll);
	
	$('popupMask').style.display='none';
	$('popupInner'+id).style.display='none';
	ap = false;
}


function picswap(o,i)
{
	var destbig=o.href;
	
	// velky do maleho
	var destsmall=$('bigpic').src;
	str=destsmall;
	str=str.substr(0,str.length-4) + '_t' + str.substr(str.length-4);
	$('smallpic'+i).src=str;
	o.href=destsmall;
	
	// maly do velkeho
	$('bigpic').src=destbig;
	return false;
}


var jsc_arr_str = [];
var jsc_arr = [];
var jsc_glue = ":";
var jsc_glue2 = "//";


function jsc_init(coo) {
	jsc_arr_str[coo] = jsc_readCookie(coo);
	jsc_arr[coo] = jsc_arr_str[coo] ? jsc_arr_str[coo].split(jsc_glue) : [];
}

function jsc_setValue(coo, val, pos) {
	if(jsc_arr[coo] == undefined) jsc_init(coo);
	if(pos < 0) pos=jsc_arr[coo].length;
	
	jsc_arr[coo][pos]=val;
	jsc_save(coo);
}

function jsc_switchValue(coo, o, pos) {
	if(o.checked) jsc_setValue(coo, 1, pos);
	else jsc_setValue(coo, 0, pos);
}

function jsc_save(coo) {
	var str='', d=new Date(), dt=d.getTime(), dt2=jsc_readCookie('modified');
	if(jsc_arr[coo]) str = jsc_arr[coo].join(jsc_glue);
	d.setTime(dt+(24*3600*365));
	var expires = " expires="+d.toGMTString();
	document.cookie = escape(coo)+"="+escape(str)+";"+expires+"; path=/";
	document.cookie = "touch="+(parseInt(dt2)+60)+";"+expires+"; path=/";
	jsc_arr_str[coo] = str;
	jsc_arr[coo] = jsc_arr_str[coo] ? jsc_arr_str[coo].split(jsc_glue) : [];
}

function unsetCookie(coo) {
	jsc_arr[coo] = [];
	jsc_save(coo);
}

function jsc_unsetValue(coo, pos) {
	if(pos == -1) return;
	if(jsc_arr[coo] == undefined) jsc_init(coo);
	jsc_arr[coo].splice(pos, 1);
	jsc_save(coo);
}

function jsc_readCookie(name) {
	var nameEQ = name + "=";
	var ca = unescape(document.cookie);
	ca = ca.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function jsc_find(coo, substr) {
	if(jsc_arr[coo] == undefined) jsc_init(coo);
	var str;
	for(i=0;i<jsc_arr[coo].length;i++) {
		str = jsc_arr[coo][i]+jsc_glue2;
		if(str.indexOf(substr+jsc_glue2) > -1) return i;
	}
	return -1;
}