/*
 * 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 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;
}
