﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="utils.js" />
/// <reference path="lw.js" />
/// Copyright 2011 www.liteweb.info

(function ($) {
	//Creating the lw object (used to hold global variables)
	var lw = this.lw ? this.lw : {};

	//switch between 2 images 
	//usually one grayscale and one colored
	$.fn.fadeOnHover = function (options) {
		var opts = {
			param: "-hover",
			duration: 500
		};
		opts = $.extend(opts, options);
		return this.each(function () {
			var $this = $(this);
			if (!this.img)
				this.img = $this.children("img");
			if (this.img.length == 0)
				return true;
			this.imgSrc = this.img[0].src;
			this.img.css("position", "absolute");
			var temp = this.imgSrc.split(".");
			var extension = temp.pop();
			this.hoverSrc = temp.join(".") + opts.param + "." + extension;

			this.hoverImg = $("<img />");
			this.hoverImg.attr("src", this.hoverSrc).css({
				posiotion: "absolute",
				opacity: 0
			}).appendTo($this);
			$this.bind("mouseover", function () {
				this.hoverImg.stop().animate({ opacity: 1 }, { duration: opts.duration });
				this.img.stop().animate({ opacity: 0 }, { duration: opts.duration });
			});
			$this.bind("mouseout", function () {
				this.hoverImg.stop().animate({ opacity: 0 }, { duration: opts.duration });
				this.img.stop().animate({ opacity: 1 }, { duration: opts.duration });
			});
		});
	};

	//created an internal popup window
	//pass the id as an anchor <a href="#popupid" />
	$.fn.popup = function (options) {
		var opts = {
			overlay: {
				color: "#000000",
				opacity: .55,
				duration: 600,
				open: null,
				close: null
			},
			duration: 500,
			close: null,
			open: null,
			openEasing: "easeInCirc",
			closeEasing: "easeOutCirc",
			top: 155
		};
		opts = $.extend(opts, options);

		function closeHandler() {
			if (typeof opts.close == "function")
				return opts.close();
			closeOverlay();
			this.el.popupDiv.animate({ top: -5000 }, { duration: opts.duration, easing: opts.closeEasing });
		}
		function closeOverlay() {
			if (typeof opts.overlay.close == "function")
				return opts.overlay.close();

			var h = $(document).height();
			var w = $(document).width();
			var overlays = lw.overlays;
			for (var i = 0; i < lw.overlays.length; i++) {
				obj = {
					width: 0,
					height: 0,
					left: i % 2 == 0 ? 0 : w,
					top: i < 2 ? 0 : h,
					opacity: 0
				};
				lw.overlays[i].animate(obj, {
					duration: opts.overlay.duration,
					complete: function () {
						$(document.body).css("overflow", "auto");
						$(document).unbind("keyup");
					}
				});
			}
		}
		function showOverlay() {
			if (typeof opts.overlay.open == "function")
				return opts.overlay.open();

			var body = $(document.body);
			body.css("overflow", "hidden");
			if (!lw.overlays) {


				lw.overlays = new Array(4);
				for (var i = 0; i < lw.overlays.length; i++) {
					lw.overlays[i] = $(document.createElement("div"));
					body.append(lw.overlays[i]);
				}
			}
			var h = $(document).height();
			var w = $(document).width();

			for (var i = 0; i < lw.overlays.length; i++) {
				var obj = {
					position: "absolute",
					width: 0,
					height: 0,
					background: opts.overlay.color,
					opacity: 0,
					top: i < 2 ? 0 : h,
					left: i % 2 == 0 ? 0 : w,
					"z-index": 9000
				};
				lw.overlays[i].css(obj);
				obj = {
					width: w / 2,
					height: h / 2,
					left: i % 2 == 0 ? 0 : w / 2,
					top: i < 2 ? 0 : h / 2,
					opacity: opts.overlay.opacity
				};
				lw.overlays[i].animate(obj, { duration: opts.overlay.duration });
			}
		}
		function clickHandler() {
			showOverlay();
			showPopup(this);
		}
		function showPopup(el) {
			var p = el.popupDiv;
			obj = {
				position: "absolute",
				left: $(window).width() / 2 - p.width() / 2,
				display: "block",
				"z-index": 9001
			};
			p.css(obj);
			p.animate({ top: opts.top },
				{ duration: opts.duration, easing: opts.openEasing });

			var closeB = el.closeButton;
			$(document).bind("keyup", function (e) {
				if (e.keyCode == 27)
					closeB.trigger("click");
			})
		}


		return this.each(function () {
			var $this = $(this);
			this.popupDiv = $("#" + this.href.split("#").pop());
			this.closeButton = $("<div class=close title=Close />").css({
				"margin-left": this.popupDiv.width() - 15,
				"margin-top": 5
			});
			this.popupDiv.prepend(this.closeButton);
			this.closeButton[0].el = this;
			this.closeButton.bind("click", closeHandler);
			$this.bind("click", clickHandler);
		});
	}


	$.fn.menu = function (options) {
		var opts = {
			duration: 300,
			easing: "easeOutQuad",
			direction: "v"
		};
		opts = $.extend(opts, options);



		function createMenu(el, parent, it, level) {
			var m = {};
			m.el = $(el);
			m._a = $(el).children("a");
			m._level = level;

			m.child = $(el).children("ul");
			if (m._a.length == 0 || m.child.length == 0)
				return;

			m.a = m._a[0];
			m.ul = m.child[0];

			m.li = m.child.children("li");
			m.maxHeight = m.child.outerHeight() + 0; //m.li.length * 23 + 22 + ($.browser.safari && $.browser.version < 526 ? (-2) : 0);
			m.maxWidth = m.child.outerWidth();

			m.visible = false;
			m.hideMe = false;
			m.parent = parent;
			m.a.menu = m;
			m.child[0].menu = m;

			m.show = function () {
				this.hideMe = false;
				if (this.visible)
					return;
				this.visible = true;

				this.child.css({
					overflow: "visible",
					visibility: "visible",
					height: 0,
					top: opts.direction == "v" && this._level == 0? this._a.position().top + this._a.outerHeight() : this._a.position().top,
					left: opts.direction == "v" && this._level == 0? this._a.position().left : this._a.position().left + this._a.outerWidth() + 2,
					width: this.child.width() < this._a.width() ? this._a.width() - 4 : this.child.width()
				});
				this.child.stop();
				var animateTo = { height: this.maxHeight };
				if (!$.browser.msie)
					$.extend(animateTo, { opacity: 1 });
				this.child.animate(animateTo, { duration: opts.duration, easing: opts.easing });
				this._a.addClass("_hover");
			};
			m.hide = function () {
				this.hideMe = true;
				window.setTimeout(function () {
					m._hide();
				}, 100);
			}
			m._hide = function () {
				if (!this.hideMe)
					return;
				this.visible = false;
				for (var i = 0; i < this.children.length; i++) {
					if (this.children[i].visible)
						return;
				}
				this.child.stop();
				var animateTo = { height: 0 };
				if (!$.browser.msie)
					$.extend(animateTo, { opacity: 0 });

				this.child.animate(animateTo, { duration: opts.duration / 2, easing: opts.easing,
					complete: function () {
						if (!this.menu.hideMe)
							return;
						this.menu.child.css("visibility", "hidden");
						this.menu._a.removeClass("_hover");
					}
				});
			}
			m.children = [];
			m.li.each(function (i, el) {
				m.children.push(el);
				el.menu = createMenu(el, this, i, level + 1);
				var $a = $($(this).children("a"));
				if ($(el).children("ul").length == 0)
					$a.addClass("arrow1");
			});
			if (m.children.length > 0 && m.parent != null) {
				m._a.addClass("arrow");
			}
			m._a.bind("mouseover", function () {
				if (this.menu) {
					this.menu.show();
				}
			});
			m._a.bind("mouseout", function () {
				if (this.menu)
					this.menu.hide();
			});
			m.child.bind("mouseover", function () {
				if (this.menu)
					this.menu.show();
			});
			m.child.bind("mouseout", function () {
				if (this.menu)
					this.menu.hide();
			});
			return m;
		}
		function hideMenu(el) {
			window.setTimeout(function () {
				_hideMenu(el);
			}, 200);
		}
		function _hideMenu(el) {
			if (el.childVisible)
				return;
			el._sub.css("display", "none");
			el._ol.css("height", 0)
			el._a.removeClass("_hover");
		}

		return this.each(function (i, el) {
			el.menu = createMenu(el, opts.parent, i, 0);
		});
	}
})(jQuery);

