function Browser()
{
}

Browser.prototype.offsetX = function (event) {
    // Return x-offset of the cursor from the event element
    return this.clientX(event) - this.eventElement(event).offsetLeft;
};

Browser.prototype.offsetY = function (event) {
    // Return the y-offset of the cursor from the event element
    return this.clientY(event) - this.eventElement(event).offsetTop;
};

Browser.prototype.compatCallback = function (obj, prop) {
    return function () {
	return obj[prop];
    }
};



// Mozilla-style browser

function MozBrowser() { }

MozBrowser.prototype = new Browser;

MozBrowser.prototype.clientX = function (event) {
    // Return the x-offset of the cursor from the window
    return event.clientX + window.scrollX;
};

MozBrowser.prototype.clientY = function(event) {
    // Return the y-offset of the cursor from the window
    return event.clientY + window.scrollY;
};

MozBrowser.prototype.inhibitDefault = function (event) {
    return event.preventDefault();
}

MozBrowser.prototype.trapDragEvents = function () {
    document.addEventListener('mousemove', dragDo, true);
    document.addEventListener('mouseup', dragEnd, true);
};

MozBrowser.prototype.untrapDragEvents = function () {
    document.removeEventListener('mousemove', dragDo, true);
    document.removeEventListener('mouseup', dragEnd, true);
};

MozBrowser.prototype.eventElement = function (event) {
    return event.target;
};

MozBrowser.prototype.newXMLHttpRequest = function () {
    return new XMLHttpRequest();
};

MozBrowser.prototype.updateImage = function(img, source, width, height) {
    img.style.width = width + 'px';
    img.style.height = height + 'px';
    img.src = source;
}    

MozBrowser.prototype.addImage = function (parent, source, width, height) {
    var img = document.createElement('img');
    img.style.position = 'absolute';
    this.updateImage(img, source, width, height);
    parent.appendChild(img);
    img.getWidth = browser.compatCallback(img, 'width');
    img.getHeight = browser.compatCallback(img, 'height');

    return img;
};


MozBrowser.prototype.updatePNG = function (node, source, width, height) {
    node.style['background'] = 'url(' + source + ')';
    node.style.width = width + 'px';
    node.style.height = height + 'px';
};


MozBrowser.prototype.clearPNG = function (node) {
    node.style.width = '0px';
    node.style.height = '0px';
    node.style.background = null;
}


// MSIE-style browser

function MSIEBrowser() { }

MSIEBrowser.prototype = new Browser;

MSIEBrowser.prototype.clientX = function(event) {
    return event.clientX + document.documentElement.scrollLeft
    + document.body.scrollLeft;
};

MSIEBrowser.prototype.clientY = function() {
    return event.clientY + document.documentElement.scrollTop
    + document.body.scrollTop;
};

MSIEBrowser.prototype.inhibitDefault = function (event) {
    event.cancelBubble = true;
    event.returnValue = false;
};

MSIEBrowser.prototype.trapDragEvents = function () {
    document.attachEvent('onmousemove', dragDo);
    document.attachEvent('onmouseup', dragEnd);
};

MSIEBrowser.prototype.untrapDragEvents = function () {
    document.detachEvent('onmousemove', dragDo);
    document.detachEvent('onmouseup', dragEnd);
};

MSIEBrowser.prototype.eventElement = function (event) {
    return event.srcElement;
};

MSIEBrowser.prototype.newXMLHttpRequest = function () {
    return new ActiveXObject("Microsoft.XMLHTTP");
};

MSIEBrowser.prototype.updateImage = function (img, source, width, height) {
    img.style.position = 'absolute';
    img.style.fontSize = '1px';
    img.style.width = width + 'px';
    img.style.height = height + 'px';
    img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + source + "',sizingMethod='scale')";
};

MSIEBrowser.prototype.addImage = function (parent, source, width, height) {
    var img = document.createElement('div');
    this.updateImage(img, source, width, height);
    parent.appendChild(img);
    img.getWidth = browser.compatCallback(img, 'clientWidth');
    img.getHeight = browser.compatCallback(img, 'clientHeight');
    return img;
};


MSIEBrowser.prototype.updatePNG = function (node, source, width, height) {
    node.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + source + "',sizingMethod='scale')";
    node.style.width = width + 'px';
    node.style.height = height + 'px';
};


MSIEBrowser.prototype.clearPNG = function (node) {
    node.style.width = '0px';
    node.style.height = '0px';
    node.style.filter = null;
};



if (navigator.userAgent.indexOf("MSIE") > 0) {
    window.browser = new MSIEBrowser();
} else {
    window.browser = new MozBrowser();
}
