(function(){
    
    $.fn.fTooltip = function(options){
        
        var s = $.extend({
            className : 'f-tooltip',
            offsetX : 20, /* offset (horizontal) of tip from top corner of input */ 
            offsetY : -5, /* offset (vertical) of tip from top corner of input */
            position : 'right', /* position of tip in relation to input */
            animationIn : { opacity: 1 },
            animationOut : { opacity: 0 },
            animationSpeed : 200,
            infoClass : 'user_tip'
        }, options);
        
        return this.each(function(){
            
            var contentDiv = $('<div class="' + (s.position === 'left' ? 'right' : 'left') + '-content" />'),
                tooltip = $('<div><div class="' + (s.position === 'left' ? 'right' : 'left') + '-arrow"/></div>')
                              .append(contentDiv)
                              .hide()
                              .addClass(s.className)
                              .css({position: 'absolute',zIndex: 100})
                              .appendTo('body');
            
            function appear() {
                
                var infoEl = $(this).next(),
                    input = $(this),
                    cssLeft = input.offset().left + (s.position === 'right' ? input.outerWidth() : -tooltip.outerWidth()) + s.offsetX;
                    
                if (!infoEl[0] || !infoEl.hasClass(s.infoClass) || !infoEl.html()) { return; }
                
                contentDiv.html(infoEl.html());
                
                tooltip
                    .css({
                        top : input.offset().top + s.offsetY + 'px',
                        left : cssLeft + 'px',
                        opacity: 0,
                        display: 'block'
                    })
                    .stop().animate(s.animationIn, s.animationSpeed);
            }
            
            function disappear() {
                if($(this).data('focused')){return;}
                tooltip.stop().animate(s.animationOut, s.animationSpeed, function(){
                    $(this).css('display', 'none');
                });
            }
            
            $('input,textarea', this)
                .unbind('.fTooltip')
                .bind('focus.fTooltip', function(){
                    $(this).data('focused', true);
                    appear.call(this);
                })
                .bind('blur.fTooltip', function(){
                    $(this).data('focused', false);
                    disappear.call(this);
                })
                .bind('mouseover.fTooltip', appear)
                .bind('mouseout.fTooltip', disappear);
            
        });
        
    };
    
    $('form#quoteForm').fTooltip({
        position : 'left', /* Tooltip appears to the LEFT of the form */
        offsetX : -20
    });
    $('form#quoteForm').fTooltip({
        position : 'right', /* Tooltip appears to the RIGHT of the form */
        offsetX : 20
    });
    
    var myWidth = 0, myOffSet = -20, myPosition = 'left';
  	if( typeof( window.innerWidth ) == 'number' ) {
    	myWidth = window.innerWidth;
  	} else if( document.documentElement && ( document.documentElement.clientWidth) ) {
    	myWidth = document.documentElement.clientWidth;
 		}
  	
  	if(myWidth > 1350) { myOffSet = 20; myPosition = 'right' }
    $('form#quickContact').fTooltip({
        position : myPosition, /* Tooltip appears to the LEFT of the form */
        offsetX : myOffSet
    });
    
})();