/*
 * jNice  * by Sean Mooney (sean@whitespace-creative.com)
 * modified by Martin Hlozek (martin.hlozek@bmedia.cz)
*/

(function($){
	$.fn.jNice = function(options){
		var self = this;
		/* each form */
		this.each(function(){
			$('select', this).each(function(index){
				var $select = $(this);
				$(this).addClass('jNiceHidden').wrap('<div class="jNiceSelect"></div>');
				var $wrapper = $(this).parent().css({zIndex: 100-index});
				$wrapper.prepend('<div><span class="jNiceField"><span class="jNiceText"></span></span><a href="#" class="jNiceOpen"></a></div><ul></ul>');
				var $ul = $('ul', $wrapper);
				$('option', this).each(function(i){
					$ul.append('<li class="' + this.className + '"><a href="#" index="'+ i +'">'+ this.text +'</a></li>');
				});
				$ul.hide().find('a').click(function(){
						$('a.selected', $wrapper).removeClass('selected');
						$(this).addClass('selected');
						/* Fire the onchange event */
						if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) {  $select[0].selectedIndex = $(this).attr('index'); $select[0].onchange(); }
						$select[0].selectedIndex = $(this).attr('index');
						$('.jNiceText', $wrapper).html($(this).html());
						$('.jNiceSelect div').removeClass();
						$('.jNiceSelect div').addClass($(this).parent().get(0).className);
						$ul.hide();
						return false;
				});
				$('a:eq('+ this.selectedIndex +')', $ul).click();
			});

			/* Apply the click handler to the Open */
			$('a.jNiceOpen', this).click(function(){
														var $ul = $(this).parent().siblings('ul');
														if ($ul.css('display')=='none'){hideSelect();} /* Check if box is already open to still allow toggle, but close all other selects */
    													$ul.slideToggle();
														var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
														$ul.animate({scrollTop: offSet});
														return false;
												});

		});

		/* Hide all open selects */
		var hideSelect = function(){
			$('.jNiceSelect ul:visible').hide();
		};

		/* Check for an external click */
		var checkExternalClick = function(event) {
			if ($(event.target).parents('.jNiceSelect').length === 0) { hideSelect(); }
		};

		/* Apply document listener */
		$(document).mousedown(checkExternalClick);

		/* Add a new handler for the reset action */
		var jReset = function(f){
			var sel;
			$('.jNiceSelect select', f).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; $('ul', $(this).parent()).each(function(){$('a:eq('+ sel +')', this).click();});});
		};
		this.bind('reset',function(){var action = function(){jReset(this);}; window.setTimeout(action, 10);});

	};

	/* Automatically apply to any forms with class jNice */
	$(function(){
		$('.jNice').jNice();	});

})(jQuery);






/* Copyright (c) 2008 Kean Loong Tan http://www.gimiti.com/kltan
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Copyright notice and license must remain intact for legal use
 * jHelpertip
 * Version: 1.0 (Jun 2, 2008)
 * Requires: jQuery 1.2+
 */
(function(A){A.fn.jHelperTip=function(C){var E=A.extend({},A.fn.jHelperTip.defaults,C);if(A(E.ttC).length==0){A('<div id="'+E.ttC.slice(1)+'"></div>').appendTo("body")}if(A(E.dC).length==0){A('<div id="'+E.dC.slice(1)+'"></div>').appendTo("body")}if(A(E.aC).length==0){A('<div id="'+E.aC.slice(1)+'"></div>').appendTo("body")}A(E.ttC).add(E.aC).css({position:"absolute",display:"inline"}).hide();A(E.dC).hide();var F=function(){if(E.source=="attribute"){A(E.aC).hide().empty()}else{A(E.ttC).hide().empty()}};A(".jHelperTipClose").bind("click",F);A(E.ttC).bind("mouseover",function(){A(E.ttC).show();return false});var B=function(H,G){if(E.source=="ajax"){D(G);A(E.ttC).html('<div><img src="'+E.loadingImg+'"/> '+E.loadingText+"</div>").show();A.ajax({type:E.type,url:E.url,data:E.data,success:function(I){A(E.ttC).html(I);A(".jHelperTipClose").unbind("click",F);A(".jHelperTipClose").bind("click",F)}})}else{if(E.source=="container"){A(E.ttC).show().empty();A(E.dC).clone(true).show().appendTo(E.ttC)}}if(E.source=="attribute"){A(E.aC).html(A(H).attr(E.attrName))}};var D=function(I){var H=I.pageY+E.topOff;var G=I.pageX+E.leftOff;if(E.source=="attribute"){A(E.aC).css({top:H,left:G,opacity:E.opacity}).show()}else{A(E.ttC).css({top:H,left:G,opacity:E.opacity}).show()}};if(E.trigger=="hover"){A(this).bind("mouseover",function(G){G.preventDefault();B(this,G);return false});A(this).bind("mousemove",function(G){D(G);return false});A(this).bind("mouseout",function(G){if(E.source=="attribute"){A(E.aC).hide().empty()}else{A(E.ttC).hide().empty()}return false})}else{if(E.trigger=="click"){A(this).bind("click",function(G){B(this,G);D(G);A(document).bind("click",function(H){if(E.autoClose){if(E.source=="attribute"){A(E.aC).hide().empty()}else{A(E.ttC).hide().empty()}}});return false})}}};A.fn.jHelperTip.defaults={trigger:"click",topOff:3,leftOff:10,source:"container",attrName:"",ttC:"#jHelperTipContainer",dC:"#jHelperTipDataContainer",aC:"#jHelperTipAttrContainer",opacity:1,loadingImg:"ajax-loader.gif",loadingText:"Loading...",type:"GET",autoClose:true}})(jQuery);