var flashID = null;

function scrollAndFlash() {
	if (!document.getElementsByTagName) return;
	if (!document.getElementById) return;
	
	// Opera doesn't get the scoll + flash
	if (window.opera) { return; }

	if (!document.getElementById("rightColumn")) return;
	var links = document.getElementById("rightColumn").getElementsByTagName("a");
	
	for (var i=0; i<links.length; i++) {
		if(links[i].href.indexOf("Industry-Expertise/White-Papers.aspx#") != -1) {
			links[i].onclick = function () {
				Scroll.to(cleanHash(this.href));
				return false;
			}
		}
	}

	if(window.location.hash.length > 0) {
		flashID = cleanHash(window.location.hash);
		yft.fadeIn(flashID,15);
	}
}

window.onload = scrollAndFlash;

function cleanHash(s) {
	return s.replace(/^[^#]*#/,'');
}

// Yellow fade technique, except we're using orange instead
var yft = {
  _shade: {1:'efcf8', 2:'ef9f1', 3:'df5ea', 4:'cf2e2', 5:'befdc', 6:'becd5', 7:'ae8cd', 8:'9e5c7', 9:'9e2bf', 10:'8deb8', 11:'7dbb2', 12:'6d8aa', 13:'6d5a4', 14:'5d29c', 15:'4ce95'},
  fadeIn: function(e,i) {
      if (i >= 1) {
      var elem = document.getElementById(e)
      if (elem) elem.style.backgroundColor = '#f' + this._shade[i];
      else return;
        if (i > 1) {
        i -= 1;
          setTimeout("yft.fadeIn('"+elem.id+"',"+i+")", 75);
        } else {
        i -= 1;
        setTimeout("yft.fadeIn('"+elem.id+"',"+i+")", 75);
        elem.style.backgroundColor = "#ffffff";
      }
    }
  }
}

var Scroll = {
	scrollLoop 		: false, 
	scrollInterval	: null,
	getWindowHeight	: function() {
		if (document.all) {  return (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.clientHeight; }
		else { return window.innerHeight; }
		},
	getScrollLeft	: function() {
		if (document.all) { return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft; }
		else { return window.pageXOffset; }
		},
	getScrollTop	: function() {
		if (document.all) { return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop; }
		else { return window.pageYOffset; }
		},
	getElementYpos	: function(el) {
		var y = 0;
		while(el.offsetParent){
			y += el.offsetTop;
			el = el.offsetParent;
			}
		return y;
		},
	to : function(id){
		flashID = id;
		if(this.scrollLoop){
			clearInterval(this.scrollInterval);
			this.scrollLoop = false;
			this.scrollInterval = null;
			}
		var container = document.getElementById('pageWrapper');
		var documentHeight = this.getElementYpos(container) + container.offsetHeight;
		var windowHeight = this.getWindowHeight();
		var ypos = this.getElementYpos(document.getElementById(id));
		if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
		this.scrollTo(0,ypos);
		},
	scrollTo : function(x,y) {
		if(this.scrollLoop) {
			var left = this.getScrollLeft();
			var top = this.getScrollTop();
			if(Math.abs(left-x) <= 2 && Math.abs(top-y) <= 2) {
				window.scrollTo(x,y);
				clearInterval(this.scrollInterval);
				this.scrollLoop = false;
				this.scrollInterval = null;
				yft.fadeIn(flashID,15);
				}
			else {
				window.scrollTo(left+(x-left)/2, top+(y-top)/2);
				}
			}
		else {
			this.scrollInterval = setInterval("Scroll.scrollTo("+x+","+y+")",100);
			this.scrollLoop = true;
			}
		}
	}
