
function hideMessage(ref, header) {
	var that = $("#" + ref);
	var element = $("#message");
	var elementHeader = $(".messageHeader");
	var elementContent = $(".messageContent");
	
	elementHeader.html("");
	elementContent.html("");
	element.fadeOut(250);
}

function showMessage(ref, header, containerRef, rightOf) {
	var that = $("#" + ref);
	var element = $("#message");
	var elementHeader = $(".messageHeader");
	var elementContent = $(".messageContent");
	var container = (containerRef) ? $("#" + containerRef) : $("#bodyWrapper");
	var bbContainer = new BoundBox();
	bbContainer.loadFromElement(container);
	
	if (!rightOf || typeof(rightOf) != "boolean")
	    rightOf = false;
	
	var offset = that.offset();
	var width = $("#" + ref + " > a").width();
	var height = $("#" + ref + " > a").height();
	if (rightOf)
	    offset.left += width + 5;
	offset.top += height + 2;
	
	var bbElement = new BoundBox(offset.top, offset.left, (offset.top + element.height()), (offset.left + element.width()));
	bbElement.constrain(bbContainer); // constrain offset to container

	element.css("top", bbElement.top + "px");
	element.css("left", bbElement.left + "px");
	
	element.css("z-index", "999");
	elementHeader.html(header);
	elementContent.html($("#" + ref + " > span").html());
	element.fadeIn(500);
}

function BoundBox(top, left, bottom, right) {
    this.top = top;
    this.left = left;
    this.bottom = bottom;
    this.right = right;
}
BoundBox.prototype.toString = function() {
    return this.top + "," + this.left + "," + this.bottom + "," + this.right;
}
BoundBox.prototype.loadFromElement = function(element) {
    if (element && element != null) {
	this.top = (isNaN(parseInt(element.css("top")))) ? 0 : parseInt(element.css("top"));
	this.left = (isNaN(parseInt(element.css("left")))) ? 0 : parseInt(element.css("left"));
        this.bottom = this.top + element.height();
        this.right = this.left + element.width();
    }
}
BoundBox.prototype.constrain = function(boxConstraint) {
    if (boxConstraint && boxConstraint != null) {
        var difference = 0;
        if (this.top < boxConstraint.top) {
            difference = (boxConstraint.top - this.top);
            this.top += difference;
            this.bottom += difference;
        }
        if (this.left < boxConstraint.left) {
            difference = (boxConstraint.left - this.left);
            this.left += difference;
            this.right += difference;
        }
        if (this.right > boxConstraint.right) {
            difference = (boxConstraint.right - this.right);
            this.right += difference;
            this.left += difference;
        }
        if (this.bottom > boxConstraint.bottom) {
            difference = (boxConstraint.bottom - this.bottom);
            this.bottom += difference;
            this.top += difference;
        }
    }
}

function limitCharInput(limit, input, display) {
  limit || 256;
  input = $(input);
  display = $(display);

  var curLen = input.val().length;
  display.html(limit - input.val().length);
}

$(document).ready(function() {
    var eleSearch = $("#txtTopSearch");
    if (eleSearch.val().length == 0 && !eleSearch.hasClass("search-watermark"))
        eleSearch.addClass("search-watermark");

    eleSearch.focus(function() {
        $(this).removeClass("search-watermark");
    });
    eleSearch.blur(function() {
        if ($(this).val().length == 0 && !$(this).hasClass("search-watermark"))
            $(this).addClass("search-watermark");
    });
});
