/**
 * @author Vlad Yakovlev (scorpix@design.ru)
 * @copyright Art.Lebedev Studio (http://www.artlebedev.ru)
 * @version 0.1 (15.11.2008)
 * @requires jQuery 1.2
 * @requires jTweener 0.2
 */
var buildingsAnimator = (function(images) {
	var blocks;

	var curSection = '';

	var isAnimate = false;

	var loadedImages = [];

	var loadedCount = 0;

	var documentLoaded = false;

	var transition = 'easeOutCubic';

	loadImages(images);

	$(function() {
		blocks = {
			sections: $('#content .filters li'),
			static: $('#content .scheme .static'),
			floor1Static: $('#content .scheme .floor1_static'),
			floor2Static: $('#content .scheme .floor2_static'),
			fromFloor1: $('#content .scheme .from_floor_1'),
			fromFloor2: $('#content .scheme .from_floor_2'),
			basement: $('#content .scheme .basement'),
			roof: $('#content .scheme .roof'),
			textFloor2: $('#content .scheme .text_floor2'),
			textFloor1: $('#content .scheme .text_floor1'),
			textRoof: $('#content .scheme .text_roof'),
			textBasement: $('#content .scheme .text_basement')
		};

		documentLoaded = true;

		if (loadedCount == images.length) {
			init();
		}
	});

	function init() {
		blocks.sections.click(onChangeSection);
		changeSection('flats');
	}

	/**
	 * Событие при выборе секции.
	 */
	function onChangeSection() {
		if (isAnimate) {
			return;
		}

		/** @type {Array} */
		var classes = $(this).attr('class').split(' ');
		/** @type {String} */
		var section;
		var prefix = 'type_';

		for (var i = 0; i < classes.length; i++) {
			if (prefix == classes[i].substr(0, prefix.length)) {
				section = classes[i].substr(prefix.length);
			}
		}

		section && changeSection(section);
	}

	function changeSection(section) {
		if (isAnimate) {
			return;
		}

		isAnimate = true;

		if (curSection == section) {
			curSection = '';
			blocks.sections.filter('.selected').removeClass('selected');
			animate(section, false, function() {
				isAnimate = false;
			});
		} else if (curSection) {
			animate(curSection, false, function() {
				curSection = section;
				blocks.sections.filter('.selected').removeClass('selected');
				blocks.sections.filter('.type_' + curSection).addClass('selected');
				animate(curSection, true, function() {
					isAnimate = false;
				});
			});
		} else {
			curSection = section;
			blocks.sections.filter('.selected').removeClass('selected');
			blocks.sections.filter('.type_' + curSection).addClass('selected');
			animate(curSection, true, function() {
				isAnimate = false;
			});
		}
	}

	function loadImages(images) {
		for (var i = 0; i < images.length; i++) {
			var image = new Image();
			loadedImages.push(image);
			image.onload = function() {
				loadedCount++;

				if (loadedCount == images.length && documentLoaded) {
					init();
				}
			};
			image.src = images[i];
		}
	}

	/**
	 * Анимириут блоки.
	 * @param {String} type Тип секции, которую нужно анимировать.
	 * @param {Boolean} show Показать секцию (<code>true</code>) или вернуть на собранный дом (<code>false</code>).
	 * @param {Function} postFunc Функция, которую нужно выполнить после завершения анимации (опциональный параметр).
	 */
	function animate(type, show, postFunc) {
		if ('parking' == type) {
			if (show) {
				blocks.basement.css('opacity', 0).removeClass('hidden');

				if ($.browser.msie) {
					jTweener.addTween(blocks.basement, {
						opacity: 1,
						time: 1.5,
						onComplete: function() {
							blocks.textBasement.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				} else {
					blocks.textBasement.css('opacity', 0).removeClass('hidden');
					jTweener.addTween(blocks.basement, {
						opacity: 1,
						time: 1.5
					});
					jTweener.addTween(blocks.textBasement, {
						opacity: 1,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				}
			} else {
				if ($.browser.msie) {
					blocks.textBasement.addClass('hidden');
					jTweener.addTween(blocks.basement, {
						opacity: 0,
						time: 1.5,
						onComplete: function() {
							blocks.basement.addClass('hidden').css('opacity', '');
							postFunc && postFunc();
						}
					});
				} else {
					jTweener.addTween(blocks.textBasement, {
						opacity: 0,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							blocks.textBasement.addClass('hidden').css('opacity', '');
						}
					});
					jTweener.addTween(blocks.basement, {
						opacity: 0,
						time: 1.5,
						onComplete: function() {
							blocks.basement.addClass('hidden').css('opacity', '');
							postFunc && postFunc();
						}
					});
				}
			}
			/*
			if (show) {
				blocks.basement.css('opacity', 0).removeClass('hidden');
				jTweener.addTween(blocks.basement, {
					opacity: 1,
					time: 1.5,
					onComplete: function() {
						postFunc && postFunc();
					}
				});
			} else {
				jTweener.addTween(blocks.basement, {
					opacity: 0,
					time: 1.5,
					onComplete: function() {
						blocks.basement.addClass('hidden').css('opacity', '');
						postFunc && postFunc();
					}
				});
			}*/
		} else if ('apartment' == type) {
			if (show) {
				if ($.browser.msie) {
					jTweener.addTween(blocks.fromFloor1, {
						top: parseInt(blocks.fromFloor1.css('top')) - 47,
						time: 0.6,
						transition: transition,
						onComplete: function() {
							blocks.textFloor1.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				} else {
					blocks.textFloor1.css('opacity', 0).removeClass('hidden');
					jTweener.addTween(blocks.fromFloor1, {
						top: parseInt(blocks.fromFloor1.css('top')) - 47,
						time: 0.6,
						transition: transition
					});
					jTweener.addTween(blocks.textFloor1, {
						opacity: 1,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				}
			} else {
				if ($.browser.msie) {
					blocks.textFloor1.addClass('hidden');
					jTweener.addTween(blocks.fromFloor1, {
						top: parseInt(blocks.fromFloor1.css('top')) + 47,
						time: 0.6,
						transition: transition,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				} else {
					jTweener.addTween(blocks.textFloor1, {
						opacity: 0,
						time: 1,
						onComplete: function() {
							blocks.textFloor1.addClass('hidden').css('opacity', '');
						}
					});
					jTweener.addTween(blocks.fromFloor1, {
						top: parseInt(blocks.fromFloor1.css('top')) + 47,
						time: 0.6,
						transition: transition,
						delay: 0.3,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				}
			}
		} else if ('flats' == type) {
			if (show) {
				blocks.floor2Static.removeClass('hidden');
				blocks.fromFloor2.removeClass('hidden');
				blocks.floor1Static.addClass('hidden');
				blocks.fromFloor1.addClass('hidden');

				if ($.browser.msie) {
					jTweener.addTween(blocks.fromFloor2, {
						top: parseInt(blocks.fromFloor2.css('top')) - 81,
						time: 0.6,
						transition: transition,
						onComplete: function() {
							blocks.textFloor2.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				} else {
					blocks.textFloor2.css('opacity', 0).removeClass('hidden');
					jTweener.addTween(blocks.fromFloor2, {
						top: parseInt(blocks.fromFloor2.css('top')) - 81,
						time: 0.6,
						transition: transition
					});
					jTweener.addTween(blocks.textFloor2, {
						opacity: 1,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				}
			} else {
				if ($.browser.msie) {
					blocks.textFloor2.addClass('hidden');
					jTweener.addTween(blocks.fromFloor2, {
						top: parseInt(blocks.fromFloor2.css('top')) + 81,
						time: 0.6,
						transition: transition,
						onComplete: function() {
							blocks.fromFloor1.removeClass('hidden');
							blocks.fromFloor2.addClass('hidden');
							blocks.floor2Static.addClass('hidden');
							blocks.floor1Static.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				} else {
					jTweener.addTween(blocks.textFloor2, {
						opacity: 0,
						time: 1,
						onComplete: function() {
							blocks.textFloor2.addClass('hidden').css('opacity', '');
						}
					});
					jTweener.addTween(blocks.fromFloor2, {
						top: parseInt(blocks.fromFloor2.css('top')) + 81,
						time: 0.6,
						transition: transition,
						delay: 0.3,
						onComplete: function() {
							blocks.fromFloor1.removeClass('hidden');
							blocks.fromFloor2.addClass('hidden');
							blocks.floor2Static.addClass('hidden');
							blocks.floor1Static.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				}
			}
		} else if ('cut' == type) {
			if (show) {
				blocks.roof.css('opacity', 0).removeClass('hidden');

				if ($.browser.msie) {
					jTweener.addTween(blocks.roof, {
						opacity: 1,
						time: 1,
						onComplete: function() {
							blocks.textRoof.removeClass('hidden');
							postFunc && postFunc();
						}
					});
				} else {
					blocks.textRoof.css('opacity', 0).removeClass('hidden');
					jTweener.addTween(blocks.roof, {
						opacity: 1,
						time: 1
					});
					jTweener.addTween(blocks.textRoof, {
						opacity: 1,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							postFunc && postFunc();
						}
					});
				}
			} else {
				if ($.browser.msie) {
					blocks.textRoof.addClass('hidden');
					jTweener.addTween(blocks.roof, {
						opacity: 0,
						time: 1,
						onComplete: function() {
							blocks.roof.addClass('hidden').css('opacity', '');
							postFunc && postFunc();
						}
					});
				} else {
					jTweener.addTween(blocks.textRoof, {
						opacity: 0,
						time: 1,
						onComplete: function() {
							blocks.textRoof.addClass('hidden').css('opacity', '');
						}
					});
					jTweener.addTween(blocks.roof, {
						opacity: 0,
						time: 1,
						delay: 0.3,
						onComplete: function() {
							blocks.roof.addClass('hidden').css('opacity', '');
							postFunc && postFunc();
						}
					});
				}
			}
		}
	}
});
