// GET STYLE FUNCTION already in default JS
// get style 
/*function getStyle(el, cssprop){
 if (el.currentStyle) //IE
  return el.currentStyle[cssprop]
 else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
  return document.defaultView.getComputedStyle(el, "")[cssprop]
 else //try and get inline style
  return el.style[cssprop]
}*/

function reposition(lyr,x_dist,y_dist,swtch,spd, func) {
	// lyr = div to move
	// x_dist = horizontal distance to move
	// v_vist = vertical distance to move
	// swth = object moving between two points or free
	// speed = interval speed
	
	// fill in variables not passed
	if (!x_dist) var x_dist=0;
	if (!y_dist) var y_dist=0;
	if (!spd) var spd=10;
		
	// fill in first-time variables
	if(!lyr.rturn) lyr.rturn = false;
	if(!lyr.moving) lyr.moving = false;
	
	
	if (swtch) {
		// fix start & end position if 'switch' layer
		if(!lyr.initx) lyr.initx=lyr.offsetLeft-parseFloat(getStyle(lyr, 'marginLeft'));
		if(!lyr.inity) lyr.inity=lyr.offsetTop-parseFloat(getStyle(lyr, 'marginTop'));
		if (!lyr.endx) lyr.endx = lyr.initx+x_dist;
		if(!lyr.endy) lyr.endy = lyr.inity+y_dist;
		//
		if (lyr.rturn == false) {
			lyr.xstart=lyr.initx;
			lyr.xgoal=lyr.endx;
			lyr.ystart=lyr.inity;
			lyr.ygoal=lyr.endy;
		} else if (lyr.rturn == true) {
			lyr.xstart=lyr.endx;
			lyr.xgoal=lyr.initx;
			lyr.ystart=lyr.endy;
			lyr.ygoal=lyr.inity;
		};
	} else {
		lyr.xstart=lyr.offsetLeft-parseFloat(getStyle(lyr, 'marginLeft'));
		lyr.xgoal=lyr.xstart+x_dist;
		lyr.ystart=lyr.offsetTop-parseFloat(getStyle(lyr, 'marginTop'));
		lyr.ygoal=lyr.ystart+y_dist;
	}
	
	if (lyr.xgoal<lyr.xstart) {
		lyr.hdir=-1;
	} else {
		lyr.hdir=1;
	};
	if (lyr.ygoal<lyr.ystart) {
		lyr.vdir=-1;
	} else {
		lyr.vdir=1;
	};

	if (lyr.moving	== false) {
		lyr.style.left=lyr.xstart+"px";
		lyr.style.top=lyr.ystart+"px";
		ivID = setInterval(function(){moveAction(lyr,spd,func)}, spd);
		lyr.moving=true;
	};
}

function moveAction(lyr, spd, func) {
	
	var xpos = parseInt(lyr.style.left);
	var ypos = parseInt(lyr.style.top);
	
	if (lyr.hdir == 1) {
		var newx = Math.ceil(xpos+(lyr.xgoal-xpos)/spd);
	} else {
		var newx = Math.floor(xpos+(lyr.xgoal-xpos)/spd);
	};
	if (lyr.vdir == 1) {
		var newy = Math.ceil(ypos+(lyr.ygoal-ypos)/spd);
	} else {
		var newy = Math.floor(ypos+(lyr.ygoal-ypos)/spd);
	};
	
	lyr.style.left = newx+"px";
	lyr.style.top = newy+"px";
	
	if (((lyr.prvx != undefined) && lyr.prvx == newx) && ((lyr.prvy != undefined) && lyr.prvy == newy)) {
		clearInterval(ivID);
		// putting layer at final position
		lyr.style.left = lyr.xgoal+"px";
		lyr.style.top = lyr.ygoal+"px";
		// turning off moving indicator
		lyr.moving=false;
		//switch values
		lyr.rturn == true ? lyr.rturn=false : lyr.rturn=true;
		// execute function
		if (func) func();
	};
	lyr.prvx = newx;
	lyr.prvy = newy;
};