/*
 * easyStTip - jQuery Plugin
 * 
 * Copyright (c) 2010 Joachim Doerr
 * Hinderling Volkart AG
 *
 * Version: 1.0.2
 * Requires: jQuery v1.3+
 *
 */

// small fix for old safari versions compatibility

// this bug occures 
// on iPhone OS since 3.2 version (iPad)
// and on iOS since 4.0 version

// as of 1.4.2 the mobile safari reports wrong values on offset()
// http://dev.jquery.com/ticket/6446
// remove once it's fixed
if (/webkit.*mobile/i.test(navigator.userAgent)
	&& "getBoundingClientRect" in document.documentElement) {
	(function ($) {
		$.fn.offsetOld = $.fn.offset;
		$.fn.offset = function () {
			var result = this.offsetOld();
			result.top -= window.scrollY;
			result.left -= window.scrollX;

			return result;
		};
	})(jQuery);
}

(function($){  

	var _easysttip, _title_text, yOffset, xOffset, _this_position, _nposition,_t=0,_l=0,_pl,_pr;
	
	$.fn.easysttips = function (options) {

		var defaults = { 
			slideway: 15,
			showSpeed: 110,
			hideSpeed: 110,
			sttp_content: 'nextdev',
			position: 'right',
			thema: 'long'
	  	};
		var _options = $.extend(defaults, options);
		var _easy_frame = '<div class="easysttip easysttip-position-' + _options.position + ' easysttip-thema-' + _options.thema + '"><div class="easysttip-tip-wrapper"><div class="easysttip-content-inner"></div><div class="easysttip-top"></div><div class="easysttip-right"></div><div class="easysttip-bottom"></div><div class="easysttip-left"></div></div></div>';

		$(this).hover(function() {
			
			var _this = $(this);
			var _text = _this.next('.easysttip-content:eq(0)');
			var _this_span = _this.parent().find('span');

			$('body').append(_easy_frame);
			_easysttip = $('div.easysttip').hide();
			
			if(_text.length != 0 || _options.sttp_content === 'nextdev'){
				var _content = _text.html();
			} else if(_options.sttp_content === 'title') {
				var _content = _this.attr('title');
			} else {
				var _content = _this.attr('title');
			}

			$('.easysttip .easysttip-content-inner').html(_content);
			_title_text = _this.attr('title');
			_this.attr('title', '');
			
			//	yOffset = _easysttip.height();
			//	xOffset = (((_easysttip.width() - 10) / 2)) - (_this.width() / 2);

			_this_position = $('#'+ this.id).offset();
			_nposition = _this_position;
			_this_height = parseInt(((_easysttip.height()/2)+5)-(_this.height()/2));
			_this_width = $('#'+ this.id +' span').width();

			_this.find('.easysttip-left, .easysttip-right').css({'height':_easysttip.height()});

			if(_options.position=='right') {
				if(($('body').width())-($(this).offset().left) < $('.easysttip').width()){
					_easysttip.removeClass('easyright').addClass('easyleft');
					_l = -($('.easysttip').width())+10-_this_width;
				} else {
					_easysttip.removeClass('easyleft').addClass('easyright');
					_l = 0;
				}
			}

			_pr = _this_width;
			_pl = _this_width;
			
			_nposition.top = parseInt(_this_position.top - _this_height);
			_nposition.left = parseInt(_this_position.left)-5+(_l);
			
			_easysttip.css(_nposition).css({'position':'absolute','z-index':'1000'});
			if(jQuery.support.opacity) {
				_easysttip.css({'padding-left':_pl+_options.slideway,'padding-right':_pr,'opacity':'0','display':'block'});
				_easysttip.animate({'padding-left':_pl,'opacity':'1'}, _options.showSpeed, function() {});
			} else {
				_easysttip.css({'padding-left':_pl+_options.slideway,'padding-right':_pr,'display':'block'});
				_easysttip.animate({'padding-left':_pl}, _options.showSpeed, function() {});
			}

			_easysttip.hover(function() {
				_this_span.addClass('hover');
			} , function() {				
				// auch als clickfunktion wegen ipad anlegen
				_this_span.removeClass('hover');
				if(jQuery.support.opacity) {
					_easysttip.animate({'padding-left':_pl+_options.slideway,'opacity':'0'}, _options.showSpeed, function() {
						_easysttip.remove();
					});
				} else {
					_easysttip.animate({'padding-left':_pl+_options.slideway}, _options.showSpeed, function() {
    					_easysttip.css({'display':'none'});
						_easysttip.remove();
					});
				}
			
			});

			if ( /webkit.*mobile/i.test(navigator.userAgent)) {
			// Todo: 
			// leg ein overlay unter transparent unter den tip - analog zur lightbox
			// onclick auf ein anderes element schließt zuerst der tip da click auf overlay geht
			
			_easysttip.click(function() {
				_this_span.removeClass('hover');
				if(jQuery.support.opacity) {
					_easysttip.animate({'padding-left':_this_width+_options.slideway,'opacity':'0'}, _options.showSpeed, function() {
						_easysttip.remove();
					});
				} else {
					_easysttip.animate({'padding-left':_this_width+_options.slideway}, _options.showSpeed, function() {
    					_easysttip.css({'display':'none'});
						_easysttip.remove();
					});
				}
			});
			}

		}, function() {
			
			$(this).attr('title', _title_text);
		
			//	$('div.easysttip').fadeOut(_options.hideSpeed, function() {
			//		$(this).remove();
			//	});
			
		});
		
	}

})(jQuery);
