var Style = { getElementStyle: function(elem, property){ var ME = arguments.callee; var value = null; if(window.getComputedStyle) { var compStyle = window.getComputedStyle(elem, ''); value = compStyle.getPropertyValue(property); }else if(elem.currentStyle){ value = elem.currentStyle[property.camelize()]; //!!String.camelize() } if(value){ return value; }else{ ME.errorMessage("Please, turn on the JS"); } } } Function.prototype.errorMessage = function(message){ alert(message); } String.prototype.camelize = function(){ var arrThis = this.split('-'); if(arrThis.length == 1){ return this; }else{ var wordCamelized = arrThis[0]; var firstSymbol; for(var i = 1; i < arrThis.length; i++){ firstSymbol = arrThis[i].substr(0, 1); arrThis[i] = arrThis[i].substr(1); arrThis[i] = firstSymbol.toUpperCase() + arrThis[i]; wordCamelized+=arrThis[i]; } return wordCamelized; } } function Movement(elem, stepX, stepY, interval, time, distance, position, stopPos){ this.elem = elem; this.stepX = stepX; this.stepY = stepY; this.interval = interval; this.time = (time) ? time * 1000 : -1; this.distance = (distance) ? distance : -1; this.distancePassed = 0; this.timeElapsed = 0; this.currentTop = 0; this.currentLeft = 0; this.position = (position) ? position : null; this.stopPos = (stopPos) ? stopPos : null; this.isMoving = false; this.objInterval = null; this.onmove = null; this.onstart = null; this.onstop = null; this.init() } Movement.prototype.init = function(){ this.setPos(); //this.start(); } Movement.prototype.setPos = function(){ this.elem.style.position = 'absolute'; if(this.position){ if(typeof this.position.X != "undefined"){ this.elem.style.left = this.position.X + "px"; this.currentLeft = this.position.X; }else{ var pX = parseInt(Style.getElementStyle(this.elem, 'left')); if(isNaN(pX)){ pX = this.elem.offsetLeft; } this.elem.style.left = pX + "px"; this.currentLeft = pX; } if(this.position.Y){ this.elem.style.top = this.position.Y + 'px'; this.currentTop = this.position.Y; }else{ var pY = parseInt(Style.getElementStyle(this.elem, 'top')); if(isNaN(pY)){ pY = this.elem.offsetTop; } this.elem.style.top = pY + 'px'; this.currentTop = pY; } } } Movement.prototype.move = function(){ var stop = false; var sX = this.stepX; var sY = this.stepY; //checking for stop flag //time if(this.time > 0){ this.timeElapsed += this.interval; if(this.timeElapsed > this.time){ stop = true; } } //distance if(this.distance > 0){ var hypo = Math.sqrt(this.stepX*this.stepX + this.stepY*this.stepY); if((this.distancePassed + hypo) >= this.distance){ var new_hypo = this.distance - this.distancePassed; var h_diff = hypo / new_hypo; var newStepY = Math.round(this.stepY / h_diff); var newStepX = Math.round(this.stepX / h_diff); hypo = new_hypo; sX = newStepX; sY = newStepY; stop = true; } this.distancePassed += hypo; } //position if(this.stopPos){ if(typeof this.stopPos.X != "undefined"){ if(sX > 0){ if((this.currentLeft + sX) > this.stopPos.X){ sX = this.stopPos.X - this.currentLeft; var s_diff = this.stepX / sX; sY = Math.round(this.stepY / s_diff); stop = true; } }else{ if((this.currentLeft + sX) < this.stopPos.X){ sX = this.stopPos.X - this.currentLeft; var s_diff = this.stepX / sX; sY = Math.round(this.stepY / s_diff); stop = true; } } }else if(typeof this.stopPos.Y != "undefined"){ if((this.currentTop + sY) > this.stopPos.Y){ sY = this.stopPos.Y - this.currentTop; var s_diff = this.stepY / sY; sX = Math.round(this.stepX / s_diff); stop = true; } } } this.currentLeft += sX; this.currentTop += sY; this.elem.style.left = this.currentLeft + 'px'; this.elem.style.top = this.currentTop + 'px'; // alert(stop); if(this.onmove){ this.onmove.func.apply(this.onmove.oThis, this.onmove.args); } if(stop){ this.stop(); } } Movement.prototype.start = function(){ this.isMoving = true; var oThis = this; oThis.move.call(oThis); this.objInterval = window.setInterval( function(){ oThis.move.call(oThis) }, this.interval ); if(this.onstart){ this.onstart.func.apply(this.onstart.oThis, this.onstart.args); } } Movement.prototype.stop = function(){ var oInt = this.objInterval; this.isMoving = false; window.clearInterval(oInt); if(this.onstop){ this.onstop.func.apply(this.onstop.oThis, this.onstop.args); } } Movement.prototype.toggle = function(){ if(this.isMoving) this.stop(); this.start(); } Movement.prototype.Debug = { out: function(what, inline){ var oDiv = document.createElement('div'); oDiv.appendChild(document.createTextNode(what)); if(inline){ oDiv.style.display = 'inline'; } document.body.appendChild(oDiv); } }