/**
 * Funkcja obslugujaca zdarzenie krecenia rolka
 * @param {Event} event - obiekt zdarzenia
 */
function wheelRoll(event) {
    var delta = 0;
    if (!event) 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
	    //Okazuje sie ze pomiedzy O9.10 a O9.20 zmienila sie obsluga krecenia skrollem, w wersjach ponizej 9.10 wlacznie delta powinna byc zanegowana
        }
    }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;
    }	
    wheel(delta,'content_text');
}

function wheel(wheel_count,id){
	if(wsp == 0) return;
    var tmp;
    wheel_count = wheel_count*6;

	scroll_btn_top = parseInt($('scroller').style.top);
	if(parseInt($('scroller').style.top) <= maxheight && parseInt($('scroller').style.top) >= 0 ) {
		tmp = scroll_btn_top - wheel_count;
        //sprawdzam czy nie wyjechalem poza granice skrolla.
        if(tmp < 0){
            tmp = 0;
        }else if(tmp > maxheight){
            tmp = maxheight
        }
        $('scroller').style.top = tmp+'px';
        $('container').style.top = - tmp * wsp + 'px';
	}
}

function $(id) {
	if(document.getElementById) obj = document.getElementById(id);
	else obj = document.all[id];
	return obj;
}

function mouseup(e){
    DRAG = 0;
    checkScroll();
}

function mousemove(e){
	if(wsp == 0) return;
    var pos;
    if (!e) e = window.event;

    if(DRAG == 1) {
        if (e.pageX || e.pageY)	{
            mouseY = e.pageY;
        }else {
            mouseY = e.clientY;
        }
        pos = scrollerTop - (startPosition - mouseY);
        if(pos > maxheight){
            pos = maxheight;
        }else if(pos < 0){
            pos = 0;
        }else{
            $('scroller').style.top = pos+'px';
            $('container').style.top = - pos * wsp + 'px' ;
        }
        checkScroll();
    }
}

function scrollerMouseDown(e){
	if(wsp == 0) return;
    if (!e) e = window.event;
    DRAG = 1;
    if (e.pageX || e.pageY)	{
        mouseY = e.pageY;
    } else {
        mouseY = e.clientY;
    }
    startPosition = mouseY;
    scrollerTop = parseInt($('scroller').style.top);
    if(scrollerTop < 0) $('scroller').style.top = '0px';
}

function checkScroll() {
	if(wsp == 0) return;
    var btn_top_val = parseInt($('scroller').style.top);
    $('container').style.top = - btn_top_val * wsp + 'px' ;
}
/** 
 * Zwraca numer nacisnietego klawisza klawiatury lub 0 (zero) w przypadku braku akcji kalawiaturowej.
 * @param {Event} e - referencja do obiektu zdarzenia
 */
function getKey(e){
    var key;
    if (e.keyCode) key = e.keyCode;
    else if (e.which) key = e.which;
    if(key == undefined) key = 0; //poprawka na IE
    if(key == 1) key = 0; //jesli akcja bez udzialu klawaitury to robie zero
    return key;
}

/**
 * Metoda obslugje zdarzenie klawiaturowe. Jesli uzytkownik naisna strzalke do ory lub w dol to przesuwam warstwe z tekstem jesli jest taka mozliwosc.
 * @param {Event} e - referencja do obiektu zdarzenia JS
 */
function keydown(e){
	if(wsp == 0) return;
    var key, pos;
    if(!e) e = window.event;
    key = getKey(e);

    //obsluguje tylko pageup, pagedown, end, home i strzalki gora/dol
    if(key != 38 && key != 40 && key != 33 && key != 34 && key != 35 && key != 36){
        return;
    }

    pos = parseInt($('scroller').style.top);
    if(key == 38){
        //strzalka w gore
        pos -= 2;
    }else if(key == 40){
        //strzalka w dol.
        pos += 2;
    }else if(key == 36){
        //HOME.
        pos = 0;
    }else if(key == 35){
        //END.
        pos = maxheight;
    }else if(key == 33){
        //PAGEUP.
        pos -= 20;
    }else if(key == 34){
        //PAGEDOWN.
        pos += 20;
    }
    pos = parseInt(pos);
    if(pos < 0){
        pos = 0;
    }else if(pos > maxheight){
        pos = maxheight;
    }
    $('container').style.top = - pos * wsp+ 'px';
    $('scroller').style.top = pos + 'px';
}

/**
 * Zdarzenie aktywowane podczas nacisniecia i przytrzymania jakiegos klawisza
 * @param {Event} e - referencja o obiektu zdarzenia JS
 */
function keypress(e){
    //alias do keydown, zasada dzialania taka sama. 
    keydown(e);
}

function imgSrc(obj, src){
    obj.src = src;
}
/**
 * Skacze do podnaego adresu
 * @param {Object} adres
 */
function jump(adres){
    parent.location.href = adres;
}

function isValidEmail(){
    if(echeck(document.forms['newsletter'].elements['email'].value)){
        document.forms['newsletter'].submit();    
    }
}


function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
    var error = "To nie jest poprawny adres email";
	if (str.indexOf(at)==-1){
	   alert(error);
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert(error);
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert(error);
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert(error);
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert(error);
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert(error);
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert(error);
	    return false
	 }
    return true					
}

