Event.observe(window, 'load', function() {
        
     if($('bookmarks-link'))
     {
        
     $('bookmarks-link').observe('mouseover', function(event){
        $('bookmarks').show();
        $('bookmarks-link').style.borderBottom = '0';
     });
     $('bookmarks-link').observe('mouseout', function(event){
        $('bookmarks').hide();
        $('bookmarks-link').style.borderBottom = '1px solid #c1c1c1';
     });
     $('bookmarks').observe('mouseover', function(event){
        $('bookmarks').show();
        $('bookmarks-link').style.borderBottom = '0';
     });
     $('bookmarks').observe('mouseout', function(event){
        $('bookmarks').hide();
        $('bookmarks-link').style.borderBottom = '1px solid #c1c1c1';
     });
     }
     
     /**
      * Ajax help balloon tooltip
      */
     Event.observe(document.body, 'click', function(event) {
          var e = Event.findElement(event, 'h2');
          
          if(e == null || e.id == 'overDiv-caption' || !(e.className == 'subLongGreen' || e.className == 'subSmallGreen'))
            return ;
          
          var oLeft;
          switch(e.className)
          {
              case 'subSmallGreen' :
                oLeft = 210;
                break;
              case 'subLongGreen' :
                oLeft = 485;
                break;
          }
          
          new Ajax.Request(helpUpdateUri,
          {
              method : 'post',
              parameters: {
                  content : e.innerHTML.unescapeHTML()
              },
              onLoading : function()
              {
                  $('overDiv-content').update(helpLoadingText);
                  Element.clonePosition($('overDiv'), e, {offsetLeft: oLeft, offsetTop: -45, setWidth: false, setHeight: false});
                  $('overDiv').show();
              },
              onSuccess : function (req)
              {
                  
                  $('overDiv-content').update(req.responseText)
              }
          });
     });
     
});

function submitTellafriend(type)
{
    new Ajax.Request ('/'+lang+'/index/friend',
            {
                 method : 'post',
			     parameters : {
				    'aid' : $('aid').value,
				    'type' : type,
				    'yname' : $('yname').value,
				    'fname' : $('fname').value,
				    'email' : $('email').value
			     },
			     onSuccess : function (req) 
			     {
    				$('tellafriend-form').update(req.responseText);
			     },
			     onLoading : function () {
			        $('tellafriend-form').update('<img src="/gfx/loading.gif" alt="loading..." style="margin: 4px;" />');
			     }
            }
	);
	
	return false;
}

var statisticsIsCustom = true;

function generateCustomStatistics()
{
	var d = Date.parse($('f_date').value.replace(/-/g, ' '));
	var minDate = Date.parse('1991 01 01');
	
	if(d < minDate)
	{
		alert(_minimumDateStr);
		return false;
	}
	
    $('statistics-form').request(
        {
            onLoading : function () {
			    $('statistics').update(
			    '<div style="position: absolute;left: 200px;top: 590px;width: 150px;background-color: #fff;">' +
			    '<h2 class="subExtraSmallGreen">Generating statistics...</h2>' +
			    '<div class="nBlock" style="border: 1px solid #c1c1c1;margin: 0;padding: 5px;width: 138px;text-align: center">' +
			    '<img src="/gfx/loading3.gif" alt="loading..." />' +
			    '</div>' +
			    '</div>'
			    +
			    $('statistics').innerHTML
			    );
			},
            onSuccess: function(req) { 
                $('statistics').update(req.responseText);
                new Effect.Pulsate('statistics', {pulses: 1});
                statisticsIsCustom = true;
            }
        }
    );
    
    return false;
}

function getMatchesFromStatistics(result, part)
{
    var x = $('statistics');
    
    if(statisticsIsCustom)
    {
	    $('statistics-form').action = lang + 'statistics/generateMatches';
	    $('statistics-form').request(
	        {
				parameters : {
					    'part' : part,
					    'result' : result
				},
	            onLoading : function () {
				    new Insertion.Top(x, '<img id="loading" src="/gfx/loading.gif" alt="loading..." style="margin: 4px;" />');
				},
	            onSuccess: function(req) { 
	                $('loading').remove();
	                x.insert({before: req.responseText});
	                $('statistics-form').action = lang + 'statistics/generate';
	            }
	        }
	    );
    }
    else
    {
    	new Ajax.Request(lang + 'statistics/generateMatches',
	        {
				parameters : {
						'player_name' : $('form_player').value,
					    'part' : part,
					    'result' : result
				},
	            onLoading : function () {
				    new Insertion.Top(x, '<img id="loading" src="/gfx/loading.gif" alt="loading..." style="margin: 4px;" />');
				},
	            onSuccess: function(req) { 
	                new Insertion.Top(x, req.responseText);
	                $('loading').remove();
	                //x.insert(req.responseText, {position: 'Top'});
	                $('statistics-form').action = lang + 'statistics/generate';
	            }
	        }
	    );
    }
}

function loadAudio(id)
{
	new Ajax.Request (lang + 'file/load/id/' + id,
            {
                 method : 'post',
			     parameters : {
			     },
			     evalScripts : true,
			     onSuccess : function (req) 
			     {
    				$('fileBox_' + id).update('<td colspan="3" style="padding: 10px">' + req.responseText + '</td>');
    				$('fileBox_' + id).show();
			     },
			     onLoading : function () {
			     }
            }
	);
}

function showTournamentPlayerMatches(season, player)
{
    new Ajax.Request ('/en/tournament/index',
            {
                 method : 'post',
			     parameters : {
				    'season' : season,
				    'player' : player
			     },
			     onSuccess : function (req) 
			     {
    				$('matches-' + player).update(req.responseText);
			     },
			     onLoading : function () {
			        $('matches-' + player).show();
			        $('matches-' + player).update('<img src="/gfx/loading.gif" alt="loading..." style="margin: 4px;" />');
			     }
            }
	);
	
	return false;
}

function submitMatchesByPeriodForm()
{
	$('matches-by-period').request(
       {
          onLoading : function () {
	    	$('player-matches').update('<img id="loading" src="/gfx/loading.gif" alt="loading..." style="margin: 10px 4px;float:right" />');
		  },
          onSuccess: function(req) { 
              $('player-matches').update(req.responseText);
          }
       }
   );
}

function fetchTournaments(year)
{
	new Ajax.Request ('/en/block/tournament/ajax',
            {
                 method : 'post',
			     parameters : {
				    'year' : year
			     },
			     onSuccess : function (req) 
			     {
    				$('tournaments').update(req.responseText);
			     },
			     onLoading : function () {
			        $('tournament-list').update('<img src="/gfx/loading.gif" alt="loading..." style="margin: 4px;" />');
			     }
            }
	);
}

function switchStatisticsTab(tab, key) 
{
    $$('li.tab').invoke('removeClassName', 'selected');
    tab.addClassName('selected');
    
    $$('div.statistics_block').each(function(element) {
        if(element.id == (key + '_asc') || element.id == (key + '_desc'))
            element.show();
        else
            element.hide();
    });
}

/**
 * Changes the default link at the bottom of articles
 * 
 * @return string value within the link textarea
 */
function changeArticleLink(){
	
	var lang = Form.getInputs('article-form','radio','article_language').find(function(radio) { 
					return radio.checked; }).value;
	
	var text = articleLinks[lang]["text"];
	var pid = articleLinks[lang]["pid"];
	var bid = articleLinks[lang]["bid"];
    
	var link = '&lt;a href="http://adserving.unibet.com/redirect.aspx?pid='+pid+'&bid='+bid+'" target="_blank" title="'+text+'" &gt;'+text+'&lt;/a&gt;'; 
	
    $('link').update(link);   
}
