﻿var isIE = /*@cc_on!@*/!1;
function rotationimg(img) {
    if (!img) return;
    this._img = typeof img == 'string' ? document.getElementById(img) : img;
    this.r = 1;
    //this.addBtn();
    this.bind();
}

rotationimg.prototype.addBtn = function() {
    if (!isIE) {
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        canvas.setAttribute('width', this._img.width);
        canvas.setAttribute('height', this._img.height);
        ctx.drawImage(this._img, 0, 0);
        this._ghost = this._img;
        this._img.parentNode.replaceChild(canvas, this._img);
        this._img = canvas;
    }
};
rotationimg.prototype.bind = function() {
    var _this = this;
    var objrotat = document.getElementById("rotat");

    if (objrotat) {
        if (window.attachEvent) {
            objrotat.attachEvent('onclick', rotation);
        } else {
            objrotat.addEventListener('click', rotation, false);
        }
    }

    function rotation() {
        if (!isIE) {
            alert("This function only supports the IE browser!");
            return;
        }

        if (_this.r > 3)
            _this.r = 0;

        if (isIE) {
            _this._img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(Rotation=' + _this.r + ')';
        }
        else {
            var ctx = _this._img.getContext('2d');

            switch (_this.r) {
                case 0:
                    _this._img.setAttribute('width', _this._ghost.width);
                    _this._img.setAttribute('height', _this._ghost.height);
                    ctx.drawImage(_this._ghost, 0, 0);
                    break;
                case 1:
                    _this._img.setAttribute('width', _this._ghost.height);
                    _this._img.setAttribute('height', _this._ghost.width);
                    ctx.rotate(90 * Math.PI / 180);
                    ctx.drawImage(_this._ghost, 0, -_this._ghost.height);
                    break;
                case 2:
                    _this._img.setAttribute('width', _this._ghost.width);
                    _this._img.setAttribute('height', _this._ghost.height);
                    ctx.rotate(180 * Math.PI / 180);
                    ctx.drawImage(_this._ghost, -_this._ghost.width, -_this._ghost.height);
                    break;
                case 3:
                    _this._img.setAttribute('width', _this._ghost.height);
                    _this._img.setAttribute('height', _this._ghost.width);
                    ctx.rotate(270 * Math.PI / 180);
                    ctx.drawImage(_this._ghost, -_this._ghost.width, 0);
                    break;
            }
        }
        _this.r++;
    }
};
