/*
    Class: RBPagesJS
        Auxilary class for RB Suite: Pages Manager.
*/
var RBPagesJS = {
    /*
        Group: Backend Data

        Object: URL
            (object) An object containing various URLs.
    */
    URL: {},

    /*
        Group: Backend Methods

        Method: ServerResponse
            Process the server response.

        Parameters:
            data - (object) An object containing the server's response.
    */
    ServerResponse: function(data) {
        switch(data.action) {
            case 'reload_page':
                DLSupportJS.CleanAdmin.OpenPage(RBPagesJS.URL.ReloadPage, false);
                break;
            default:
                DLSupportJS.CleanAdmin._ServerResponse(data);
        }

        // Hide the loading image
        DLSupportJS.HideLoadingImage();
    },

    /*
        Method: UploadFile
            Upload a file.

        Parameters:
            form - (mixed) Either the ID or the DOM element of the form that
                will be uploading the file.

        Returns:
            (boolean) This method will always return true.
    */
    UploadFile: function(form) {
        // Upload the file
        DLSupportJS.UploadFile(form, {
            loading_duration: 0,
            finish: RBPagesJS.ServerResponse
        });

        // Show the loading image
        DLSupportJS.ShowLoadingImage();

        return true;
    },

    /*
        Group: Frontend Methods

        Method: CreateFullImageContainer
            Create a container for the full image.
    */
    CreateFullImageContainer: function() {
        // Get the container
        var container = $('RBPages_FullImageContainer');

        // We only need to create and insert this once
        if(!container) {
            container = new Element('div', {id: 'RBPages_FullImageContainer'});
            container.hide();

            document.body.appendChild(container);
        }

        // Get the overlay div
        var overlay = $('DLSupportJS_OverlayDiv');

        // Set the dimension of the container
        container.setStyle({
            width: overlay.getWidth()-40+'px',
            height: overlay.getHeight()-50+'px'
        });

        container.show();
    },

    /*
        Method: GoToPage
            Go to the specified page.

        Parameters:
            page_number - (int) The page to show.
    */
    GoToPage: function(page_number) {
        --page_number;

        // Get all of the main pages
        var pages = $$('.RBPages_MainPages');

        // Hide other pages, show requested page
        for(var i = 0; i < pages.length; ++i) {
            if(i == page_number) {
                pages[i].show();
            } else {
                pages[i].hide();
            }
        }
    },

    /*
        Method: HideFullImageContainer
            Hide the full image container.
    */
    HideFullImageContainer: function() {
        var container = $('RBPages_FullImageContainer');
        if(container) {
            container.hide();
        }

        DLSupportJS.HideOverlay({
            finish: function() {
                // We need to set the hide the flash items
                $('logo').setStyle({visibility: ''});
                $('flash_menu').setStyle({visibility: ''});
            }
        });
    },

    /*
        Method: ShowMainFullImage
            Show the full size image.

        Parameters:
            page_number - (int) The number of the page whose full size image
                will be shown.
            num_of_pages - (int) The number of pages there are.
    */
    ShowMainFullImage: function(page_number, num_of_pages) {
        // We need to set the hide the flash items
        $('logo').setStyle({visibility: 'hidden'});
        $('flash_menu').setStyle({visibility: 'hidden'});

        // Show the overlay
        DLSupportJS.ShowOverlay({
            finish: RBPagesJS._ShowMainFullImage.curry(page_number, num_of_pages)
        });
    },
    _ShowMainFullImage: function(page_number, num_of_pages) {
        // Create the containing div
        RBPagesJS.CreateFullImageContainer();
        var container = $('RBPages_FullImageContainer');
        container.update('');

        // Set the navigation
        var navigation = new Element('div');
        navigation.addClassName('TheNavigation');

        // Add the previous link if it is not the first page
        if(page_number > 1) {
            var previous = new Element('a', {href: 'javascript: void(0);'});
            previous.addClassName('PreviousLink');
            previous.update('&laquo; Previous');

            Event.observe(previous, 'click', RBPagesJS.ShowMainFullImage.curry(
                page_number*1 - 1, num_of_pages
            ));

            navigation.insert(previous);
        }

        // Add in the separator if the current page is a middle page
        if((page_number > 1) && (page_number < num_of_pages)) {
            var separator = new Element('span');
            separator.addClassName('Separator').update('|');

            navigation.insert(separator);
        }

        // Add in the next link if it is not the last page
        if(page_number < num_of_pages) {
            var next = new Element('a', {href: 'javascript: void(0);'});
            next.addClassName('NextLink');
            next.update('Next &raquo;');

            Event.observe(next, 'click', RBPagesJS.ShowMainFullImage.curry(
                page_number*1 + 1, num_of_pages
            ));

            navigation.insert(next);
        }

        // Add in the close link
        var close = new Element('a', {href: 'javascript: void(0);'});
        close.addClassName('CloseLink');
        close.update('Close x');
        Event.observe(close, 'click', function(page_number) {
            RBPagesJS.GoToPage(page_number);
            RBPagesJS.HideFullImageContainer();
        }.curry(page_number));
        navigation.insert(close);

        // Get the page
        var page = $$('.RBPages_MainPages')[page_number-1];
        var image_to_show = page.select('.TheImage .TheFullSize img')

        // Nothing to do if there is no image to show
        if(image_to_show.length < 1) {
        	container.insert(navigation);
        	return;
        }

        // Set the image
        var image = new Element('img', {alt: 'Page '+page_number+' Image'});
        image.src = image_to_show[0].src;
        image.addClassName('TheImage');

        // Set the content
        var content = new Element('div');
        content.addClassName('TheContent');
        content.update(page.select('.TheContent')[0].innerHTML);

        // Add in the content
        container.insert(navigation);
        container.insert(image);
        container.insert(content);

        // Fix the image width if it is too large
        var image_width = image.getWidth();
        var max_width = container.getWidth()-50;
        if(image_width > max_width) {
            image.setStyle({
                height: (image.getHeight() * max_width/image_width)*1+'px',
                width: max_width+'px'
            });
        }
    },

    /*
        Method: ShowWorkSampleFullImage
            Show the full size image.
    */
    ShowWorkSampleFullImage: function() {
        // We need to set the hide the flash items
        $('logo').setStyle({visibility: 'hidden'});
        $('flash_menu').setStyle({visibility: 'hidden'});

        // Show the overlay
        DLSupportJS.ShowOverlay({
            finish: RBPagesJS._ShowWorkSampleFullImage
        });
    },
    _ShowWorkSampleFullImage: function() {
        // Create the containing div
        RBPagesJS.CreateFullImageContainer();
        var container = $('RBPages_FullImageContainer');
        container.update('');

        // Set the navigation
        var navigation = new Element('div');
        navigation.addClassName('TheNavigation');

        // Add in the close link
        var close = new Element('a', {href: 'javascript: void(0);'});
        close.addClassName('CloseLink');
        close.update('Close x');
        Event.observe(close, 'click', RBPagesJS.HideFullImageContainer);
        navigation.insert(close);

        // Set the image
        var image = new Element('img', {alt: 'Work Sample Image'});
        image.src = $$('.RBPages_WorkSamples .TheImage .TheFullSize img')[0].src;
        image.addClassName('TheImage');

        // Add in the content
        container.insert(navigation);
        container.insert(image);

        // Fix the image width if it is too large
        var image_width = image.getWidth();
        var max_width = container.getWidth()-50;
        if(image_width > max_width) {
            image.setStyle({
                height: (image.getHeight() * max_width/image_width)*1+'px',
                width: max_width+'px'
            });
        }
    },


    /*
        Group: Initialization Methods

        Method: InitializeBottomNav
            Initialize the bottom navigation.
    */
    InitializeBottomNav: function() {
        // Get the images
        var images = $$('#bottom_menu ul li a img');
        for(var i = 0; i < images.length; ++i) {
            if(!images[i].ancestors()[1].hasClassName('selected')) {
                images[i].setOpacity(0.25);

                Event.observe(images[i], 'mouseover', function(image) {
                    new Effect.Opacity(image, {from: 0.25, to: 1.0, duration: 0.5});
                }.curry(images[i]));
                Event.observe(images[i], 'mouseout', function(image) {
                    new Effect.Opacity(image, {from: 1.0, to: 0.25, duration: 0.5});
                }.curry(images[i]));
            }
        }
    },

    /*
        Method: InitializeMainPages
            Initialize the main pages.

        Parameters:
            num_of_pages - (int) The number of pages.
    */
    InitializeMainPages: function(num_of_pages) {
        // The page number
        var page_number;

        // Deal with the images
        var images = $$('.RBPages_MainPages .TheImage .TheThumbnail img');
        for(var i = 0; i < images.length; ++i) {
            page_number = images[i].ancestors()[2].readAttribute('RBPages_PageNumber');
            Event.observe(images[i], 'click', RBPagesJS.ShowMainFullImage.curry(
                page_number, num_of_pages
            ));
        }

        // Deal with the links
        var links = $$('.RBPages_MainPages .TheNavigation a');
        for(i = 0; i < links.length; ++i) {
            page_number = links[i].ancestors()[1].readAttribute('RBPages_PageNumber');

            if(links[i].hasClassName('PreviousLink')) {
                --page_number;
            } else {
                ++page_number;
            }

            Event.observe(links[i], 'click', RBPagesJS.GoToPage.curry(page_number));
            links[i].href = 'javascript: void(0);';
        }
    },

    /*
        Method: InitializeWorkSamplePages
            Initialize the work sample pages.
    */
    InitializeWorkSamplePages: function() {
        // Deal with the image
        var image = $$('.RBPages_WorkSamples .TheImage .TheThumbnail img');
        if(image.length > 0) {
            Event.observe(image[0], 'click', RBPagesJS.ShowWorkSampleFullImage);
        }
    }
};