/*      Gadanke Image Browser
 *
 *      (C) Copyright 2010 Martin Clemons
 */

var imageBrowser = {
    'bookDivs'      : [],           // array of book div elements
    'books'         : [],           // array of custom book objects with image URLs and URL of actual product
    'currentBook'   : '',           // index of current book being viewed
    'currentImage'  : '',           // index of current image being viewed
    
    'init'          : function () {
                        var imageLinks, urlParts, req;
                        // get all shareBook divs, process enclosed a tags to extract information
                        this.bookDivs = $('.shareBook');
                        
                        for (var i=0; i<this.bookDivs.length; i++) {    // for each div
                            this.books.push({ 'imageURLs'  : [],
                                              'productURL' : ''
                                              });
                            this.books[i].productURL = $(this.bookDivs[i]).children('a.productLink').attr('href');
                            imageLinks = $(this.bookDivs[i]).children('a.bookImage');
                            for (var j=0; j<imageLinks.length; j++) {   // for each link
                                this.books[i].imageURLs.push($(imageLinks[j]).attr('href'));
                            } 
                        }
                        // add select method to divs
                        $(this.bookDivs).each(function (index, element) {
                                            $(element).click(function () {imageBrowser.selectBook(index);});
                                         });
                        // set defaults
                        this.currentBook = false;
                        
                        // extract parameter from URL
                        urlParts = document.URL.split('?loadJournal=', 2);
                        if (urlParts.length > 1) {
                            this.selectBook(urlParts[1].substring(0,2));
                        } else {
                            this.selectBook(0);
                        }
                    },
                    
    'selectBook'    : function (indexOfCaller) {
                        if (indexOfCaller === this.currentBook || this.bookDivs[indexOfCaller] === undefined) {
                            return;
                        }
                        $(this.bookDivs[this.currentBook]).removeClass('shareBookSelected');                // set and remove highlighting
                        $(this.bookDivs[indexOfCaller]).addClass('shareBookSelected');
                        this.currentBook = indexOfCaller;
                        //$('span#product-link > a').attr('href', this.books[this.currentBook].productURL);   // update purchase link
                        this.currentImage = 0;                                                              // update images
                        this.setImage();
                    },
                    
    'setImage'      : function () {
                        $('#dynamic-img').fadeOut(300, function () {
                            $('#dynamic-img').attr('src', imageBrowser.books[imageBrowser.currentBook].imageURLs[imageBrowser.currentImage]);
                            $('#dynamic-img').load(function () {$(this).fadeIn(300);});
                        });
                    },
                    
    'incImage'      : function () {
                        if (this.currentImage < this.books[this.currentBook].imageURLs.length - 1) {
                            this.currentImage++;
                            this.setImage();
                        } else if (this.currentImage != 0) {
                            this.currentImage = 0;
                            this.setImage();
                        }
                    },
                    
    'decImage'      : function () {
                        if (this.currentImage > 0) {
                            this.currentImage--;
                            this.setImage();
                        } else if (this.currentImage != this.books[this.currentBook].imageURLs.length - 1) {
                            this.currentImage = this.books[this.currentBook].imageURLs.length - 1;
                            this.setImage();
                        }
                    }
};

$(document).ready(function (){imageBrowser.init();});
