(function($){
$.facybox=function(data,klass){
$.facybox.loading()
$.facybox.content_klass=klass
if(data.image)revealImage(data.image)
else if(data.div)revealHref(data.div)
else if($.isFunction(data))data.call($)
else $.facybox.reveal(data)}
$.extend($.facybox,{
settings:{
opacity:0.25,
overlay:true,
modal:false,
imageTypes:['png','jpg','jpeg','gif'],
imageMimeTypes:['image/jpeg','image/png','image/gif']
},
html:function(){
return '\
<div id="facybox" style="display:none;">\
<div class="popup">\
<table>\
<tbody>\
<tr>\
<td class="nw"/><td class="n"/><td class="ne"/>\
</tr>\
<tr>\
<td class="w"/>\
<td class="body">\
<div class="footer"></div>\
<a href="#" class="close"></a>\
<div class="content">\
</div>\
</tbody>\
</table>\
</div>\
</div>\
<div class="loading"></div>\
'
},
loading: function(){
init()
if($('.loading',$('#facybox'))[0])return
showOverlay()
$.facybox.wait()
if(!$.facybox.settings.modal){
$(document).bind('keydown.facybox',function(e){
if(e.keyCode==27)$.facybox.close()
})}
$(document).trigger('loading.facybox')
},
wait: function(){
var $f=$('#facybox')
$('.content',$f).empty()
$('.body',$f).children().hide().end().append('<div class="loading"></div>')
$f.fadeIn('fast')
$.facybox.centralize()
$(document).trigger('reveal.facybox').trigger('afterReveal.facybox')
},
centralize: function(){
var $f=$('#facybox')
var pos=$.facybox.getViewport()
var wl=parseInt(pos[0]/2)-parseInt($f.find("table").width()/2)
var fh=parseInt($f.height())
if(pos[1]>fh){
var t=(pos[3]+(pos[1]-fh)/2)
$f.css({'left': wl,'top': t})
}else{
var t=(pos[3]+(pos[1]/10))
$f.css({'left': wl,'top': t})}
},
getViewport: function(){
return [$(window).width(),$(window).height(),$(window).scrollLeft(),$(window).scrollTop()]
},
reveal: function(content){
$(document).trigger('beforeReveal.facybox')
var $f=$('#facybox')
$('.content',$f)
.attr('class',($.facybox.content_klass||'')+' content')
.html(content)
$('.loading',$f).remove()
var $body=$('.body',$f)
$body.children().fadeIn('fast')
$.facybox.centralize()
$(document).trigger('reveal.facybox').trigger('afterReveal.facybox')
},
close: function(){
$(document).trigger('close.facybox')
return false}
})
$.fn.facybox=function(settings){
var $this=$(this)
if(!$this[0])return $this
if(settings)$.extend($.facybox.settings,settings)
if(!$.facybox.settings.noAutoload)init()
$this.bind('click.facybox',function(){
$.facybox.loading()
var klass=this.rel.match(/facybox\[?\.(\w+)\]?/)
$.facybox.content_klass=klass ? klass[1]:''
revealHref(this.href)
return false
})
return $this}
function init(){
if($.facybox.settings.inited)return
else $.facybox.settings.inited=true
$(document).trigger('init.facybox')
makeBackwardsCompatible()
var imageTypes=$.facybox.settings.imageTypes.join('|')
$.facybox.settings.imageTypesRegexp=new RegExp('\.('+imageTypes+')','i')
$('body').append($.facybox.html())
var $f=$("#facybox")
if($.browser.msie){
if(parseInt($.browser.version)<=6){
var css="<style type='text/css' media='screen'>* html #facybox_overlay { position: absolute; height: expression(document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight:document.body.offsetHeight + 'px');}</style>"
$('head').append(css)
$(".close",$f).css({
'right': '15px'
})}}
$('#facybox .close').click($.facybox.close)}
function makeBackwardsCompatible(){
var $s=$.facybox.settings
$s.imageTypes=$s.image_types || $s.imageTypes
$s.facyboxHtml=$s.facybox_html || $s.facyboxHtml}
function revealHref(href){
if(href.match(/#/)){
var url=window.location.href.split('#')[0]
var target=href.replace(url,'')
if(target=='#')return
$.facybox.reveal($(target).html(),$.facybox.content_klass)
}else if(href.match($.facybox.settings.imageTypesRegexp)){
revealImage(href)
}else{revealAjax(href)}}
function revealImage(href){
var $f=$("#facybox")
$('#facybox .content').empty()
$.facybox.loading()
var image=new Image()
image.onload=function(){$.facybox.reveal('<div class="image"><img src="'+image.src+'" /></div>',$.facybox.content_klass)}
image.src=href}
function skipOverlay(){
return $.facybox.settings.overlay==false || $.facybox.settings.opacity===null}
function showOverlay(){
if(skipOverlay())return
if($('#facybox_overlay').length==0){
$("body").append('<div id="facybox_overlay" class="facybox_hide"></div>')}
$('#facybox_overlay').hide().addClass("facybox_overlayBG")
.css('opacity',$.facybox.settings.opacity)
.fadeIn(200)
if(!$.facybox.settings.modal){
$('#facybox_overlay').click(function(){$(document).trigger('close.facybox')})}}
function hideOverlay(){
if(skipOverlay())return
$('#facybox_overlay').fadeOut(200,function(){
$("#facybox_overlay").removeClass("facybox_overlayBG").
addClass("facybox_hide").
remove()
})}
$(document).bind('close.facybox',function(){
$(document).unbind('keydown.facybox')
var $f=$("#facybox")
if($.browser.msie){
$('#facybox').hide()
hideOverlay()
$('#facybox .loading').remove()
}else{
$('#facybox').fadeOut('fast',function(){
$('#facybox .content').removeClass().addClass('content')
hideOverlay()
$('#facybox .loading').remove()
})}
$(document).trigger('afterClose.facybox')
})
})(jQuery)

