﻿var _apoteksgruppen = function () {

    this.modules = [];
    this.unregister = function (module) {
        this.modules[modulename] = null;
        this[modulename] = null;

    };
    this.register = function (modulename, module) {
        this.modules[modulename] = module;
        this[modulename] = new module(this);
        this.modules[modulename].core = this;
    };
};
var apoteksgruppen = new _apoteksgruppen();

_apoteksgruppen.prototype.__Methods = function (base) {
    this.replaceHTML = function (id, html) {
        $(document).ready(function () {
            $("#" + id).replaceWith(html);
        });
    };
    this.createCookie = function (name, value, expire) {
        if (expire) {
            var d = new Date();
            d.setTime(d.getTime() + (expire * 24 * 60 * 60 * 1000));
            var e = "; expires=" + d.toGMTString();
        }
        else
            var e = "";
        document.cookie = name + "=" + value + e + "; path=/";
    };
    this.readCookie = function (name) {
        var b = "";
        if (document.cookie.length > 0) {
            cS = document.cookie.indexOf(name + "=");
            if (cS != -1) {
                cS = cS + name.length + 1;
                cE = document.cookie.indexOf(";", cS);
                if (cE == -1)
                    cE = document.cookie.length;
                b = unescape(document.cookie.substring(cS, cE));
            }
        }
        if (b == null || b == "null")
            b = "";
        return b;
    };
    this.countObj = function (obj) {
        var count = 0;
        for (property in obj) count++;

        return count;
    };
    this.params = {};
    this.updateStoreList = function (el, query, withQuery) {

        this.params[query] = $(el).val();

        if ($(el).val() != 0) {
            var url = (withQuery == true) ? window.location.href : window.location.href.split('?')[0];
            url = url.match(/\?/) ? url + '&' : url + '?';
            if (this.countObj(this.params) > 1) {
                for (var key in this.params) {
                    var re = RegExp(';?' + key + '=?[^&;]*', 'g');
                    url = url.replace(re, '');
                    url += '&' + key + '=' + this.params[key];
                }
            } else {
                for (var key in this.params) {
                    var re = RegExp(';?' + query + '=?[^&;]*', 'g');
                    url = url.replace(re, '');
                    url += query + '=' + this.params[key];
                }
            }
            url = url.replace(/[&&]$/, '&');
            url = url.replace(/\?[;&]/, '?');
            url = url.replace(/[&&]{2}/g, '&');
            window.location.replace(url);
        }
    };
    this.toggle = function (iconObj, obj) {
        var icon = $(iconObj);
        var e = $(obj);
        if (e.hasClass('showBlock')) {
            if (icon.hasClass('minusicon')) { icon.removeClass('minusicon'); }
            icon.addClass('plusicon');
            e.removeClass('showBlock');
            e.addClass('hideBlock');
        } else {
            icon.removeClass('plusicon');
            icon.addClass('minusicon');
            e.removeClass('hideBlock');
            e.addClass('showBlock');
        }
    };
    this.toggleAll = function (option) {
        var icon, e, i, j, blockClass = "showBlock", iconClass = "minusicon", ob = "hideBlock", ib = "plusicon";
        if (option === 'collapse') {
            blockClass = "hideBlock";
            iconClass = "plusicon";
            ob = "showBlock";
            ib = "minusicon";
        }
        icon = $("." + ib);
        e = $("." + ob);
        if (e.length > 0) {
            for (i = 0; i < e.length; i++) {
                var x = $(e[i]);
                if (x.hasClass(ob)) { x.removeClass(ob); }
                x.addClass(blockClass);
            }
        }
        if (icon.length > 0) {
            for (j = 0; j < icon.length; j++) {
                var y = $(icon[j]);
                if (y.hasClass(ib)) { y.removeClass(ib); }
                y.addClass(iconClass);
            }
        }
    };

    this.getStreet = function () { /*We are not using this function since when this code is in external script, copy past of the street address is not working here.*/
        var geocoder = new google.maps.Geocoder();
        $(function () {
            $("#km_control").autocomplete({
                source: function (request, response) {
                    if (geocoder == null) {
                        geocoder = new google.maps.Geocoder();
                    }
                    request.term = request.term.replace(/^\s+|\s+$/, '')
                    if (request.term.length > 3) {
                        if (request.term.toLowerCase().indexOf('sweden') == -1) {
                            request.term = request.term + ' sweden';
                        }
                        //request.term = request.term.replace(/ä/g, 'a');
                        geocoder.geocode({ 'address': request.term, 'region': 'SE' }, function (results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {
                                var searchLoc = results[0].geometry.location;
                                var lat = results[0].geometry.location.lat();
                                var lng = results[0].geometry.location.lng();
                                $('#lat').val(lat);
                                $('#lng').val(lng);
                                apoteksgruppen.methods.params.lat = lat;
                                apoteksgruppen.methods.params.lng = lng;
                            }
                            else {
                                $('#lat').val(0);
                                $('#lng').val(0);
                                apoteksgruppen.methods.params.lat = 0;
                                apoteksgruppen.methods.params.lng = 0;
                            }
                        });
                    }
                    else {
                        $('#lat').val(0);
                        $('#lng').val(0);
                        apoteksgruppen.methods.params.lat = 0;
                        apoteksgruppen.methods.params.lng = 0;
                    }
                }
            });
        });
    };
};
apoteksgruppen.register("methods", apoteksgruppen.__Methods);


_apoteksgruppen.prototype.__Onload = function (base) {
    this.core = base;

    this.initLoad = function () {
        $(document).ready(function () {
            this.core = base;
            /*Try catch added to fix issue in opening the webtree pages in editor.*/
            try {
                /*Intressealaman page: To display the news description b/w intresealaman page */
                var hiddenID = $("#hiddeninterests");
                if (hiddenID.length > 0 && hiddenID.text() !== "") {
                    this.core.methods.replaceHTML('description', hiddenID[0].innerHTML);
                }
                /*Home page and Landing pages: For slider/flash displayed in home pages and landing pages*/
                var flash = $("#slider");
                if (flash.length > 0) {
                    $('#slider').s3Slider({
                        timeOut: 3000
                    });
                }
                /*Products list Page: To move reduce width for the regular products module when the OTC products exists*/
                var prodControl = $('#productOTC');
                if (prodControl.length > 0 && prodControl.html() !== "") {
                    $('#productRegular').addClass('withOTC');
                }

               

                /*Common for all pages: To bring the borders to equal heights for overall site.*/
                $(document).ready(function () {
                    $('.subcolumns').equalHeights('dl:not(dl.shadeborder)');
                    $('.subcolumns').equalHeights('li.fix');
                    $('.products ol').equalHeights('li.fix');
                    $('.news.landing ol').equalHeights('dl:not(dl.shadeborder)');
                  
                });
                $.ajax({
                    url: document.location.href,
                    success: function () {
                        // your code to insert the relevant content
                        $('.subcolumns').equalHeights('dl:not(dl.shadeborder)');
                        $('.subcolumns').equalHeights('li.fix');
                        $('.products ol').equalHeights('li.fix');
                        $('.news.landing ol').equalHeights('dl:not(dl.shadeborder)');
                       
                        //equalHeight($(".block")); // insert this at the end of each call to the RSS files
                    }
                });
                $.get("/apoteksgruppen/upload/files/css/style.css", function (data) {
                    $('.subcolumns').equalHeights('dl:not(dl.shadeborder)');
                    $('.subcolumns').equalHeights('li.fix');
                    $('.products ol').equalHeights('li.fix');
                    $('.news.landing ol').equalHeights('dl:not(dl.shadeborder)');
                   
                });


                $('.news article ol li.fix dl').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('article ol li.fix').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('article ol li.clubproduct').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('article ol li.fix dl dd.prod_info').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.loc_store article').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.products.regularOTC').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                /* Changed the article tage to class Name */
                $('.news .article ol li.fix dl').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.article ol li.fix').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.article ol li.clubproduct').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.article ol li.fix dl dd.prod_info').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });
                $('.loc_store .article').css({ "behavior": "url('/apoteksgruppenedit/upload/files/css/css3pie_for_ie/pie.htc')" });

            }
            catch (e) { }
        });
    }
    this.load = this.initLoad();
};
apoteksgruppen.register("onload", apoteksgruppen.__Onload);

$.fn.equalHeights = function (clas) {
    $(this).each(function () {
        var currentTallest = 0;
        $(this).find(clas).each(function (i) {
            if ($(this).height() > currentTallest) { currentTallest = ($(this).height()  ); }
        });
        if ($.browser.msie && $.browser.version == 6.0) { $(this).find(clas).css({ 'height': currentTallest }); }
        $(this).find(clas).css({ 'min-height': currentTallest });
    });
    return this;
};




