
var mousex = 0;
var mousey = 0;
var storage_type = false;
var storage_el = 0;

if (isMoz)
{
  document.captureEvents(Event.MOUSEMOVE || Event.CLICK || Event.ONLOAD);
  if( window.sessionStorage )
  {
    storage_type = 'storage';
  }
}
else if (isIE)
{
  storage_el = document.getElementById('cloud');
  if( storage_el )
  {
    storage_type = 'behaviour';
    storage_el.style.behavior = "url('#default#userData')";
    storage_el.load('itext');
  }
}
else
{
    storage_type = false;
}

document.onmousemove=MousePos;
document.onclick=MouseClick;
window.onload=Sledz;

var fetching_feeds = 0;
var words;

function storeFeeds(fds)
{
    if( storage_type )
    {
        var feed = '';
        for(var f in fds)
        {
            feed = "{i:"+fds[f].i+",t:'"+escape(fds[f].t)+"' ,d:'"+escape(fds[f].d)+"'}";
            switch ( storage_type )
            {
                case 'storage':
                    window.sessionStorage.setItem('feed_'+fds[f].i, feed);
                    break;
                case 'behaviour':
                    if( storage_el )
                    {
                        storage_el.setAttribute('feed_'+fds[f].i, feed);
                    }
                    break;
            }
        }
        if( storage_el )
        {
            try
            {
                storage_el.save('itext');
            }
            catch( e )
            {
            }
        }
    }
}

function fetchFeedFromCache(id)
{
    if( storage_type )
    {
        var feed;;
        switch ( storage_type )
        {
            case 'storage':
                feed = window.sessionStorage.getItem('feed_'+id);
                if( feed )
                {
                    feed = feed.value;
                }
                break;
            case 'behaviour':
                if( storage_el )
                {
                    feed = storage_el.getAttribute('feed_'+id);
                }
                break;
        }
        if( feed )
        {
            eval('var f = '+feed+';');
            f.t = unescape(f.t);
            f.d = unescape(f.d);
            return f;
        }
        else
        {
            return null;
        }
    }
}

function storeWords(wds)
{
    if( storage_type )
    {
        var word = '';
        for(var w in wds)
        {
            word = "{i:"+wds[w].i+",w:'"+escape(wds[w].w)+"'}";
            switch ( storage_type )
            {
                case 'storage':
                    window.sessionStorage.setItem('word_'+wds[w].i, word);
                    break;
                case 'behaviour':
                    if( storage_el )
                    {
                        storage_el.setAttribute('word_'+wds[w].i, word);
                    }
                    break;
            }
        }
        if( storage_el )
        {
            try
            {
                storage_el.save('itext');
            }
            catch( e )
            {
            }
        }
    }
}

function fetchWordFromCache(id)
{
    if( storage_type )
    {
        var word;
        switch ( storage_type )
        {
            case 'storage':
                word = window.sessionStorage.getItem('word_'+id);
                if( word )
                {
                    word = word.value;
                }
                break;
            case 'behaviour':
                if( storage_el )
                {
                    word = storage_el.getAttribute('word_'+id);
                }
                break;
        }
        if(word)
        {
            eval('var w = '+word+';');
            w.w = unescape(w.w);
            return w;
        }
        else
        {
            return null;
        }
    }
}

function ShowWords()
{
	var el = document.getElementById('cloud');
    if ('undefined' != el)
    {
        var txt = '';
        if (words.length > 0)
        {
            for( w in words )
            {
                txt += ' <a class="wrd" style="font-size: '+getWordSize(words[w].i)+'em" href="javascript:hint(\''+words[w].w+'\')"  title="'+words[w].w+'" id="w{$words[w].i}">'+words[w].w+'</a> ';
            }
        }
        else
        {
            txt = 'Brak informacji';
        }
        el.innerHTML=txt;
    }    
}

function getWordId(word)
{
    for( var w in words)
    {
        if(words[w].w==word)
        {
            return words[w].i;
        }
    }
    return null;
}

function getWordSize(id)
{
    for(var w1 in wrd)
    {
        if(wrd[w1].i==id)
        {
            return wrd[w1].s;
        }
    }
    return 0.5;
}

function WordsFetchSuccess(o)
{
    if (o.responseText)
    {
    	eval('var www = '+o.responseText+';');
        storeWords(www);
        for(var w in www)
        {
            o.argument.words.push(www[w]);
        }
    }
    words = o.argument.words;

    ShowWords();
}

function WordsFetchFail(o)
{
}

function FeedsFetchSuccess(o)
{
    var feeds = new Array();
    if (o.responseText)
    {
	    eval('feeds = '+o.responseText+';');
        storeFeeds(feeds);
    }

    if (o.argument.feeds)
    {
        for(var f in o.argument.feeds )
        {
            feeds.push(o.argument.feeds[f]);
        }
    }
    fetching_feeds = 0;
    showHint(o.argument.word, feeds);
}

function FeedsFetchFail(o)
{
  var el = document.getElementById('wordhint');
  el.innerHTML = '<div class="hinthead">' + o.argument.word + '</div><div style="text-align:center">Błąd pobierania</div>';
  el.style.width = '150px';
  el.style.visibility = 'visible';
  fetching_feeds = 0;
}

function fetchWords()
{
    var list = new Array();
    var found_words = new Array();
    var word;

    for( w in wrd )
    {
        word = fetchWordFromCache(wrd[w].i);
        if (word == null || 'undefined'==word || !word.i)
        {
            list.push(wrd[w].i);
        }
        else
        {
            found_words.push(word);
        }
    }
    if (list.length>0)
    {
        var request = list.join(',');
        var callback =
        {
    	    success: WordsFetchSuccess,
    	    failure: WordsFetchFail,
            timeout: 10000,
            argument: {words:found_words}
        };
        YAHOO.util.Connect.asyncRequest('POST', '/wrd', callback, 'w='+request);
    }
    else
    {
        WordsFetchSuccess({argument: {words:found_words}});
    }
}

function fetchFeeds(fd, word)
{
    var found_feeds = new Array();
    var retrieve_feeds = new Array();
    var feed;
    var el = document.getElementById('wordhint');
    el.innerHTML = '<div class="hinthead">'
      + word
      + '</div><div style="text-align:center"><img src="/i/laduje.gif" alt="pobieranie" class="loadimg" /></div>';
    el.style.width = '150px';
    positionBox(el);
    el.style.visibility = 'visible';

    var fds = new String(fd).split(',');
    for(f in fds)
    {
        feed = fetchFeedFromCache(fds[f]);
        if (feed == null || 'undefined'==feed || !feed.i)
        {
            retrieve_feeds.push(fds[f]);
        }
        else
        {
            found_feeds.push(feed);
        }
    }
    if(retrieve_feeds.length>0)
    {
        var request = retrieve_feeds.join(',');
        var callback =
        {
    	    success: FeedsFetchSuccess,
    	    failure: FeedsFetchFail,
            timeout: 10000,
            argument: {word: word, feeds: found_feeds}
        };
        YAHOO.util.Connect.asyncRequest('POST', '/fds', callback, 'f='+request);
    }
    else
    {
        FeedsFetchSuccess(
            {
                argument: {word: word, feeds: found_feeds}
            }
        );
    }
}

function Sledz(e)
{
    fetchWords();
}

function MouseClick(e)
{
	var el = document.getElementById('wordhint');
	el.style.visibility = 'hidden';
}

function MousePos(e)
{
  e = e || window.event;
  if(isMoz)
  {
    mousex = e.pageX;
    mousey = e.pageY;
  }
  else if (isIE)
  {
    mousex = e.offsetX + document.body.offsetLeft;
    mousey = e.offsetY + document.body.offsetTop + document.documentElement.scrollTop;
  }
  else
  {
    mousex = e.pageX;
    mousey = e.pageY;
  }
}

function hint( word )
{
    if( fetching_feeds == 0)
    {
    	var en = [];
        var wid = getWordId( word );
        if( wid != null )
        {
        	for(var i in fd)
        	{
        		for(var j in fd[i].w)
        		{
        			if (fd[i].w[j] == wid)
        			{
        				en.push(fd[i].i);
        			}
        		}
        	}
        	if(en.length > 0)
        	{
                fetchFeeds(en, word);
        	}
        }
    }
}

function positionBox(el)
{
  if( isMoz || isIE )
  {
    el.style.left = Math.max(0, Math.min(mousex, document.body.clientWidth-el.clientWidth-50)) + 'px';
    el.style.top = Math.max(0, Math.min(mousey, document.body.clientHeight-el.clientHeight)) + 'px';
  }
  else
  {
    el.style.left = Math.max(0, Math.min(mousex, document.body.clientWidth - el.clientWidth)) + 'px';
    el.style.top = Math.max(0, Math.min(mousey, document.body.clientHeight + document.body.scrollTop - el.clientHeight - 50)) + 'px';
  }
}

function showHint(word, en)
{
  var el = document.getElementById('wordhint');
  el.style.width = 'auto';
  el.innerHTML = '<div class="hinthead">' + word + '</div><div id="hbody" class="hintbody"><div>';
  var hbody = document.getElementById('hbody');
  hbody.style.height = Math.min(en.length * 17, 400) + 'px';
  for(e in en)
  {
    hbody.innerHTML += '<a style="display: block" href="/fr?f='+en[e].i+'" target="_blank" title=\''+en[e].d+'\'>'+en[e].t+'</a> ';
  }
  window.status = en[e].d;
  positionBox(el);

  if(undefined != _uacct && _uacct != '')
  {
    _uff=0;
    urchinTracker();
  }
}
