(function ($) {
	var balloons = {
	
		config : {
			logging : false,
			d2r : Math.PI * 2 / 360,
			min : -3,
			max : 3,
			start : -3,
			increment : 0.125,
			selector : '.balloon',
			intervalSpeed : 1000 / 15,
			property : null,
			items : {}
		},
		
		init : function (overrides) {
			this.config.property = this.getProperty();
			this.fire();	
			this.events();
		},
		
		rotate : function (elem, degree, flag) {
			if (this.config.property === 'filter') {
				var rad = degree * this.config.d2r,
					cos = Math.cos(rad),
					sin = Math.sin(rad),
					left, top;
				
				elem.filters.item(0).M11 = cos;
				elem.filters.item(0).M12 = -sin;
				elem.filters.item(0).M21 = sin;
				elem.filters.item(0).M22 = cos;
			} else if (this.config.property) {
				elem.style[this.config.property] = 'rotate(' + degree + 'deg)';
			}
		},
		
		constant : function (that) {
			var increment = this.config.increment,
				degree;

			switch (this.config.items['balloon' + $(that).index()].i) {
				case this.config.max : 
					this.config.items['balloon' + $(that).index()].direction = 'down'; 
					break;
				
				case this.config.min : 
					this.config.items['balloon' + $(that).index()].direction = 'up'; 
					break;
			}
			
			if (this.config.items['balloon' + $(that).index()].direction === 'up') {
				degree = this.config.items['balloon' + $(that).index()].i += increment;
			} else {
				degree = this.config.items['balloon' + $(that).index()].i -= increment;
			}
			
			this.rotate(that, degree);
		},
		
		reset : function (that) {
			var i = this.config.items['balloon' + $(that).index()].i,
				increment = this.config.increment,
				degree;
			
			if (i === 0) {
				degree = 0;
			} else if (i > 0) {
				degree = this.config.items['balloon' + $(that).index()].i -= increment;
			} else {
				degree = this.config.items['balloon' + $(that).index()].i += increment; 
			}
			
			this.rotate(that, degree);
			i === 0 ? clearInterval(this.config.intervalOut) : $.noop();
		},
		
		fire : function () {
			var that = this,
				delay = 750;
			
			$(this.config.selector).each(function (i) {
				var _this = this;
				
				that.config.items['balloon' + $(_this).index()] = {
					i : that.config.start,
					direction : i > 2 ? 'up' : 'down',
					interval : null
				};
				
				that.config.start += 2;
				
				if (that.config.property === 'filter' && !~_this.style.filter.indexOf('Matrix')) {
					_this.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(sizingMethod="auto expand")';
				}
				that.config.items['balloon' + $(_this).index()].interval = setInterval(function () {
					that.constant(_this);
				}, that.config.intervalSpeed);
			});
		},
		
		events : function () {
			var that = this;
			
			$(this.config.selector).bind('mouseover', function () {
				$(this).dequeue();
				$(this).animate({
					marginLeft : '-10px',
					marginTop : '-10px'
				}, 750);
			}).bind('mouseleave', function () {
				$(this).dequeue();
				$(this).animate({
					marginLeft : '0px',
					marginTop : '0px'
				}, 750);
			});
		},
		
		getProperty : function () {
			var options = ['transform', 'MozTransform', 'filter', 'WebkitTransform', 'OTransform'],
				style = document.createElement('div').style;
				
			for (var i in options) {
				if (typeof style[options[i]] === 'string') {
					return options[i];
				}
			}
			return false;
		},
		
		log : function (message) {
			if (this.config.logging && typeof console !== 'undefined') {
				console.log(message);
			}
		}
		
	};
	
	$(document).ready(function () {
		balloons.init();
	});
}(jQuery));
