/*Functions*/
overAdd = function() {
	var el = document.getElementById("mainnav");
	if (el != null) {
		var sfEls = el.getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
				if (this.nextSibling != null) this.nextSibling.className += ' nextLI';
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(" over", "");
				if (this.nextSibling != null) this.nextSibling.className=this.nextSibling.className.replace(" nextLI", "");
			}
		}
	}
}
function getTarget(e) {
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
  targ = targ.parentNode;
  return targ;
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
/*Global ticker vars*/
var tickBool=false;
var originalListWidth;
var evArray = new Array();
var timeOut=-1;

function tickerFunction() {
  var tickWrap = document.getElementById("ticknews_wrapper");
  if(tickWrap!=null) {

    var tickWrapInner = document.getElementById("tick_sizer");
    var tickList = tickWrapInner.getElementsByTagName("ul")[0];

    originalListWidth = tickList.offsetWidth;

    if(originalListWidth>=tickWrap.offsetWidth-10) {

      var tickListAfter = tickList.cloneNode(true);
      tickWrapInner.appendChild(tickListAfter);

      var aEls = tickWrapInner.getElementsByTagName("a");
      for(var i=0;i<aEls.length;i++){
        aEls[i].onmouseover=function() {
          stopTick();
        }
        aEls[i].onmouseout=function() {
          startTick();
        }
      }

      setTimeout('startTick()', 3000);
    }
  }
}
function stopTick() {
  tickBool = false;
  evArray = null;
  evArray = new Array();
  //timeOut = null;
}
function startTick() {
  if(!tickBool && evArray.length==0) {
    evArray.push('tick');
    tickBool=true;
    tickLeft();
  }
}
function tickLeft() {
  if(!tickBool) return;
  //var tickList= document.getElementById("tickList");
  var tickList= document.getElementById("tick_sizer");
  var currLeft = tickList.style.left;
  if(currLeft==null || currLeft=='') currLeft=0;
  else(currLeft=currLeft.replace('px', ''));
  currLeft-=1;
  if(currLeft<=-originalListWidth) { //Reset the list to position 0;
    currLeft=0;
  }
  tickList.style.left=currLeft+'px';
  clearTimeout(timeOut);
  timeOut = setTimeout('tickLeft()', 20);
}
function submitOnEnter(e) {
  try {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if(code==13) {
      document.getElementById("datechooser_form").submit();
      return false;
    }
  }
  catch(ex) {}
}

function replaceQuery(s, v, str) {
	if (str == null) str = window.location.href;
	aStr = "";
	idx = str.indexOf("#");
	if (idx != -1) {
		aStr = str.substring(idx, str.length);
		str = str.substring(0, idx);
	}
	s1 = '?' + s + '=';
	l = s1.length;
	lIdx = str.indexOf(s1);
	if (lIdx == -1) {
		s1 = '&' + s + '=';
		lIdx = str.indexOf(s1);
	}
	if (lIdx == -1) {
		str += s1 + v;
		str += aStr;
		return str;
	}
	lStr = str.substring(0, lIdx+l);
	rStr = str.substring(lStr.length, str.length);
	rIdx = rStr.indexOf("&");
	if (rIdx != -1) rStr = rStr.substring(rIdx, rStr.length)
	else rStr = '';
	str = lStr + v + rStr;
	str += aStr;
	return str;
}

function replaceLocation(s1, v1, s2, v2) {
	url = replaceQuery(s1, v1);
	if (s2 != null) url = replaceQuery(s2, v2, url);
	window.location.href = url;
}

function printPage() {
	url = replaceQuery('media', 'PRINT');
	window.open(url);
}


function setPartQuery(partid) {
	str = window.location.href;

	args = arguments;
	argStr = "";
	if (args.length > 2) {
		aStr = "";
		idx = str.indexOf("#");
		if (idx != -1) {
			aStr = str.substring(idx, str.length);
			str = str.substring(0, idx);
		}
		for (j=1; j<args.length; j=j+2) {
			s = args[j];
			v = args[j+1];

			if (s.indexOf("#") == 0) {
				str = str + s;
			}
			else {
				qry = s + "=" + partid + ":";

				idx = str.indexOf(qry);
				if (idx == -1) {
					idx = str.indexOf("?");
					if (idx != -1) flag = "&"
					else flag = "?";
					str = str + flag + qry + v;
				}
				else {
					lStr = str.substring(0, idx + qry.length);
					rStr = str.substring(lStr.length, str.length);
					idx = rStr.indexOf("&");
					if (idx != -1) rStr = rStr.substring(idx, rStr.length)
					else rStr = '';
					str = lStr + v + rStr;
				}
			}
		}
		if (str.indexOf("#") == -1) str = str + aStr;
		if (document.all) window.location.replace(str)
		else window.location.href = str;
	}
}

function changePic(elVar, pathVar) {

	//alert(document.getElementById("img_large").src);
	//document.images['img_large'].src=pathVar;

	document.getElementById("img_large").src = pathVar;
	var textVar = elVar.getAttribute("title");

	var el = document.getElementById("img_description");
	if (el.childNodes.length == 0) el.appendChild(document.createTextNode(textVar))
	else el.firstChild.nodeValue=textVar;

}

/*Startup*/
if (window.attachEvent) addLoadEvent(overAdd);
addLoadEvent(tickerFunction);