﻿/**
* jquery rotate
* version 0.1
* author vermote mathieu
*/
(function($) {
    $.fn.rotate = function(options) {

        // init defaults
        // ============
        var defaults = {
            degrees: 0,
            duration: 0,
            speed: 1,
            direction: "CW",
            onComplete: null
        }
        var options = $.extend(defaults, options);

        // public methods
        // ==============
        function getTransformProperty(element) {
            var properties = ['transform', 'WebkitTransform', 'MozTransform', 'msTransform', 'OTransform'];
            var p;
            while (p = properties.shift()) {
                if (typeof element.style[p] != 'undefined') { return p; }
            }
            return false;
        }
        function getDegreesFromDiv(elementId) {
            //get the current degrees from the style attribute
            var result = 0;
            try {
                var startSubString = $("#" + elementId).attr('style').indexOf("rotate(") + 7;
                var endSubString;
                if ($("#" + elementId).attr('style').indexOf("rad)") != -1) {
                    endSubString = $("#" + elementId).attr('style').indexOf("rad)");
                    result = $("#" + elementId).attr('style').substring(startSubString, endSubString);
                    result = (result / (2 * Math.PI)) * 360; // Convert radians to degrees
                }
                else {
                    endSubString = $("#" + elementId).attr('style').indexOf("deg)");
                    result = $("#" + elementId).attr('style').substring(startSubString, endSubString);
                }
                if (result == "" || endSubString == -1) { return 0; }
                else { return parseFloat(result); }
            } catch (err) { return 0; }
        }
        function functionComplete() {
            //done

            if (options.onComplete)
                options.onComplete.call(this);
        }

        // return method
        // ==============
        return this.each(function() {
            if (options.degrees == 0 && options.duration == 0) {
                //if no options are given, then the dom-element keeps turning
                var div = document.getElementById($(this).attr("id"));
                var property = getTransformProperty(div);
                if (property) {
                    var d = 0;
                    setInterval(
                        function() {
                            if (options.direction == "CCW") {
                                d = d - options.speed; //COUNTER CLOCKWISE
                            } else {
                                d = d + options.speed; //CLOCKWISE
                            }
                            div.style[property] = 'rotate(' + (d % 360) + 'deg)';
                        },
                        50
                    );
                }
                functionComplete();
            }
            else if (options.duration != 0) {
                //if duration is not 0, then this function will only last as long as the specified duration (in milliseconds)
                //Speed doesn't matter in this function
                var div = document.getElementById($(this).attr("id"));
                var startDegrees = getDegreesFromDiv($(this).attr('id'));
                var endDegrees = 0;
                if (options.degrees.indexOf('=') == -1) { var endDegrees = options.degrees; }
                else { var endDegrees = (startDegrees + parseFloat(options.degrees.replace("=", ""))) % 360; }

                var timeInterval = 10;
                
                if (Math.abs(startDegrees - (options.degrees % 360)) > 180) {
                    timeInterval = Math.abs(options.duration / (Math.round(startDegrees - (options.degrees % 360)) - 360))
                    //alert("Math.abs(" + options.duration + " / (" + parseFloat(options.degrees % 360) + "-" + startDegrees + "))=" + timeInterval);
                } else {
                    timeInterval = Math.abs(options.duration / (startDegrees - (options.degrees % 360)))
                }
                //alert(timeInterval);



                var property = getTransformProperty(div);
                if (property) {
                    var d = startDegrees;
                    var rotationInterval = setInterval(
                        function() {
                            if (options.direction == "CCW") {
                                if (d == 0) { d = 360; }
                                d = d - options.speed; //COUNTER CLOCKWISE
                            } else {
                                if (d == 360) { d = 0; }
                                d = d + options.speed; //CLOCKWISE
                            }
                            div.style[property] = 'rotate(' + (d % 360) + 'deg)';

                            if (Math.round(d) % 360 == Math.round(endDegrees) % 360)
                            { functionComplete(); clearInterval(rotationInterval); }

                        },
                        timeInterval
                    );
                }
            }
            else {
                var div = document.getElementById($(this).attr("id"));
                var property = getTransformProperty(div);
                if (property) {
                    div.style[property] = 'rotate(' + (options.degrees) + 'deg)';
                }
                functionComplete();
            }

        });
    }
})(jQuery);


