/**
 *  plugin customized for KickXL
 *
 * Copyright (c) 2011 virgial Berveling
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create custom titlelabels.
 *
 * @example $('ul#imageLinks img').divtitle({custom css object});
*/

(function($) { // hide the namespace

$.extend($.ui, {divtitle: {version: "0.1"}});

var PROP_NAME = 'divtitle';

function Divtitle() {	
	this._defaults = { // Global defaults for all the instances
		showOn: 'focus'
	};
	this._css = {color: '#000','font-size': '10px', border: '1px solid #990', background:'#ffe',padding:'3px'}
}

$.extend(Divtitle.prototype, {
	/* Class name added to elements to indicate already configured with a divtitle. */
	markerClassName: 'hasDivTitle',

	addMarkerClass : function(d) {
		d.addClass(this.markerClassName);
	}
})



$.fn.divtitle = function(options){

	/* Initialise the divtitle. */
	if (!$.divtitle.initialized) {
		$('body').append('<div id="divtitleElement" style="display:none;position:absolute;top:0px;left:0px;z-index:1000;height:0px;width:0px"><div id="divtitleText"  style="position:absolute;display:block;text-indent:0px;margin:20px 0px 0px 0px;line-height:10px;width:300px">Title panel</div></div>');
			
		// override custom css stettings
		for (attrname in options) {$.divtitle._css[attrname] = options[attrname];}
		$('#divtitleText').css($.divtitle._css);
	
		$(this).each(function(){
			
			$(this).attr({divtitle : "Klik op deze (i) voor meer info.<br/><br/>" + $($(this).attr('title')+" p:first").html() });
			
			$(this).removeAttr('title').removeAttr('alt'); // alt tag removing for IE7
			$(this).mousemove(function(event) {
				var X = event.pageX;
				
				// check if label jumps offscreen on the right. If so get it back. 
				if (X > ($(window).width()-10-$('#divtitleText').width())) X = ($(window).width())-10-$('#divtitleText').width();

				$('#divtitleElement').css({left: X, top: event.pageY});
			}).mouseenter(function(){ 
				if ($.divtitle.timer) clearTimeout($.divtitle.timer); 
				$.divtitle.timer = setTimeout(function(){$('#divtitleElement').hide();$.divtitle.timer = false},4000);
				$('#divtitleText').html($(this).attr('divtitle'));
				document.titleTimeout = setTimeout(function(){$('#divtitleElement').fadeIn(200);},1000);
                        }).mouseout(function(){
				clearTimeout(document.titleTimeout);
                                $('#divtitleElement').hide();
			});
		});
		$.divtitle.initialized = true;
	}

}

$.divtitle = new Divtitle(); // singleton instance
$.divtitle.initialized = false;
$.divtitle.timer = false;
$.divtitle.uuid = new Date().getTime();
$.divtitle.version = "0.1";

// Workaround for #4055
// Add another global to avoid noConflict issues with inline event handlers
window.DP_jQuery = $;

})(jQuery);

$(function(){
	/*--------------------------- 
	debug (only for console browsers)
	---------------------------*/
	$.debug = function(t){if( typeof(console) != 'undefined') console.log(t)}
	$('ul#imageLinks img').divtitle({color : "#F00"});
})

