﻿/* 李小华开发的基于jQuery浮动广告插件，可用于浮动于页面左右两边的在线咨询等方面。
 *
 * 说明：该插件基于网络上的一位作者写的插件，尊姓大名我忘记，在此表示感谢！
 * 修改了：代码结构、呈现样式、增加了关闭时可再现功能、扩展了配置。
 *
 * 2010年09月20日。
 * 联系QQ：191038449
 * MSN:lxh0129@163.com
 */
(function($) { 
    $.fn.jFloat = function(o) {
        o = $.extend({
            top:60,//广告距页面顶部距离
            left:0,//广告左侧距离
            right:0,//广告右侧距离
            width:100,//广告容器的宽度
            height:360,//广告容器的高度
            minScreenW:800,//出现广告的最小屏幕宽度，当屏幕分辨率小于此，将不出现对联广告
            position:"left",//对联广告的位置left-在左侧出现,right-在右侧出现
            allowClose:true, //是否允许关闭 
            title:'在线咨询',
            closeText:'x',
            headerCss:'',
            boardCss:'',
            contentCss:'',
            closeShowCss:''//关闭后再现的css
        }, o || {});
      var static_header_style="padding:2px;padding-top:6px;z-index:2000;font-size:12px;border-bottom:1px solid #f1f1f1;color:white;height:14px;";//静态样式。
      var static_closebtn_style="border:1px solid #000;border:none;height:12px;display:block;width:12px;text-align:center;float:right;cursor:pointer;";//静态关闭按钮样式。
      var static_closebtn_id="jfloat_closebtn";
      var static_header_id="jfloat_header";
      
      var closeshow_object=createCloseShow(o.position);//当关闭时，附在窗口旁边的显示块。创建的时候是隐藏的。
      
	  var h=o.height;
      var showAd=true;
      var fDiv=$(this);
      if(o.minScreenW>=$(window).width()){
          fDiv.hide();
          showAd=false;
       }
       else{
		   fDiv.css("display","block");
           var headerHtml='';
           if(o.headerCss=='')
           {
                headerHtml='<div id="'+static_header_id+'" style="'+static_header_style+'" class="closeFloat">'+o.title+'<span id="'+static_closebtn_id+'" style="'+static_closebtn_style+'" title="close">'+o.closeText+'</span></div>';
           }
           else
           {
                headerHtml='<div id="'+static_header_id+'" style="'+static_header_style+'" class="'+o.headerCss+'">'+o.title+'<span id="'+static_closebtn_id+'" style="'+static_closebtn_style+'" title="close">'+o.closeText+'</span></div>';
           }
           switch(o.position){
               case "left":
                    if(o.allowClose){
                       fDiv.prepend(headerHtml);
					   $("#"+static_closebtn_id,fDiv).click(function(){fDiv.hide();showCloseShow();showAd=false;})
					   h+=20;
					}
                    fDiv.css({position:"absolute",left:o.left+"px",top:o.top+"px",width:o.width+"px",height:h+"px",overflow:"hidden"});
                    break;
               case "right":
                    if(o.allowClose){
                       fDiv.prepend(headerHtml)
					   $("#"+static_closebtn_id,fDiv).click(function(){fDiv.hide();showCloseShow();showAd=false;})
					   h+=20;
					}
                    fDiv.css({position:"absolute",left:"auto",right:o.right+"px",top:o.top+"px",width:o.width+"px",height:h+"px",overflow:"hidden"});
                    break;
            };//switch
        };//else
        function ylFloat(){
            if(!showAd){return}
            var windowTop=$(window).scrollTop();
            if(fDiv.css("display")!="none")
                fDiv.css("top",o.top+windowTop+"px");
        };
        function csFloat(){
            var windowTop=$(window).scrollTop();
            if(closeshow_object.css("display")!="none")
                closeshow_object.css("top",o.top+windowTop+"px");
        };
        function doFloat(){ylFloat();csFloat();};
        
        function createCloseShow(pos){//pos:left or right
            var close_div_id="jf_closeshow_"+pos;
            var close_div_style="padding:2px;padding-top:20px;z-index:2000;font-size:12px;border:1px solid #f1f1f1;border-left:none;color:white;height:30px;width:10px;vertical-align:middle;cursor:pointer;";
            var showText="";
            switch(o.position){
                case "left":
                    showText="》";
                    break;
                case "right":
                    showText="《";
                    break;
            };
            var closeDiv='<div id="'+close_div_id+'" style="'+close_div_style+'" class="'+o.closeShowCss+'" title="Show Again">'+showText+'</div>';
            $(closeDiv).appendTo($(document.body));
            
            $("#"+close_div_id).css("display","none");
            $("#"+close_div_id).click(function(){$(this).hide();fDiv.show();showAd=true;});//alert($("#"+close_div_id).html());
            return $("#"+close_div_id);
        };
        function showCloseShow(){
            closeshow_object.css("display","block");
            var windowTop=$(window).scrollTop();
            switch(o.position){
                case "left":
                    closeshow_object.css({position:"absolute",left:"0px",top:o.top+windowTop+"px",overflow:"hidden"});
                    break;
                case "right":
                    closeshow_object.css({position:"absolute",left:"auto",right:"0px",top:o.top+windowTop+"px",overflow:"hidden"});
                    break;
            }
        }

      $(window).scroll(doFloat) ;
      $(document).ready(doFloat);     
    }; 
})(jQuery);







