﻿String.prototype.format = function () {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp('\\{' + i + '\\}', 'gi');
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
};

/* Rosenberg Namespace */

var Rosenberg = {
    ui: {}, utils: {}, basePath: "" 
};



Rosenberg.init = function () {
    Rosenberg.basePath = $dnn.hostUrl;
    Rosenberg.ui.gallery.init();
    Rosenberg.ui.menu.init();
    Rosenberg.ui.homeGallery.init();
    Rosenberg.ui.stores.init();
    Rosenberg.ui.fancyBox.init();
}

Rosenberg.ui.fancyBox = {
    init: function () {
        $("a.fancy").fancybox({
            'titleShow': false
        });

    }
}

Rosenberg.utils.text =
{
    init: function () {

    },
    config: {
        pattern: function () { return '<img src="' + Rosenberg.basePath + "DesktopModules/Contento/Text/TextImageHandler.ashx?message={0}&fontName={1}&fontSize={2}&fontColor=_{3}&fontStyle={4}&tabid=50" + '" alt="{0}"/>' }
    },
    getImage: function (msg, font, size, color, style) {
        return this.config.pattern().format(msg, font, size, color, style);
    }
}

Rosenberg.utils.image = {
    init: function () {

    },
    config: {
        handlerPattern: Rosenberg.basePath + "DesktopModules/Contento/Thumbnails/Image.ashx?i={0}&w={1}&h={2}"
    },
    resize: function (src, w, h) {
        return this.config.handlerPattern().format(src, w, h);
    }
};

Rosenberg.ui.stores =
{
    init: function () {

    /*
        $("#stores").isotope({
            
            itemSelector: '.store'
        }); */

            var config = {
                over: function () {
                var offset = $(this).offset();
                var descrption = $(this).find(".description");


                $(this).find(".description").show();
            },    
                timeout: 200, 
                out: function () {

                $(this).find(".description").hide();
            } 
            };

            $(".store").hoverIntent(config)

            /*
        window.setInterval(function () {
            $("#stores").filter(':animated').stop();

            $("#stores").isotope({ layoutMode: 'cellsByRow',
                cellsByRow: {
                    columnWidth: 122,
                    rowHeight: 60
                }
            });

        }, 500);*/





    }
}


Rosenberg.ui.menu = {
    init: function () {
        $('ul.rosenberg-menu > li > a').each(function (index) {
            var message = $(this).html();
            var img = Rosenberg.utils.text.getImage(message.replace("&amp;","%26"), "PraxisCom-Semibold.ttf", 11, "ffffff", "Regular");
            $(this).html(img);
        });

        $('ul.rosenberg-menu').supersubs({ minWidth: 10, maxWidth: 80, extraWidth: 2 }).superfish({ autoArrows: false, dropShadows: false });
    }
}

Rosenberg.ui.gallery = {
    init: function () {
        if ($('.pager-entry').children().length < 2) {
            $('.next').hide();
            $('.prev').hide();
        }
        else {
            $('.pager-entry').cycle({
                fx: 'scrollLeft',
                speed: 'fast',
                timeout: 0,
                next: '.next',
                prev: '.prev'
            });
        }
        $('.pager-item').click(function () {
            $(".detail img").attr("src", $(this).attr("rel"));
        });

    }

}

Rosenberg.ui.homeGallery = {
    init: function () {

        $('.home-gallery .teasers').cycle({
            fx: 'fade',
            speed: 'slow',pager:'.pager',
            timeout: 4000, speed: 2500, pagerAnchorBuilder: function(idx, slide) { 
            return '<a href="#"><img src="' + Rosenberg.utils.image.resize(jQuery(slide).attr("data-img"),50,50) + '" width="50" height="50" /></a>'; 
    } 
        });


    }

}

$(document).ready(function () {
    Rosenberg.init();
});
