(function($) {
$.wBox=function(data,klass){},
 
  $.extend($.wBox, {
    settings: {
      opacity      : 0,
      overlay      : true,
      loadingImage : 'http://a.pojaaimg.cn/2010/search/js/wbox/loading.gif',
      closeImage   : 'http://a.pojaaimg.cn/2010/search/js/wbox/close.png',
      title:"普加灯箱效果",
      wBoxHtml  : '\
    <div id="wBox" style="display:none;"> \
      <div class="popup"> \
        <table> \
          <tbody> \
            <tr> \
              <td class="tl"/><td class="b"/><td class="tr"/> \
            </tr> \
            <tr> \
              <td class="b"/> \
              <td class="body"><table class="title"><tr><td><div class="itemTitle"></div></td>\
                  <td width="20px"><div class="close"></div></td></tr></table> \
                </div> \
                <div class="content"> \
                </div> \
              </td> \
              <td class="b"/> \
            </tr> \
            <tr> \
              <td class="bl"/><td class="b"/><td class="br"/> \
            </tr> \
          </tbody> \
        </table> \
      </div> \
    </div>'
    },

    loading: function() {
      init()
      if ($('#wBox .loading').length == 1) return true
      showOverlay()

      $('#wBox .content').empty()
      
      $('#wBox .body').children().hide().end().append('<div class="loading"><img src="'+$.wBox.settings.loadingImage+'"/></div>')

      $('#wBox').css({
        top:    getPageScroll()[1] + (getPageHeight() / 10),
        left:   385.5
      }).show()

      $(document).bind('keydown.wBox', function(e) {
        if (e.keyCode == 27) $.wBox.close()
        return true
      })
      $(document).trigger('loading.wBox')
    },

    reveal: function(data) {
      $(document).trigger('beforeReveal.wBox');      
      $('#wBox .content').append(data);
      $('#wBox .loading').remove();   
      
      $('#wBox .body').children().fadeIn();
      $('#wBox').css('left', $(window).width() / 2 - ($('#wBox table').width() / 2)); 
      $(document).trigger('reveal.wBox').trigger('afterReveal.wBox')
    },

    close: function() {
      $(document).trigger('close.wBox')
      return false
    }
    
  })


  $.fn.wBox = function(settings) {
    init(settings)
    
    function clickHandler() {
      $.wBox.loading(true)
      // support for rel="wBox.inline_popup" syntax, to add a class
      // also supports deprecated "wBox[.inline_popup]" syntax
      $("#wBox .itemTitle").html(this.title);
      $t=$(this); 
      fillBox(this.title,this.href,$t.attr("type"),$t.attr("geo"));
      return false
    }

    return this.click(clickHandler)
  }

 
  function init(settings) {
    var $s = $.wBox.settings
    if ($s.inited) return true
    else $s.inited = true

    $(document).trigger('init.wBox')
    makeCompatible()

    if (settings) $.extend($s, settings)
    $('body').append($s.wBoxHtml)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $s.closeImage
    preload[1].src = $s.loadingImage

    $('#wBox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    });

    $('#wBox .close').click($.wBox.close).hover(function(){
        $(this).addClass("on");     
    },function(){
        $(this).removeClass("on");
    }); 
  }

  function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;   
    }
    return new Array(xScroll,yScroll) 
  }


  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) { // all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }   
    return windowHeight
  }

  function makeCompatible() {
    var $s = $.wBox.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.closeImage = $s.close_image || $s.closeImage
    $s.wBoxHtml = $s.wBox_html || $s.wBoxHtml
  }

  
  function fillBox(name,url,type,geo) {
    var str="";
    switch(type){
        case "map"://地图
        str="<iframe style='width:570px;height:370px;' frameborder='0' src='"+url+"'></iframe>";
        break;
        case "video"://视频
        str="<iframe style='width:497px;height:378px;' frameborder='0' src='"+url+"'></iframe>";
        break;
        case "bus"://公交驾车
        str= '<div class="wBoxBus">';       
        var briefTit = name.substring(0, 12) + (name.length > 12 ? "..." : "");
        str += '<div class="busItem top"><div class="itemleft">到这里来</div><div class="itemcon"><div class="fhitem">出发地：<input type="text" class="inptext" id="FHinput"/></div><div class="fhitem mid">目的地：' + briefTit + '</div><div class="fhInput"><input id="FHbus" type="button" class="orng" value="公交方案" onclick="toHere($(\'#FHinput\').val(),\'青岛\',\'b\',\''+geo+'\',\''+name+'\',\'FHinput\');return false;"/><input id="FHdr" type="button" class="green" value="驾车导航" onclick="toHere($(\'#FHinput\').val(),\'青岛\',\'d\',\''+geo+'\',\''+name+'\',\'FHinput\');return false;"/></div></div><div class="c"></div></div>';
        str += '<div class="busItem"><div class="itemleft">从这里出发</div><div class="itemcon"><div class="fhitem">出发地：' + briefTit + '</div><div class="fhitem mid">目的地：<input type="text"  class="inptext" id="THinput" /></div><div class="fhInput"><input id="THbus" type="button" class="orng" value="公交方案" onclick="fromHereHere($(\'#THinput\').val(),\'青岛\',\'b\',\''+geo+'\',\''+name+'\',\'THinput\');return false;"/><input id="THdr" type="button" class="green" value="驾车导航" onclick="fromHereHere($(\'#THinput\').val(),\'青岛\',\'d\',\''+geo+'\',\''+name+'\',\'THinput\');return false;"/></div></div></div><div class="c"></div></div>';

        break;
        
    }
    $.wBox.reveal(str);
  }  

  function skipOverlay() {
    return $.wBox.settings.overlay == false || $.wBox.settings.opacity === null 
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('wBox_overlay').length == 0) 
      $("body").append('<div id="wBox_overlay" class="wBox_hide"></div>')

    $('#wBox_overlay').hide().addClass("wBox_overlayBG")
      .css('opacity', $.wBox.settings.opacity)
      .click(function() { $(document).trigger('close.wBox') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#wBox_overlay').fadeOut(200, function(){
      $("#wBox_overlay").removeClass("wBox_overlayBG")
      $("#wBox_overlay").addClass("wBox_hide") 
      $("#wBox_overlay").remove()
    })
    
    return false
  }

  $(document).bind('close.wBox', function() {
    $(document).unbind('keydown.wBox')
    $('#wBox').fadeOut(function() {
      $('#wBox .content').removeClass().addClass('content')
      hideOverlay()
      $('#wBox .loading').remove()
    })
  })

})(jQuery);

