//copyright of TSC
scroll.prototype.scrollNorth = function() { this.startScroll(90) }
scroll.prototype.scrollSouth = function() { this.startScroll(270) }
scroll.prototype.scrollWest = function() { this.startScroll(180) }
scroll.prototype.scrollEast = function() { this.startScroll(0) }

scroll.prototype.startScroll = function(deg, speed) {//alert(this.vFindSign)

if (this.loaded)
{//alert("startScroll")
if (this.aniTimer) window.clearTimeout(this.aniTimer)
this.overrideScrollAngle(deg)
this.speed = speed ? speed : this.origSpeed
this.lastTime = (new Date()).getTime() - this.y.minRes
this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
}
}
scroll.prototype.endScroll = function() {//alert("endtScroll")
if (this.loaded)
{
window.clearTimeout(this.aniTimer)
this.aniTimer = 0;
this.speed = this.origSpeed
}
}
scroll.prototype.overrideScrollAngle = function(deg) {//alert("overrideScrollAngle")
if (this.loaded)
{
deg = deg % 360
if (deg % 90 == 0) {
var cos = deg == 0 ? 1 : deg == 180 ? -1 : 0
var sin = deg == 90 ? -1 : deg == 270 ? 1 : 0
} else {
var angle = deg * Math.PI / 180
var cos = Math.cos(angle)
var sin = Math.sin(angle)
sin = -sin
}
this.fx = cos / (Math.abs(cos) + Math.abs(sin))
this.fy = sin / (Math.abs(cos) + Math.abs(sin))
this.stopH = deg == 90 || deg == 270 ? this.scrollLeft : deg < 90 || deg > 270 ? this.scrollW : 0
this.stopV = deg == 0 || deg == 180 ? this.scrollTop : deg < 180 ? 0 : this.scrollH
}
}
scroll.prototype.overrideScrollSpeed = function(speed) {//alert("overrideScrollSpeed")
if (this.loaded) this.speed = speed
}
scroll.prototype.scrollTo = function(stopH, stopV, aniLen) {//alert("scrollTo")
if (this.loaded)
{
if (stopH != this.scrollLeft || stopV != this.scrollTop) {
if (this.aniTimer) window.clearTimeout(this.aniTimer)
this.lastTime = (new Date()).getTime()
var dx = Math.abs(stopH - this.scrollLeft)
var dy = Math.abs(stopV - this.scrollTop)
var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2))
this.fx = (stopH - this.scrollLeft) / (dx + dy)
this.fy = (stopV - this.scrollTop) / (dx + dy)
this.stopH = stopH
this.stopV = stopV
this.speed = d / aniLen * 1000
window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
}
}
}
scroll.prototype.jumpTo = function(nx, ny) { //alert("jumpTo")
if (this.loaded)
{

nx = Math.min(Math.max(nx, 0), this.scrollW)
ny = Math.min(Math.max(ny, 0), this.scrollH)
if (document.getElementById('myScrollContent').offsetHeight < ContentSize) 
	 {return}
this.scrollLeft = nx
this.scrollTop = ny
if (this.y.ns4)this.content.moveTo(-nx, -ny)
else {
this.content.style.left = -nx + "px"
this.content.style.top = -ny + "px"

}
}
}

scroll.minRes = 10
scroll.ie = document.all ? 1 : 0
scroll.ns4 = document.layers ? 1 : 0
scroll.dom = document.getElementById ? 1 : 0
scroll.mac = navigator.platform == "MacPPC"
scroll.mo5 = document.getElementById && !document.all ? 1 : 0
scroll.prototype.scroll = function() {
this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
var nt = (new Date()).getTime()
var d = Math.round((nt - this.lastTime) / 1000 * this.speed)
if (d > 0)
{
var nx = d * this.fx + this.scrollLeft
var ny = d * this.fy + this.scrollTop
var xOut = (nx >= this.scrollLeft && nx >= this.stopH) || (nx <= this.scrollLeft && nx <= this.stopH)
var yOut = (ny >= this.scrollTop && ny >= this.stopV) || (ny <= this.scrollTop && ny <= this.stopV)
if (nt - this.lastTime != 0 &&
((this.fx == 0 && this.fy == 0) ||
(this.fy == 0 && xOut) ||
(this.fx == 0 && yOut) ||
(this.fx != 0 && this.fy != 0 && xOut && yOut)))
{
this.jumpTo(this.stopH, this.stopV)
this.endScroll() //hit endScroll when it's end of page
}
else {
this.jumpTo(nx, ny)
this.lastTime = nt
}
}
}
function scroll(id, left, top, width, height, speed, contentWidth, initLeft, initTop)
{//alert("scroll")
var y = this.y = scroll
if (!initLeft) initLeft = 0
if (!initTop) initTop = 0
if (!contentWidth) contentWidth = width
if (document.layers && !y.ns4) history.go(0)
if (y.ie || y.ns4 || y.dom) {
this.loaded = false
this.id = id
this.origSpeed = speed
this.aniTimer = false
this.op = ""
this.lastTime = 0
this.clipH = height
this.clipW = width
this.scrollTop = initTop
this.scrollLeft = initLeft
this.gRef = "scroll_"+id
eval(this.gRef+"=this")
var d = document

d.write('<style type="text/css">')
d.write('#' + this.id + 'Container { left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px; clip:rect(0 ' + width + ' ' + 1 + ' 0); overflow:hidden; }')
d.write('#' + this.id + 'Container, #' + this.id + 'Content { position:relative; }')
d.write('#' + this.id + 'Content { left:' + (-initLeft) + 'px; top:' + (-initTop) + 'px; width:' + contentWidth + 'px;}')
d.write('</style>')
}
}
scroll.prototype.load = function() {//alert("load")
var d, lyrId1, lyrId2
d = document
lyrId1 = this.id + "Container"
lyrId2 = this.id + "Content"
this.container = this.y.dom ? d.getElementById(lyrId1) : this.y.ie ? d.all[lyrId1] : d.layers[lyrId1]
this.content = obj2 = this.y.ns4 ? this.container.layers[lyrId2] : this.y.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
this.docH = Math.max(this.y.ns4 ? this.content.document.height : this.content.offsetHeight, this.clipH)
this.docW = Math.max(this.y.ns4 ? this.content.document.width : this.content.offsetWidth, this.clipW)
this.clipH = this.container.offsetHeight
this.scrollH = this.docH - this.clipH
//this.scrollH = this.docH - this.container.offsetHeight
this.scrollW = this.docW - this.clipW
this.loaded = true
this.scrollLeft = Math.max(Math.min(this.scrollLeft, this.scrollW),0)
this.scrollTop = Math.max(Math.min(this.scrollTop, this.scrollH),0)

//=======================================
var vFindSign; //use it to check if # sign is found or not, not find=0, find=1
if (checkforsign()) {
		vFindSign = 1;
		var vAnchorName; //store anchor name
		var offsetValue;
		vAnchorName = getAnchorName();
	
		//var c = getAnchorPosition(vAnchorName); //where c.y is the y-coordinate for anchor

		//var newPosition; //new position for anchor section

		//newPosition = c.y - (this.clipH / 2)
		

		
		offsetValue = document.getElementById(vAnchorName).offsetTop

		if (document.getElementById(vAnchorName).offsetParent.id == 'InTheNews' + vAnchorName) {
			offsetValue = document.getElementById(vAnchorName).offsetParent.offsetTop + 
				document.getElementById('TSC In The News').offsetTop + 
				document.getElementById('TSC In The News').offsetHeight
		}
		if (document.getElementById(vAnchorName).offsetParent.id == '') {
			offsetValue = document.getElementById(vAnchorName).offsetParent.offsetTop
		}
		this.jumpTo(this.scrollLeft, offsetValue)
}else {
		vFindSign = 0;
		this.jumpTo(this.scrollLeft, this.scrollTop) // can pass the cordinates of #
}
}

//==========================================================================================
function checkforsign() {
//this function is used for checking for # sign on URL
		//==>added to check if the URL has # as href or not===========
		
		LocationExp = /location=/gi;
		
		if (LocationExp.test(document.location.href)) {
			return true; // location found	
		}
		else {
			return false; //can't find location
		}
		
		//=============================================================
}//end of function
function getAnchorName() {
// look for anchor name
		
		LocationExp = /location=/gi;
		AmpExp = /&/gi;
		
		// get everything after the location parameter
		var val = unescape(document.location.href);
		var ary_href_anchor = val.split(LocationExp);
		var href_anchor = ary_href_anchor[1]
		
		// make sure there are no other paramaters
		ary_href_anchor = href_anchor.split(AmpExp);
		href_anchor = ary_href_anchor[0]
		return href_anchor; //second half of array is anchor name
}
