/**
 * @author Vlad Yakovlev (red.scorpix@gmail.com)
 * @link www.scorpix.ru
 * @requires jQuery
 * @requires jCommon
 */

var rotater = (function() {

	/** Интервал в миллисекундах — шаг анимации. */
	var interval = Math.round(1000 / 30);

	/** Функции анимации прелоадера. */
	var animateFuncs = {
		ease: function(time, step, borderTime) {
			return Math.round(time * step / borderTime) % 360;
		},
		bounce: function(time, step, borderTime) {
			time = time % borderTime;
			var k = 0 > time - borderTime / 2 ? time : borderTime - time;
			return Math.round(k * step / 1000) % 360;
		},
		inOutBounce: function(time, step, borderTime) {
			time = time % borderTime;
			time = 0 > time - borderTime / 2 ? time : borderTime - time;
			var maxAngle = borderTime * step / 1000 / 2;

			if ((time /= borderTime / 4) < 1) {
				return Math.round(maxAngle / 2 * time * time * time) % 360;
			} else {
				return Math.round(maxAngle / 2 * ((time -= 2) * time * time + 2)) % 360;
			}
		},
		inCubic: function(time, step, borderTime) {
			var maxAngle = borderTime * step / 1000;

			if (time < borderTime) {
				return Math.round(maxAngle * (time /= borderTime) * time * time) % 360;
			} else {
				var t1 = 1 - interval / borderTime;
				var t2 = 1;
				step = maxAngle * (t2 * t2 * t2 - t1 * t1 * t1);
				return Math.round(maxAngle + (time - borderTime) * step / 1000 * interval) % 360;
			}
		}
	};

	/**
	 * @param {String|Element|jQuery} preloaderEl
	 * @param {Function} func
	 * @param {String} color
	 * @param {Number} angleSpeed
	 * @param {Number} borderTime
	 */
	function preloaderItem(number, containerEl, color, func, angleSpeed, borderTime) {

		var
			/**
			 * @type {Element}
			 */
			pathEl,
			width,
			height,
			curTime = 0,
			timeoutId,
			createShape,
			updateShape;

		if ($c.support.svg) {
			if (1 == number) {
				createShape = function() {
					var rootEl = document.createElementNS($c.ns.svg, 'svg');
					rootEl.setAttribute('version', '1.1');
					rootEl.setAttribute('viewBox', '0 0 6120 6120');
					rootEl.setAttribute('width', width);
					rootEl.setAttribute('height', height);
					containerEl.appendChild(rootEl);

					pathEl = document.createElementNS($c.ns.svg, 'path');
					pathEl.setAttribute('fill', color);
					pathEl.setAttribute('d', 'M 6005,2882 C 5950,2867 5751,2786 5733,2650 C 5712,2490 5935,2340 5937,2339 C 5993,2303 6019,2235 6001,2172 C 5983,2108 5926,2064 5860,2061 C 5752,2058 5547,2011 5524,1931 C 5487,1804 5595,1603 5636,1543 C 5670,1493 5671,1428 5638,1377 C 5605,1326 5545,1299 5485,1310 C 5485,1310 5417,1321 5334,1321 C 5198,1321 5124,1292 5099,1264 C 5045,1203 5072,998 5111,880 C 5130,823 5113,761 5069,720 C 5025,680 4961,670 4906,695 C 4857,717 4705,774 4591,774 C 4544,774 4523,765 4517,760 C 4461,726 4421,535 4423,386 C 4424,325 4387,269 4331,245 C 4275,221 4209,234 4166,277 C 4110,333 3961,449 3860,449 C 3849,449 3839,448 3831,445 C 3738,413 3653,228 3625,115 C 3610,54 3560,9 3498,1 C 3436,-7 3376,24 3346,78 C 3289,181 3162,334 3068,334 C 2971,332 2840,179 2781,76 C 2750,20 2686,-10 2623,1 C 2560,12 2511,62 2501,125 C 2492,183 2431,406 2306,441 C 2293,445 2280,447 2265,447 C 2146,447 2006,324 1967,281 C 1923,233 1854,218 1795,244 C 1736,270 1699,331 1705,395 C 1713,485 1699,702 1607,760 C 1585,774 1554,781 1516,781 C 1406,781 1273,721 1231,699 C 1172,666 1100,677 1052,724 C 1004,771 994,844 1026,903 C 1027,905 1142,1123 1023,1267 C 980,1320 917,1345 833,1345 C 746,1345 671,1317 671,1317 C 609,1292 537,1312 496,1365 C 455,1418 454,1492 494,1546 C 536,1603 646,1796 604,1925 C 572,2022 361,2058 269,2059 C 202,2060 144,2104 126,2168 C 108,2232 134,2301 190,2337 C 192,2338 407,2484 393,2655 C 382,2786 184,2866 116,2884 C 48,2902 1,2963 3,3033 C 4,3103 54,3163 123,3178 C 126,3179 373,3234 394,3411 C 416,3595 201,3718 200,3718 C 139,3752 109,3823 127,3890 C 145,3957 208,4004 278,4001 L 285,4001 C 312,4001 548,4005 610,4148 C 678,4305 507,4501 506,4502 C 459,4553 453,4629 491,4687 C 530,4745 602,4769 667,4746 C 668,4746 742,4720 829,4720 C 917,4720 981,4745 1025,4796 C 1141,4932 1026,5158 1025,5159 C 993,5220 1006,5295 1057,5342 C 1108,5388 1185,5393 1242,5355 C 1277,5332 1391,5270 1496,5270 C 1539,5270 1575,5280 1606,5300 C 1746,5389 1707,5650 1706,5652 C 1694,5717 1727,5783 1787,5813 C 1846,5843 1918,5830 1964,5782 C 2006,5738 2150,5612 2194,5612 C 2207,5612 2219,5614 2230,5618 C 2339,5653 2413,5852 2432,5942 C 2445,6004 2494,6052 2557,6061 C 2565,6062 2572,6063 2580,6063 C 2634,6063 2686,6034 2712,5985 C 2769,5882 2896,5729 2990,5729 C 3087,5731 3218,5884 3277,5987 C 3308,6042 3370,6071 3431,6062 C 3493,6053 3543,6005 3556,5943 C 3575,5851 3648,5652 3755,5620 C 3766,5617 3777,5615 3790,5615 C 3905,5615 4053,5740 4095,5784 C 4140,5830 4209,5843 4268,5816 C 4327,5789 4361,5727 4354,5662 C 4344,5571 4352,5355 4442,5308 C 4469,5294 4503,5287 4544,5287 C 4682,5287 4829,5364 4830,5364 C 4885,5393 4952,5386 4999,5346 C 5046,5306 5064,5241 5044,5182 C 5003,5063 4978,4864 5035,4797 C 5066,4761 5148,4739 5256,4739 C 5342,4739 5414,4753 5415,4753 C 5479,4766 5544,4736 5576,4678 C 5608,4621 5599,4550 5555,4502 C 5553,4500 5377,4303 5449,4155 C 5522,4004 5773,4002 5785,4002 C 5786,4002 5788,4002 5789,4002 C 5853,4002 5909,3962 5931,3902 C 5953,3840 5932,3771 5880,3732 C 5764,3644 5647,3496 5663,3430 C 5686,3336 5858,3219 5968,3170 C 6026,3144 6062,3085 6057,3021 C 6055,2966 6063,2934 6014,2888 L 6005,2882 z');
					rootEl.appendChild(pathEl);
				};
			} else {
				createShape = function() {
					var rootEl = document.createElementNS($c.ns.svg, 'svg');
					rootEl.setAttribute('version', '1.1');
					rootEl.setAttribute('viewBox', '0 0 6120 6120');
					rootEl.setAttribute('width', width);
					rootEl.setAttribute('height', height);
					containerEl.appendChild(rootEl);

					pathEl = document.createElementNS($c.ns.svg, 'polygon');
					pathEl.setAttribute('fill', color);
					pathEl.setAttribute('points', '3060.012,329.505 3379.818,4.689 3625.524,389.162 4005.587,138.381 4166.311,565.648 4589.992,399.773 4658.791,851.266 5107.563,777.488 5081.365,1233.6 5535.593,1255.085 5415.637,1695.705 5855.465,1811.595 5646.86,2217.552 6053.078,2422.969 5765.07,2776.462 6120,3061.99 5765.07,3347.605 6053.078,3701.098 5646.86,3906.429 5855.465,4312.472 5415.637,4428.361 5535.593,4868.896 5081.365,4890.467 5107.563,5346.578 4658.791,5272.8 4589.992,5724.293 4166.311,5558.42 4005.587,5985.686 3625.524,5734.905 3379.818,6119.382 3060.012,5794.562 2740.161,6119.382 2494.495,5734.905 2114.39,5985.686 1953.688,5558.42 1530.004,5724.293 1461.252,5272.8 1012.436,5346.578 1038.632,4890.467 584.385,4868.896 704.385,4428.361 264.556,4312.472 473.097,3906.429 66.878,3701.098 354.928,3347.605 0,3061.99 354.928,2776.462 66.878,2422.797 473.097,2217.552 264.556,1811.595 704.385,1695.705 584.385,1255.085 1038.632,1233.6 1012.436,777.488 1461.252,851.266 1530.004,399.773 1953.688,565.648 2114.39,138.381 2494.495,389.162 2740.161,4.689');
					rootEl.appendChild(pathEl);
				};
			}

			updateShape = function(angle) {
				var offsetX = Math.round(3060);
				var offsetY = Math.round(3060);
				pathEl.setAttribute('transform', 'rotate(' + angle + ',' + offsetX + ',' + offsetY + ')');
			};
		} else {
			if (1 == number) {
				createShape = function createVml() {
					pathEl = document.createElement('v:shape');
					pathEl.style.width = width + 'px';
					pathEl.style.height = height + 'px';
					pathEl.style.left = '0px';
					pathEl.style.top = '0px';
					pathEl.style.position = 'absolute';
					pathEl.setAttribute('coordsize', '6120 6120');
					pathEl.setAttribute('path', 'M 6005,2882 C 5950,2867 5751,2786 5733,2650 C 5712,2490 5935,2340 5937,2339 C 5993,2303 6019,2235 6001,2172 C 5983,2108 5926,2064 5860,2061 C 5752,2058 5547,2011 5524,1931 C 5487,1804 5595,1603 5636,1543 C 5670,1493 5671,1428 5638,1377 C 5605,1326 5545,1299 5485,1310 C 5485,1310 5417,1321 5334,1321 C 5198,1321 5124,1292 5099,1264 C 5045,1203 5072,998 5111,880 C 5130,823 5113,761 5069,720 C 5025,680 4961,670 4906,695 C 4857,717 4705,774 4591,774 C 4544,774 4523,765 4517,760 C 4461,726 4421,535 4423,386 C 4424,325 4387,269 4331,245 C 4275,221 4209,234 4166,277 C 4110,333 3961,449 3860,449 C 3849,449 3839,448 3831,445 C 3738,413 3653,228 3625,115 C 3610,54 3560,9 3498,1 C 3436,-7 3376,24 3346,78 C 3289,181 3162,334 3068,334 C 2971,332 2840,179 2781,76 C 2750,20 2686,-10 2623,1 C 2560,12 2511,62 2501,125 C 2492,183 2431,406 2306,441 C 2293,445 2280,447 2265,447 C 2146,447 2006,324 1967,281 C 1923,233 1854,218 1795,244 C 1736,270 1699,331 1705,395 C 1713,485 1699,702 1607,760 C 1585,774 1554,781 1516,781 C 1406,781 1273,721 1231,699 C 1172,666 1100,677 1052,724 C 1004,771 994,844 1026,903 C 1027,905 1142,1123 1023,1267 C 980,1320 917,1345 833,1345 C 746,1345 671,1317 671,1317 C 609,1292 537,1312 496,1365 C 455,1418 454,1492 494,1546 C 536,1603 646,1796 604,1925 C 572,2022 361,2058 269,2059 C 202,2060 144,2104 126,2168 C 108,2232 134,2301 190,2337 C 192,2338 407,2484 393,2655 C 382,2786 184,2866 116,2884 C 48,2902 1,2963 3,3033 C 4,3103 54,3163 123,3178 C 126,3179 373,3234 394,3411 C 416,3595 201,3718 200,3718 C 139,3752 109,3823 127,3890 C 145,3957 208,4004 278,4001 L 285,4001 C 312,4001 548,4005 610,4148 C 678,4305 507,4501 506,4502 C 459,4553 453,4629 491,4687 C 530,4745 602,4769 667,4746 C 668,4746 742,4720 829,4720 C 917,4720 981,4745 1025,4796 C 1141,4932 1026,5158 1025,5159 C 993,5220 1006,5295 1057,5342 C 1108,5388 1185,5393 1242,5355 C 1277,5332 1391,5270 1496,5270 C 1539,5270 1575,5280 1606,5300 C 1746,5389 1707,5650 1706,5652 C 1694,5717 1727,5783 1787,5813 C 1846,5843 1918,5830 1964,5782 C 2006,5738 2150,5612 2194,5612 C 2207,5612 2219,5614 2230,5618 C 2339,5653 2413,5852 2432,5942 C 2445,6004 2494,6052 2557,6061 C 2565,6062 2572,6063 2580,6063 C 2634,6063 2686,6034 2712,5985 C 2769,5882 2896,5729 2990,5729 C 3087,5731 3218,5884 3277,5987 C 3308,6042 3370,6071 3431,6062 C 3493,6053 3543,6005 3556,5943 C 3575,5851 3648,5652 3755,5620 C 3766,5617 3777,5615 3790,5615 C 3905,5615 4053,5740 4095,5784 C 4140,5830 4209,5843 4268,5816 C 4327,5789 4361,5727 4354,5662 C 4344,5571 4352,5355 4442,5308 C 4469,5294 4503,5287 4544,5287 C 4682,5287 4829,5364 4830,5364 C 4885,5393 4952,5386 4999,5346 C 5046,5306 5064,5241 5044,5182 C 5003,5063 4978,4864 5035,4797 C 5066,4761 5148,4739 5256,4739 C 5342,4739 5414,4753 5415,4753 C 5479,4766 5544,4736 5576,4678 C 5608,4621 5599,4550 5555,4502 C 5553,4500 5377,4303 5449,4155 C 5522,4004 5773,4002 5785,4002 C 5786,4002 5788,4002 5789,4002 C 5853,4002 5909,3962 5931,3902 C 5953,3840 5932,3771 5880,3732 C 5764,3644 5647,3496 5663,3430 C 5686,3336 5858,3219 5968,3170 C 6026,3144 6062,3085 6057,3021 C 6055,2966 6063,2934 6014,2888 L 6005,2882 z');
					pathEl.setAttribute('fillcolor', color);
					pathEl.setAttribute('stroked', false);
					containerEl.appendChild(pathEl);
				};
			} else {
				createShape = function createVml() {
					pathEl = document.createElement('v:group');
					pathEl.style.width = width + 'px';
					pathEl.style.height = height + 'px';
					pathEl.style.left = '0px';
					pathEl.style.top = '0px';
					pathEl.style.position = 'absolute';
					pathEl.setAttribute('coordsize', '6120 6120');
					containerEl.appendChild(pathEl);

					var pEl = document.createElement('v:polyline');
					pEl.setAttribute('points', '3060.012,329.505 3379.818,4.689 3625.524,389.162 4005.587,138.381 4166.311,565.648 4589.992,399.773 4658.791,851.266 5107.563,777.488 5081.365,1233.6 5535.593,1255.085 5415.637,1695.705 5855.465,1811.595 5646.86,2217.552 6053.078,2422.969 5765.07,2776.462 6120,3061.99 5765.07,3347.605 6053.078,3701.098 5646.86,3906.429 5855.465,4312.472 5415.637,4428.361 5535.593,4868.896 5081.365,4890.467 5107.563,5346.578 4658.791,5272.8 4589.992,5724.293 4166.311,5558.42 4005.587,5985.686 3625.524,5734.905 3379.818,6119.382 3060.012,5794.562 2740.161,6119.382 2494.495,5734.905 2114.39,5985.686 1953.688,5558.42 1530.004,5724.293 1461.252,5272.8 1012.436,5346.578 1038.632,4890.467 584.385,4868.896 704.385,4428.361 264.556,4312.472 473.097,3906.429 66.878,3701.098 354.928,3347.605 0,3061.99 354.928,2776.462 66.878,2422.797 473.097,2217.552 264.556,1811.595 704.385,1695.705 584.385,1255.085 1038.632,1233.6 1012.436,777.488 1461.252,851.266 1530.004,399.773 1953.688,565.648 2114.39,138.381 2494.495,389.162 2740.161,4.689');
					pEl.setAttribute('fillcolor', color);
					pEl.setAttribute('stroked', false);
					pathEl.appendChild(pEl);
				};
			}

			updateShape = function updateVml(angle) {
				pathEl.style.rotation = angle;
			};
		}

		init();

		/**
		 * Инициализация объекта.
		 * @constructor
		 */
		function init() {
			if ('string' == typeof containerEl) {
				containerEl = document.getElementById(containerEl);
			} else if (containerEl.jquery) {
				containerEl = containerEl[0];
			}

			width = containerEl.clientWidth;
			height = containerEl.clientHeight;

			createShape();
		}

		/**
		 * Шаг анимации.
		 */
		function step() {
			curTime += interval;
			var angle = func(curTime, angleSpeed, borderTime);
			updateShape(angle);

			timeoutId = setTimeout(step, interval);
		}

		return {

			play: function() {
				if (timeoutId) return;
				timeoutId = setTimeout(step, interval);
			},

			pause: function() {
				clearTimeout(timeoutId);
				timeoutId = 0;
			},

			stop: function() {
				clearTimeout(timeoutId);
				timeoutId = 0;
				curTime = 0;
				updateShape(0);
			}

		};
	}

	return function(number, el, color, func, angleSpeed, borderTime) {
		if (undefined === func) {
			func = animateFuncs.ease;
		} else if ('string' == typeof func) {
			func = animateFuncs[func];
		}

		return preloaderItem(number, el, color, func, undefined === angleSpeed ? 360 : angleSpeed, undefined === borderTime ? 5000 : borderTime);
	}
})();

$(function() {
	$('#left_panel .offer').each(function() {
		var el = $(this);

		var rotaterItem = rotater($('body').hasClass('home') ? 1 : 2, el, el.css('background-color'));
		el.css('background-color', 'transparent');

		el.hover(function() {
			rotaterItem.play();
		}, function() {
			rotaterItem.pause();
		});
	});
});