// Ajax functions. (C) Rob Heslinga

ajax_url = base + 'code/ajax.php';

function doAjaxRequest (succes_callback, params)
    {
    new Ajax.Request
        (
        ajax_url,
            {
            method: 'post',
            parameters: params,
            onSuccess: succes_callback
            }
        );
    }


function updateBlock (transport, id)
    {
    new Effect.Opacity(id,
                          {
                          from: 1.0,
                          to: 0.0,
                          duration: 0.3,
                          queue: 'front',
                          asynchronous:true,
                          afterFinish : function(){$(id).innerHTML = transport.responseText;}
                          }
                      );
    new Effect.Opacity(id, { from: 0.0, to: 1.0, asynchronous:true, duration: 0.3, queue: 'end'} );
    }

function loadProgram (id, nr_of_results, team_id, fade)
{
    params = '?action=loadProgram&team_id=' + team_id + '&nr_of_results=' + nr_of_results;
    if (fade)
        {
         doAjaxRequest (function(transport){updateBlock(transport, id);}, params);
        }
    else
        {
        doAjaxRequest (function(transport){$(id).innerHTML = transport.responseText;}, params);
        }

}

function loadProgramDetail (id, match_id, team_id)
{
    params = '?action=loadProgramDetail&match_id=' + match_id + '&team_id=' + team_id;
    doAjaxRequest (function(transport){updateBlock(transport, id);}, params);
}

function loadMatchResults (id, nr_of_results, team_id, fade)
{
        params = '?action=loadMatchResults&team_id=' + team_id + '&nr_of_results=' + nr_of_results;
        if (fade)
            {
            doAjaxRequest (function(transport){updateBlock(transport, id);}, params);
            }
        else
            {
            doAjaxRequest (function(transport){$(id).innerHTML = transport.responseText;}, params);
            }
}

function loadMatchResultDetail (id, match_id, team_id)
{
    params = '?action=loadMatchResultDetail&match_id=' + match_id + '&team_id=' + team_id;
    doAjaxRequest (function(transport){updateBlock(transport, id);}, params);
}
// function loadProgramm (team_id)
//     {
//     xitem = $('program');
//     params = '?action=loadProgram&team_id=' + team_id;
//     new Ajax.Updater
//         (
//         xitem,
//         base + 'code/ajax.php',
//             {
//             parameters: params,
//             method: 'post'
//             }
//         );
//     }


// function loadMatchResults (team_id)
//     {
//     xitem = $('matchresults');
//     params = '?action=loadMatchResults&team_id=' + team_id;
//     new Ajax.Updater
//         (
//         xitem,
//         base + 'code/ajax.php',
//             {
//             parameters: params,
//             method: 'post'
//             }
//         );
//     }

function deleteReaction (reaction_id)
    {
    if (!confirm('Reactie verwijderen?'))
        {
        return;
        }

    params = '?action=deleteReaction&reaction_id=' + reaction_id;
    div = $('reaction_' + reaction_id);
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                      var notice = $('reaction_' + reaction_id);
                      if (notice != undefined)
                            {
                            notice.hide();
                            }
                      }
            }
        );
    }

function deletePosting (reaction_id)
    {
    if (!confirm('Bericht verwijderen?'))
        {
        return;
        }

    params = '?action=deletePosting&posting_id=' + reaction_id;
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                      document.location = base;
                      }
            }
        );
    }


function loadPostingList (id_to_change, page)
    {
    xitem = $(id_to_change);
    params = '?action=loadPostingList&page=' + page;
    new Ajax.Updater
        (
        xitem,
        base + 'code/ajax.php',
            {
            parameters: params,
            method: 'post'
            }
        );
    }

function loadReactionPage (id_to_change, cat, cat_id, page)
    {
    xitem = $(id_to_change);
    params = '?action=loadReactionPage&category='+cat+'&category_id=' + cat_id +'&page=' + page;
    new Ajax.Updater
        (
        xitem,
        base + 'code/ajax.php',
            {
            parameters: params,
            method: 'post'
            }
        );
    }


function loadTeamRanking (id_to_change, team_id)
    {
    xitem = $(id_to_change);
    params = '?action=updateRanking&team_id=' + team_id;
    new Ajax.Updater
        (
        xitem,
        base + 'code/ajax.php',
            {
            parameters: params,
            method: 'post'
            }
        );
    }

function reloadRanking (team_id)
    {
    params = '?action=reloadRanking&team_id=' + team_id;
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                        alert('De stand is bijgewerkt!');
                      }
            }
        );
    }

function swapPhoto (id_to_change, photo_id)
    {
    xitem = $(id_to_change);
    params = '?action=updatePhotoPage&photo_id=' + photo_id
    new Ajax.Updater
        (
        xitem,
        base + 'code/ajax.php',
            {
            parameters: params,
            method: 'post'
            }
        );
    }

function setActivePosting (posting_id)
    {
    params = '?action=setActivePosting&posting_id=' + posting_id;
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                        alert('Het bericht staat in de lijst met mededelingen');
                      }
            }
        );
    }

function unsetActivePosting (posting_id)
    {
    params = '?action=unsetActivePosting&posting_id=' + posting_id;
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                        alert('Het bericht is uit de lijst met mededelingen gehaald');
                      }
            }
        );
    }

function addPostingBookmark (posting_id)
    {
    params = '?action=addPostingBookmark&posting_id=' + posting_id;
    new Ajax.Request
        (
        base + 'code/ajax.php',
            {
            method: 'post',
            parameters: params,
            onComplete: function (transport)
                      {
                        alert('Bookmark toegevoegd!');
                      }
            }
        );
    }

Ajax.Responders.register
    (
        {
        onCreate: function()
            {
            if($('content') && Ajax.activeRequestCount>0)
                {
                $('content').style.cursor = 'wait';
                }
            },
        onComplete: function()
            {
            if($('content') && Ajax.activeRequestCount==0)
                {
                $('content').style.cursor = 'auto';
                }
            },
        onError: function()
            {
            if($('content') && Ajax.activeRequestCount==0)
                {
                $('content').style.cursor = 'auto';
                }
            }
        }
    );

function correctPNG() //handle PNG transparency in IE 6
{
 for(var i=0; i < document.images.length; i++)
 {
  var img = document.images[i];
  var imgName = img.src.toUpperCase();

  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
  {
   var imgID = (img.id) ? "id='" + img.id + "' " : "";
   var imgClass = (img.className) ? "class='" + img.className + "' " : "";
   var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
   var imgStyle = "display:inline-block;" + img.style.cssText;

   if (img.align == "left") imgStyle = "float:left;" + imgStyle;

   if (img.align == "right") imgStyle = "float:right;" + imgStyle;

   if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;

   var strNewHTML = "<span " + imgID + imgClass + imgTitle
   + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
   + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
   + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
   img.outerHTML = strNewHTML
   i = i-1
  }
 }
}

function sfhover()
{
  var sfEls = document.getElementById("nav").getElementsByTagName("LI");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
    }
  }
}

window.onload = function()
{
  var supported = /MSIE ((5\.5)|(6))/.test(navigator.userAgent) && navigator.platform == "Win32";

  if( supported )
  {
    correctPNG();
    sfhover();
  }
}