/*
	* Infobox
	* http://jobspice.com
	* Copyright (c) 2009 Dane Hurtubise
*/

$('document').ready(function () {
    $('.infobox').infobox();
});

jQuery.fn.infobox = function() {
    return this.each(function () {
        $(this).unbind('click').click(function() {
            $('embed, object, select').css({ 'visibility' : 'hidden' });
            var href = $(this).attr("href");
            set_interface(href);
            fill_content(href);
            return false;
        });
    });

    function set_interface(href) {
		$('body').append('<div id="jquery-overlay"></div><div id="jquery-infobox"><div id="jquery-popup"><div id="infobox-border-top"><div id="infobox-border-topleft"><div id="infobox-border-topfill"></div></div></div><div id="infobox-top"><div id="infobox-top-left"><div id="infobox-top-fill"><div id="infobox-close">Close<img src="img/icon-close.png"></div></div></div></div><div id="infobox-middle"><div id="infobox-middle-left"><div id="infobox-content"></div><div id="infobox-footer"></div></div></div><div id="infobox-bottom"><div id="infobox-bottom-left"><div id="infobox-bottom-fill"><div id="infobox-copyright">Dane Hurtubise&nbsp;&copy;&nbsp;2009</div></div></div></div><div id="infobox-border-bottom"><div id="infobox-border-bottomleft"><div id="infobox-border-bottomfill"></div></div></div></div></div>');
		var offset = 120;

        $('#infobox-close').click(function() { 
            $('#jquery-infobox').fadeOut("fast", function() { $('#jquery-infobox').remove(); });
            $('#jquery-overlay').fadeOut("fast", function() { $('#jquery-overlay').remove(); });
            $('embed, object, select').css({ 'visibility' : 'visible' });
        });

        var scroll = getPageScroll();

        $('#jquery-overlay').css({
            backgroundColor: '#000',
            opacity: 0.8,
            width: $(window).width(),
            height: $(window).height()
        }).fadeIn();

        $('#jquery-infobox').css({
            top: (scroll[1] + $(window).height()) / 2 - offset,
            left: scroll[0]
        }).show();

        $(window).resize(function() {
            $('#jquery-overlay').css({
                width: $(window).width(),
                height: $(window).height()
            });
            $('#jquery-infobox').css({
                top: (scroll[1] + $(window).height()) / 2 - offset,
                left: scroll[0]
            });
        });
    };

    function fill_content(href) {
        var imageTypes = [ 'png', 'jpg', 'jpeg', 'gif' ];
        imageTypes = imageTypes.join('|');
        var regexp = new RegExp('\.' + imageTypes + '$', 'i');
    
        if (href.match(/#/)) {
            var url    = window.location.href.split('#')[0];
            var target = href.replace(url,'');
            load_data(href, $(target).clone());
        } else { //if (href.match(regexp)) {
            var target = $('<img id="infobox-image" />');
            target.attr('src', href);
            target.load(function() {
                load_data(href, target);
            });
        }
    }

    function load_data(href, target) {
        $('#infobox-content').addClass('loading');
        target.hide();

        $('#infobox-content').append(target);

        var curWidth = $('#infobox-content').width();
        var curHeight = $('#infobox-content').height();
        var curTop = $('#infobox-content').position().top;
        var newWidth = $('#'+target.attr("id")).width();
        var newHeight = $('#'+target.attr("id")).height();

        var newTop = curTop + (curHeight - newHeight) / 2;

        $('#jquery-popup').animate({
                width: newWidth + 20,
                top: newTop
            }, 400);

        $('#infobox-content').animate({ 
                width: newWidth,
                height: newHeight,
            }, 400, function() { target.fadeIn(); $('#infobox-content').removeClass('loading'); });
    };

    // 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) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};
}