var deg2radians = Math.PI * 2 / 360;
function doRotate(obj, deg) {
if(obj.filters) {
rad = deg * deg2radians ;
costheta = Math.cos(rad);
sintheta = Math.sin(rad);
try
{
// fDebug(deg+" : DXImageTransform.Microsoft.BasicImage"+Math.floor(deg%60/15));
 obj.style.filter="progid:DXImageTransform.Microsoft.BasicImage(rotation="+(Math.floor(deg%60/15))+");";
/*
 obj.filters.item("DXImageTransform.Microsoft.BasicImage").M11 = costheta;
 obj.filters.item("DXImageTransform.Microsoft.BasicImage").M12 = -sintheta;
 obj.filters.item("DXImageTransform.Microsoft.BasicImage").M21 = sintheta;
 obj.filters.item("DXImageTransform.Microsoft.BasicImage").M22 = costheta;
*/
}
catch(e){}
} else {
obj.style['-moz-transform'] = "rotate(" + deg + "deg)";
obj.style.MozTransform = "rotate(" + deg + "deg)";
obj.style['-webkit-transform'] = "rotate(" + deg + "deg)";
 if(doRotate.prototype.fOnChange)
  doRotate.prototype.fOnChange(deg);
}
}
doRotate.prototype.fOnChange=null;
function wheel(event){
var delta = 0;
if (!event) /* For IE. */
event = window.event;
if (event.wheelDelta) { /* IE/Opera. */
delta = event.wheelDelta/120;

/** In Opera 9, delta differs in sign as compared to IE.*/
if (window.opera)
delta = -delta;
} else if (event.detail) { /** Mozilla case. */
/** In Mozilla, sign of delta is different than in IE.
* Also, delta is multiple of 3.*/
delta = -event.detail;
}
/** If delta is nonzero, handle it.
* Basically, delta is now positive if wheel was scrolled up,
* and negative, if wheel was scrolled down.
*/
if (delta)
handleScroll(delta);
/** Prevent default actions caused by mouse wheel.
* That might be ugly, but we handle scrolls somehow
* anyway, so don't bother here..*/
if (event.preventDefault)
event.preventDefault();
event.returnValue = false;
}

function handleScroll(delta) {
window._x_wheel_rotation += -delta * 3;
window._x_wheel_rotation %= 360;
doRotate(document.getElementById("dvMap"), window._x_wheel_rotation);
//console.log('rotation: ' + window._x_wheel_rotation);
}
window._x_wheel_rotation = 0;

function remap_rotate(x, y, deg) {
var deg2radians = Math.PI * 2 / 360;
var rad = -deg * Math.PI * 2 / 360;
var costheta = Math.cos(rad);
var sintheta = Math.sin(rad);
var nx = costheta * x - sintheta * y;
var ny = sintheta * x + costheta * y;
//console.log('in: ' + x +', '+y+', '+ deg+' out:' +nx+', '+ny);
return [nx, ny];
}
// set up drag translation stuff for map
function setDragHandler(gmaps,gDrag) {
gmaps.Event.addListener(gDrag, 'dragstart', function(something){
window.originalX = something.clientX;
window.originalY = something.clientY;
window.gdX = window.gDragThing.left;
window.gdY = window.gDragThing.top;
});
gmaps.Event.addListener(gDrag, 'drag', function(something){
var newX = something.clientX - window.originalX;
var newY = something.clientY - window.originalY;
var newCoords = remap_rotate(newX, newY, window._x_wheel_rotation);
window.gDragThing.left = window.gdX + newCoords[0];
window.gDragThing.top = window.gdY + newCoords[1];
});
window.gDragThing = gDrag;
}
