﻿/**
 * jQuery-ToolTipPosition-1.0
 *
 * 文件支持:    jquery.1.2.6.js
 * Copyright (c) 2008 TGL
 *
 * $建立时间: 2008-10-22 $
 *
 **/
 (function($) {
     $.fn.extend({
         toolTipPosition: function(options) {
             options = $.extend({
                 obj: null,               //tip对象
                 Horizontal: 0,           //水平偏移量
                 Vertical: 0,             //垂直偏移量
                 offsetHorizontal: true,  //水平偏移
                 offsetVertical: true,     //垂直偏移
                 conterHorizontal: true,  //验证过中线
                 conterVertical: true,
                 VerifyVertical: false  //验证越界上界
             }, options);
             var obj = this.eq(0);
             var offset = obj.offset();
             offset.conterVertical = false;
             offset.conterHorizontal = false;
             var size = { width: obj[0].offsetWidth, height: obj[0].offsetHeight };
             if (options.offsetHorizontal) {
                 // var t = (document.documentElement.clientHeight - temp.height()) / 2 + $(document).find("html")[0].scrollTop; 
                 if (((offset.left - $(document).find("html")[0].scrollLeft) * 2 + size.width) > document.documentElement.clientWidth && options.conterHorizontal) {//过了中间线
                     offset.left -= ($(options.obj).width() + options.Horizontal);
                     offset.conterHorizontal = true
                 } else {//没有过中线
                     offset.left += (size.width + options.Horizontal);
                 }
             } else {
                 offset.left += options.Horizontal;
             }
             if (options.offsetVertical) {
                 if (((offset.top - $(document).find("html")[0].scrollTop) * 2 + size.height) > document.documentElement.clientHeight && options.conterVertical) {//过了中间线
                     offset.top -= ($(options.obj).height() + options.Vertical);
                     offset.conterVertical = true
                 } else {//没有过中线
                     offset.top += (size.height + options.Vertical);
                 }
             } else {
                 offset.top += options.Vertical;
             }
             if (options.VerifyVertical) {
                 if (offset.top < $(document).find("html")[0].scrollTop + 10)
                     offset.top = $(document).find("html")[0].scrollTop + 10;
             }
             return offset;
         },
         toolTipCenter: function(elem) {
             var obj = $(this).eq(0);
             elem = $(window);
             var options = {
                 left: (elem.width() - obj.width()) / 2,
                 top: (elem.height() - obj.height()) / 2
             };
             obj.css("left", options.left).css("top", options.top);
             return this;
         }
     });

 } (jQuery))