/* todo this is just a quick move to jquery with essential fixes, this needs to be re-written & documented */

// Global Carrier App Object
var CARRIER = (function(carrier) {
    return carrier;
}( CARRIER || {} ));
 

$(function() {

    /* Top nav ie hover state */
    if($.browser.msie) {
        $('#topnav li').hover(
            function () {
                $(this).addClass("over");
            },
            function () {
                $(this).removeClass("over");
            }
        );
    }

    //init galleries
    CARRIER.gallery.init();
});


// Define Gallery - todo move to sep file after redesign
CARRIER.gallery = (function() {
    var self = this;

    //Only Public Method
    var init = function() {
        self.totalImages = parseInt($("#total-photo-number").html(), 10),
        self.showcaseImg = $("#showcase_img"),
        self.photoCredit = $(".photo-credit"),
        self.folder = $("#showcase_img").attr("class"),
        self.galleryName = self.folder;
        
        _bindButtons();
        _showCredits(1); //handle initial load
    },

    //Change Image controller called via previous/next buttons
    _iterator = function(direction, galleryType) {
        var currentImgCnt = parseInt($("#current-photo-number").html(), 10);

        if(direction == "next") {
            currentImgCnt++;
        } else {
            currentImgCnt--;
        }

        _showImage(galleryType, currentImgCnt);
        _showCredits(currentImgCnt);
        _updateCurrentPhoto(currentImgCnt);
        _updateButtons();
    },


    /* show image - todo this will be refactored to cycle arbritrary divs not the image names */
    _showImage = function(galleryType, imgCnt) {
        self.showcaseImg.attr("src",  "/images/"+galleryType+"/"+self.folder+"/"+self.folder+"-"+imgCnt+".jpg");
        $.publish('/gallery/imageLoad', [self.folder, imgCnt]);
    },


    /* update current photo, this is how we track, TODO refactor to track this without the DOM */
    _updateCurrentPhoto = function(currentPhoto) {
        $("#current-photo-number").html(currentPhoto);
    },


    /* handle photo credits */
    _showCredits = function(image) {
        //if photocredit thingy has class that matches current image show
        var photoCreditMatch = self.galleryName + "_credit-" + image;

        self.photoCredit.each(function() {
            if($(this).hasClass(photoCreditMatch)) {
                $(this).show();
            } else {
                $(this).hide();
            }
        });
    },


    /* update buttons*/
    _updateButtons = function() {
        var current = parseInt($("#current-photo-number").html(), 10);
        if(current >= self.totalImages) {
            $("#next_off").show();
            $("#next_btn").hide();
        } else {
            $("#next_off").hide();
            $("#next_btn").show();
        }

        if (current != 1) {
            $("#previous_off").hide();
            $("#previous_btn").show();
        } else {
            $("#previous_off").show();
            $("#previous_btn").hide();
        }
    },


    /* bind buttons */
    _bindButtons = function() {
        $("#next_btn").bind('click', function() {
            var galleryType = $(this).attr("class");
            _iterator("next", galleryType);
            return false;
        });

        $("#previous_btn").bind('click', function() {
            var galleryType = $(this).attr("class");
            _iterator("previous", galleryType);
            return false;
        });
    };

    /* return public methods */
    return {
        init: init
    };

})();
