/**
 * Initalize floabox
 *
 * @param obj $ jQuery object
 */
function floatbox_init($, options)
{
	$.extend({
		'loader_image' : '/img/loader.gif'
	}, options);

	var floatboxed     = false;
	var default_width  = 620;
	var default_height = 530;

	function show_floatbox(content, fn) {
		if (content) {
			$('#floatbox>div.kotak>div.content').append(content);
			$('#floatbox>div.kotak>div.content>*').hide();
			$(content).show();
		}
		$('#floatbox>div.overlay')
			.show()
			.css('opacity', 0)
			.fadeTo('medium', 0.8, function() {
				$('#floatbox>div.kotak').fadeIn(function() {
					if (typeof fn == 'function') {
						fn();
					}
                    floatboxed = true;
				});
			});
	}

	function hide_floatbox() {
		$('#floatbox>div.kotak')
			.css('backgroundImage', 'none')
			.fadeOut(function() { $('#floatbox>div.kotak>div.content').show(); });
		$('#floatbox>div.overlay').fadeOut(function() { $(this).hide(); floatboxed = false; });
	}

	function resize_floatbox(width, height, fn) {
		var to_width = {
			'width': width+'px',
			'marginLeft': '-'+(width/2)+'px'
		};
		var to_height = {
			'height': height+'px',
			'marginTop': '-'+(height/2)+'px'
		};
		$('#floatbox>div.kotak').animate(to_width, 'medium', function() {
			$(this).animate(to_height, 'medium', function() {
				if (typeof fn == 'function') {
					fn();
				}
			});
		});
	}

	$.fn.apply_floatbox = function() {
        this.each(function() {
            var t = 'div';
            if (this.hash.length > 2) {
                // autohide target divs
                if ($(this.hash).size() > 0) {
                    $(this.hash).hide();
                } else {
                    // target div does not exist, disable floatbox link
                    t = 'disable';
                }
            } else {
                if ($(this).hasClass('image') || this.href.match(/\.(jpg|gif|png)$/)) {
                    // link is an image
                    t = 'image';
                }
            }
            $(this).click(function() {
                if (t == 'div') {
                    var content = $(this.hash)[0];
                    var w = $('#floatbox>div.kotak').width();
                    var h = $('#floatbox>div.kotak').height();
                    var r = (w!=default_width || h!=default_height);
                    if (r) {
                        $('#floatbox>div.kotak>div.content').css('overflow-y', 'hidden');
                    }
                    show_floatbox(content, function() {
                        if (r) {
                            resize_floatbox(default_width, default_height, function() {
                                $('#floatbox>div.kotak>div.content').css({
                                    'overflow-x': 'hidden',
                                    'overflow-y': 'scroll'
                                });
                            });
                        }
                    });
                } else if (t == 'image') {
                    $('#floatbox>div.kotak').css({
                        'backgroundImage': 'url("'+options.loader_image+'")',
                        'backgroundPosition': '50% 50%',
                        'backgroundRepeat': 'no-repeat'
                    });
                    $('#floatbox>div.kotak>div.content').hide();
                    var a = this;
                    show_floatbox(null, function() {
                        var img = new Image();
                        $(img).load(function() {
                            resize_floatbox(img.width, img.height, function() {
                                $('#floatbox>div.kotak').css('backgroundImage', 'url("'+img.src+'")');
                            });
                        });
                        img.src = a.href;
                    });
                }
                return false;
            });
        });
	}

    $('a[rel=floatbox]').apply_floatbox();

	$('#floatbox a.close, #floatbox>div.overlay').click(function() {
		hide_floatbox();
		return false;
	});

	$(document).keydown(function(e) {
		if (e.keyCode == 27 && floatboxed) {
			hide_floatbox();
		}
	});
}

