﻿
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        var F = function () { };
        F.prototype = o;
        return new F();
    };
}

Function.prototype.method = function (name, func) {
    if (!this.prototype[name]) {
        this.prototype[name] = func;
        return this;
    }
};

String.method('trim', function () {
    return this.replace(/^\s|\s+$/g, '');
});

Number.method('integer', function () {
    return Math[this < 0 ? 'ceil' : 'floor'](this);
});

/* -------------------------- Array Filter prototype extention
http://www.tutorialspoint.com/javascript/array_filter.htm  */
if (!Array.prototype.filter) {
    Array.prototype.filter = function (fun , thisp) {
        var len = this.length;
        if (typeof fun != "function")
            throw new TypeError();

        var res = new Array();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, this))
                    res.push(val);
            }
        }

        return res;
    };
}

function getSetting(settingsObject, id) {
    for (var i = 0; i < settingsObject["setting"].length; i++) {
        if (settingsObject["setting"][i].id == id) {
            return settingsObject["setting"][i];
        }
    }
}

function GetRowClass() {
    rowId++;
    var rowClass = rowId % 2 == 0 ? "even" : "odd";
    if (rowId == 1) { rowClass += " selected"; }
    return rowClass;
}

var rowid;

function programFilter() {
    this.htmlContainerId = "#filterContainer";
    this.templateSection = "template-section";
    this.tempalteProgram = "template-program";
    this.source = {
        programs: {},
        careerLevelList: {},
        contentLevelList: {},
        durationList: {},
        topicList: {}
    };
    this.sort = "";
    this.filter = "";
    this.filterSection = "";
    this.currentSection = {};
    this.orderBy = "";
    this.careerLevel = "";
    this.contentLevel = "";
    this.topic = "";
    this.duration = "";
    this.html = "";
}

if (!programFilter.prototype.update) {
    programFilter.prototype.update = function () {

        console.log("HTML container: " + this.htmlContainerId);

        // FILL TABLE
        var filteredPrograms = this.source.programs["programs"];

        // PERFORM FILTER
        filteredPrograms = filteredPrograms.filter(this.programFilterFunction, this);
        $(this.htmlContainerId + " .filter-result-count").html(filteredPrograms.length);

        console.info("filter on (career-content-topic-duration) : " + this.careerLevel + " - " + this.contentLevel + " - " + this.topic + " - " + this.duration);
        console.info("Sections by: " + this.filterSection);
        console.info("Order by:" + this.orderBy);
        console.info("Count: " + filteredPrograms.length);

        // Clear list
        $(this.htmlContainerId + " .filter-result-table tbody").children().remove();

        // RENDER HTML
        rowId = 0;

        // loop all items of the selected section
        var previousSection = "";
        for (var i = 0; i < this.source[this.filterSection]["setting"].length; i++) {
            this.currentSection = this.source[this.filterSection]["setting"][i];

            // * take sub filter
            var temp = filteredPrograms.filter(this.sectionFilterFunction, this);
            if (temp.length == 0) {
                continue;
            }
            $(this.templateSection).tmpl(this.currentSection).appendTo(this.htmlContainerId + " .filter-result-table tbody");

            // * sort temp result -- sort not yet implemented WARNING gives problems in IE8
            //temp.sort(this.programSorterFunction);

            // * print out temp result
            $(this.templateProgram).tmpl(temp).appendTo(this.htmlContainerId + " .filter-result-table tbody");

        }
    };
}

if (!programFilter.prototype.sectionFilterFunction) {
    programFilter.prototype.sectionFilterFunction = function (element, index, array) {
        if (this.filterSection !== undefined && this.filterSection.toString() !== "") {
            if (element[this.filterSection].indexOf(this.currentSection.id) != -1) {
                return true;
            }
        }
    };
}

if (!programFilter.prototype.programFilterFunction) {
    programFilter.prototype.programFilterFunction = function programFilterFunction(element, index, array) {
        var retVal = true;

        if (this.careerLevel != "" && retVal === true) {
            if (element.careerLevel.indexOf(this.careerLevel) === -1) {
                retVal = false;
            }
        }

        if (this.contentLevel != "" && retVal === true) {
            if (element.contentLevel.indexOf(this.contentLevel) === -1) {
                retVal = false;
            }
        }

        if (this.topic != "" && retVal == true && element.topic !== undefined) {
            if (element.topic.indexOf(this.topic) === -1) {
                retVal = false;
            }
        }

        if (this.duration != "" && retVal == true && element.duration !== undefined) {
            if (element.duration.indexOf(this.duration) === -1) {
                retVal = false;
            }
        }

        return retVal;
    };
}

if (!programFilter.prototype.programSorterFunction) {
    programFilter.prototype.programSorterFunction = function programSorterFunction(a, b) {
        switch (this.orderBy) {
            case "program":
                console.warn("sort not verified");
                return a < b;
                break;
            case "startdate":
                console.warn("sort not supported");
                break;
            case "language":
                console.warn("sort not supported");
                break;
            case "duration":
                console.warn("sort not supported");
                break;
            default:
                break;
        }
    };
}

    

