/**
 * @author sartois
 */
(function($) {
    
    /**
     * Load the needed CSS
     */
    if( typeof kinkama == "function" ){
    	//include_css("/Views/skins/default/Misc/jQuery.Ui.kToolTip.css");
    	include_css("/Views/skins/default/Misc/jQuery.Ui.ToolTip.css");
    }
    else console.error("jquery.Ui.kTooltip can't get jQuery.Ui.kToolTip.css. Please, check it !");
    
    var arrowEq = {
    	"left-left":"ui-tooltip-arrow-lc",
    	"left-top":"ui-tooltip-arrow-lt",
    	"top-left":"ui-tooltip-arrow-tl",
    	"top-top":"ui-tooltip-arrow-tc",
    	"top-right":"ui-tooltip-arrow-tr",
    	"right-top":"ui-tooltip-arrow-rt",
    	"right-right":"ui-tooltip-arrow-rc",
    	"right-bottom":"ui-tooltip-arrow-rb",
    	"bottom-right":"ui-tooltip-arrow-br",
    	"bottom-bottom":"ui-tooltip-arrow-bc",
    	"bottom-left":"ui-tooltip-arrow-bl",
    	"left-bottom":"ui-tooltip-arrow-lb"
    };
    
    /**
     * ui.kTooltip plugin definition
     */
    $.widget( "ui.kTooltip", {
        
        _init:function() {
            
            return $(this).each(function() {
            
                var o = this.options, self = $(this), content, intervalRef;
                
                if( o.contentType === "title" )
                {	
                	 content  = self.attr("title");
                	 self.attr("title","");
                }
                else{
                	self.attr("title","");
                	/**
                	 * @todo : trop spécifique à Kinkama
                	 */
                	if( typeof o.content.info != "undefined" )
                		content = o.content.info;
                	else
                		content = o.content;
                }
               
                /**
                 * Create the tooltip ( markup, content, and display:none )
                 */
                var markup = ( this.options.customTTipMarkup.length > 0 ) ? o.customTTipMarkup : o.tTipMarkup;
                var hideIt = true ;
                
                for (i = 0; i < errorText.length ; i++) 
                {
                	
                	if ( errorText[i].field != '' && o.id == errorText[i].field ) 
                	{
                		var errorMessage = errorText[i].error ;
                		hideIt = false ;
                		break ;
                	}
                }
                
                var toolTip = $( markup ).attr("id", "ui-toolTip-"+o.id)
                				.width( o.width )
                				.append( content + o.arrowMarkUp )
                				.addClass( arrowEq[o.arrowWay] )
                				;
                
                if ( hideIt == true ) 
                {
                	var toolTip = $( markup ).attr("id", "ui-toolTip-"+o.id)
	      				.width( o.width )
	      				.append( content + o.arrowMarkUp )
	      				.addClass( arrowEq[o.arrowWay] ).hide()
	      				;
                
                }
                else
                {
                	
                	var toolTip = $( markup ).attr("id", "ui-toolTip-"+o.id)
	      				.width( o.width )
	      				.append(errorMessage + o.arrowMarkUp )
	      				.addClass( ' ui-state-error' )
	      				;
                }
                
                if( o.appendToParent === "" ){
                /**
                 * Append the tooltip to the parent label element
                 * Define hover event
                 */
                //console.log( o.nodeName );
                $("label[for='"+o.nodeName+"']")
                	.closest( o.contentWrapper )
                	.append( toolTip )
                	.css("position","relative")
                	.find("#ui-toolTip-"+o.id)
                	.css("position","absolute")
                	.css("left", o.leftOffset )
                	.css("top", o.topOffset )
                	.end()
                	.hover( function(){
                		/* reference sur le tooltip */
                		//console.log( o.id );
                		$("#ui-toolTip-"+o.id).show();
                	},   
                	function(){
                		$("#ui-toolTip-"+o.id).hide(); 
                	});
                }
                else{
                	$( o.appendToParent )
                	.append( toolTip )
                	.css("position","relative")
                	.find("#ui-toolTip-"+o.id)
                	.css("position","absolute")
                	.css("left", o.leftOffset )
                	.css("top", o.topOffset )
                	.end()
                	.hover( function(){
                		/* reference sur le tooltip */
                		//console.log( o.id );
                		$("#ui-toolTip-"+o.id).show();
                	},   
                	function(){
                		$("#ui-toolTip-"+o.id).hide(); 
                	});
                	
                }	
                
                /*
				setTimer = function( element ) {  
                	intervalRef = setInterval(function(element){
                		showTip( element );
                	}, o.delay );  
                };
                
                stopTimer = function() {  
                	clearInterval( intervalRef );  
                };
                
                showTip = function( element ){  
                	stopTimer();  
                	$(element).animate({ "opacity": "1"}, o.speed ); 
                };
                 */
                
            });    
        }, 
        
        destroy: function() {
            // default destroy
            $.widget.prototype.destroy.apply(this, arguments); 
        }

    });
    
    $.extend( $.ui.kTooltip, {
       
        defaults: {
            
            id : "tooltip",
            
            appendToParent : "",
            
            nodeName : "inputWrapper",
            
            contentType : "title", // or "custom"
               
            content: "", // if custom, set the content here
            
            infoClass : "tooltip.info", //css class
            
            errorClass : "tooltip.error", //idem
            
            width:200,
            
            contentWrapper : "p",
            
            leftOffset : "50px" ,
            
            topOffset : "50px" ,
        
            customTTipMarkup : '' ,
            
            arrowWay : "bottom-left", 
             
            tTipMarkup : '<div class="ui-widget ui-widget-content ui-tooltip"></div>',
            	 	
            arrowMarkUp : '\
            	<div id="" class="ui-tooltip-pointer ui-widget-content"> \
    	 			<div class="ui-tooltip-pointer-inner"/> \
    	 		</div> ',
    	 			
    	 	eventsToShowTooltip : "mouseover mouseout mousemove",
            	 	
            eventsToHideTooltip : "",
            
            delay : 300,
            
            speed : 200
            
        }
    });


})(jQuery);
