


function fixImgs(whichId, maxW, maxH) {
if(document.getElementById(whichId)){
var pix=document.getElementById(whichId).getElementsByTagName('img');
  for (i=0; i<pix.length; i++){
    w=pix[i].width;
    h=pix[i].height;
    if (w > maxW) {
      f=1-((w - maxW) / w);
      pix[i].width=w * f;
      pix[i].height=h * f;
    }
    h=pix[i].height;
    w=pix[i].width;
    if (h > maxH){
      f=1-((h - maxH) / h);
      pix[i].width=w * f;
      pix[i].height=h * f;
    }
    if (pix[i].height<maxH && pix[i].className=='prodthumb') {
        mrg=(maxH-pix[i].height)/2;
        pix[i].style.marginTop=mrg+'px';
    }
  }}
}

var horiz=0;
var off=0;
var offLeft=0;
var hmax=0;
var win=0;
var bsize=0;

function menu_scrollleft()
{
	horiz=-2;
	horizontal();
}
function menu_scrollright()
{
	horiz=2;
	horizontal();
}
function hmenu_stop()
{
	horiz=0;
}
function horizontal()
{
	pos=_htresc.scrollLeft;
	horiz_scrollBy(horiz);
    if (horiz != 0){
    	if(_htresc.scrollLeft<=0 || _htresc.scrollLeft>=hmax-win)
    		horiz=0;
    	setTimeout("horizontal()", 10);
    }
}

function horiz_scrollBy(y)
{
	_htresc.scrollLeft+=y;
    if (_htresc.scrollLeft > hmax-win)
      _htresc.scrollLeft = hmax-win;
    if (_htresc.scrollLeft < 0)
      _htresc.scrollLeft = 0;
	npos=_htresc.scrollLeft;
	spos=bsize*npos/(hmax-win);
	_hscroller.style.marginLeft=spos+"px";	
}

function horiz_scrollTo(y)
{
	h=_hbar.clientWidth-_hscroller.clientWidth;
	_htresc.scrollLeft=(hmax-win)*y/h;
	npos=_htresc.scrollLeft;
	spos=bsize*npos/(hmax-win);
	spos=Math.floor(spos);
	_hscroller.style.marginLeft=spos+"px";	
}
function chalcul(elt)
{
	s=elt.offsetLeft;
	e=elt;
	while(e.offsetParent && e.tagName != 'BODY'){
//		alert(e.tagName + ' ' +e.id + ' ' + e.offsetLeft);
		s+=e.offsetLeft;
		e=e.offsetParent;
	}
	return s;
}

function horiz_start_drag(e)
{
    if (typeof e == 'undefined') e = window.event;
	if (typeof e.pageX == "undefined"){
//		off=_hscroller.offsetLeft+_hscroller.clientWidth/2 - Number(e.clientY);
        off = 0;//-_hscroller.clientWidth;
		offLeft=chalcul(_hbar);
	}
	else
		offLeft=_hbar.offsetLeft;
	document.onmousemove = horiz_drag;
	document.onmouseup = horiz_end_drag;
	return false;
}

function horiz_drag(e)
{
	e=fhixE(e);
	y=e.pageX;
	if (y<offLeft+_hscroller.clientWidth/2) y=offLeft+_hscroller.clientWidth/2;
	if (y>offLeft+_hbar.offsetWidth-_hscroller.clientWidth/2) y=offLeft+_hbar.offsetWidth-_hscroller.clientWidth/2;
	y-=offLeft+_hscroller.clientWidth/2;
	horiz_scrollTo(y);
	return false;
}

function horiz_end_drag(e)
{
	document.onmousemove = null;
	document.onmouseup = null;
}

function fhixE(e)
{
    if (typeof e == 'undefined') e = window.event;
    if (typeof e.pageX == 'undefined')
		e.pageX = e.clientX+off;
    return e;
}

function _honresize(){
    hmax=__htresc.clientWidth;
    win=_htresc.clientWidth;
    bsize=_hbar.clientWidth-_hscroller.clientWidth;
	horiz_scrollBy(0);
    setTimeout ("__honresize()", 100);
}

function __honresize(){
    hmax=__htresc.clientWidth;
    win=_htresc.clientWidth;
    bsize=_hbar.clientWidth-_hscroller.clientWidth;
	horiz_scrollBy(0);
}

function _honload()
{
        fixImgs('trescprod', 160, 160);
  
  
        _htresc=document.getElementById('trescprod');
		__htresc=document.getElementById('trescprod__');
		_hbar=document.getElementById('hscrollbar');
		_hscroller=document.getElementById('hscroller');


	if (_hscroller != null){
		_hscroller.onmousedown = horiz_start_drag;
		_htresc.scrollLeft=0;
        hmax=__htresc.clientWidth;
        win=_htresc.clientWidth;
        if (hmax>win){
          document.getElementById("hscrollbar").style.display="block";
        }

        bsize=_hbar.clientWidth-_hscroller.clientWidth;
	}

//	if (window.addEventListener)
			/** DOMMouseScroll is for mozilla. */
//			window.addEventListener('DOMMouseScroll', whheel, false);
	/** IE/Opera. */
//	window.onmousewheel = document.onmousewheel = whheel;
}


/*	kolko myszki;-)
 *
 *
 */
function ishOverTresc(elt)
{
	e=elt;
	while (e.id != 'trescprod' && e.parentNode && e.tagName != 'BODY'){
		e=e.parentNode;
	}
	if (e.id == 'trescprod')
		return true;

	return false;
}

/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta, e) {
		if (!ishOverTresc(e.target))
			return true;
        if (delta < 0)
			horiz_scrollBy(30);
        else
			horiz_scrollBy(-30);
}

/** Event handler for mouse wheel event.
 */
function whheel(event){
        var delta = 0;
		var ret=true;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                ret=handle(delta, event);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault && !ret)
                event.preventDefault();
	event.returnValue = ret;
}


