/*****************************   anDeRan   ************************************/

jQuery(function($) {
	$(".x-button").hover(
		function () {
			if ($.browser.msie) {
				$(this).children("span").stop(true).fadeTo("fast", 0.50);
			}
			$(this).stop(true).fadeTo("fast", 0.50);
		},
		function () {
			if ($.browser.msie) {
				$(this).children("span").fadeTo("fast", 1);
			}
			$(this).fadeTo("fast", 1);
		}
	);
	$("h1").hover(
		function () {
			$(this).stop(true).fadeTo("fast", 0.50);
		},
		function () {
			$(this).fadeTo("fast", 1);
		}
	);

	if ($.browser.msie && $.browser.version < 8) {
		if ($.browser.version < 7) {
			$(".list-item").hover(
				function () {
					$(this).addClass("item-on");
				},
				function () {
					$(this).removeClass("item-on");
				}
			);

			$("table[summary='c2'] tr").each(function () {
				$(this).addClass("row-0");
			});
		}
		$("input.field, textarea").focus(
			function () {
				$(this).css({backgroundColor: '#80A5D4'});
			}
		);
		$("input.field, textarea").blur(
			function () {
				$(this).css({backgroundColor: '#7095C4'});
			}
		);
	}

	if (jQuery().fancybox) {
		$("div.fancybox a, a.fancybox").fancybox({
			'zoomOpacity'   : true,
			'overlayShow'   : false,
			'zoomSpeedIn'   : 500,
			'zoomSpeedOut'  : 500,
			'overlayShow'   : true,
			'overlayColor'  : '#000',
			'overlayOpacity': 0.5
		});
	}

	var get_vars = function (names) {
		window._labels = {};
		var i, name;
		$.getJSON("/scripts/requests.php?lang=" + $("html").attr("lang") + "&action=var", { 'vars[]': names }, function (data) {
			$.each(data, function (key, val) {
				window._labels[key] = val;
			});
		});
	};

	$("#heli-model").each(function () {
		get_vars(['l_error_flash', 'l_error_color', 'l_reset_changes']);
	});

	$("#heli-colors .tabs span").click(
		function () {
			if (!$(this).hasClass("tab-on")) {
				var prev_btn = $("#heli-colors .tabs span.tab-on");
				prev_btn.removeClass("tab-on");
				$(this).addClass("tab-on");
				var prev_obj = $("#colors-" + prev_btn.attr("id").replace(/.+(\d)$/, '$1'));
				var next_obj = $("#colors-" + $(this).attr("id").replace(/.+(\d)$/, '$1'));

				next_obj.parent().stop(true, true).animate({"height": next_obj.outerHeight(true) + "px"});
				$(prev_obj).stop(true, true).animate({"height": "toggle", "opacity": "toggle"});
				$(next_obj).stop(true, true).animate({"height": "toggle", "opacity": "toggle"});
			}
		}
	);

	$.fn.idle = function (time) {
		var o = $(this);
		o.queue(function () {
			setTimeout(function () {
				o.dequeue();
			}, time);
		});
		return this;
	}

	var flashError = function (flash, text) {
		flash.children(".error-msg").stop(true, true).remove();

		var msg = $('<div>');
		msg.addClass("error-msg");
		msg.hide();
		msg.html(text);
		flash.parent().append(msg);
		msg.css("margin-top", (175 - Math.round(msg.outerHeight() / 2)) + "px");
		msg.css("margin-left", (300 - Math.round(msg.outerWidth() / 2)) + "px");
		msg.bind("selectstart mousedown", function () {
			return false;
		});
		msg.fadeIn("fast").idle(2000).fadeOut("normal", function () {
			$(this).remove();
		});
	}

	$("#heli-colors p.colors img").click(
		function () {
			if (!$(this).hasClass("clr-on")) {
				var prev_btn = $("#" + $(this).parent().attr("id") + " img.clr-on");
				prev_btn.removeClass("clr-on");
				$(this).addClass("clr-on");
				$(this).parent().find("input[name*='color']").val($(this).attr("id").replace("clr-", ""));

				var flash = $("#heli-model object, #heli-model embed");
				var value = $(this).attr("class");
				var curr_price = parseFloat(value.replace(/.*p-(\d+)\-(\d+).*/, "$1.$2"));
				$(this).parent().find("input[name*='price']").val(curr_price);

				var price = 0;
				$("#heli-colors input[name*='price']").each(function () {
					price += parseFloat($(this).val());
				});

				$(".total-price strong").each(function () {
					var currency = $(this).html().replace(/\d+(.+)/, '$1');
					$(this).html(price + currency);
				});

				if (value.indexOf("c-") != -1) {
					var name = $(this).parent().attr("id").replace(/.*(color)s(\-\d).*/, "$1$2");
					value = value.replace(/.*c-([\da-z]{6}).*/i, "$1");
					try {
						flash[0].sendToFlash(name, value);
					} catch (e) {
//						flashError(flash, _labels.l_error_flash);
					}
				} else {
//					flashError(flash, _labels.l_error_color);
				}
			}
		}
	);

	$("#heli-colors p.colors").each(function () {
		var span = $("<span>").addClass("img-hint");
		$("<span>").addClass("h-left").appendTo(span);
		$("<span>").addClass("h-right").appendTo(span);
		var bottom = $("<span>").addClass("h-bottom").appendTo(span);
		var text = $("<span>").addClass("h-text").appendTo(span);
		$(this).prepend(span);
		$(this).find("img").removeAttr("title");

		$(this).find("img").hover(function () {
			var title = $(this).attr("alt");
			if (title) {
				text.text(title);
				var half = Math.round(span.width() / 2);
				bottom.css({"left": half - 6 + "px"});
				span.css({"left": (this.offsetLeft - half + this.offsetWidth / 2) + "px", "top": (this.offsetTop - 45) + "px"});
				span.stop(true, true).fadeIn("fast");
			}
		}, function () {
			var title = $(this).attr("alt");
			if (title) {
				span.stop(true, true).fadeOut("fast");
			}
		});
	});

	$("#heli-colors div.fill-container img").click(
		function () {
			if (!$(this).parent().hasClass("fill-on")) {
				var prev_btn = $("#heli-colors div.fill-container .fill-on");
				prev_btn.removeClass("fill-on");
				$(this).parent().addClass("fill-on");
				$(this).parent().parent().parent().find("input[name*='fill']").val($(this).attr("id").replace("fill-", ""));

				var flash = $("#heli-model object, #heli-model embed");
				var value = $(this).attr("class");
				var curr_price = parseFloat(value.replace(/.*p-(\d+)\-(\d+).*/, "$1.$2"));
				$(this).parent().parent().parent().find("input[name*='price']").val(curr_price);

				var price = 0;
				$("#heli-colors input[name*='price']").each(function () {
					price += parseFloat($(this).val());
				});

				$(".total-price strong").each(function () {
					var currency = $(this).html().replace(/\d+(.+)/, '$1');
					$(this).html(price + currency);
				});

				if (value.indexOf("p-") != -1) {
					var value = $(this).parent().parent().attr("class").replace("f-tab-", "");
					try {
						flash[0].sendToFlash('fill', value);
					} catch (e) {
//						flashError(flash, _labels.l_error_flash);
					}
				} else {
//					flashError(flash, _labels.l_error_color);
				}
			}
		}
	);

	var changeTotal = function () {
		var price = parseInt($("form input[name='price']").val(), 10);
		$("form span.price").each(function () {
			price += parseInt($(this).text(), 10);
		});

		$(".total-price strong").each(function () {
			var currency = $(this).html().replace(/\d+(.+)/, '$1');
			$(this).html(price + currency);
		});
	};

	$("select.choose-price").change(function () {
		var price = $(this).children("option:selected").attr("class");
		var label = $(this).parent().children("span.price");
		var currency = label.html().replace(/\d+(.+)/, "$1");
		label.html(price + currency);
		changeTotal();
		$(this).blur();
	});

	$("table[summary='c1']").each(function () {
		$(this).find("tr").each(function (i, val) {
			$(this).addClass(i % 2 ? "row-1" : "row-0");
		});
	});

	$("div.files li").each(function (i, val) {
		if (!i && $.browser.msie && $.browser.version < 7) {
			$(this).addClass("li-first");
		}
		$(this).append('<br class="clear" />');
	});

	var reset = $("#reset-params");
	if (reset.length) {
		get_vars(['l_reset_changes']);
		reset.click(function () {
			if (confirm(_labels.l_reset_changes)) {
				$("select.choose-price").each(function () {
					this.selectedIndex = 0;
					$(this).change();
				});
			}
			return false;
		});
	}

	$("#reset-colors").click(function () {
		if (confirm(_labels.l_reset_changes)) {
			$("p.colors img.clr-on").removeClass("clr-on");
			var first = $("p.colors").find("img:first");
			first.addClass("clr-on");
			first.parent().children("input:hidden").val(first.attr("id").replace("clr-", ""));

			first.each(function () {
					var curr_price = parseFloat($(this).attr("class").replace(/.*p-(\d+)\-(\d+).*/, "$1.$2"));
					$(this).parent().find("input[name*='price']").val(curr_price);
			});

			var price = parseFloat($("#heli-colors").find("input[name='price-basic']").val());
			first.parent().parent().find("input[name*='price']").each(function () {
				price += parseFloat($(this).val());
			});

			$(".total-price strong").each(function () {
				var currency = $(this).html().replace(/\d+(.+)/, '$1');
				$(this).html(price + currency);
			});
		}
		return false;
	});

	$("#home-links li").each(function () {
		$(this).append('<span class="line"></span>');
		if (!$.browser.msie || $.browser.version > 6) {
			$(this).find("span.line").css("opacity", 0.2);
		}
		$('<img>').attr('src', '/images/common/home_point_left.png');
		$('<img>').attr('src', '/images/common/home_point_right.png');

		$(this).find("a").hover(
			function () {
				var parent = $(this).parent();
				var height = parent.innerHeight();
				if (!$.browser.msie || $.browser.version > 6) {
					parent.find("span.line").stop(true).css("display", "block").animate({"height": height + "px", "opacity": 1}, "fast");
				} else {
					parent.find("span.line").stop(true).css("display", "block").animate({"height": height + "px"}, "fast");
				}
			},
			function () {
				var parent = $(this).parent();
				if (!$.browser.msie || $.browser.version > 6) {
					parent.find("span.line").animate({"height": "0px", "opacity": 0.2}, "fast");
				} else {
					parent.find("span.line").animate({"height": "1px"}, "fast", "linear", function () {
						$(this).css("display", "none");
					});
				}
			}
		);
	});

	$("#helicopters").each(function () {
		$(this).prepend('<span class="prev"><span></span><a href="#"></a></span>');
		$(this).prepend('<span class="next"><span></span><a href="#"></a></span>');
		(new Image()).src = '/images/common/home_prev.jpg';
		var btn_prev = $(this).find("span.prev");
		var btn_next = $(this).find("span.next");
		$('<img>').attr('src', '/images/common/home_prev.jpg');
		$.each([btn_prev, btn_next], function () {
			$(this).find("a").hover(
				function () {
					$(this).prev().stop(true).fadeTo("normal", 0.6);
				},
				function () {
					$(this).prev().stop(true).fadeTo("normal", 1);
				}
			);
		});

		var objects = $(this).find("div.objects");
		var elements = objects.children();
		var max = elements.length * 500;
		objects.css("width", max + "px");
		var current = 0;

		elements.each(function (i, val) {
			if (i) {
				$(this).fadeTo(0, 0);
			}
			$(this).hover(
				function () {
					$(this).find(".mirror").show();
				},
				function () {
					$(this).find(".mirror").hide();
				}
			);
		});
		btn_prev.fadeOut();
		if (max > 500) {
			btn_next.fadeIn();
		}

		btn_prev.find("a").click(function () {
			if (current == 0) {
				return false;
			}

			current += 500;
			objects.animate({"left": current + "px"});

			var index = Math.abs(current) / 500;
			var el_curr = elements.get(index);
			var el_next = elements.get(index + 1);
			$(el_curr).fadeTo("slow", 1);
			$(el_next).fadeTo("fast", 0);

			if (current == 0) {
				btn_prev.fadeOut();
			}
			btn_next.fadeIn();
			
			return false;
		});
		btn_next.find("a").click(function () {
			if (current - 500 == -max) {
				return false;
			}
			current -= 500;
			objects.animate({"left": current + "px"});

			var index = Math.abs(current) / 500;
			var el_curr = elements.get(index);
			var el_prev = elements.get(index - 1);
			$(el_curr).fadeTo("slow", 1);
			$(el_prev).fadeTo("fast", 0);

			if (current - 500 == -max) {
				btn_next.fadeOut();
			}
			btn_prev.fadeIn();

			return false;
		});
	});

	$("#map-canvas").each(function () {
		if (GBrowserIsCompatible()) {
			var map = new GMap2($(this).children("#canvas")[0]);
			map.setCenter(new GLatLng(48.092757, 32.585449), 6);
			map.addControl(new GLargeMapControl3D());
			map.enableScrollWheelZoom();

			var balloon = new GIcon(G_DEFAULT_ICON);
			balloon.image = "/images/common/map_point.png";

			$(this).children("div.point").each(function () {
				var lat     = parseFloat($(this).children(".lat").text());
				var lng     = parseFloat($(this).children(".lng").text());
				var name    = $(this).children(".name").text();
				var content = $(this).children(".content").html();

				var marker = new GMarker(new GLatLng(lat, lng), { icon: balloon });
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml("<h2 class=\"overlay\">" + name + "</h2><div class=\"overlay\">" + content + "</div>", { maxWidth: 300 });
				});
			});
		}
	});

	var fly = true, __step = 1;
	if (fly && (!$.browser.msie || $.browser.version > 6)) {
		var clouds = $("body.home #clouds");
		clouds.each(function () {
			var offset = 0;
			setInterval(function () {
				offset += __step;
				clouds.css("background-position", offset + "px 49px");
			}, 60);
		});
	}

	var home = $("body.home");
	if (home.length) {
		$(window).load(function () {
			$(home).find("#footer div.inner").fadeIn();
		});
		$(document).keydown(function(event) {
			switch (event.keyCode) {
			case 90:
				var footer = $("#footer");
				footer.css("display") == 'none' ? footer.fadeIn() : footer.fadeOut();
				break;
			case 88:
				$("#header").slideToggle();
				break;
			case 67:
				__step = -__step;
				break;
			case 86:
				--__step;
				break;
			case 66:
				++__step;
				break;
			}
		});
	} else {
		if ($.browser.safari) {
			$("<link>").attr({ rel: "stylesheet", type: "text/css", href: "/scripts/safari.css", media: "screen"}).appendTo("head");
		}
	}
});
