window.onload = function() {
  external_links();
  popup_init();
  table_rows_init();
  if (document.getElementById("wallpaper_sizes"))
      document.getElementById("wallpaper_sizes").selectedIndex = 0;
}

function external_links() {
    if (!document.getElementsByTagName) {return false;}
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++)
  if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external")
      anchors[i].target = "NotJets";
}
function popup_init()
{
  if (!document.getElementsByTagName) {return false;}
  var anchors = document.getElementsByTagName('a');
  for(i=0; i<anchors.length; i++)
    if (anchors[i].className == 'popup')
      anchors[i].onclick = popup;
  return true;
}
function popup(link)
{
  var link = $(link);
  var size = link.getAttribute('rel');
  var url = link.getAttribute('href');

  /* computes dimensions passed from "rel" tag; format: <width>x<height>, output: width=<width>,height=<height> */
  if (size) {
    size = size.split('x');
    var popup_size = 'width='+size[0]+',height='+size[1];
  } else {
    var popup_size = 'width=726,height=500';
  }

  /* window options */
  var win_options = 'left=0,top=0,dependent=1,resizable=1,scrollbars=1,location=0,menubar=1,statusbar=0,'+popup_size;

  /* creates new window */
  if (win = window.open(link.href,'_blank',win_options))
    return false;
  
  /* follows link on failure */
  return true; 
}

function table_rows_init(){

  var id = document.getElementById('roster');
  if (id) {
    var tr_all = id.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
    for (var i = 0; i < tr_all.length; i++){
        if (tr_all[i].className == 'clickable' || tr_all[i].className == 'clickable second') {
      tr_all[i].onmouseover = row_over;
      tr_all[i].onmouseout = row_off;}
    }
  }
}
function row_over() { 
    if (this.className == 'clickable second')
  this.className = 'clickable second row_over'; 
    else if (this.className == 'clickable')
  this.className = 'clickable row_over'; 
}
function row_off() { 
  if (this.className == 'clickable second row_over')
    this.className = 'clickable second'; 
  else if (this.className == 'clickable row_over')
    this.className = 'clickable'; 
}
function faq_show_answer(number) {
    if (!document.getElementsByTagName) {return false;}

    // var faq_intro = document.getElementById('faq_intro');
    // faq_intro.style.display = 'none';

    var h2_answers = document.getElementById('h2_answers');
    h2_answers.style.display = 'block';

    var h2_faq = document.getElementById('h2_faq');
    h2_faq.style.display = 'none';

    var answer = 'answer_' + number;
    var divs = document.getElementsByTagName('div');
  
    for(i=0; i<divs.length; i++){
  if (divs[i].className == 'answer'){
      if (divs[i].id != answer){
    divs[i].style.display = 'none';
      }
      else{
    divs[i].style.display = 'block';
      }
  }
    }

    var question = 'question_' + number;
    var lis = document.getElementsByTagName('li');
    for(i=0; i<lis.length; i++){
  if (lis[i].className == 'question'){
      if (lis[i].id != question){
    lis[i].childNodes[0].className = '';
      }
      else{
    lis[i].childNodes[0].className = 'active';
      }
  }
    }

    return true;
}

function show_dates(id) {
  if (!document.getElementsByTagName) {return false;}

  document.getElementById('dates_1').style.display = 'none';
  document.getElementById('dates_2').style.display = 'none';
  document.getElementById('dates_3').style.display = 'none';
  document.getElementById('dates_4').style.display = 'none';
  document.getElementById('dates_5').style.display = 'none';

  var e = document.getElementById(id);
  e.style.display = 'block';
  
  document.getElementById('daterange_1').className = 'button-top';
  document.getElementById('daterange_2').className = 'button-top';
  document.getElementById('daterange_3').className = 'button-top';
  document.getElementById('daterange_4').className = 'button-top';
  document.getElementById('daterange_5').className = 'button-top';

  var range_id = 'daterange_' + id.substr(6 ,1);
  document.getElementById(range_id).className = 'button-top-active';

  return true;
}


function doMenuAction(path) {
    document.location = path;
}

function doNavigationAction(flashURL) {
    document.location = flashURL;
}


function htmlSubstr(htmlString, maxLength, moreText, stripTags) {
    // htmlSubstr is XHTML compliant.
    // It is case sensitive and empty HTML elements like <br> should be written <element/>.

    if (stripTags) htmlString = stripHtml(htmlString);

    if (htmlString != null && htmlString != "" && maxLength != null && maxLength != "") {
  
        isText = true;
  var r = "";
  var i = 0;
      
  var currentChar = "";
        var lastSpacePosition = -1;
        var lastChar = "";
        
  var tagsArray = new Array();
        var currentTag = "";
        var tagLevel = 0;

        var noTagLength = 0;
        
  // Calculate string length without tags
  for (j=0;j<htmlString.length;j++) {
      currentChar = htmlString.charAt(j);
      if (currentChar == "<") { isText = false }
      if (isText) { noTagLength++ }
      if (currentChar == ">") { isText = true }    
  }
        
        // Parser loop
        for (j=0;j<htmlString.length;j++) {
            currentChar = htmlString.charAt(j);
            r += currentChar ;
            
      // Lesser than event
      if (currentChar == "<") { isText = false }
      
            // Character handler
            if (isText) {
                // Memorize last space position
                if (currentChar == " ") { lastSpacePosition = j } else { lastChar = currentChar }
                i++;
      } else {
    currentTag += currentChar;
      }
      
            // Greater than event
            if (currentChar == ">") {
                isText = true;
                
    // Opening tag handler
    if (currentTag.indexOf("<") != -1 && currentTag.indexOf("/>") == -1 && currentTag.indexOf("</") == -1) {
        // Tag has attribute(s)
        if (currentTag.indexOf(" ") != -1) {
      currentTag = currentTag.substr(1,currentTag.indexOf(" ")-1);
        } else {
      // Tag doesn't have attribute(s)
      currentTag = currentTag.substr(1,currentTag.length-2);
        }
                    
                    tagsArray[tagLevel] = currentTag
      tagLevel++;
    } else if (currentTag.indexOf("</") != -1) {
                    
        // Closing tag handler
                    tagsArray[tagLevel-1] = null;
                    tagLevel--;
    }
                
                currentTag = "";
      }    

            if (i == maxLength) { break }
        }
  
        // Cut HTML string at last space position
        if (maxLength < noTagLength) {        
            if (lastSpacePosition != -1) {
                r = htmlString.substr(0,lastSpacePosition);
      } else {
    r = htmlString.substr(0,j);
      }
  }

        if (maxLength < noTagLength) {        
            if (lastChar != ".") { r += "..." } else { r += ".." }
      r += " " + moreText
    }

        // Close broken XHTML elements
        for (a=tagsArray.length-1;a>=0;a--) {
            if (tagsArray[a] != null) { r += "</" + tagsArray[a] + ">" }
        }
        
        return r; // Return new HTML string
  
    } else {
  alert("hmtlSubstr() function needs parameters : hmtlSubstr(htmlString,maxLength)");
    }
}


function stripHtml(htmlString) {

    if (htmlString == null || htmlString == "") {
  return htmlString;
    }
    return htmlString.replace(/(<([^>]+)>)/ig, ""); // CJS: WARNING: IE6 eats all trailing and leading whitespace, so This <br />\n That displays as ThisThat
}


function openExternalSite(url) {
    window.open('' + url, 'NotJets');
}

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=730,height=650');");
}

function popUpImageDownload(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=730,height=650');");
}

function popUpAudioDownload(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('/news/multimedia/download/' + URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=670,height=122');");
}

function showShopAd() {
    // Insert click tracking URL here
    document.phpAds_ct0 ='Insert_Clicktrack_URL_Here'

    var awrz_rnd = Math.floor(Math.random()*99999999999);

    var awrz_protocol = location.protocol.indexOf('https')>-1?'https:':'http:';
    if (!document.phpAds_used) document.phpAds_used = ',';
    document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
    document.write (awrz_protocol+"//marketing.newyorkjets.com/adjs.php?n=aa27863c");
    document.write ("&zoneid=18&target=_self");
    document.write ("&exclude=" + document.phpAds_used);
    document.write ("&loc=" + escape(window.location));
    if (document.referrer)
  document.write ("&referer=" + escape(document.referrer));
    document.write ('&r=' + awrz_rnd);
    document.write ("&ct0=" + escape(document.phpAds_ct0));
    document.write ("'><" + "/script>");
}

function showOrHideSizeOption(id, size, visible)
{
    if (visible)
    document.getElementById("size_" + size).style.display = 'list_item';
    else
       document.getElementById("size_" + size).style.display = 'none';   
}

function show_image(image_id, has_large, has_medium, has_small){
    document.getElementById("big_image").src = '/multimedia/pictures/photos/' + image_id + '/multimedia.jpg';
    document.getElementById("big_image").name = image_id;

    document.getElementById("wallpaper_sizes").selectedIndex = 0;

    //    showOrHideSizeOption(image_id, 'multimedia');
    showOrHideSizeOption(image_id, 'large', has_large);
    showOrHideSizeOption(image_id, 'medium', has_medium);
    showOrHideSizeOption(image_id, 'small', has_small);
}

function show_wallpaper(params){

    if (params == '') return false;

    var tab = params.split('|');
    var image_name = tab[0];
    var width = parseInt(tab[1]);
    var height = parseInt(tab[2]);

        window.open ("/multimedia/pictures/photos/" + document.getElementById("big_image").name + "/" + image_name, 'Wallpaper', 'menubar=no, toolbar=no, location=no, scrollbars=no, resizable=no, status=no, width='+(width+20) +', height='+(height+20));
}

function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

// Character counter
function Contar(entrada,salida,texto,caracteres) {
  var entradaObj=getObject(entrada);
  var salidaObj=getObject(salida);
  var longitud=caracteres - entradaObj.value.length;
  if(longitud <= 0) {
    longitud=0;
    entradaObj.value=entradaObj.value.substr(0,250);
    alert("Maximum character count reached.");
  }
  salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
}

function printWindow(){
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}

function show_stats(section){
  var div_array = new Array();
  var button_array = new Array();
  div_array[0] = $('offense');
  div_array[1] = $('defense');
  div_array[2] = $('special_teams');
  button_array[0] = $('offense_button');
  button_array[1] = $('defense_button');
  button_array[2] = $('special_teams_button');
  for(i=0; i<div_array.length; i++){
    if (div_array[i].getAttribute('id') == section){
      div_array[i].style.display = 'block';
      button_array[i].className = 'button-top-active';
    } else {
      div_array[i].style.display = 'none';
      button_array[i].className = 'button-top';
    }
  }
}
