/*
 *  facebook.show($(id))
 */

  facebox = function(data) {
    facebox.init()
    facebox.loading();
    (typeof data == "function") ? data.call() : facebox.reveal(data)
  }

  facebox.settings = {
    loading_image : '/images/facebox/loading.gif',
    close_image   : '/images/facebox/closelabel.gif',
    image_types   : [ 'png', 'jpg', 'jpeg', 'gif' ],
    facebox_html  : '\
  <div id="facebox" style="display:none;"> \
    <div class="popup"> \
      <table border="0" cellpadding="0" cellspacing="0"> \
        <tbody> \
          <tr> \
            <td class="tl" id="facebox-tl"/><td class="b" id="facebox-b-first"/><td class="tr" id="facebox-tr"/> \
          </tr> \
          <tr class="middle"> \
            <td class="b"/> \
            <td class="body" id="facebox-body"> \
              <div class="content" id="facebox-content"> \
              </div> \
              <div class="footer"> \
                <a href="#" class="close" id="facebox-close"> \
                  <img src="" title="close" class="close_image" id="facebox-close_image" /> \
                </a> \
              </div> \
            </td> \
            <td class="b"/> \
          </tr> \
          <tr> \
            <td class="bl" id="facebox-bl"/><td class="b"/><td class="br" id="facebox-br"/> \
          </tr> \
        </tbody> \
      </table> \
    </div> \
  </div>'
  }

  facebox.loading = function() {
    if ($('facebox-loading')) return true

  //  $('facebox-content').innerHTML = "";
    //$('facebox-body').children().hide().end().
		
		if (this.cache) {
			if ($('facebox-content').childNodes.length > 0) {
				$('facebox-content').removeChild($('facebox-content').childNodes[0]);
			}
		} else {
		  	$('facebox-content').innerHTML = '<div class="loading" id="facebox-loading"><img src="'+facebox.settings.loading_image+'"/></div>'
		}
		
    var pageScroll = facebox.getPageScroll()
    $('facebox').setStyle({
      top:	pageScroll[1] + (facebox.getPageHeight() / 10) + 'px',
      left:	pageScroll[0] + 'px'
    }).show()

    $(document).observe('keypress', function(e) {
      if (e.keyCode == 27) facebox.close()
    })
  }

  facebox.reveal = function(data, klass) {
    //if (klass) $('facebox-content').addClass(klass)

	if (this.caches[this.cache]) {
		$('facebox-content').appendChild(this.caches[this.cache]);
	} else {
	if (typeof data == 'string') {
	    $('facebox-content').innerHTML = data
			this.caches[this.cache] = $('facebox-content').childNodes[0];
	} else {
		$('facebox-content').innerHTML = "";
		$('facebox-content').appendChild(data);
		this.caches[this.cache] = data;
	}
	
	if (this.cache) {
		this.cache_func[this.cache]();	
	}
	
}
	
    //$('facebox-loading').remove()
    //$('facebox-body').children().fadeIn('normal')
  }

  facebox.close = function(e) {

    $(document).stopObserving('keypress')
	//$('#facebox').fadeOut(function() {

      $('facebox-content').className = "";
	  $('facebox-content').addClassName('content')
	  $('facebox').hide();

    //})

	try{
	Event.stop(e)
	} catch(e) {}
    return false
  }
	
	facebox.bindshow = function(target_cache, func, e) {
		target = Event.element(e);
		while (target.tagName.toLowerCase() != 'a')
			target = target.parentNode;
		this.cache = target_cache;
		if (this.caches==undefined) {
			this.caches = [];
			this.cache_func = [];
		}
		this.cache_func[this.cache] = func;
		facebox.show(target);
	}
	
	facebox.updateCache = function (cache_key) {
		this.caches[cache_key] = $('facebox-content').childNodes[0];;
	}
	
  facebox.show = function(element) {
	var that = element;

	
    facebox.init()

    var image_types = facebox.settings.image_types.join('|')
    image_types = new RegExp('\.' + image_types + '$', 'i')


      facebox.loading(true)

      // support for rel="facebox[.inline_popup]" syntax, to add a class
//      var klass = that.rel.match(/facebox\[\.(\w+)\]/)
//      if (klass) klass = klass[1]
			klass = "facebox"
			
			if (this.cache && this.caches[this.cache]) {
				facebox.reveal();
				return
			}
			
      // div
      if (that.href.match(/#/)) {
        var target = $(that.href.split('#')[1]);
				if (target) {
						facebox.reveal(target.cloneNode(true).show(), klass)
				}
      // image
      } else if (that.href.match(image_types)) {
        var image = new Image()
        image.onload = function() {
          facebox.reveal('<div class="image"><img src="' + image.src + '" /></div>', klass)
        }
        image.src = that.href
      // ajax
      } else {
        new Ajax.Request(that.href, { 
						parameters : { authenticity_token : $('authenticity').value }, 
						onComplete : function(data) { facebox.reveal(data.responseText, klass) 
				}})
      }

      return false
    
    return that
  }

  facebox.init = function() {
    if (facebox.settings.inited) {
      return true
    } else {
      facebox.settings.inited = true
    }


		var div = document.createElement('div');
		document.body.appendChild(div);
		div.innerHTML = facebox.settings.facebox_html


    var preload = [ new Image(), new Image() ]
    preload[0].src = facebox.settings.close_image
    preload[1].src = facebox.settings.loading_image

    $A(['facebox-b-first','facebox-bl','facebox-br','facebox-tl','facebox-tr']).each(function(xxx) {
      preload.push(new Image());
      preload.slice(-1).src = $(xxx).getStyle('background-image').replace(/url\((.+)\)/, '$1')
    })

    $('facebox-close').observe('click', facebox.close)
    $('facebox-close_image').src = facebox.settings.close_image
  }

  // getPageScroll() by quirksmode.com
  facebox.getPageScroll = function() {
    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) 
  }

  // adapter from getPageSize() by quirksmode.com
  facebox.getPageHeight = function() {
    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
  }

