/*
 * N17Box (for jQuery)
 * Version: 1.0
 * @requires: jQuery v1.2 or later
 */
 
 /* Public Functions */
 // 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 Mode
    {       
        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);
 }
 
 // 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;
 }
 
 // Figures out what is needed to display
 // Formats are:
 //     div     :   #id
 //     image   :   blah.extension
 //     ajax    :   anything else
 function fillN17boxFromHref(href, klass, close)
 {
    // div
    if (href.match(/#/))
    {
        var url     =   window.location.href.split('#')[0];
        var target  =   href.replace(url, '');

	if ($(target).width() > 500)
		$(target).css('width', 500);
	$('.body').css('width', $(target).width() + 20);

	N17Box.reveal($(target).clone().show(), klass, close);
	N17Box.toggleCloseButton(true);
    }
    // image
    else if (href.match(N17Box.Settings.imageTypesRegexp))
    {
        fillN17BoxFromImage(href, klass, close);
    }
    // ajax
    else
    {
        fillN17BoxFromAjax(href, klass, close);
    }
 }
 
 function fillN17BoxFromImage(href, klass, close)
 {
    var image = new Image();
    image.onload = function() {
        N17Box.reveal('<img src="' + image.src + '" />', klass, close);
    }
    image.src = href;
 }
 
 function fillN17BoxFromAjax(href, klass, close)
 {
    $.get(href, function(data) {
        N17Box.reveal(data, klass, close);
    });
 }
 
 function skipOverlay()
 {
    return N17Box.Settings.overlay == false || N17Box.Settings.opacity === null;
 }
 
 function showOverlay(outsideClickToHide)
 {
    if (skipOverlay())
        return;
        
    if ($('#N17box_overlay').length == 0)
        $("body").append('<div id="N17box_overlay"></div>');
        
    $('#N17box_overlay').hide().addClass("N17box_overlayBG").css('opacity', N17Box.Settings.opacity).fadeIn(200);
    if (outsideClickToHide)
        $('#N17box_overlay').click(function() {
            $(document).trigger('close_N17box');
        });
        
    return false;
 }
 
 function hideOverlay()
 {
    if (skipOverlay())
        return;
        
    $('#N17box_overlay').fadeOut(200, function() {
        $('#N17box_overlay').removeClass("N17box_overlayBG");
        $('#N17box_overlay').addClass("N17box_hide");
        $('#N17box_overlay').remove();
    });
    
    return false;
 }
 
 // Bindings
 $(document).bind('close.N17box', function() {
    $('#N17box').fadeOut(function() {
        $('#N17box .content').removeClass().addClass('content');
        hideOverlay();
        $('#N17box .loading').remove();
    });
 });
 
 $(document).bind('keydown.N17box', function(e) {
    if (e.keyCode == 27 && $('#N17box .close').is(':visible'))
        N17Box.close();
    return true;
});
 
 N17FaceBox = function()
 {
    var settings = { 
                        opacity             :   0.5,
                        overlay             :   true,
                        loadingImage        :   '/media/images2/N17box/loading.gif',
                        loadingImageWidth   :   '32px',
                        closeImage          :   '/media/images2/N17box/closelabel.gif',
                        imageTypes          :   [ 'png', 'jpg', 'jpeg', 'gif' ],
                        html                :   '<div id="N17box" style="display: none;"> \
                                                    <div class="popup"> \
                                                        <div class="body clearfix"> \
														<div class="boxContainer"> \
															<div class="topLeftCorner"></div> \
															<div class="topRightCorner"></div> \
															<div class="content clearfix"></div> \
																<div class="footer"> \
																	<a href="#" class="close"><img src="images2/N17box/closelabel.gif" alt="close" class="closeimage" \/></a> \
																</div> \
															<div class="contentWide"></div> \
															<div class="contentHeight"></div> \
															<div class="bottomLeftCorner"></div> \
															<div class="bottomRightCorner"></div> \
														</div> \
                                                        </div> \
                                                    </div>\
                                                </div>',
                        pleaseWaitHtml      :   '<div id="pleasewait" style="width: 100%; margin: 0 auto; text-align: center;"> \
                                                    <img src="/media/images2/ajax-loader.gif" alt="Please Wait" \/> \
						    <br /> \
						    @@HTML@@ \
                                                 </div>'
                    };
    this.init(settings);
 }

 $.extend(N17FaceBox.prototype,
 {
     // Object variables
     Settings: null,

     LoadingImageWidth: null,

     init: function(settings) {
         if (settings)
             this.Settings = settings;

         if (this.Settings.inited)
             return true;
         else
             this.Settings.inited = true;

         $(document).trigger('init.N17box');

         var imageTypes = this.Settings.imageTypes.join('|');
         this.Settings.imageTypesRegexp = new RegExp('\.(' + imageTypes + ')$', 'i');
         $('body').append(this.Settings.html);

         var preload = [new Image(), new Image()];
         preload[0].src = this.Settings.closeImage;
         preload[1].src = this.Settings.loadingImage;

         $('#N17box').find('.b:first, .bl, .br, .tl, .tr').each(function() {
             preload.push(new Image());
             preload.slice(-1).src = $(this).css('background-image').replace(/url\((.+)\)/, '$1');
         });

         $('#N17box .close').click(this.close);
         $('#N17box .closeimage').attr('src', this.Settings.closeImage);
     },

     toggle: function(data, close, klass) {
         this.loading();

         if (data.ajax)
             fillN17BoxFromAjax(data.ajax, klass, close);
         else if (data.image)
             fillN17BoxFromImage(data.image, klass, close);
         else if (data.div)
             fillN17boxFromHref(data.div, klass, close);
         else if ($.isFunction(data))
             data.call($);
         else
             this.reveal(data, klass, close);
     },

     pleaseWait: function(HTML) {
         this.loading();

         var PleaseWaitHTML = this.Settings.pleaseWaitHtml;

         if (HTML) {
             PleaseWaitHTML = PleaseWaitHTML.replace(/@@HTML@@/g, HTML);
         }
         else
             PleaseWaitHTML = PleaseWaitHTML.replace(/@@HTML@@/g, "Please Wait");

         this.toggle(PleaseWaitHTML, false);
     },

     loading: function() {
         this.init();

         if ($('#N17box .loading').length == 1)
             return true;

         showOverlay();

         $('#N17box .content').empty();
         $('#N17box .body').children().hide().end().append('<div class="loading"><img src="' + this.Settings.loadingImage + '" /></div>');
         $('#N17box').css({
             top: getPageScroll()[1] + (getPageHeight() / 10),
             left: $(window).width() / 2 - 205
         }).show();

         $(document).trigger('loading.N17box');
     },

     reveal: function(data, klass, close) {
         $(document).trigger('beforeReveal.N17box');

         if (klass)
             $('#N17box .content').addClass(klass);

         $('#N17box .content').children().remove();
         $('#N17box .content').append(data);
         $('#N17box .loading').remove();
         $('#N17box .body').children().fadeIn('slow');

         // If not please wait, make sure close is shown
         if (!close)
             $('#N17box .close').hide();

         $('#N17box').css('left', $(window).width() / 2 - ($('#N17box').width() / 2));
         $(document).trigger('reveal.N17box').trigger('afterReveal.N17box');
     },

     appendContent: function(area, data) {
         $('#N17box ' + area).append(data);
     },

     clearContent: function(area) {
         $('#N17box ' + area).empty();
     },

     removeContent: function() {
         $('#N17box .content').children().remove();
     },

     toggleCloseButton: function(show) {
         if (!show)
             $('#N17box .footer').hide();
         else
             $('#N17box .footer').show();
     },

     close: function() {
         $(document).trigger('close.N17box');
         return false;
     }
 });
 
 // Init N17Box
 var N17Box;
 $(function() {
    N17Box = new N17FaceBox();
    $('a[rel*=N17box]').click(function() {
        N17Box.toggle({ div : $(this).attr('href') }, true);
        return false; 
    });
 });
