(function($) {
  $.winbox = function(data, klass) {
    $.winbox.loading()

    if (data.ajax) fillWinboxFromAjax(data.ajax)
    else if (data.image) fillWinboxFromImage(data.image)
    else if (data.div) fillWinboxFromHref(data.div)
    else if ($.isFunction(data)) data.call($)
    else $.winbox.reveal(data, klass)
  }

  /*
   * Public, $.winbox methods
   */

  $.extend($.winbox, {
    settings: {
      opacity      : 0,
	  topxy		: 0,
      leftxy	: 0,
      overlay      : true,
      loadingImage : 'http://gouwoo.com/img/winbox/loading.gif',
      imageTypes   : [ 'png', 'jpg', 'jpeg', 'gif' ],
      winboxHtml  : '\
    <div id="winbox" 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"> \
			  <div class="box_close"></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 ($('#winbox .loading').length == 1) return true
      showOverlay()

      $('#winbox .content').empty()
      $('#winbox .body').children().hide().end().
        append('<div class="loading"><img src="'+$.winbox.settings.loadingImage+'"/></div>')

      $('#winbox').css({					   
        //top:	getPageScroll()[1] + (getPageHeight() / 5),
        //left:	385.5
		top:	$.winbox.settings.topxy + 10,
        left:	$.winbox.settings.leftxy - 50
      }).show()
	  
      $(document).bind('keydown.winbox', function(e) {
        if (e.keyCode == 27) $.winbox.close()
        return true
      })
      $(document).trigger('loading.winbox')
    },

    reveal: function(data, klass) {
		
      ///$(document).trigger('beforeReveal.winbox')
	  $('#winbox .content').html(data);
      //$('#winbox .content').append(data)
      $('#winbox .loading').remove()
      $('#winbox .body').children().fadeIn('normal')
	  if(klass =='c')
	  {
		  $('#winbox').css('left', $(window).width() / 2 - ($('#winbox table').width() / 2))
		  $('#winbox').css('top', 100)
	  }else if(($.winbox.settings.leftxy + $('#winbox table').width()) > (($(window).width() / 2) + 400))
	  {
		  $('#winbox').css('left', (($(window).width() / 2) + 400) - $('#winbox table').width() - 10)
	  }
      ///$(document).trigger('afterReveal.winbox')
	  //$(document).trigger('reveal.winbox').trigger('afterReveal.winbox')
    },

    close: function() {
      $(document).trigger('close.winbox')
      return false
    }
  })

  /*
   * Public, $.fn methods
   */

  $.fn.winbox = function(type,settings) {
    init(settings)
    function clickHandler(e) {
	  var evt = e || window.event
      $.winbox.settings.topxy = evt.pageY
      $.winbox.settings.leftxy = evt.pageX
	  
      $.winbox.loading(true)
	  var klass = this.rel.match(/winbox\[?\.(\w+)\]?/)
      if (klass) 
	  {
		  klass = klass[1]
	  }
      fillWinboxFromAjax(this.href, type,klass)
      return false
    }

    return this.click(clickHandler)
  }

  /*
   * Private methods
   */

  // called one time to setup winbox on this page
  function init(settings) {
    if ($.winbox.settings.inited) return true
    else $.winbox.settings.inited = true

    $(document).trigger('init.winbox')
    makeCompatible()

    var imageTypes = $.winbox.settings.imageTypes.join('|')
    $.winbox.settings.imageTypesRegexp = new RegExp('\.' + imageTypes + '$', 'i')

    if (settings) $.extend($.winbox.settings, settings)
    $('body').append($.winbox.settings.winboxHtml)

    var preload = [ new Image(), new Image() ]
    preload[0].src = $.winbox.settings.loadingImage

    $('#winbox').find('.b:first, .bl, .br, .tl, .tr').each(function() {
      preload.push(new Image())
      preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('#winbox .box_close').click($.winbox.close)
  }
  // getPageScroll() by quirksmode.com
  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) {
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  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
  }

  // Backwards compatibility
  function makeCompatible() {
    var $s = $.winbox.settings

    $s.loadingImage = $s.loading_image || $s.loadingImage
    $s.imageTypes = $s.image_types || $s.imageTypes
    $s.winboxHtml = $s.winbox_html || $s.winboxHtml
  }


  function fillWinboxFromAjax(href, type,klass) {
	href = href.replace('http://gouwoo.com/','')
	href = href.replace('http://','')
	if(type=='urlbox')
	{
		href = '/do?mod='+ type +'&url=' + href;
	}else
	{
		href = '/js/' + href;
	}
	$.get(href, function(data) { $.winbox.reveal(data,klass) })
  }

  function skipOverlay() {
    return $.winbox.settings.overlay == false || $.winbox.settings.opacity === null 
  }

  function showOverlay() {
    if (skipOverlay()) return

    if ($('winbox_overlay').length == 0) 
      $("body").append('<div id="winbox_overlay" class="winbox_hide"></div>')

    $('#winbox_overlay').hide().addClass("winbox_overlayBG")
      .css('opacity', $.winbox.settings.opacity)
      .click(function() { $(document).trigger('close.winbox') })
      .fadeIn(200)
    return false
  }

  function hideOverlay() {
    if (skipOverlay()) return

    $('#winbox_overlay').fadeOut(200, function(){
      $("#winbox_overlay").removeClass("winbox_overlayBG")
      $("#winbox_overlay").addClass("winbox_hide") 
      $("#winbox_overlay").remove()
    })
    
    return false
  }

  /*
   * Bindings
   */

  $(document).bind('close.winbox', function() {
    $(document).unbind('keydown.winbox')
    $('#winbox').fadeOut(function() {
	  $('#winbox .content').html("");
      $('#winbox .content').removeClass().addClass('content')
      hideOverlay()
      $('#winbox .loading').remove()
    })
  })

})(jQuery);


