var dx=0;
var dy=0;
var dragon=false;
var imagewidth;
var imageheight;

function autoscroll(){
	if(!dragon){
		dy=0;
		dx=-1;
	  	var scroller1=document.getElementById('scroller1');
		var scroller2=document.getElementById('scroller2');
		if (parseInt(scroller2.style.left)>0) scroller2.style.left=(parseInt(scroller2.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)>(imagewidth)) scroller1.style.left=(parseInt(scroller1.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller2.style.left)<(imagewidth*-2)) scroller2.style.left=(parseInt(scroller2.style.left)+(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)<(imagewidth*-1)) scroller1.style.left=(parseInt(scroller1.style.left)+(imagewidth*2)) + "px";
		scroller1.style.left=(parseInt(scroller1.style.left) + dx) + "px";
		scroller2.style.left=(parseInt(scroller2.style.left) + dx) + "px";
		newtop=(parseInt(scroller1.style.top) + dy);
		if(newtop>0) newtop=0;
		if(newtop<(300-imageheight)) newtop=300-imageheight;
		scroller1.style.top = newtop + "px";
		scroller2.style.top = newtop + "px"
		timerID  = setTimeout("autoscroll()", 50);
  }	
}


function dragging(){
	if(dragon){
	  var scroller1=document.getElementById('scroller1');
		var scroller2=document.getElementById('scroller2');
		if (parseInt(scroller2.style.left)>0) scroller2.style.left=(parseInt(scroller2.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)>(imagewidth)) scroller1.style.left=(parseInt(scroller1.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller2.style.left)<(imagewidth*-2)) scroller2.style.left=(parseInt(scroller2.style.left)+(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)<(imagewidth*-1)) scroller1.style.left=(parseInt(scroller1.style.left)+(imagewidth*2)) + "px";
		scroller1.style.left=(parseInt(scroller1.style.left) + dx) + "px";
		scroller2.style.left=(parseInt(scroller2.style.left) + dx) + "px";
		newtop=(parseInt(scroller1.style.top) + dy);
		if(newtop>0) newtop=0;
		if(newtop<(300-imageheight)) newtop=300-imageheight;
		scroller1.style.top = newtop + "px";
		scroller2.style.top = newtop + "px"
		timerID  = setTimeout("dragging()", 10);
  }	
}

function draggingbar(){
	if(dragon){
	  var scroller1=document.getElementById('scroller1');
		var scroller2=document.getElementById('scroller2');
		if (parseInt(scroller2.style.left)>0) scroller2.style.left=(parseInt(scroller2.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)>(imagewidth)) scroller1.style.left=(parseInt(scroller1.style.left)-(imagewidth*2)) + "px";
		if (parseInt(scroller2.style.left)<(imagewidth*-2)) scroller2.style.left=(parseInt(scroller2.style.left)+(imagewidth*2)) + "px";
		if (parseInt(scroller1.style.left)<(imagewidth*-1)) scroller1.style.left=(parseInt(scroller1.style.left)+(imagewidth*2)) + "px";
		scroller1.style.left=(parseInt(scroller1.style.left) + dx) + "px";
		scroller2.style.left=(parseInt(scroller2.style.left) + dx) + "px";
		timerID  = setTimeout("draggingbar()", 10);
  }	
}

function stopdrag(e){
				 dragon=false;
}

function move(e){
         var theview2=document.getElementById('theview');
         var offsetx=getAbsoluteLeft(theview2);
         var offsety=getAbsoluteTop(theview2);
				 if (!window.pageYOffset) scrolly=document.body.scrollTop;
				 		else scrolly=window.pageYOffset;
	       var offsetwidth=Math.ceil(theview2.offsetWidth/2);
         var offsetheight=Math.ceil(theview2.offsetHeight/2);
				 if(!e) e=event;
				 dx=Math.ceil((e.clientX-offsetx-offsetwidth)/25)*-1;
				 //dy=Math.ceil((e.clientY-scrolly-offsety-offsetheight)/25)*-1;
                 dy=0;
}

function startdrag(e){
dragon=true;
//get the view
var theview2=document.getElementById('theview');
var offsetx=getAbsoluteLeft(theview2);
var offsety=getAbsoluteTop(theview2);
if (!window.pageYOffset) scrolly=document.body.scrollTop;
		else scrolly=window.pageYOffset;
var offsetwidth=Math.ceil(theview2.offsetWidth/2);
var offsetheight=Math.ceil(theview2.offsetHeight/2);
//calculate the dx and dy
dx=Math.ceil((e.clientX-offsetx-offsetwidth)/25)*-1;
//dy=Math.ceil((e.clientY-offsety-offsetheight)/25)*-1;
dy=0;
//document.onmousemove=dragging;
timerID  = setTimeout("dragging()", 10);
if (e.preventDefault)
			 e.preventDefault();
    document.onmousemove=move
}

function startdragbar(e){
dragon=true;
//get the view
var thebar2=document.getElementById('thebar');
var offsetx=getAbsoluteLeft(thebar2);
var offsety=getAbsoluteTop(thebar2);
if (!window.pageYOffset) scrolly=document.body.scrollTop;
		else scrolly=window.pageYOffset;
var offsetwidth=Math.ceil(thebar2.offsetWidth/2);
//calculate the dx and dy
dx=Math.ceil((e.clientX-offsetx-offsetwidth)/25)*-1;
dy=0;

timerID  = setTimeout("draggingbar()", 10);
if (e.preventDefault)
			 e.preventDefault();
    document.onmousemove=move
}

function getAbsoluteLeft(obj){
	var left=0;
	left=obj.offsetLeft;
	while(obj=obj.offsetParent){
				 left=left+obj.offsetLeft;
	}
	return left;
}

function getAbsoluteTop(obj){
	var top=0;
	top=obj.offsetTop;
	while(obj=obj.offsetParent){
				 top=top+obj.offsetTop;
	}
	return top;
}

function changelocation(imageurl,imagew,imageh){
		var scroller1=document.getElementById('scroller1');
		var scroller2=document.getElementById('scroller2');
		scroller1.src="http://www.armourhe.co.uk/images/nothing.gif";
		scroller2.src="http://www.armourhe.co.uk/images/nothing.gif";
		timerID  = setTimeout("changelocation_load('"+imageurl+"',"+imagew+","+imageh+")", 1000);
}

function changelocation_load(imageurl,imagew,imageh){
		var scroller1=document.getElementById('scroller1');
		var scroller2=document.getElementById('scroller2');
		scroller1.style.left="0px";
		scroller1.style.top=Math.ceil((300-imageh)/2)+"px";
		scroller2.style.left="0px";
		scroller2.style.top=Math.ceil((300-imageh)/2)+"px";
		imagewidth=imagew;
		imageheight=imageh;
		scroller1.src=imageurl;
		scroller2.src=imageurl;
		dragon=true; 
		//timerID  = setTimeout("autoscroll()", 10);
}

function hoverthumb(thumb){
				 var thethumb=document.getElementById('thumb');
				 thethumb.src=thumb;
				 document.onmousemove=movethumb
}

function hidethumb(){
				 var thethumb=document.getElementById('thumb');
                 var thethumbbubble=document.getElementById('thumbbubble');
				 document.onmousemove=null;
				 thethumbbubble.style.top="-150px";
}

function movethumb(e){
				 if (!e) e=event;
				 var thethumb=document.getElementById('thumbbubble');
				 thethumb.style.left=(e.clientX+5)+"px";
				 thethumb.style.top=(e.clientY+5)+"px";
}