﻿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 condecta = condecta || {}

condecta.ui = {}; condecta.common = {}; condecta.forms = {}; condecta.basePath = "", condecta.portalId = 0, condecta.tabId =0;


condecta.init = function () {
    condecta.basePath = $dnn.hostUrl;
    condecta.common.init();
    condecta.ui.init();
    condecta.forms.init();

};

condecta.common.init = function () {
    condecta.common.search.init();
    condecta.common.search.getSuggestions("condecta");

};

condecta.ui.init = function () {
    condecta.ui.downloads.init();
    condecta.ui.print.init();
    condecta.ui.fancy.init();
    /*condecta.ui.search.init();*/
    /*condecta.ui.menuFix.init();*/
};



condecta.forms.init = function () {
    condecta.forms.toilet.init();
};

condecta.forms.toilet = {
    init: function () {
        var imgs = { "Basic": "http://www.condecta.ch/contento/Portals/6/media/produkte_events/kabinen_modell_basic.jpg",
            "Comfort": "http://www.condecta.ch/contento/Portals/6/media/produkte_events/Comfort_3000_3.jpg", "Camping": "http://www.condecta.ch/contento/Portals/6/media/produkte_events/mobitoil_camping_100.jpg"
        };
        var $form = $("#dnn_ctr1191_Default_Form_divForm, #dnn_ctr1273_Default_Form_divForm, #dnn_ctr1276_Default_Form_divForm, #dnn_ctr1282_Default_Form_divForm");
        $form.find("div.UDT_Field").addClass("clearfix");
        var $cabins = $form.find("select[id*='Kabinenmodell'], select[id*=Modèle]").each(function (index) {
            var $sel = $(this);
            $sel.width(40);
            $sel.css('margin-right', '10px');
            var id = $(this).attr("id");
            $.each(imgs, function (img) {
                if (id.indexOf(img) != -1) {
                    $sel.closest(".UDT_Field").append("<img src='" + imgs[img] + "' style='clear:both;display:block;'/>");

                }


            });

        });
    }
}

condecta.ui.menuFix = {
    init: function () {
        $("li.selected").parents("li").addClass("selected");
    }

}

condecta.ui.print = {
    init: function () {
        $(".btn-print").click(function () {
            document.getElementById("dnn_SKINSELECT_cboSkins").selectedIndex = 1;
            __doPostBack('dnn$SKINSELECT$cboSkins', '1');
            return false;
        });


    }
}

condecta.ui.search = {

    init: function () {
        $("#search-box").autoSuggest(condecta.common.search.getSuggestionsUrl, { queryParam: "s", type: "POST", selectedItemProp: "Title", selectedValueProp: "Title", searchObjProps: "Title", selectionLimit: 1, selectionClick:
        function (elem) {

        }
        });
    }
}

condecta.ui.downloads = {

    init: function () {
        /*$(".download-btn").click(function () {
            var data = $(this).attr("data");

            $(".downloads").find("[data='" + data + "']").toggleClass("active");
            return false;
        });*/ /* disabled by request */
        $(".downloads .filter").chosen();
        $(".downloads .filter").chosen().change(function () {
            var url = document.location.href;
            url = (url.indexOf("?") == -1) ? url : url.split("?")[0];

            document.location = url + encodeURI("?" + $(this).val());
        }
        );
    },
    trigger: function (container_id) {
        var url = "http://www.condecta.ch/contento/DesktopModules/Contento/Downloads/Basket.ashx?portalid=0&tabid=705&id=";
        var ids = [];
        var checked = $("#" + container_id).find(".active").each(function () {
            //fill the array with the values
            var id = $(this).attr("data");


            if (jQuery.inArray(id, ids) == -1) {

                url = url + id + ";";
                ids.push(id);
            }


        });

        if (ids.length == 0) {
            alert("Sie haben keine Downloads ausgewählt");
        }
        else {

            document.location = url;
        }

    }

};

condecta.common.search = {
    init: function () {
        condecta.common.search.searchServiceUrl = condecta.basePath + "DesktopModules/contento/Search/SearchService.asmx";
        condecta.common.search.getSuggestionsUrl = condecta.common.search.searchServiceUrl + "/GetSuggestions";
    },
    getSuggestions: function (s) {


        $.ajax({
            type: "POST",
            url: condecta.common.search.getSuggestionsUrl,
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{"s":"' + s + '"}',

            success: function (data) {
                return data;
            }
                    , error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error");
                    }
        });




    }

}

condecta.common.image = {
    init: function () {

    },
    config: {
        pattern: function () { return condecta.basePath + "DesktopModules/UserDefinedTable/makethumbnail.ashx?image={0}&w={1}&h={2}" + '&portalid=' + condecta.portalId + '&tabid=' + condecta.tabId; }
    },
    resize: function (src, w, h) {
        return condecta.common.image.config.pattern().format(src, w, h);
    }
};


condecta.ui.fancy = {
    init: function () {

        $('img.fancy').click(function () {
            $.fancybox({
                //'orig'			: $(this),               
                'href': condecta.ui.fancy.urls[$(this).attr("src")],
                'transitionIn': 'elastic',
                'transitionOut': 'elastic',
                'showCloseButton': true,
                'titlePosition': 'inside'
            });
        });
        $('img.fancy').each(function () {

            var src = $(this).attr("src");
            var w = $(this).width();
            var h = $(this).height();
            var resized = condecta.common.image.resize(src, w, h);
            $(this).attr("src", resized);

            condecta.ui.fancy.urls[resized] = src;
        });
        $("a.fancy").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'showCloseButton': true,
            'titlePosition': 'inside'
        });


    },
    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/condectabruggneu/js/jquery.fancybox/closelabel.gif" /></a></span></div>';
    }

};

$(document).ready(function () {
    condecta.init();

});
