var end_time;

function getClip(layer_id){
	var layer = document.getElementById(layer_id);
	if(layer){
		var returnValue = new Array();
		if(layer.style.clip.indexOf(",")!=-1){
			returnValue = layer.style.clip.split(",");
		}else{
			returnValue = layer.style.clip.split(" ");
		}
		for(i=0;i<returnValue.length;i++){
			returnValue[i] = returnValue[i].replace("rect(","");
			returnValue[i] = returnValue[i].replace(")","");
			returnValue[i] = returnValue[i].replace("px","");
		}
	}else{
		var returnValue = false;
	}
	return(returnValue);	
}

function prepLayer(layer_id,amount,time,clipHeight,clipWidth){
	var layer = document.getElementById(layer_id);
	if(layer){
		var ul_layer = layer.getElementsByTagName("ul")[0];
		var layerHeight = ul_layer.offsetHeight;
		var clipArray = new Array();
		try{
			ul_layer.style.clip = "rect(0px,"+clipWidth+"px,"+clipHeight+"px,0px)";
		}catch(err){
			ul_layer.style.clip = "rect(0px "+clipWidth+"px "+clipHeight+"px 0px)";
		}
		clipArray = getClip(ul_layer.id);
		if(clipArray!=false){
			var clipTop = clipArray[0];
			var clipBottom = clipArray[2];
			var clipWidth = clipArray[1];
		}
		if(ul_layer.style.clip.indexOf(",")!=-1){
			ul_layer.style.clip = "rect("+clipTop+"px,"+clipWidth+"px,"+clipBottom+"px,0px)";
		}else{
			// for IE
			time = time*4;
			ul_layer.style.clip = "rect("+clipTop+"px "+clipWidth+"px "+clipBottom+"px 0px)";
		}
		var topper = ul_layer.offsetTop;
		var span_tags = layer.getElementsByTagName("span");
		if(span_tags){
			for(i=0;i<span_tags.length;i++){
				if(span_tags[i].className.indexOf('scroller')!=-1){
					if(span_tags[i].className.indexOf('up')!=-1){
						span_tags[i].onmousedown = function(){
							scrollLayer(ul_layer.id,-amount,time,layerHeight,0);
						}
						span_tags[i].onmouseup = function(){
							stopScroll();
						}
					}else{ // assuming className is 'down' if it is not 'up'
						span_tags[i].onmousedown = function(){
							scrollLayer(ul_layer.id,amount,time,layerHeight,0);
						}
						span_tags[i].onmouseup = function(){
							stopScroll();
						}
					}
				}
			}
		}
	}
}

function scrollLayer(layer_id,amount,time,layerHeight,iteration){
	var layer = document.getElementById(layer_id);
	var topper = parseInt(layer.offsetTop);
	clipArray = getClip(layer_id);
	if(clipArray!=false){
		var clipTop = parseInt(clipArray[0]);
		var clipBottom = parseInt(clipArray[2]);
		var clipWidth = parseInt(clipArray[1]);
	}
	if(layer){
		// the following WHILE loop creates a slower stop that
		// is easier for users.  since amount increments, this
		// loop decrements it to make up for the last remaining
		// pixels
		while((clipTop+amount < 0 || clipBottom+amount > layerHeight) && amount!=0){
			if(amount>0){
				amount--;
			}else{
				amount++;
			}
		}
		if(amount!=0){
			clipTop += amount;
			clipBottom += amount;
			topper -= amount;
		}else{
			return;	
		}
		if(layer.style.clip.indexOf(",")!=-1){
			layer.style.clip = "rect("+clipTop+"px,"+clipWidth+"px,"+clipBottom+"px,0px)";
		}else{
			//this is for IE
			layer.style.clip = "rect("+clipTop+"px "+clipWidth+"px "+clipBottom+"px 0px)";
		}
		layer.style.top = topper+"px";
		iteration++;
		//if(iteration>5)amount=1.1*amount;
		if(iteration>5){
			if(amount>0){
				amount++;
			}else{
				amount--;
			}
		}
		end_time = setTimeout("scrollLayer('"+layer_id+"',"+amount+","+time+","+layerHeight+","+iteration+")",time);
	}
}

function stopScroll(){
	if(end_time) clearTimeout(end_time);
}

function primeLayer(){
	prepLayer("featured_news_wrapper",1,10,350,194);
}

