﻿/* Helper Methods*/

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;
};


var Geo = Geo || { ui: {}, utils: {}, basePath: "", portalId:0, tabId:0};


Geo.init = function () {
    Geo.basePath = $dnn.hostUrl;
    Geo.ui.fancy.init();
    Geo.ui.cycle.init();
};

/* Utils */

/* Image Related  Utils */
Geo.utils.image = {
    init: function () {

    },
    config: {
        pattern: function () { return Geo.basePath + "DesktopModules/UserDefinedTable/makethumbnail.ashx?image={0}&w={1}&h={2}" + '&portalid=' + Geo.portalId + '&tabid=' + Geo.tabId; }
    },
    resize: function (src, w, h) {
        return this.config.pattern().format(src, w, h);
    }
};

Geo.ui.cycle = {
    init: function () {
        $('div.slideshow').cycle({
            fx: 'fade',
            speed: 2500,
            timeout:  2000,
            random: 1
        });
    }
};

Geo.ui.fancy = {
    init: function () {

        $('img.fancy').click(function () {
            $.fancybox({
                //'orig'			: $(this),               
                'href': Geo.ui.fancy.urls[$(this).attr("src")],
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'showCloseButton': false,
                'titlePosition': 'inside',
                'titleFormat': Geo.ui.fancy.formatTitle
            });
        });
        $('img.fancy').each(function () {

            var src = $(this).attr("src");
            var w = $(this).width();
            var h = $(this).height();
            var resized = Geo.utils.image.resize(src, w, h);
            $(this).attr("src", resized);

            Geo.ui.fancy.urls[resized] = src;
        });
        $("a.fancy").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'showCloseButton': false,
            'titlePosition': 'inside',
            'titleFormat': Geo.ui.fancy.formatTitle
        });

        $(".cover-image a.fancy").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'showCloseButton': false,
            'titlePosition': 'inside',
            'titleFormat': Geo.ui.fancy.formatDownloadSheetTitle
        });


    },
    urls: [],
    formatTitle: function (title, currentArray, currentIndex, currentOpts) {
        return '<div id="tip-title">' + (title && title.length ? '<b>' + title + '</b>' : '') + 'Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '<span><a style="border:0;" href="javascript:;" onclick="$.fancybox.close();"><img src="/contento/Portals/35/Skins/geobruggneu/js/jquery.fancybox/closelabel.gif" /></a></span></div>';
    },
    formatDownloadSheetTitle: function (title, currentArray, currentIndex, currentOpts) {
        var $sigma = $(currentArray[currentIndex]);
        var fileUrl = $sigma.attr('data-url');
        var fileSize = $sigma.attr('data-size');
        return '<div id="tip-title">' + (title && title.length ? '<b>' + title + '</b>' : '') + '<a href="' + fileUrl +  + (currentIndex + 1) + '" target="_blank">Download PDF (' + fileSize + ')</a><span><a style="border:0;" href="javascript:;" onclick="$.fancybox.close();"><img src="/contento/Portals/35/Skins/geobruggneu/js/jquery.fancybox/closelabel.gif" /></a></span></div>';
    }

};




$(document).ready(function () {
    Geo.init();
});
