(function ($) {

    /**
    * @constructor
    */
    function KeyValueCollection() {
        //	
    }
    KeyValueCollection.prototype.addPair = function (key, value) {
        this[key] = value;
    };
    KeyValueCollection.prototype.getValue = function (key) {
        return this[key];
    };
    KeyValueCollection.prototype.contains = function (key) {
        return (this[key] != null);
    };

    /**
    * @constructor
    */
    function AsyncContent() {
        this.contentGroup = new KeyValueCollection();
        this.placeholders = [];
    }
    AsyncContent.prototype.addContentGroup = function (key, contentCollection) {
        if (this.contentGroup.contains(key)) {
            var cc = this.contentGroup.getValue(key);
            cc = cc.concat(contentCollection);
        }
        else {
            this.contentGroup.addPair(key, contentCollection);
        }
    };
    AsyncContent.prototype.addContentObserver = function (key, observer) {
        var placeholder = {
            key: key,
            observer: observer,
            images: [],
            loadPendingOrComplete: false
        };
        this.placeholders.push(placeholder);
    };
    AsyncContent.prototype.loadContent = function (key) {
        if (this.contentGroup.contains(key)) {
            var cg = this.contentGroup; //scoping???
            $.each(this.placeholders, function (i, placeholder) {
                if (placeholder.key == key) {
                    if (!placeholder.loadPendingOrComplete) {
                        placeholder.loadPendingOrComplete = true;
                        var roomImgElem = $('#roomImageTemplate').clone(),
                            imgCount = cg.getValue(key).length,
                            curImg = 0;
                        $(roomImgElem).attr("id", "rmImg-" + key);

                        //var imgCount = cg.getValue(key).length;
                        //var curImg = 0;

                        //remove arrows if not needed
                        if (imgCount < 2) {
                            $('a', roomImgElem).remove();
                        }

                        $.each(cg.getValue(key), function (i, imageData) {
                            var img = new Image();
                            img.width = imageData.width;
                            img.height = imageData.height;
                            img.src = imageData.url;
                            placeholder.images.push(img);
                            $(img).load(function () {
                                curImg++;

                                //could be trouble with concurrency
                                if (curImg == imgCount) {
                                    $.each(placeholder.images, function (i, image) {
                                        $('.slides', roomImgElem).append(image);
                                    });

                                    $('.slides img', roomImgElem).hide().filter(':first').show();
                                    $('.imageloader', placeholder.observer).hide();
                                    roomImgElem.closest('.roomWidget').trigger('contentLoaded');
                                    roomImgElem.show();
                                }
                                else {
                                    var nexImg = curImg + 1,
                                        message = "loading image " + nexImg + " of " + imgCount;
                                    $('.imageloader .message', placeholder.observer).text(message);
                                }
                            });
                        });

                        placeholder.observer.append(roomImgElem);
                        roomImgElem.hide();
                        $(roomImgElem).attr("class", "roomImgs"); //should block display until hidden
                    }
                }
            });
        }
    };
    //    AsyncContent.prototype.loadAll = function () {
    //        var cg = this.contentGroup; //scoping???
    //        $.each(this.placeholders, function (i, placeholder) {
    //            //alert(cg.getValue(placeholder.key).length);
    //            //loadContent(placeholder.key, placeholder.element);
    //            if (cg.contains(placeholder.key)) {
    //                $.each(cg.getValue(placeholder.key), function (i, imageData) {
    //                    var img = new Image();
    //                    img.src = imageData.url;
    //                    //$(img).load(imageData.url, function () {
    //                    $(img).load(function () {
    //                        placeholder.observer.append(img);
    //                    });
    //                    //alert(imageData.url);
    //                });
    //            }
    //        });
    //    };


    // Create a new instance of the application and store it in the window.
    window.acObj = new AsyncContent();

    // Return a new application instance.
    return (window.acObj);
})(jQuery);

$(document).ready(function () {

    //bind delayed img loading to vtabs for lodging
    $('.v-tab-container').each(function (i, o) {
        $('.v-tabs a.v-tab', o).filter(':not(:first)').click(function () {
            acObj.loadContent(this.rel.substring(1));
        });
    });

    $('.roomWidget').bind('contentLoaded', function () {
        //attach any needed behavior to lazy loaded content
        var o = this;
        var roomtourWrapper = $('.slides img', o),
            maxIndex = roomtourWrapper.length - 1,
            index = 0;
        // only show the first image, hide the rest
        roomtourWrapper.hide().filter(':first').show();

        function swapRoomtour(index, prevIndex) {
            roomtourWrapper.css('z-index', 4);
            roomtourWrapper.eq(index).css('z-index', 5);
            roomtourWrapper.eq(index).fadeIn(200, function () {
                roomtourWrapper.eq(prevIndex).stop(true, false); //kill any overlapping fades
                roomtourWrapper.eq(prevIndex).fadeTo(0, 1); //restore prev to alpha 1 if interrupted
                roomtourWrapper.eq(prevIndex).hide();
            });
            $('.detail .floorplan area', o).removeClass('current');
            $('.detail .floorplan area', o).eq(index).addClass('current');
        }

        $('.detail .floorplan area', o).mouseover(function () {
            // check if this item doesn't have class "current"
            // if it has class "current" it must not execute the script again
            if (this.className.indexOf('current') == -1) {
                //var $th = $(this);
                index = $(this).prevAll().length;
                //get currently active promo index for swap			
                var prevElem = $('.detail .floorplan area.current', o),
                    prev = prevElem.parent().children().index(prevElem);
                swapRoomtour(index, prev);
            }
            return false;
        });

        $('a.navLeft', o).click(function () {
            var prevIndex = index;
            if (index == 0) {
                index = maxIndex;
            }
            else index--;
            swapRoomtour(index, prevIndex);
        });

        $('a.navRight', o).click(function () {
            var prevIndex = index;
            if (index == maxIndex) {
                index = 0;
            }
            else index++;
            swapRoomtour(index, prevIndex);
        });
    });
});
