/** Class for creating and managing a scrollbar.
	Counts the current position of cursor and which scrollable elements should be displayed. */
var Scrolling = {

	// Id of the scrollbar
	scrollbarId: 'scrollbar',
	// Number of elements on one page
	elemsPerPage: 5,
	// Number of all elements
	noElements: 0,
	// Ordinal number of the first element
	elementId: 0,
	// Top y-coordinate of the scrollbar
	scrollY: null,
	// Height of the scrollbar
	scrollSize: null,
	// Current position of the cursor
	scrollPos: null,
	// Height of the cursor
	barHeight: null,
	// Indicates whether the mouse button is pressed or not
	mouse: false,
	// Indicates whether the cursor was moved or not (otherwise the scrollbar area was clicked)
	moveBar: false,
	// Stores a timestamp about a scrolling event if useful
	date: 0,

	/**
	 * Generate the scrollbar and counts the basic parameters of it.
	 *
	 * @param elementId   Parent element of the scrollbar.
	 */
	init: function(elementId) {
		document.getElementById(elementId).innerHTML = Scrolling.generateScrollbar();
	    Scrolling.scrollSize = $('#scroll').css('height');
		Scrolling.scrollSize = parseInt(Scrolling.scrollSize.substr(0, Scrolling.scrollSize.length-2));
	    Scrolling.barHeight = $('#bar').css('height');
		Scrolling.barHeight = parseInt(Scrolling.barHeight.substr(0, Scrolling.barHeight.length-2));
		$('#bar').css('top', -Math.floor(Scrolling.barHeight / 2)+"px");
	    Scrolling.scrollY = Scrolling.findYPos(document.getElementById('scroll'));
	},

	/**
	 * Find the first element from the scrollbar position.
	 *
	 * @return integer of the ordinal number of the element.
	 */
	findFirstElement: function() {
		Scrolling.scrollPos = Scrolling.findYPos(document.getElementById('bar')) - Scrolling.scrollY + Math.floor(Scrolling.barHeight / 2);
		Scrolling.lastElementId = Scrolling.noElements - Scrolling.elemsPerPage;
		Scrolling.elementId = Math.floor(Scrolling.scrollPos / Scrolling.scrollSize * Scrolling.lastElementId);
		if (Scrolling.elementId > Scrolling.lastElementId) {
			Scrolling.elementId = Scrolling.lastElementId;
    	}
    	return Scrolling.elementId;
	},

	/**
	 * Replace the scrollbar cursor.
	 *
	 * @param id   Id of the element, which assigns the new position of the cursor.
	 */
	replaceScrollbar: function(id) {
		Scrolling.elementId = id;
    	Scrolling.lastElementId = Scrolling.noElements - Scrolling.elemsPerPage;
    	if (Scrolling.lastElementId > 0) {
    		if (Scrolling.lastElementId < id) {
    			Scrolling.lastElementId = id;
			}
    		Scrolling.scrollPos = Math.ceil(Scrolling.elementId / Scrolling.lastElementId * Scrolling.scrollSize);
		} else if (Scrolling.elementId == 0) {
			Scrolling.scrollPos = 0;
		} else {
			Scrolling.scrollPos = Scrolling.scrollSize;
		}
		$('#bar').css('top', (Scrolling.scrollPos - Math.floor(Scrolling.barHeight / 2)) + "px");
	},

	/**
	 * Replace the scrollbar cursor depending on current mouse position.
	 *
	 * @param e   Mouse event object.
	 */
	mouseMoveScrollbar: function(e) {
	    if (Scrolling.noElements <= Scrolling.elemsPerPage) {
			return false;
	    }
	    if (e.pageY < Scrolling.scrollY) {	
			$('#bar').css('top', - Math.floor(Scrolling.barHeight / 2) + "px");
	    } else if (e.pageY > Scrolling.scrollY + Scrolling.scrollSize) {	
			$('#bar').css('top', (Scrolling.scrollSize - Math.floor(Scrolling.barHeight / 2)) + "px");
	    } else {	
			$('#bar').css('top', (e.pageY - Scrolling.scrollY - Math.floor(Scrolling.barHeight / 2)) + "px");
	    }
	    Scrolling.findFirstElement();
	},

	/**
	 * Count the new first element's number when paging.
	 *
	 * @param direction   The paging direction. Forward if true, backward if false.
	 */
	pageUpDown: function(direction) {
	    if (direction == true) {
	    	Scrolling.elementId += Scrolling.elemsPerPage;
	    	if (Scrolling.elementId > Scrolling.noElements - Scrolling.elemsPerPage) {
	    		Scrolling.elementId = Scrolling.noElements - Scrolling.elemsPerPage;
			}
		} else if (direction == false) {
	    	Scrolling.elementId -= Scrolling.elemsPerPage;
	    	if (Scrolling.elementId < 0) {
	    		Scrolling.elementId = 0;
			}
		}
	},

	/**
	 * Find the left position of an object.
	 *
	 * @param obj   The object.
	 * @return integer, the left position in pixels.
	 */
	findXPos: function(obj) {
	    var curleft = 0;
	    if (obj.offsetParent != null) {
			do {
				curleft += obj.offsetLeft;
			} while (obj = obj.offsetParent);
			return curleft;
	    }
	},

	/**
	 * Find the top position of an object.
	 *
	 * @param obj   The object.
	 * @return integer, the top position in pixels.
	 */
	findYPos: function(obj) {
	    var curtop = 0;
	    if (obj.offsetParent != null) {
			do {
		    	curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return curtop;
	    }
	},

	/**
	 * Generate the html representation of the scrollbar.
	 *
	 * @return string of the result html.
	 */
	generateScrollbar: function() {
		var html = '<div id="'+Scrolling.scrollbarId+'">' +
    		'<div id="prev" class="arrow">&nbsp;&nbsp;&nbsp;</div>' +
    		'<div class="dummy"><p /></div>' +
			'<div id="scroll">' +
    		'<div id="bar"><p /></div></div>' +
    		'<div class="dummy"><p /></div>' +
			'<div id="next" class="arrow">&nbsp;&nbsp;&nbsp;</div></div>';
    	return html;
	},

	/**
	 * Show or hide the scrollbar.
	 *
	 * @param visible   True, if the scrollbar should be visible, false otherwise.
	 */
	setVisible: function(visible) {
		var scrollbarObj = document.getElementById(Scrolling.scrollbarId);
		if (visible) {
			scrollbarObj.style.display = 'block';
		} else {
			scrollbarObj.style.display = 'none';
		}
	}
};

/**
 * Disable text selection plug-in for JQuery.
 * For the purpose to avoid result pane text selection during the scrolling.
 */
 
(function($) {
    if ($.browser.mozilla) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'none'
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).bind('selectstart', function() {
                    return false;
                });
            });
        };
    } else {
        $.fn.disableTextSelect = function() {
            return this.each(function() {
                $(this).mousedown(function() {
                    return false;
                });
            });
        };
    }
})(jQuery);

(function($) {
    if ($.browser.mozilla) {
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).css({
                    'MozUserSelect' : 'text'
                });
            });
        };
    } else if ($.browser.msie) {
        $.fn.enableTextSelect = function() {
            return this.each(function() {
                $(this).unbind('selectstart');
            });
        };
    } else {
        $.fn.enableTextSelect = function() {
            return this.each(function() {
				$(this).unbind('mousedown');
            });
        };
    }
})(jQuery);
