/*
 *  Adds an annoying Clippy with tips on the page
 *  @author:  H. Yankov (hristo.yankov at gmail dot com)
 *  @version: 1.0.0 (Jan/19/2011)
 *	http://yankov.us
 */

var __clippyOpts;
 
function __changeClippyText() {
	var msg = __clippyOpts.hints[__getRandomInt(0, __clippyOpts.hints.length - 1)];
	$("div.clippyText").html(msg);
	setTimeout("__changeClippyText();", __clippyOpts.hintChangeTimeOut);
}

function __relocateClippy() {
	var x = __getRandomInt(10, $(document).width() - 50);
	var y = __getRandomInt(10, $(document).height() - 50);
	$("div.clippyContainer").css({ left: x, top: y});
}

function __getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
 
(function($) {
$.fn.addClippy = function(options) {
	var targetNode = $(this);

	// Init
	$.fn.addClippy.defaults = {
		initialPosition: { left: 100, top: 100 },
		initialPositionRandom: false,
		hints: [],
		hintChangeTimeOut: 10000,
		icon: 'Clippy.png',
		initialText: 'It looks like you need help',
		relocateOnHover: false
	}
    __clippyOpts = $.extend({}, $.fn.addClippy.defaults, options);
	
	// Add Clippy's DOM to the page
	targetNode.append(
		"<div class='clippyContainer'>" +
			"<div class='clippyIcon'><img src='" + __clippyOpts.icon + "'></div>" +
			"<div class='clippyText'>" + __clippyOpts.initialText  + "</div>" +
			"<div class='clippyClear'></div>" +
		"</div>"
	);
	
	// Clippy should be positioned randomly
	if (__clippyOpts.initialPositionRandom)
		__relocateClippy();
	// Clippy should have a fixed position
	else
		$("div.clippyContainer").css(__clippyOpts.initialPosition);
	
	// Clippy should be repositioned on hover
	if (__clippyOpts.relocateOnHover)
		$("div.clippyIcon").hover(__relocateClippy, function () { });
	
	// Set the timer job to change the hints
	setTimeout("__changeClippyText();", __clippyOpts.hintChangeTimeOut);
};
})(jQuery);
