﻿/*
*		jCombox 1.0b | 2008.08.25
*		jQuery: HTML Select Tag Replacement Plugin 1.0b
*		Licensed under a Creative Commons Attribution 3.0 License
*
*		Tested to work on Firefox2/3, IE6/7, Safari3.1(Win) and Opera9.5 versions
*		Limitations: no keyboard usage + no multiple selections > checkout next version
*		_______________________________________________
*		http://jquerylab.com | Sponsored by http://12tb.com
*		ruvanepa@gmail.com
*
*		2009.03.30
*		Unpacked with http://dean.edwards.name/packer/ (helped by http://paulirish.com/2007/unobfuscating-packed-javascript/)
*		Bugfix by kai@ich-geh-kaputt.de:
*		- quotes were not correctly escaped when replacing the select's by div's
*		- special characters were not correctly escaped when embeding option-values in an URL
*
*		How-to use: $('.select').jcombox();
*			or replce all <select> tags: $('select').jcombox();
*/

(function($) {
	$.fn.setOffCss = function(a) {
		var b = this.parent();
		if ($(window).height() + (document.documentElement.scrollTop || document.body.scrollTop) < b.offset().top + this.height() + b.height()) {
			objTop = b.offset().top - ($.browser.msie ? 3 : 1) - this.height()
		} else {
			objTop = b.offset().top + ($.browser.msie ? -1 : 1) + b.height()
		}
		return this.css({
			left: b.offset().left - ($.browser.msie ? 2 : 0),
			top: objTop,
			width: b.width() + ($.browser.msie ? 2 : 0)
		})
	};
	$.fn.fadesTo = function(o) {
		return this.animate({
			opacity: o
		},
		0,
		function() {
			if (o == 1 && $.browser.msie) {
				this.style.removeAttribute("filter")
			}
		})
	};
	$.fn.jcombox = function(o) {
		o = $.extend({
			fn: null,
			fx: "toggle",
			fxType: "easeOutBounce",
			fxDelay: 0,
			set: false,
			theme: "default",
			linkUrl: null
		},
		o || {});
		return this.each(function() {
			var b = $(this),
			_7 = "",
			_8 = b.attr("name"),
			_9 = $("option", b).eq(0).text(),
			_a = $("option", b).eq(0).val(),
			_b = "#" + _8,
			_c = b.attr("size"),
			_d = b.attr("rows") || 0,
			_e = b.attr("width") || 0,
			_f = b.attr("disabled") || 0,
			_10 = b.attr("onchange") || b.attr("onChange") || 0;
			$("option", b).each(function() {
				if ($(this).attr("selected")) {
					_9 = $(this).text();
					_a = $(this).val()
				}
				var tmp = ("<a href=\"" + (o.linkUrl != null ? "?" + _8 + "=" + encodeURIComponent($(this).val()) : "#") + "\" rel=\"" + $(this).val().replace(/"/g, '&#34') + "\" >" + $(this).text() + "</a>");
				//alert(tmp);
				_7 += tmp;
			});
			if (_c > 0) {
				b.replaceWith("<div class=\"jcombox-m " + o.theme + "\" id=\"" + _8 + "\"><input type=\"hidden\" name=\"" + _8 + "\" value=\"" + _a + "\" /><div>" + _7 + "</div></div>");
				//alert("<div class=\"jcombox-m " + o.theme + "\" id=\"" + _8 + "\"><input type=\"hidden\" name=\"" + _8 + "\" value=\"" + _a + "\" /><div>" + _7 + "</div></div>");
				$(_b).css("background-image", "none");
				if (!$("div a", _b).eq(0).text()) {
					$("div a", _b).eq(0).text("-")
				}
				$(_b).width($("div", _b).width() + 40 + "px");
				$(_b).height($("div a", _b).height() * _c + ($.browser.msie ? 4 : 2) + "px")
			} else {
				b.replaceWith("<div class=\"jcombox " + o.theme + "\" id=\"" + _8 + "\"><span>" + _9 + "</span><input type=\"hidden\" name=\"" + _8 + "\" value=\"" + _a + "\" /><div class=\"menu\">" + _7 + "</div></div>");
				//alert("<div class=\"jcombox " + o.theme + "\" id=\"" + _8 + "\"><span>" + _9 + "</span><input type=\"hidden\" name=\"" + _8 + "\" value=\"" + _a + "\" /><div class=\"menu\">" + _7 + "</div></div>");
				var c = $(_b);
				if ($("span", _b).width() > $("div", _b).width()) {
					$(_b).width($("span", _b).width() + 35 + "px")
				} else {
					$(_b).width($("div", _b).width() + 40 + "px")
				}
				if (_d > 1) {
					$("div", _b).height($("span", _b).height() * _d + ($.browser.msie ? 2 : 0) + "px")
				}
				$(document).click(function(e) {
					if ($("div", _b).css("display") == "block") {
						$("div", _b).hide()
					}
				});
				c.hover(function() {
					clearTimeout(this.timeout)
				},
				function() {
					this.timeout = setTimeout(function() {
						if ($("div", _b).css("display") == "block") {
							c.trigger("click")
						}
					},
					750)
				});
				if (!$("span", _b).text()) {
					$("span", _b).text("-")
				}
				if (o.set) {
					c.css({
						"border": "transparent",
						"background-color": "transparent",
						"padding-right": "2px"
					});
					$("span", _b).css({
						"border": "transparent",
						"background-image": c.css("background-image"),
						"background-position": "left center",
						"padding": "0 0px 3px 5px"
					})
				}
			}
			$("div", _b).find("a[rel='" + $(":hidden", _b).val() + "']").addClass("current");
			$(_b).bind("jcDisable",
			function() {
				$(":hidden", _b).val("");
				if ($("div", _b).css("display") == "block" && !_c > 0) {
					$("div", _b).hide()
				}
				$(_b).fadesTo(0.3).unbind("click");
				$("div a", _b).unbind("click").bind("click",
				function() {
					return false
				})
			});
			$(_b).bind("jcEnable",
			function() {
				$(this).fadesTo(1);
				$(":hidden", this).val($(".current", this).attr("rel"));
				if (!_c > 0) {
					$(_b).unbind("click").bind("click",
					function(e) {
						var a = $("div", _b);
						switch (o.fx) {
						case "toggle":
							a.setOffCss().toggle();
							break;
						case "slide":
							a.setOffCss().slideToggle(o.fxDelay);
							break;
						case "fade":
							a.setOffCss().animate({
								opacity:
								"toggle"
							},
							o.fxDelay).fadesTo(1);
							break;
						case "slideFade":
							a.setOffCss().animate({
								opacity:
								"toggle",
								height: "toggle"
							},
							o.fxDelay).fadesTo(1);
							break;
						case "easing":
							a.setOffCss().slideToggle(o.fxDelay, o.fxType);
							break
						}
						e.stopPropagation()
					})
				}
				$("a", _b).unbind("click").bind("click",
				function() {
					if ($(this).hasClass("disabled")) {
						return false
					}
					$("a", _b).removeClass("current");
					$(this).addClass("current");
					if ($.browser.safari) {
						$(this).fadesTo(0.99).fadesTo(1)
					}
					if (!_c > 0) {
						$("span", _b).text($(this).text());
						$(_b).trigger("click")
					}
					$(":hidden", _b).val($(this).attr("rel"));
					if (o.fn) {
						o.fn()
					}
					if ($.browser.msie && _10) {
						eval(_10())
					} else {
						if (_10) {
							eval(_10)
						}
					}
					return false
				});
				$("a", this).css("color", $("." + o.theme).css("color"));
				$("span", this).css("overflow", "hidden")
			});
			if (b.attr("disabled")) {
				$(_b).trigger("jcDisable")
			} else {
				$(_b).trigger("jcEnable")
			}
			$(_b).bind("selectstart mousedown",
			function() {
				return false
			}).css("MozUserSelect", "none");
			$(_b).hover(function() {
				$(this).addClass(o.theme + "-hover");
				if (o.set) {
					$("span", _b).css("background-image", c.css("background-image"))
				}
			},
			function() {
				$(this).removeClass(o.theme + "-hover");
				if (o.set) {
					$("span", _b).css("background-image", $("." + o.theme).css("background-image"))
				}
			});
			if (_e) {
				$(_b).width(_e + "px")
			}
		})
	};
	var d = ["jcEnable", "jcDisable"];
	for (var i = 0; i < d.length; i++) {
		$.fn[d[i]] = (function(a) {
			return function() {
				this.trigger(a)
			}
		})(d[i])
	}
	$.fn.jcSelected = function(a) {
		if (a) {
			return $(".current", this).text()
		} else {
			return $(":hidden", this).val()
		}
	};
	$.fn.jcAdd = function(a, b, c) {
		if (c) {
			$("a.current", this).removeClass()
		}
		$("div", this).append("<a " + (c ? "class=\"current\"": "") + " href=\"#\" rel=\"" + a + "\" >" + b + "</a>");
		if (c) {
			$("span", this).text($(".current", this).text())
		}
		this.jcEnable()
	};
	$.fn.jcEdit = function(a, b) {
		$(".current", this).replaceWith("<a class=\"current\" href=\"#\" rel=\"" + a + "\" >" + b + "</a>");
		this.jcEnable();
		if ($(".current", this).text()) {
			$("span", this).text($(".current", this).text())
		}
	};
	$.fn.jcClear = function(a) {
		if (a) {
			$("a.current", this).removeClass()
		} else {
			$(".current", this).slideUp(function() {
				$(this).remove()
			})
		}
		this.jcEnable();
		$("span", this).text("-");
		$(":hidden", this).val("")
	}
})(jQuery);
