(function($){
	if ($.browser.msie && $.browser.version < 7) {
		DD_roundies.addRule(".popup .btm, .popup .top, .popup .wrapper, .popup");
	}
	$(document).ready(function(){
		// popup close
		$(".close_popup").click(function(){
			$(this).closest(".popup").hide();
		});

		
		$(".scroll_content").slider({
			scroll_content: '.scroll-container .scroll-content',
			drag_selector: '.scrollbar .scroll-handle',
			increment: 15
		});
	});
	
	
	
	$.fn.extend({
		slider: function(opts){
			opts = $.extend({
				scroll_content: '.scroll_content',
				drag_selector: '.scroller_bar',
				up_button: '.scroller_up',
				down_button: '.scroller_down',
				increment: 10
			}, opts);
			return this.each(function(i){
				// scrolling fn
				var container = $(this),
					handle = $(opts.drag_selector, container),
					scroll_content = $(opts.scroll_content, container).css({position:'relative'}),
					down_button = $(opts.down_button, container),
					up_button = $(opts.up_button, container),
					scale = 0,
					limit = handle.parent().height() - handle.height(),
					cur_pos = 0;
				
				scale = - ((scroll_content.height() - scroll_content.parent().height()) / limit);
				if (scale < 0) {
					
					var move = function(x,y) {
						if (typeof(y)=='undefined') {
							y = x;
						}
						if (y >= 0 && scale < 0 && y <= limit) {
							scroll_content.css({"top": y * scale });
							cur_pos = scroll_content.css("top");
						}
					};
					handle.parent().css({cursor: 'pointer', display: 'block'}).bind('click.scroll', function(e){
						var offset = e.pageY - ($(this).offset()).top - handle.height()/2;
						var y = offset < 0 ? 0 : offset > limit ? limit : offset;
						move(y);
						handle.css('top', y);
					});
					if (down_button.length) {
						down_button.bind('click.scroll', function(e){
							var y = parseInt(handle.css('top'),10) ?  parseInt(handle.css('top'),10) + opts.increment : opts.increment;
							if (y < limit) {
								move(y);
								handle.css('top', y);
							}
							else {
								move(limit);
								handle.css('top', limit);
							}
						});
					}
					if (up_button.length) {
						up_button.bind('click.scroll', function(e){							
							if (parseInt(handle.css('top'),10) - opts.increment > 0) {
								move(parseInt(handle.css('top'),10) - opts.increment);
								handle.css('top', parseInt(handle.css('top'),10) - opts.increment);
							}
							else {
								move(0);
								handle.css({top: 0});
							}
						});
					}
					
					if ($.fn.wheel && up_button.length && down_button.length && !$.browser.msie && !$.browser.safari) {
						container.wheel(function(e){
							e.preventDefault();
							if (e.delta == 1) {
								up_button.trigger('click');
							}
							else {
								down_button.trigger('click');
							}
						});
					}
					var reset_drag = function(){
						handle.DraggableDestroy(); // ie bug fix
						handle.Draggable({
							axis: 'vertically',
							containment: 'parent',
							helperclass:  'button',
							onDrag: move,
							onStop: function(){
								reset_drag();
							}
						});
					};
					reset_drag();
				}
			});
		}
	});	
	
	
	$.popup_open = false;
	
	function showPopup(id){
		if ($.popup_open) {
			return;
		}		
		$.popup_open = true;		
		if (!$.popup) { $.popup = $("<div class='loading' style='z-index:1000'></div>").prependTo("body"); }		
		$.ajax({
			type: 'get',
			url: '/ajax/popup/definition/id/'+id,
			success: function(html){
				$.popup.html(html).show();
				$("input, textarea").each(function(){
					var el = $(this);
					if (!el.val()) {
						el.val(el.attr('label'));
						el.focus(function(){if (this.value == el.attr('label')) {this.value='';}});
						el.blur(function(){if (!this.value) {this.value=el.attr('label');}});
					}
				});
			}					
		});
	}
	function closePopup() {
		$("input, textarea", $.popup).unbind('focus').unbind('blur');
		$.popup.children().remove();
		$.popup.hide();
		$.popup_open = false;
	}
	function fadeOut(end) {
		if (end) {
			window.location.reload(true);
			return;
		}
		closePopup();
	}
	
	function sendPopup(id) {
		if (window.event && window.event.preventDefault) {
			window.event.preventDefault();
		}
		var data = $("form", $.popup).serialize();
		
		$.ajax({
			type: 'post',
			url: '/ajax/popup/definition/id/'+id,
			data: data,
			success: function(html){
				$.popup.html(html).show();
				$("input, textarea", $.popup).each(function(){
					var el = $(this);
					if (!el.val()) {
						el.val(el.attr('label'));
						el.focus(function(){if (this.value == el.attr('label')) {this.value='';}});
						el.blur(function(){if (!this.value) {this.value=el.attr('label');}});
					}
				});
			}
		});
		$("input, textarea", $.popup).unbind('focus').unbind('blur');
		$.popup.children().remove();
	}
	window.showPopup = showPopup;
	window.fadeOut = fadeOut;
	window.closePopup = closePopup;
	window.sendPopup = sendPopup;
	
})(window.jQuery);