// iGOLPING site script



var HeadlineTimer = false;

var Headlines = false;

window.setTimeout("BasicInit()",500);

window.setInterval("RepositionKiosk()",150);



function BasicInit() {

  if (!(document)) {

    window.setTimeout("BasicInit()",500);

    return;

  }



  navs = document.getElementsByTagName("DIV");

  for (n=0; n<navs.length; n++) {

    nc = navs[n].className;

    nc = nc.toLowerCase();

    if ((nc == "nav") || (nc == "subnav")) {

      navs[n].onmouseover = NavOver;

      navs[n].onmouseout = NavOut;

      navs[n].onclick = NavClick;

    }

  }



  if (Headlines) {

    //HeadlineTimer = window.setTimeout("RotateHeadline()", 15*1000);

    window.setTimeout("CheckFlash()", 500);

    if (parent) {

      if (parent.Headlines) {

	parent.document.getElementById("pollbox").innerHTML = document.getElementById("pollbox").innerHTML;

      }

    }

  }



  CheckSwingIllustration();

}



var HomeFlash = false;

var FlashFinished = false;

var FlashingNavElement = false;

var FlashingNavTag = "";



function CheckFlash() {

  if (FlashFinished) {return;}

  window.setTimeout("CheckFlash()", 500);



  if (HomeFlash == false) {

    if (document.homebanner) {HomeFlash = document.homebanner;}

    if (window.homebanner) {HomeFlash = window.homebanner;}

    if (HomeFlash) {

      HomeFlash.SetVariable("Looping", "");

    } else {

      return;

    }

  }



  Flashing = HomeFlash.GetVariable("Flashing");

  Finished = HomeFlash.GetVariable("Finished");

  HomeFlash.SetVariable("Looping", "");



  if ((Finished == true) || (Finished == "true")) {

    FlashFinished = true;

    Flashing = "";

    RotateHeadline();

  }



  if (Flashing != FlashingNavTag) {

    FlashingNavTag = Flashing;

    if (FlashingNavElement) {

      FlashingNavElement.style.backgroundImage = "";

    }

    FlashingNavElement = false;

    if (FlashingNavTag) {

      var divs = document.getElementsByTagName("DIV");

      for (var n=0; n<divs.length; n++) {

	if (divs[n].className == "nav") {

	  if (divs[n].innerHTML.match(FlashingNavTag)) {

	    FlashingNavElement = divs[n];

	  }

	}

      }

      if (FlashingNavElement) {

	FlashingNavElement.style.backgroundImage = "url(img/navblink.gif)";

      }

    }

  }

}



function CheckSwingIllustration() {

  // Smart scaling for SwingIllustration pages

  var e = document.getElementsByTagName("TABLE");

  for (n=0; n<e.length; n++) {

    if (e[n].className == "SwingIllustration") {

      w = document.body.offsetWidth;

      if (w) {

	if (w<600) {w=600;}

	w -= 145*2; // nav bar and ad

	w -= 24; // img boundary

	w = Math.floor(w/2);

	if (w>350) {w=350;}

	h = Math.floor(328 * w / 350);

	SetChildImgSize(e[n], w, h);

      }

    }

  }

}



function SetChildImgSize(e, w, h) {

  if (e.src) {

    if (e.src.match(/jpg$/)) {

      e.style.width = w+"px";

      e.style.height = h+"px";

    }

  } else {

    for (var n=0; n<e.childNodes.length; n++) {

      SetChildImgSize(e.childNodes[n], w, h);

    }

  }

}



function NavOver() {

  e = this.childNodes[1];

  if (e) {e.style.textDecoration="underline";}

}



function NavOut() {

  e = this.childNodes[1];

  if (e) {e.style.textDecoration="";}

}



function NavClick() {

  e = this.childNodes[1];

  if (e) {

    if (e.href.match(/\.pdf$/)) {return;}

    document.location = e.href;

  }

}



var prevh = false;

function RepositionKiosk() {

  winh = 0;

  if (document.body.offsetHeight > winh) {winh = document.body.offsetHeight;}

  if (document.body.scrollHeight > winh) {winh = document.body.scrollHeight;}

  if (winh == prevh) {return;}

  pageh = winh;



  prevh = winh;

  imgh = 338;

  navh = 0;

  navs = document.getElementsByTagName("DIV");

  for (n=0; n<navs.length; n++) {

    nc = navs[n].className;

    nc = nc.toLowerCase();

    if ((nc == "nav") || (nc == "subnav") || (nc == "loginbox")) {

      h = navs[n].offsetTop+navs[n].offsetHeight+62;

      if (h>navh) {navh = h;}

    }

  }

  if (winh-navh < imgh) {

    document.body.style.backgroundPosition = "0px "+navh+"px";

  } else if (pageh < winh) {

    document.body.style.backgroundPosition = "0px "+(winh-imgh)+"px";

  } else {

    document.body.style.backgroundPosition = "0px bottom";

  }



  TipScan();

}



function TipScan() {

  e = document.getElementById("TipArchive");

  if (e) {

    h = 300;

    if (window.innerHeight) {

      h = window.innerHeight;

    } else if (document.body.offsetHeight) {

      h = document.body.offsetHeight;

    }

    h -= 45; // space for search bar

    h -= 20; // space for pagenation

    es = document.getElementsByTagName("*");

    for (n=0; n<es.length; n++) {

      if (es[n].offsetHeight > 0) {

	if (es[n].tagName == "H1") {h -= es[n].offsetHeight;}

	if (es[n].className == "logobar") {h -= es[n].offsetHeight;}

	if (es[n].className == "legal") {h -= es[n].offsetHeight;}

      }

    }

    if (h<300) {h=300;}

    e.style.height = h+"px";

  } else {

    window.setTimeout("TipScan()", 400);

  }

}



var lbox = false;

function FlashLogin() {

  lbox = document.getElementById("sidelogin");

  if (lbox) {

    document.forms[0].user.focus();

    if (lbox.scrollIntoView) {lbox.scrollIntoView(false);}

    speed = 200;

    times = 3;

    ms = 10;

    for (n=0; n<times; n++) {

      window.setTimeout('SetLboxColor("#3897CF")', ms);

      ms += speed;

      window.setTimeout('SetLboxColor("")', ms);

      ms += speed;

    }

    return false;

  }

}



function SetLboxColor(c) {

  lbox.style.backgroundColor = c;

}



function RotateHeadline() {

  HeadlineTimer = false;

  OnHeadline += 1;

  if (OnHeadline >= Headlines.length) {OnHeadline = 0;}

  ShowHeadline(OnHeadline);

}



function ShowHeadline(num) {

  if (HeadlineTimer) {

    window.clearTimeout(HeadlineTimer);

    HeadlineTimer = window.setTimeout("RotateHeadline()", 60*1000);

  } else {

    HeadlineTimer = window.setTimeout("RotateHeadline()", 15*1000);

  }



  if (Headlines[num]) {

    OnHeadline = num;

    e = document.getElementById("headline");

    if (e) {

      args = Headlines[OnHeadline];

      var img = "img/front/"+args.image;

      img = img.replace("front/tips/", "tips/");



      //var c = "<h1>"+args.title+"</h1>\n";

      var c = "<h1>iGOLPING News</h1>\n";

      c += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td rowspan=\"2\" style=\"vertical-align: top;\"><div class=\"hairline\" style=\"margin-right: 10px; width: 300px; height: 300px; background: black url("+img+") no-repeat scroll center center;\"></div></td><td style=\"vertical-align: top;\">";



      c += "<b>"+args.title+"</b><br>";

      c += args.content+"\n<div class=\"gobar\"><a href=\""+args.golink+"\">"+args.gotext+"<img src=\"img/btn-tinynext.gif\"></a></div></td></tr>\n";

      c += "<tr><td style=\"vertical-align: bottom;\"><small><b>Today's Headlines:</b><br>";

      for (id=0; id<Headlines.length; id++) {

	if (id != OnHeadline) {

	  c += "&bull; <a href=\"index.php?headline="+id+"\" onClick=\"return ShowHeadline("+id+")\">"+Headlines[id].title+"</a><br>";

	} else {

	  c += "&bull; <b>"+Headlines[id].title+"</b><br>";

	}

      }

      c += "</small></td></tr></table>";

      e.innerHTML = c;

      return false;

    }

  }

  return true;

}



function SubmitSurvey(f) {

  /*

  var undefined;

  alert(f.answer.value);

  if (f.answer.value === undefined) {

    alert("Please select an option to vote for.");

    return false;

  }

  */

  f.target = "surveyfrm"; //document.getElementById("surveyfrm");

  return true;

}



function ResizeForumImg(e) {

  maxw = 400;

  maxh = 300;

  maxr = maxw/maxh;



  w = e.width;

  h = e.height;

  r = w/h;



  if ((w<=maxw)&&(h<=maxh)) {

    // do nothing

  } else if (r>maxr) {

    h = maxw/r;

    w = maxw;

  } else {

    w = maxh*r

    h = maxh;

  }



  e.style.width = w+"px";

  e.style.height = h+"px";

}

