﻿function getSeoCampDetailUrl(id, tab, lang, localizedTab, key, prefix, baseUrl) {
    return String.format(
    "{0}/{1}/{2}/{3}/{4}/{5}-{6}",
    baseUrl, lang, prefix, key, localizedTab, id, tab);
};

function getPropertyValueById(props, propId) {
    if (props == null || props.length == 0)
        return null;
    for (var i = 0; i < props.length; i++) {
        if (props[i].PropertyId === propId)
            return props[i].Value;
    }
};

function gridPaging(index, size) {
    this.pageIndex = index;
    this.pageSize = size;
};

function searchOptions(keepPager, zoomToView) {
    this.keepPager = keepPager;
    this.zoomToView = zoomToView === null ? false : zoomToView;
};

var comparison = {
    max: 5,
    comparisonItemTemplate: null,
    url: "compare.aspx",
    ids: new Array(),
    infos: new Array(),
    langId: 0,
    cookieExpiration: 0.021, // 0.021 day ~= 30 min
    init: function () {
        var idsCookie = $telerik.$.cookie("comparison_ids");
        if (idsCookie != null && idsCookie.length > 0) {
            $telerik.$.each(idsCookie.split(','), function (index, value) { comparison.ids[index] = parseInt(value); });
        }

        if (this.ids.length > 0)
            for (var i = 0; i < this.ids.length; i++)
                Xperiensis.CampCz.Webservice.CampInfoWS.GetCampInfo(this.ids[i], this.langId, this.getCampInfoSuccess, null, i);

        this.updateCounter();
    },

    add: function (newId) {
        var atIndex = this.ids.length;
        var warnTextCtrl = document.getElementById("toomanyitems");
        var warning = document.getElementById(warnTextCtrl.value);
        if (this.ids.length === this.max) {
            //     alert(warning);
            alert(warnTextCtrl.value);
            return false;
        }

        if ($telerik.$.inArray(newId, this.ids) >= 0) return;
        this.ids[atIndex] = newId;

        // get camp info from ws and store it in the array
        Xperiensis.CampCz.Webservice.CampInfoWS.GetCampInfo(newId, this.langId, this.getCampInfoSuccess, null, atIndex);
        //this.infos[atIndex] = newId;
        this.updateCounter();
        this.storeCookie();
    },

    remove: function (id) {
        var j = 0;
        while (j < this.ids.length) {
            if (this.ids[j] == id) {
                this.ids.splice(j, 1);
                this.infos.splice(j, 1);
                break;
            } else { j++; }
        }

        this.storeCookie();
        this.updateCounter();
        updateComparison();
    },

    clear: function () {
        this.ids = new Array();
        this.infos = new Array();
        this.updateCounter();

        //Delete cookie
        $telerik.$.cookie("comparison_ids", null, { path: '/' });

        updateComparison();
    },

    updateCounter: function () {
        var counter = $telerik.$(".compare-info strong");
        var link = $telerik.$(".link_porovnat");
        if (counter.length > 0)
            counter.text(this.ids.length);
        if (link.length > 0) {
            if (this.ids.length > 1) {
                var query = new String();
                $telerik.$.each(this.ids, function () { if (query.length > 0) query += ","; query += this; });
                link.attr("href", this.url + '&ids=' + query);
//                link.removeAttr("disabled");
//                link.css("text-decoration", null);
                link.show();
            }
            else {
                link.hide();
                //                link.removeAttr("href");
                //                link.attr("disabled", "disabled");
                //                link.css("text-decoration", "none");
            }
        }
    },

    getCampInfoSuccess: function (result, options) {
        comparison.infos[options] = result;
        updateComparison();
    },

    storeCookie: function () {
        $telerik.$.cookie("comparison_ids", comparison.ids, { expires: this.cookieExpiration, path: '/' });
    }
};
