;(function($) {

function actionMenu(menuSelector, linkAction, timeWait, event, position, qthis) {
	var current;
	var timeout;
	
	function hideMenu() {
		$(menuSelector).hide('slow');
	}

	var menu = $(menuSelector)
	.mouseover(function() {
		clearTimeout(timeout);
	})
	.mouseout(function() {
		clearTimeout(timeout);
		timeout = setTimeout(hideMenu, 250);		
	});
	
	menu.find('a').click(function() {
		if (typeof linkAction == 'function') {
			linkAction(current, $(this));
		}
		return false;
	});

	return qthis.each(function() {
		var self = $(this);
		function action() {
			current = $(this);
			
			var offset = $(this).offset();
			if (position == 'side') {
				menu.css('top', offset.top + 4)
					.css('left', offset.left  + $(this).width() + 14)
					.show('fast');
			} else {
				menu.css('top', offset.top + $(this).height() + 4)
					.css('left', offset.left)
					.show('fast');
			}
				
			clearTimeout(timeout);
			timeout = setTimeout(hideMenu, timeWait);		
		};
		
		if (event == 'click') {
			self.click(action);
		} else {
			self.mouseover(action);
		}
	});
}; 


$.fn.popupMenu = function(menuSelector, linkAction) {
	actionMenu(menuSelector, linkAction, 1000, 'click', 'bottom', this);
};

$.fn.hoverMenu = function(menuSelector, linkAction) {
	actionMenu(menuSelector, linkAction, 3000, 'mouseover', 'side', this);
};
})(jQuery);

/************************************************/
/*
 */

;(function($) {

$.fn.suggestArticle = function(articleType) {
	return this.each(function() {
		var self = $(this);
		var last = '';
		var timeout;
	
		function searchAction() {
			var current = self.attr('value');
			if (current == last) {
				return;
			}
			
			last = current;
			$("#Suggestions").hide('fast');
			
			if (current == '') {
				return;
			}
		
			$("#Suggestions")
				.load("/suggest/" + articleType + "?q=" + escape(current), function() {
						$(this).show("slow");
					});
		}
	
		self
		.keydown(function() {
			clearTimeout(timeout);
			timeout = setTimeout(searchAction, 1000);		
		})
		.blur(function() {
			clearTimeout(timeout);
			searchAction();		
		});
	});
}; 

})(jQuery);


var selectText = function(field, start, end) {
	if (field.createTextRange) {
		var selRange = field.createTextRange();
		selRange.collapse(true);
		selRange.moveStart("character", start);
		selRange.moveEnd("character", end - start);
		selRange.select();
	} else if (field.setSelectionRange) {
		field.setSelectionRange(start, end);
	} else {
		if (field.selectionStart) {
			field.selectionStart = start;
			field.selectionEnd = end;
		}
	}
	field.focus();
};

window['selectText'] = selectText; 



/************************************************/
/* Message To Autocomplete
 */
 
 ;(function($) {

$.fn.suggestPerson = function(menuSelector) {
	var current;
	var timeout;
	var keyTimeout;
	
	function hideMenu() {
		$(menuSelector).hide('slow');
	}

	var menu = $(menuSelector)
	.mouseover(function() {
		clearTimeout(timeout);
	})
	.mouseout(function() {
		clearTimeout(timeout);
		timeout = setTimeout(hideMenu, 250);		
	});
	
	function findWord(text, position) {
		var start = position;
		for (; start > 0; start--) {
			var c = position.charAt(start);
			if (!/A-Z-a-z0-9\-_/.match(c)) {
				continue;
			}
		}
		/*if (position > text.length()) {
			position = text.length();
		}
	
		var start = position;
		var end = position;
		*/
		
		alert(text.substring(start));
		
		return text.substring(start);
	}

	return this.each(function() {
		var last = '';
		var self = $(this);
		var current;
		
		function keyAction() {
			var currentText = findWord(self.attr('value'));
			if (currentText == last) {
				return;
			}			
			
			var offset = current.offset();
			menu.css('top', offset.top + current.height() + 8)
				.css('left', offset.left)
				.show('fast');
				
			clearTimeout(timeout);
			timeout = setTimeout(hideMenu, 5000);
			
			$(menuSelector)
				.html('<img src="/images/loading.gif"/>')
				.load("/suggest/person?q=" + escape(currentText), function() {
					menu.find('.MemberShort').click(function() {
						hideMenu();
						var member = current.attr('id').substring(7);
						var text = self.attr('value');
						self.attr('value', text + member);
						return false;
					});
				});					
		}

		self.keydown(function() {
			current = $(this);		
			clearTimeout(timeout);
			timeout = setTimeout(keyAction, 1000);
		});		
	});
}; 

})(jQuery);
 
 
 
/************************************************/
/*
 */

function _stars() {
	$('.StarClick')
		.live('click', function() {
		    var self = $(this);
			var href = self.attr('href');
			
			self.children().fadeOut('slow');
//			self.children().attr("src", "/images/loading_18.gif");
			$.post(href, function(data) {
				self.parent().parent().replaceWith(data);
			});
			return false;
		});
}

function _searchBox() {
	var hasText = false;

	$('.SearchBox input')
	.focus(function() {
		$(this).removeClass('SearchBox-nofocus')
		$(this).addClass('SearchBox-focus');
		
		var t = $(this).attr('value');
		if (!hasText) {
			$(this).attr('value', '');
		} 
		
	})
	.blur(function() {
		$(this).removeClass('SearchBox-focus')
		$(this).addClass('SearchBox-nofocus');
		
		var t = $(this).attr('value');
		if (t == '') {
			hasText = false;
			var t = $(this).attr('value', 'Search');
		} else {
			hasText = true;
		}
	})
	.addClass('SearchBox-nofocus');
}

function decorate() {
	_stars();
	_searchBox();
	
	$('.StatusTag').popupMenu('#IssueMenu', function(link, menu) {
		var id = link.attr('id').substring(6);
		var action = menu.attr('id').substring(6);
		var link = "/" + id + "?status=" + action;

		$.post(link, function(data) {
			location.reload(true);
		});		
	});
}

window['decorate'] = decorate; 
 