$.fn.hycustooltip = (function ()
{
	$(this).each(function(index) {
		var httclass = 'htt'+(index + 1);
		$(this).addClass(httclass);
		var sticky = $('.'+httclass).attr('httsticky');
		if(sticky == 'true'){
			$('.'+httclass).live('mouseenter', function(){ showhycustooltip(httclass); });
			$('.hycus-tooltip-wrap-'+httclass).live('mouseleave', function(){ removehycustooltip(httclass); });
		}else{
			$('.'+httclass).live('hover', function(){ showhycustooltip(httclass); });
			$('.'+httclass).live('mouseleave', function(){ removehycustooltip(httclass); });
		}
	});
});

function showhycustooltip(httclass){
	$('.hycus-tooltip-wrap').remove();
	var elementpos = $('.'+httclass).position();
	var elementwidth = $('.'+httclass).width();
	var elementheight = $('.'+httclass).height();

	var htttype = $('.'+httclass).attr('htttype');
	if(htttype){ var type = htttype; }
	if(!type){ var type = 'title'; }

	var httheight = $('.'+httclass).attr('httheight');
	if(httheight){ var height = httheight; }
	if(!height){ var height = '20'; }

	var httwidth = $('.'+httclass).attr('httwidth');
	if(httwidth){ var width = httwidth; }
	if(!width){ var width = '200'; }

	var httbordercolor = $('.'+httclass).attr('httbordercolor');
	if(httbordercolor){ var bordercolor = httbordercolor; }
	if(!bordercolor){ var bordercolor = '#000000'; }

	var httbgcolor = $('.'+httclass).attr('httbgcolor');
	if(httbgcolor){ var bgcolor = httbgcolor; }
	if(!bgcolor){ var bgcolor = '#ffffff'; }

	var htttxtcolor = $('.'+httclass).attr('htttxtcolor');
	if(htttxtcolor){ var txtcolor = htttxtcolor; }
	if(!txtcolor){ var txtcolor = '#000000'; }

	var httposition = $('.'+httclass).attr('httposition');
	if(httposition){ var position = httposition; }
	if(!position){ var position = 'topleft'; }

	switch(position){
		case 'topleft':
			var wrapstyle = 'top:'+(elementpos.top - height - 25)+'px; left:'+(elementpos.left + elementwidth -10)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-top: 20px solid '+bordercolor+'; border-right: 20px solid transparent;position:relative;left:10px;';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<div class="hycus-tooltip-content" style="'+icontentstyle+'"></div><div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div>';
			break;
		case 'topmiddle':
			var wrapstyle = 'top:'+(elementpos.top - height - 25)+'px; left:'+(elementpos.left - width/2 + elementwidth/2)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 20px solid '+bordercolor+';';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<div class="hycus-tooltip-content" style="'+icontentstyle+'"></div><center><div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div></center>';
			break;
		case 'topright':
			var wrapstyle = 'top:'+(elementpos.top - height - 25)+'px; left:'+(elementpos.left - width + 10)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-top: 20px solid '+bordercolor+'; border-left: 20px solid transparent;position:relative;left:'+(width - 30)+'px;';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<div class="hycus-tooltip-content" style="'+icontentstyle+'"></div><div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div>';
			break;
		case 'bottomleft':
			var wrapstyle = 'top:'+(elementpos.top + elementheight)+'px; left:'+(elementpos.left + elementwidth -10)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-bottom: 20px solid '+bordercolor+'; border-right: 20px solid transparent; position:relative;left:10px;';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div><div class="hycus-tooltip-content" style="'+icontentstyle+'"></div>';
			break;
		case 'bottommiddle':
			var wrapstyle = 'top:'+(elementpos.top + elementheight)+'px; left:'+(elementpos.left - width/2 + elementwidth/2)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 20px solid '+bordercolor+'; ';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<center><div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div></center><div class="hycus-tooltip-content" style="'+icontentstyle+'"></div>';
			break;
		case 'bottomright':
			var wrapstyle = 'top:'+(elementpos.top + elementheight)+'px; left:'+(elementpos.left - width + 10)+'px;';
			var arrowstyle = 'width: 0; height: 0; border-bottom: 20px solid '+bordercolor+'; border-left: 20px solid transparent;position:relative;left:'+(width - 30)+'px;';
			var icontentstyle = 'border:thin solid '+bordercolor+'; background-color:'+bgcolor+';padding:5px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;height:'+height+'px;';
			var hycusttinner = '<div class="hycus-tooltip-arrow" style="'+arrowstyle+'"></div><div class="hycus-tooltip-content" style="'+icontentstyle+'"></div>';
			break;
	}

	$('body').append('<div class="hycus-tooltip-wrap hycus-tooltip-wrap-'+httclass+'" style="width:'+width+'px;position:absolute;'+wrapstyle+'">'+hycusttinner+'</div>');

	if( type == 'title'){
		var ttcontent = $('.'+httclass).attr('title');
		$('.hycus-tooltip-wrap-'+ httclass + ' .hycus-tooltip-content').html(ttcontent);
	}else if(type == 'ajax'){
		var ajaxlink = $('.'+httclass).attr('link');
		$.ajax({
			url: ajaxlink,
			success: function(html){
				$('.hycus-tooltip-wrap-'+ httclass + ' .hycus-tooltip-content').html(html);
			}
		});
	}
}
function removehycustooltip(httclass){
	$('.hycus-tooltip-wrap-'+ httclass).remove();
}
