/* 
 * Gallery Object
 *
 * This object takes care of all the javascript
 * magic for the gallery replacements and
 * ui functions for displaying it
 *
 * Requires jquery
 * Requires scrollTo plugin
 * Requires localscroll plugin
 */

function Gallery(id, galleryId, element)
{

    //Properties
    this.domObject    = $("#" + id);
    this.amount       = '';
    Gallery.galleryId = galleryId;
    Gallery.type         = element.hasClass('internal');
    Gallery.inAnimation = false;

    if(Gallery.type) {
        Gallery.increment = 3;
        Gallery.decrement = 3;
    }else   {
        Gallery.increment = 2;
        Gallery.decrement = 2;
    }

    //Slide the current view down to display next three
    Gallery.showNext = function(object) {

        if(Gallery.inAnimation) {
            return false;
        }
        Gallery.inAnimation = true;

        if(Gallery.isNotEnd(object)) {
            if($(object).parents('.infoBar').prev('.galleri').hasClass('engelsk')) {
                Gallery.prev        = 'previous';
                Gallery.next        = 'next';
                Gallery.show        = 'showing';
                Gallery.of          = 'out of';
                Gallery.image       = 'Image ';
                Gallery.close       = 'Close';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev.gif';
            }else if($(object).parents('.infoBar').prev('.galleri').hasClass('dansk')) {
                Gallery.prev        = 'forrige';
                Gallery.next        = 'nęste';
                Gallery.show        = 'viser';
                Gallery.of          = 'af';
                Gallery.image       = 'Billede ';
                Gallery.close       = 'Luk';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
            }else   {
                Gallery.prev        = 'forrige';
                Gallery.next        = 'nęste';
                Gallery.show        = 'viser';
                Gallery.of          = 'af';
                Gallery.image       = 'Billede ';
                Gallery.close       = 'Luk';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
            }

            var nextImage = $(object).parents('.infoBar').prev('.mask').find('.image-container').eq(($(object).parents('.infoBar').data('current-image')));
            $(object).parents(".infoBar").prev('.mask').scrollTo(nextImage, {duration:500, axis:'x', onAfter: function() {Gallery.updateStatus($(object), 'up');}});
            $(object).prev('span').removeClass('inactive');
        }else   {
            Gallery.inAnimation = false;
        }
        
    }

    Gallery.isNotEnd = function(object) {
        var length = $(object).parents('.infoBar').prev('.mask').find('.image-container').length;
        if(($(object).parents('.infoBar').data('current-image') + (Gallery.increment)) != length) {
            return true;
        }else   {
            return false;
        }

    }

    Gallery.isAtBeginning = function(object) {
        if($(object).parents('.infoBar').data('current-image') == 1)  {
            return true;
        }else   {
            return false
        }
    }

    //Slide the current view up to display previous three
    Gallery.showPrev = function(object) {

        if(Gallery.inAnimation) {
            return false;
        }

        Gallery.inAnimation = true;

        if(!Gallery.isAtBeginning(object)) {
            if($(object).parents('.infoBar').prev('.galleri').hasClass('engelsk')) {
                Gallery.prev        = 'previous';
                Gallery.next        = 'next';
                Gallery.show        = 'showing';
                Gallery.of          = 'out of';
                Gallery.image       = 'Image ';
                Gallery.close       = 'Close';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev.gif';
            }else if($(object).parents('.infoBar').prev('.galleri').hasClass('dansk')) {
                Gallery.prev        = 'forrige';
                Gallery.next        = 'nęste';
                Gallery.show        = 'viser';
                Gallery.of          = 'af';
                Gallery.image       = 'Billede ';
                Gallery.close       = 'Luk';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
            }else   {
                Gallery.prev        = 'forrige';
                Gallery.next        = 'nęste';
                Gallery.show        = 'viser';
                Gallery.of          = 'af';
                Gallery.image       = 'Billede ';
                Gallery.close       = 'Luk';
                Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
                Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
                Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
            }

            var prevImage = $(object).parents('.infoBar').prev('.mask').find('.image-container').eq(($(object).parents('.infoBar').data('current-image')-2));
            $(object).parents('.infoBar').prev('.mask').scrollTo(prevImage, {duration:500, axis: 'x', onAfter: function(){Gallery.updateStatus($(object), 'down')}});
        }else   {
            Gallery.inAnimation = false;
        }
    }

    //Update the gallery status bar
    Gallery.updateStatus = function(object, direction) {

        switch(direction) {

            case 'up':

                var currentImage = object.parents('.infoBar').data('current-image') + 1;
                var galleryLength = $(object).parents('.infoBar').prev('.mask').find('.image-container').length;
                object.parents('.infoBar').data('current-image', currentImage );
                object.parent('span').prev('span').text(currentImage + '-' + (currentImage+Gallery.increment) + ' ' + Gallery.of + ' ' + galleryLength );

                if(!Gallery.isNotEnd(object)) {
                    $(object).addClass('inactive');
                }

                break;

            case 'down':

                var currentImage = object.parents('.infoBar').data('current-image') - 1;
                var galleryLength = $(object).parents('.infoBar').prev('.mask').find('.image-container').length;
                object.parents('.infoBar').data('current-image', currentImage);
                object.parent('span').prev('span').text(currentImage + '-' + (currentImage+Gallery.increment) + ' ' + Gallery.of + ' ' + galleryLength );

                if(Gallery.isNotEnd(object)) {
                    $(object).parents('.gallery-navigation').children('.gallery-navigation-next').removeClass('inactive');
                }

                if(Gallery.isAtBeginning(object)) {
                    $(object).addClass('inactive');
                }

                break;

        }

        Gallery.inAnimation = false;

    }

    //Fetch the images for this gallery
    this.fetchGallery = function(type) {

        $.ajax({
            url      : 'fileadmin/template/getGallery.php',
            type     : 'POST',
            dataType : 'json',
            data     : 'galleryId=' + Gallery.galleryId,
            success  : function(json) {
                            
                         Gallery.insertGallery(json, id, element);

            },
            error    : function(ajaxObject,text,error) {

            }
        });

    }

    //Insert the gallery into the dom
    Gallery.insertGallery = function(json, id) {

        if(!json.contains || json.images.length == 0) {
            
            $("#" + id).children('img').replaceWith('<p>Galleri eksisterer ikke eller der mangler billeder i galleriet.</p>');
            return false;
        }

        //Language control
        if($("#" +id).hasClass('engelsk')) {
            Gallery.prev        = 'previous';
            Gallery.next        = 'next';
            Gallery.show        = 'showing';
            Gallery.of          = 'out of';
            Gallery.image       = 'Image ';
            Gallery.close       = 'Close';
            Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close.gif';
            Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next.gif';
            Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev.gif';
        }else if($("#" + id).hasClass('dansk')) {
            Gallery.prev        = 'forrige';
            Gallery.next        = 'nęste';
            Gallery.show        = 'viser';
            Gallery.of          = 'af';
            Gallery.image       = 'Billede ';
            Gallery.close       = 'Luk';
            Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
            Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
            Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
        }else   {
            Gallery.prev        = 'forrige';
            Gallery.next        = 'nęste';
            Gallery.show        = 'viser';
            Gallery.of          = 'af';
            Gallery.image       = 'Billede ';
            Gallery.close       = 'Luk';
            Gallery.closeButton = 'fileadmin/template/img/lightbox/lightbox-btn-close-da.gif';
            Gallery.nextButton  = 'fileadmin/template/img/lightbox/lightbox-btn-next-da.gif';
            Gallery.prevButton  = 'fileadmin/template/img/lightbox/lightbox-btn-prev-da.gif';
        }

        //Determine if the title of the gallery should be shown
        if($("#"+ id).hasClass('titel')) {
            var title = json.images[0].title;
            $('<h3 class="gallery-title"></h3>')
                .text(title)
                .insertBefore("#" + id);
        }

        this.amount = json.images.length;

        var counter = 0;
        for(i=0;i<this.amount;i++) {
            copyright = "";
            caption   = "";

            if(Gallery.type === false) {
                if((counter%3) == 0) {
                    $('<div class="row-seperator row_' + counter + '"></div>').appendTo($("#" + id));
                }
            }else   {
                if((counter%4) == 0) {
                    $('<div class="row-seperator row_' + counter + '"></div>').appendTo($("#" + id));
                }
            }

            //$('<div class="row-seperator-hor row_' + counter + '">&nbsp;</div>').appendTo($("#" + id));

            if(json.images[i].copyright != '') {
                copyright = 'Copyright \251 ' +  json.images[i].copyright;
            }

            if(json.images[i].caption != '') {
                caption = json.images[i].caption;

                if(copyright != '') {
                    caption += ' | ';
                }
            }

            var container = $('<div class="image-container"></div>');
            var link  = $('<a href="' + json.images[i].src + '" title="' + caption + copyright + '" rel="lightbox[group_' + id + ']" class="lightbox_' + id + '"></a>');
            var image = $('<img src="' + json.images[i].thumb + '" width="133" height="133" />');
            
            $("#" + id).children('.gallery-loader').remove();

            image.appendTo(link);
            link.appendTo(container);
            container.appendTo("#" + id);

            counter++;
   
        }

        //Instantiate the lightbox effect
        $(".lightbox_" + id).lightbox({
            imageClickClose         : false,
            fileBottomNavCloseImage : 'fileadmin/template/img/lightbox/close.gif',
            fileLoadingImage        : 'fileadmin/template/img/lightbox/loading.gif',
            navbarOnTop             : false,
            fitToScreen             : true,
            strings : {
			prevLinkTitle : Gallery.prev,
			nextLinkTitle : Gallery.next,
			prevLinkText  : Gallery.prev,
			nextLinkText  : Gallery.next,
			closeTitle    : Gallery.close,
			image         : '',
			of            : ' ' + Gallery.of + ' '
		}
        });

        //Add navbar at the bottom of the gallery
        if(Gallery.type === true) {
            var className = 'infoBar internal-infoBar';
        }else   {
            var className = 'infoBar';
        }

        var infoBar = $('<div class="' + className + '"></div>')
                        .data('current-image', 1)
                        .html('<span class="displaying"></span>');
                                              
        var navbar = $('<span class="gallery-navigation"></span>');
        var prevLink = $('<span class="gallery-navigation-prev inactive"></span>')
                        .html('<a href="javascript:void(false);">' + Gallery.prev + '</a> | ')
                        .click(function() {
                                Gallery.showPrev(this);
                        })
                        .appendTo(navbar);
                        
        var nextLink = $('<span class="gallery-navigation-next"></span>')
                        .html('<a href="javascript:void(false);">' + Gallery.next + '</a>')
                        .click(function() {
                                Gallery.showNext(this);
                        })
                        .appendTo(navbar);
                        
        navbar.appendTo(infoBar);

        var parentMask = $("#" + id).parents('.mask');

        infoBar.insertAfter(parentMask);

        if(Gallery.type) {
            var length = 4;
        }else   {
            var length = 3;
        }

        if(infoBar.prev('.mask').find('.image-container').length <= length) {
            nextLink.addClass('inactive');
            prevLink.addClass('inactive');
        }

        if(infoBar.prev('.mask').find('.image-container').length <= length) {
            var displayText = infoBar.prev('.mask').find('.image-container').length
        }else   {
            var displayText = (Gallery.increment + 1);
        }

        var displaying = '1-' + displayText + ' ' + Gallery.of + ' ' + Gallery.amount;
        infoBar.children('.displaying').text(displaying);  
    }


}

//Front controller

$(document).ready(function() {

   if($(".galleri").length !== 0) {

        $(".galleri").each(function(i) {

           $(this).wrap('<div class="mask" />');

           if(!$(this).hasClass('internal')) {
               $(this).parent('.mask').addClass('external');
           }

           $('<img src="fileadmin/template/img/lightbox/loading.gif" class="gallery-loader" />').appendTo($(this));

           $(this).attr('id', 'gallery' + i);
           $(this).addClass('galleri-hor');

           gallery = new Gallery('gallery'+i, $(this).attr('rel'), $(this));

           gallery.fetchGallery();

        });

   }

});
