/* <![CDATA[ */
var mouse_offset = 2,
    Clock = {};
(function($) {
var 
Point = function(x, y) {
    this.x = x;
    this.y = y;
};

Clock = {
    
    CENTRE: new Point(0, 0),
    MINUTE:  0,
    HOUR: 1,
    
    spinning: false,
    
    minute_handle: null,
    hour_handle: null,
    display: null,
    previous_hour: 0,
    
    active_hand: null,
    active_handle: null,
    
    offset: [],
    position: [new Point(0, 0), new Point(0, 0)],
    rotation: [180, 180],
    difference: 0,
    
    mouse: [new Point(0, 0), new Point(0, 0)],
    
    init: function()
    {
        this.minute_handle = $('#minute');
        this.hour_handle = $('#hour');
        this.display = $('display-time');
        
        this._setOffSets();
        
        this._bindEvents(this.minute_handle);
        this._bindEvents(this.hour_handle); 
        
        this._bindMouseUp($(document));
        this._setTime();
    },
    
    bindDocument: function()
    {
        $(document).bind('mousemove', function(e) {
            e.preventDefault();  
            var x = -1 * (Math.round(Clock.active_handle.width() / 2 + 0.01) - (e.pageX - Clock.offset[Clock.active_hand].left));
            var y = Math.round(Clock.active_handle.height()/2+0.01) - (e.pageY - Clock.offset[Clock.active_hand].top); 
            Clock.mouse[Clock.active_hand] = new Point(x,y); 
            if (Clock.spinning && Clock.active_handle) {
                var deg1 = Angle.betweenPoints(Clock.position[Clock.active_hand], Clock.CENTRE),
                    deg2 = Angle.betweenPoints(Clock.mouse[Clock.active_hand], Clock.CENTRE);
                    
                //console.log(deg2);
                Clock.difference = (deg1-deg2);
                //console.log(Clock.difference);
                    
                Clock.difference = Clock.difference < 0 ? Clock.difference + 360 : Clock.difference > 360 ? Clock.difference-360 : Clock.difference;
                Clock.difference = Clock.difference > 180 ? Clock.difference - 360 : Clock.difference;
                Clock.active_handle.rotate(Clock.rotation[Clock.active_hand] + Clock.difference);
                
                Clock.updateDisplay(Clock.rotation[Clock.active_hand] + Clock.difference);
            }
        });
    },
    
    unBindDocument: function()
    {
        $(document).unbind('mousemove');
    },
    
    updateDisplay: function(rotation)
    {
        
        var minute_angle = 0; 
            hour_angle =  0, 
            
            rotation = rotation < 0 ? rotation + 360 : rotation > 360 ? rotation-360 : rotation,
            
            minutes = 0,
            hours = 0,
            time = "";
        
        if (Clock.MINUTE == Clock.active_hand) {
            minute_angle = rotation;
            minutes = Math.round((minute_angle / 6));
            minutes = 60 == minutes ? 0 : minutes;
            document.getElementById('display-time-minutes').value = (10 > minutes ? '0' + minutes : minutes); 
        }
        else {
            hour_angle = rotation;
            hours = Math.floor((hour_angle / 30));
            hours = 12 < hours ? hours - 12 : hours;
            hours = 0 == hours ? 12 : hours;
            document.getElementById('display-time-hours').value = (10 > hours ? '0' + hours : hours);

            if (11 == Clock.previous_hour && 12 == hours || 12 == Clock.previous_hour && 11 == hours) {
                document.getElementById('display-time-meridiem').value = 'am' == document.getElementById('display-time-meridiem').value ? 'pm' : 'am';
            }

            
            Clock.previous_hour = hours;
        }
    },            
    
    _bindEvents: function(el)
    {
        this._bindMouseDown(el);
        this._bindMouseUp(el);
    },
    
    _bindMouseDown: function(el) 
    {
        el.bind("mousedown", function(e) {
            e.preventDefault();
            Clock.spinning = el.attr('id');
            
            Clock.active_handle = el;
            Clock.active_hand = 'minute' == Clock.spinning ? Clock.MINUTE : Clock.HOUR; 
            Clock.position[Clock.active_hand] = Clock.mouse[Clock.active_hand];
            //Clock.rotation[Clock.active_hand] = 180;
                        
            Clock.bindDocument();  
        });
    },
    
    _bindMouseUp: function(el) 
    {
        el.bind("mouseup", function(e) {
            e.preventDefault();
            Clock.spinning = false;
            //Clock.active_handle = null;
            Clock.position[Clock.active_hand] = new Point(0, 0);
            Clock.position[Clock.active_hand] = Clock.mouse[Clock.active_hand];
            Clock.rotation[Clock.active_hand] += Clock.difference;
            Clock.difference = 0;
            
            Clock.unBindDocument();
        });
    },
    
    _setOffSets: function()
    {
        Clock.offset[Clock.MINUTE] = Clock.minute_handle.offset();
        Clock.offset[Clock.HOUR] =  Clock.hour_handle.offset();
    },
    
    _setTime: function()
    {
        var meridiem = 'am'; 
            time = new Date();
        
        var minutes = time.getMinutes(),
            hours = time.getHours();
            
        if (hours > 12) {
            meridiem = 'pm';
            hours -= 12;
        }
        else if (hours == 12) {
            meridiem = 'pm';
        }
            
        
        $('#display-time-hours').val((hours < 10 ? '0' + hours : hours));
        $('#display-time-minutes').val((minutes < 10 ? '0' + minutes : minutes));
        $('#display-time-meridiem').val(meridiem);
        
        var minute_offset = 180 + (6 * minutes),
            hour_offset = 180 + (30 * hours) + ((minutes / 60) * 30);
        
        $('#hour').rotate(Clock.rotation[Clock.HOUR] + hour_offset);
        $('#minute').rotate(Clock.rotation[Clock.MINUTE] + minute_offset);
    }  
        
},

Angle = {
    TO_DEGREES: 180/Math.PI,
    
    get: function(dx,dy) 
    {
        var ang;
        if (dx!=0) {
            var rad = Math.atan(dy/dx) + (dx<0?Math.PI:0),
                ang = rad*this.TO_DEGREES;
                
            if (ang<0) {
                ang+=360;
            };
        } 
        else {
            ang = dy>0 ? 90 : 270;
        }
        return ang;
    },

    betweenPoints: function(p1, p2) 
    {
        
        var dx = p1.x-p2.x;
        var dy = p1.y-p2.y;
        
        return this.get(dx, dy);
    }
};

$(window).resize(function() {
    Clock._setOffSets();
});
})(jQuery);
          
/* ]]> */
        

