//***************************************
//   G e n e r a l   F u n c t i o n s  
//***************************************

// Get XML HTTP Request Object
function GetAjax() {
  var xmlHttp;
  try { xmlHttp=new XMLHttpRequest(); }   // Firefox, Opera 8.0+, Safari
  catch (e) {   // Internet Explorer
    try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) {
      try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return(xmlHttp);
}

// Find Window Width
function WindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth + 15;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth + 15;
  }
  return(myWidth);
}

// Hide Div
function hidediv(div_id) { 
  document.getElementById(div_id).style.visibility = 'hidden'; 
}

// Show Div
function showdiv(div_id) { 
  var leftPos = WindowWidth();
  leftPos = leftPos/2;
  leftPos = leftPos-360;
  document.getElementById(div_id).style.left = String(leftPos) + "px";
  document.getElementById(div_id).style.visibility = 'visible'; 
} 

// Find and object by ID
function findObjID(elementId) {
  var element;
  if (document.all)
    return(document.all[elementId]);
  else if (document.getElementById)
    return(document.getElementById(elementId));
}

// Get page name via Javascript
function GetPageName()
{
  var sPath = window.location.pathname;
  var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  return(sPage);
}

// Get a parameter via Javascript
function GetParam(name)
{
  var start=location.search.indexOf("?"+name+"=");
  if (start<0) start=location.search.indexOf("&"+name+"=");
  if (start<0) return '';
  start += name.length+2;
  var end=location.search.indexOf("&",start)-1;
  if (end<0) end=location.search.length;
  var result='';
  for(var i=start;i<=end;i++) {
    var c=location.search.charAt(i);
    result=result+(c=='+'?' ':c);
  }
  return unescape(result);
}

//************************************************
//   F l e a   M a r k e t    F u n c t i o n s  
//************************************************

// AJAX Load News Story Function
function loadFleaMarket() {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID("FleaMarketContainer");
  CompURL = "fmp.php";
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

//*********************************
//   N e w s   F u n c t i o n s  
//*********************************

// AJAX Fetch News Summary Function
function fetchNewsHeaders(yyyy, numArticles, AJAXtarget) {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID(AJAXtarget);
  CompURL = "news/ni.php?y=" + escape(yyyy) + "&n=" + escape(numArticles);

  oTitleElement = findObjID("NSTitle");
  if(oTitleElement) {
  oTitleElement.innerHTML = "News Stories for 20" + yyyy;
  }
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

// AJAX Load News Story Function
function loadStory(yyyy, nnnn) {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID("newsStoryContainer");
  CompURL = "news/ns.php?y=" + escape(yyyy) + "&n=" + escape(nnnn);
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

// AJAX Fetch Top News Story Number Function
function fetchTopStory(yyyy) {
  var xmlHttp;
  xmlHttp = GetAjax();
  CompURL = "news/ts.php?y=" + escape(yyyy);
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  loadStory(yyyy,xmlHttp.responseText);
}

// Fetch Story Master Routine
function fetchStory(yyyy,nnnn)
{
  if(GetPageName()=="news.html") {
    loadStory(yyyy,nnnn);
  } else {
    location.href="news.html?y="+yyyy+"&n="+nnnn;
  }
}

// Load News Page Master Routine
function loadNewsPage()
{
  var y=GetParam('y');
  var n=GetParam('n');
  
  if(n=='') {
    var dteNow = new Date();
    var yy = String(dteNow.getFullYear());
    y = yy.replace("20","");
    fetchNewsHeaders(y,'100','NSList');
    fetchTopStory(y);
  } else {
    fetchNewsHeaders(y,'100','NSList');
    fetchStory(y,n);
  }
}

//***********************************************
//   R a c i n g   P a g e   F u n c t i o n s  
//***********************************************

// Master Racing Load Function
function loadRacingPage()
{
  loadPilotTable();
  loadStandingsTable();
}

// AJAX Load Pilot Table Function
function loadPilotTable() {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID("Pilot_Info");
  CompURL = "pilots.php";
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

// AJAX Load Standings Table Function
function loadStandingsTable() {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID("Championship_Standings");
  CompURL = "standings.php";
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

//*********************************************
//   M u l t i M e d i a   F u n c t i o n s  
//*********************************************

// AJAX Fetch Albums for a given category
function listAlbums(mType, sTitle) {
  var xmlHttp;
  xmlHttp = GetAjax();
  oWriteElement = findObjID('albumList');
  CompURL = "multimedia/en.php?m=" + escape(mType);
  
  oTitleElement = findObjID('albumTitle');
  if(oTitleElement) {
    oTitleElement.innerHTML = sTitle;
  }
  xmlHttp.open("GET",CompURL,false);
  xmlHttp.send(null);
  oWriteElement.innerHTML = xmlHttp.responseText;
}

// AJAX Load album contents for a given album
function loadAlbum(mType,mAlbum) {

  if(mType == 'videos') {  // Video List
    
    var xmlHttp;
    xmlHttp = GetAjax();
    oWriteElement = findObjID('contentBody');
    CompURL = "multimedia/vl.php?a=" + escape(mAlbum);
    xmlHttp.open("GET",CompURL,false);
    xmlHttp.send(null);
    oWriteElement.innerHTML = xmlHttp.responseText;
  }
  else
  {  // Picture Album
    var xmlHttp;
    xmlHttp = GetAjax();
    oWriteElement = findObjID('contentBody');
    CompURL = "multimedia/pa.php?a=" + escape(mAlbum);
    xmlHttp.open("GET",CompURL,false);
    xmlHttp.send(null);
    oWriteElement.innerHTML = xmlHttp.responseText;

    // Initialize Slimbox Album
    jQuery(function($) {
      $("a[rel^='lightbox']").slimbox({}, null, function(el) {
        return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
      });
    });
  }
}            

