var ShopConsts = {
    Pages: {
      Kosarba: '/kosar/pag_kosarba.aspx',
      Osszehasonlito: '/egyeb/osszehasonlito/pag_osszehasonlito.aspx',
      TermekKepek: '/cikkek/reszletek/kepek/pag_cikkekKepei.aspx',
      PartnerReszletekPopup: '/altalanos/partner/pag_partnerreszletekpop.aspx'
    }
};

var WebConfig = WebConfig || { ApplicationPath: '' };

(function() {

    function Shop() {

    }

    Shop.prototype.KosarbaTesz = function(cikkid, referer) {
        var ref = referer || (window.location.pathname + window.location.search);

        this.SendForm(this.ResolveVirtualPath(ShopConsts.Pages.Kosarba), { 'cikkid': cikkid, 'return': ref });
    };

    Shop.prototype.ShowTermekKepek = function(cikkid, width, height) {
        var win = this.OpenNewWindow(this.ResolveVirtualPath(ShopConsts.Pages.TermekKepek), 'termek_kepek', 'center', { status: 'no', width: width, height: height });
        win.focus();
    };

    Shop.prototype.ResolveVirtualPath = function(path) {
        var a = WebConfig.ApplicationPath;
        if (a == '/') {
            a = '';
        }
        if (path.substring(0,1) == '~') {
            return a + path.substring(1);
        }
        return a + path;
    };

    Shop.prototype.SendForm = function(action, params, method) {
        method = method || 'post';

        var form = this.CreateElement('form', { method: method, action: action });

        var input;
        for (var p in params) {
            input = this.CreateElement('input', { type: 'hidden', name: p, value: params[p] });

            form.appendChild(input);
        }

        document.body.appendChild(form);
        form.submit();
    };

    Shop.prototype.CreateElement = function(tag, attributes) {
        var e = document.createElement(tag);

        for (var a in attributes) {
            e[a] = attributes[a];
        }

        return e;
    };

    Shop.prototype.Format = function(format) {
        var result = format;
        for (var i = 1; i < arguments.length; i++) {
            result = result.replace('{' + (i - 1) + '}', arguments[i]);
        }
        return result;
    };

    Shop.prototype.OpenNewWindow = function(url, name, position, options) {
        var settings = {
            width: 200, height: 200,
            top: 0, left: 0,
            scrollbars: 'yes',
            location: 'no', directories: 'no', status: 'yes',
            menubar: 'no', toolbar: 'no', resizable: 'no'
        };

        settings = this.Merge(options, settings);

        if (position == 'random') {
            settings.left = (screen.width) ? Math.floor(Math.random() * (screen.width - settings.width)) : 100;
            settings.top = (screen.height) ? Math.floor(Math.random() * ((screen.height - settings.height) - 75)) : 100;
        } else if (position == 'center') {
            settings.left = screen.width ? (screen.width - settings.width) / 2 : 100;
            settings.top = screen.height ? (screen.height - settings.height) / 2 : 100;
        }

        var sets = '';
        for (var k in settings) {
            if (sets.length > 0) sets = sets + ',';
            sets = sets + k + '=' + settings[k];
        }

        return window.open(url, name, sets);
    };

    Shop.prototype.Merge = function(o1, o2) {
        o1 = o1 || {};
        for (var key in o1) {
            if (o1[key] === undefined) continue;
            o2[key] = o1[key];
        }
        return o2;
    };

    window.Shop = new Shop();

})();
