// Rewriting index frame hash to go directly to a specific album/image

var redirecter;

onReady(function() {
   redirecter = new HashRedirecter();
});

function HashRedirecter() {
    var aMatches;
    var This = this;

    // Extract src from frame src
    this.domFrame = document.getElementById('mainFrame');
    this.srcOriginal = this.domFrame.src;
    aMatches = this.srcOriginal.match(/iloapp.([^\/]+)\/gallery\/([^\?\#]+)/);

    if (aMatches) {
        this.domain = aMatches[1];
        this.galleryLocation = aMatches[2];
    } else {
        return;
    }

    if (this.isMobile()) {

        // Show mobile version without frame
        window.location = 'http://iloapp.' + this.domain + '/gallery/' + this.galleryLocation + '?Mobile';
        return;

    } else {

        // Update frame for the first time
        this.updateFrame();

        // Begin interval
        setInterval(function () {
            This.updateFrame();
        }, 500);
    }
}

HashRedirecter.prototype =
{
    hashCurrent: '',

    updateFrame: function() {
        var txtFrameSrc = 'http://iloapp.' + this.domain + '/gallery/' + this.galleryLocation + '?';
        var domFrame = document.getElementById('mainFrame');
        var aMatches;

        if (location.hash != this.hashCurrent) {
            // Change domFrame src according to hash
            if (aMatches = location.hash.match(/^#home(\.(\d+))?$/)) {
                if (aMatches[2] != null) {
                   txtFrameSrc += 'Home&page=' + parseInt(aMatches[2]);
                } else {
                   txtFrameSrc += 'Home';
                }
            } else {
                // Get album and image ids from hash
                aMatches = location.hash.match(/^#(edit|slideshow)?(\d+)(?:\.(\d+))?/);
                if (aMatches) {
                    var albumId = aMatches[2];
                    var imageId = aMatches[3];

                    if (aMatches[1] == 'edit') {
                        txtFrameSrc += 'EditAlbum&album=' + albumId;
                    } else if (aMatches[1] == 'slideshow') {
                        txtFrameSrc += 'SlideShow&album=' + albumId;
                    } else {
                        txtFrameSrc += 'Album&album=' + albumId;
                    }

                    // Check if option image id was given
                    if(imageId != null) {
                        txtFrameSrc += '&image=' + imageId;
                    }
                }
            }

            this.domFrame.src = txtFrameSrc;

            // Update hashCurrent so it doesn't keep checking
            this.hashCurrent = location.hash;

        } else {

            // Check if cookie contains a message
            var aMatches = document.cookie.match(/gmsg(\d+)=([^;]+)/);
            if(aMatches) {
                var galleryId = aMatches[1];
                var message = aMatches[2];
                var expires = new Date(0);

                // Delete the cookie
                document.cookie = 'gmsg' + galleryId + '=_;path=/;domain=.' + this.domain +
                        ';expires=' + expires.toGMTString();

                // Set hash according to message
                if (!navigator.userAgent.match(/safari/i)) {
                    location.replace('#' + message);
                    this.hashCurrent = '#' + message;
                }
            }
        }
    },

    isMobile: function () {
        return ((BO.iphone || BO.android || document.cookie.match(/is_mobile=true/)) &&
                !document.cookie.match(/is_mobile=false/));
    }
};