﻿(function($){
  $.floatbox=function(options)
  {
    var isie6 = $.browser.msie && $.browser.version=='6.0';
    var pageHeight = $(window.document).height()>$(window).height()?$(window.document).height():$(window).height();
    var boxHeight = parseInt($(window).height()/2,10);

    var settings=$.extend(true,
    {
      bg:'floatbox_bg',
      box:'floatbox_box',
      width:parseInt($(window).width()/2,10),
      height:'auto',
      url:'/',
      finish:function(data){},
      fade:false,
      bgConfig:
      {
        position:'absolute',
        zIndex:8,
        width:'100%',height:pageHeight+'px',top:'0px',left:'0px',
        opacity:'0.45',
        backgroundColor:'#FFF',display:'none'
      },
      boxConfig:
      {
        position:(isie6)?'absolute':'fixed',
        zIndex:9,
        width:'50%',height:'auto',top:'50%',left:'50%',
        border:'6px solid #919457',
        backgroundColor:'#fff',display:'none'
      }
    },
    options);
    var showBox=function()
    {
      var content = '\
      <div class="close_floatbox" style="position:absolute;right:6px;top:6px;_width:16px;_height:16px;">\
        <a href="javascript:void(0);" style="font-weight:bold;text-decoration:none;font-size:13px;color:#000">×</a>\
      </div>\
      <div class="content" style="overflow:hidden">\
        <iframe src="'+ settings.url +'" frameborder="0" style="width:100%"></iframe>\
      </div>';
      $('<div></div>')
        .attr('id',settings.bg)
        .css(settings.bgConfig)
        .appendTo('body')
        .end();
      $('<div></div>')
        .attr('id',settings.box)
        .html(content)
        .css(settings.boxConfig)
        .appendTo('body')
        .css('width',settings.width+'px')
        .css('margin-left','-'+$('#'+settings.box).width()/2+'px')
        .css('margin-top','-'+$('#'+settings.box).height()/2+'px')
        .find('.close_floatbox')
        .bind('click',function(){closeBox();})
        .end();
      if(settings.fade){
        $('#'+settings.bg).fadeIn(200,function(){
          $('#'+settings.box).fadeIn(200);
        });
      }
      else{
        $('#'+settings.bg).show().parent().find('#'+settings.box).show();
      }
    };
    var closeBox=function()
    {
      if(settings.fade){
        $('#'+settings.box).fadeOut(200,function(){
          $('#'+settings.bg).fadeOut(200,function(){
            $('#'+settings.box).remove();
            $('#'+settings.bg).remove();
          });
        });
      }
      else{
        $('#'+settings.box+',#'+settings.bg).hide();
        window.setTimeout(function(){
          $('#'+settings.box).remove();
          $('#'+settings.bg).remove();
        },500);
      }
    };
    var init=function(){
      showBox();
      $(document).one('keypress',function(e)
      {
        var escKey=$.browser.mozilla?0:27;
        if(e.which===escKey){
          closeBox();
        }
      })
      .one('keydown',function(e){
        var escKey=$.browser.mozilla?0:27;
        if(e.which===escKey){
          closeBox();
        }
      });

      $('#'+settings.box+' iframe').load(function(){
        var winh = $(this).contents().find('body').height() + 10;
        if (winh < 200) winh = 220;
        $(this).css('height',winh+'px');
        if (settings.height == 'auto') {
          $('#'+settings.box+' .content').height(winh);
        }
        else {
          $('#'+settings.box+' .content').height(settings.height);
        }
        $('#'+settings.box).css('margin-top','-'+($('#'+settings.box).height()/2)+'px');
        $(this).contents().find('#faceBoxComplete').bind('click',function(){
          closeBox();
          //var r = $(this.document).find('#faceBoxReturnData').html();
          var r = window.frames[window.frames.length-1].faceBoxReturnData;
          settings.finish(r);
        });
      });
      if(isie6){
        //$('body, html').css({height:'100%',width:'100%'});
        $(window).bind('scroll',function(){
          $('#'+settings.box).css('top',document.documentElement.scrollTop+($(window).height()/2)+'px');
        });
      }
    };
    init();
  };
})(jQuery);

