function in_array(myValue,myArray)
{

    for(var i=0; i<myArray.length; i++){
        if(myArray[i] == myValue) return true;
    }
    return false;
}

function $(id)
{
    return document.getElementById(id);
}

function handyEffects()
{
    _effect = new Object();
    _effect.initFade = initFade;
    _effect.doFade = doFade;
    _effect.doFadePieces=doFadePieces;
    _effect.pieces = Array();
    _effect.timeout = Array();

    this._effect=_effect;

    function doFade(myObj, newValue)
    {
        document.getElementById(myObj).style.opacity=newValue;
        document.getElementById(myObj).style.filter = 'alpha(opacity=' + newValue*100 + ')';
        document.getElementById(myObj).style.visibility = 'visible';
    }

    function doFadePieces(myObj, letter, speed, maxPieces, direction)
    {
        if(_effect.pieces[myObj+'_'+letter]) return 0;
        _effect.pieces[myObj+'_'+letter] = 1;
        if(letter<maxPieces)
        for (var j=1;j<speed+1;j++)
        {
            if(direction)
            {
                var newValue = 0+(j/speed);
                setTimeout("if("+newValue+">=0.1) _effect.doFadePieces('"+myObj+"',"+(letter+1)+","+speed+","+maxPieces+","+direction+"); _effect.doFade('handy-"+myObj+"-"+letter+"',"+newValue+")",j*100);
            }
            else
            {
                var newValue = 1-(j/speed);

                setTimeout("if("+newValue+"<=0.9) _effect.doFadePieces('"+myObj+"',"+(letter+1)+","+speed+","+maxPieces+","+direction+"); _effect.doFade('handy-"+myObj+"-"+letter+"',"+newValue+")",j*100);
            }
        }
    }

    function initFade(myObj, mode, speed, direction, endValue)
    {

        clearTimeout(_effect.timeout[myObj]);
        if(direction==null) {
            direction=1;
            startvalue = '0';
        }
        else
        {
            startvalue = (!direction) * 100;
        }

        speed = 100/speed;

        if(mode == 'none') {
            document.getElementById(myObj).style.visibility='visible';
        }
        else if(mode == 'all' || mode==null || mode == '')
        {
//            if(_effect.pieces[myObj+'_all']) return 0;
//            _effect.pieces[myObj+'_all'] = 1;
            document.getElementById(myObj).style.opacity=startvalue;
            document.getElementById(myObj).style.visibility='visible';

                for (var i=1;i<(speed)+2;i++)
                {
                    if(direction)
                    {
                        if( endValue>(0+i/speed) )
                        _effect.timeout[myObj] = setTimeout("_effect.doFade('"+myObj+"',"+(0+i/speed)+")",200*i);
                    }
                    else
                    {
                        if( endValue<(0+i/speed) )
                        _effect.timeout[myObj] = setTimeout("_effect.doFade('"+myObj+"',"+(1-i/speed)+")",200*i);
                    }
                }
        }
        else
        {
// #########

var allowedTags = ['img', 'hr', 'table'];
var string = $(myObj).innerHTML;
var pieceCounter = 0;
var output = '';
var myChar='';

for(var i=0; i<string.length; i++)
{
    myChar = string.charAt(i);
    if(myChar == "<")
    {
        var bTagName = 0;
        var tagName = '';
        i++;
        for(var htmlCode=''; string.charAt(i)!='>'; i++)
        {
            myChar = string.charAt(i);
            if((myChar == ' ' || myChar == '/' && string.charAt(i+1)=='>') && tagName) bTagName=1; // nawy troche juz jest i znak == spacja
            else if(!bTagName) tagName += myChar;
            htmlCode += myChar;
        }

        if(in_array(tagName.toLowerCase(),allowedTags))
        {
            if(htmlCode.charAt(htmlCode.length-1) == '/')
            {
                htmlCode=htmlCode.substring(0,htmlCode.length-1);
                output += '<'+htmlCode +' style="opacity:'+startvalue+';" id="handy-'+myObj+'-'+mode+'-'+pieceCounter+'" />';
            }
            else output += '<' + htmlCode +' style="opacity:'+startvalue+';" id="handy-'+myObj+'-'+mode+'-'+pieceCounter+'">';
            pieceCounter++;
        }
        else output += '<' + htmlCode + '>';
    }
    else
    {
        for(var text = '';i<string.length && string.charAt(i)!='<';i++)
        {
            text += string.charAt(i);
        }

        if(mode == 'letter')
        {
            for(j=0;j<text.length;j++)
            {
                var myChar = text.charAt(j);
                if(myChar == '&')
                {
                    j++;
                    while(text.charAt(j)!=';')
                    {
                        myChar += text.charAt(j);
                        j++;
                    }
                    myChar += ';';
                }
                output += "<span style=\"opacity:"+startvalue+"\" id=\"handy-"+myObj+"-letter-"+pieceCounter+"\">"+myChar+"</span>";
                pieceCounter++;
            }
        }
        else if(mode == 'word')
        {
            var words=text.split(" ");
            var length = words.length;
            for(j=0;j<length;j++)
            {
               output += "<span style=\"opacity:"+startvalue+"\" id=\"handy-"+myObj+"-word-"+pieceCounter+"\">"+words[j]+" </span>";
               pieceCounter++;
            }
        }
        i--;
    }
}

// #############
            $(myObj).style.visibility='visible';
            $(myObj).style.opacity='1';
            $(myObj).innerHTML = output;
            _effect.doFadePieces(myObj+'-'+mode, 0, speed, pieceCounter, direction);
        }

    }
}


function handyAjax()
{
    myHandy = new Object();
    myHandy.RequestCounter=0;
    myHandy.sendMethod = 'GET';
    myHandy.http_request = null;
    myHandy._onload = '';
    this._onload='';

    this.doAnimation = function(options)
    {

        if(options.text)
        {
            myHandy._onload += "$('"+options.target+"').innerHTML = '"+options.text.replace(/\$(\w+)/g,"'+myHandy.myData[\"$1\"]+'")+"';";
        }

        myHandy._onload += "myEffects = new handyEffects();";
        speed =(options.speed) ? options.speed : 10;
        mode = (options.mode) ? options.mode : 'letter';
        direction = (options.direction!=null) ? options.direction : 1;
        if(options.effect=='fade')
        {
            myHandy._onload +="myEffects._effect.initFade('"+options.target+"', '"+mode+"', "+speed+","+direction+");";
        }
    }

    this.getHttpRequest = function()
    {
        myHandy.http_request = null;
        if (window.XMLHttpRequest)
        { // Opera, Mozilla, Safari,...
            myHandy.http_request = new XMLHttpRequest();
            if (myHandy.http_request.overrideMimeType)
            {
                 myHandy.http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject)
            { // IE
                try
                {
                    myHandy.http_request = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e)
                {
                    try
                    {
                        myHandy.http_request = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {}
                }
            }
        if (!myHandy.http_request) return null;
    }

    this.getHttpRequest()

    this.makeRequest = function(url,sendMethod,sendArray,onload)
    {
        var sendData='';
        if(sendMethod) myHandy.sendMethod = sendMethod.toUpperCase();

        myHandy._onload += onload;

        myHandy.RequestCounter++;
        myHandy.http_request.onreadystatechange = myHandy.alertContents;
        // wszystkie encje &amp; zamieniamy na & 
        url = url.replace(/%/g,"%25");
        url = url.replace(/&amp;/g,"&");
        if(url.indexOf('?')==-1) url+='?'; else url+='&';

        myHandy.http_request.open(myHandy.sendMethod, url+'c='+myHandy.RequestCounter, true);
        if(sendArray)
        {
            var k=0;
            for(var i in sendArray)
            {
                sendData += ((k) ? '&' : '')+i+'='+encodeURIComponent(sendArray[i]);
                k=1;
            }
            myHandy.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        }
        myHandy.http_request.send(sendData);
    }

    myHandy.alertContents = function()
    {
        if(myHandy.http_request.readyState == 4)
        {
            if (myHandy.http_request.status == 200)
            {
                eval(myHandy.http_request.responseText);

                if(myHandy._onload) eval(myHandy._onload);
            }
            else
            {
                alert('Connection error');
            }
        }
        else
        {
            // ciągle nie gotowe
        }
    }
}