﻿

/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace("EduDesk");

EduDesk.SubjectSelectorTest = function(element) {
EduDesk.SubjectSelectorTest.initializeBase(this, [element]);
}


EduDesk.SubjectSelectorTest.prototype = {
    addSubject: function(name, value, sub) {
        sub = sub || $("#" + this.get_cmbsubject());
        sub.append($('<option></option>').val(value).text(name));
    },

    onsubjectsLoaded: function(e) {
        var sub = $("#" + this.get_cmbsubject());
        var newSub = $("<select></select>").attr("id", sub.attr("id")).attr("data-post", sub.attr("data-post")).attr("name", sub.attr("name")).css("width", "100%");
        this.addSubject("--- Select a Subject ---", e[0].Second, newSub);
        for (var i = 0; (i < e.length); i++) {
            this.addSubject(e[i].First, e[i].Second, newSub);
        }
        sub.replaceWith(newSub);
        this.attachSubjectEvent();

        if (this._dynamicSelectSubject != -1) {
            newSub.val(this._dynamicSelectSubject).change(); ;
            this._dynamicSelectSubject = -1;
        }
    },

    attachSubjectEvent: function() {
        var self = this;
        $("#" + this.get_cmbsubject()).change(function(e) {
            //if ($("#" + self.get_cmbsubject()).val() != "-1") {
            if (self.lastSubject != self.get_subjectid()) {
                self.lastSubject = self.get_subjectid();
                if (self._autopostback) eval($("#" + self.get_cmbsubject()).attr("data-post"));

                var handler = self.get_events().getHandler("subjectchanged");
                if (handler) {
                    handler(self, self.lastSubject);
                }

            }
        });
    },

    initialize: function() {
        EduDesk.SubjectSelectorTest.callBaseMethod(this, 'initialize');

        // Add custom initialization here
        //$(document).ready(function() {
        var self = this;

        var host = $("[id$='Master_CurrentSiteID']").val();
        var siteid = -1;
        if (host.toLowerCase().indexOf('wizardtm') != -1) {
            siteid = 0;
        }
        else if (host.toLowerCase().indexOf('testwizard') != -1) {
            siteid = 2;
        }
        else if (host.toLowerCase().indexOf('clickerschool') != -1) {
            siteid = 3;
        }
        else if (host.toLowerCase().indexOf('districtbenchmark') != -1) {
            siteid = 1;
        }
        
        if (this.get_cmbsubjectarea() != "null" && this.get_schoolcode()) {
            $("#" + this.get_cmbsubjectarea()).change(function(e) {
                //alert($("#" + self.get_cmbsubjectarea() + " :selected").val());
                $("#" + self.get_cmbsubject() + " >option").remove();
                self.addSubject("--- Loading Subjects ---", -1);
                edu.pageMethod("GetSchoolSubjects",
                                       { "subjectArea": $("#" + self.get_cmbsubjectarea() + " :selected").val(), filter: self.get_filter(), school: self.get_school(), site: siteid },
                                       $del(self, self.onsubjectsLoaded), null, "/Controls/SubjectSelection/SubjectsPage.aspx");
            });

        }
        else if (this.get_cmbsubjectarea() != "null") {
            $("#" + this.get_cmbsubjectarea()).change(function(e) {
                //alert($("#" + self.get_cmbsubjectarea() + " :selected").val());
                $("#" + self.get_cmbsubject() + " >option").remove();
                self.addSubject("--- Loading Subjects ---", -1);
                edu.pageMethod("GetSubjects2",
                                       { "subjectArea": $("#" + self.get_cmbsubjectarea() + " :selected").val(), filter: self.get_filter(), loadall: self.get_loadall(), site: siteid },
                                       $del(self, self.onsubjectsLoaded), null, "/Controls/SubjectSelection/SubjectsPage.aspx");
            });
        }

        this._postback = $("#" + this.get_cmbsubject()).attr("onchange");

        this.lastSubject = this.get_subjectid();
        this.attachSubjectEvent();
        ///});
    },
    dispose: function() {
        //Add custom dispose actions here
        EduDesk.SubjectSelectorTest.callBaseMethod(this, 'dispose');
    },

    _cmbsubject: null,
    _cmbsubjectarea: null,
    _requestareaID: null,
    _autopostback: false,
    _filter: true,
    _site: -1,
    _loadall: false,
    _schoolcode: false,
    _school: -1,
    _postback: "",
    lastSubject: -1,
    _dynamicSelectSubject: -1,

    get_cmbsubject: function() {
        return this._cmbsubject;
    },

    set_cmbsubject: function(id) {
        this._cmbsubject = id;
    },

    get_cmbsubjectarea: function() {
        return this._cmbsubjectarea;
    },

    set_cmbsubjectarea: function(id) {
        this._cmbsubjectarea = id;
    },

    get_filter: function() {
        return this._filter;
    },

    set_filter: function(id) {
        this._filter = id;
    },

    get_site: function() {
        return this._site;
    },

    set_site: function(id) {
        this._site = id;
    },

    get_loadall: function() {
        return this._loadall;
    },

    set_loadall: function(id) {
        this._loadall = id;
    },

    get_schoolcode: function() {
        return this._schoolcode;
    },


    set_schoolcode: function(id) {
        this._schoolcode = id;
    },

    get_school: function() {
        return this._school;
    },

    set_school: function(id) {
        this._school = id;
    },

    get_subjectid: function() {
        return parseInt($("#" + this.get_cmbsubject() + " :selected").val());
    },

    get_subjectareaid: function() {
        return parseInt($("#" + this.get_cmbsubjectarea() + " :selected").val());
    },

    set_subject: function(subID, subAreaID) {
        if (subID == this.get_subjectid() && this.get_subjectareaid() == subAreaID)
            return;

        if (subAreaID != -1) {
            if (this.get_subjectareaid() == subAreaID) {
                $("#" + this.get_cmbsubject()).val(subID).change();
            }
            else {
                $("#" + this.get_cmbsubjectarea()).val(subAreaID).change();
                this._dynamicSelectSubject = subID;
            }
        }
        else { $("#" + this.get_cmbsubject()).val(subID).change(); }
    },

    get_subjectname: function() {
        return $("#" + this.get_cmbsubject() + " :selected").text();
    },

    get_autopostback: function() {
        return this._autopostback;
    },

    add_subjectchanged: function(handler) {
        this.get_events().addHandler("subjectchanged", handler);
    },

    remove_subjectchanged: function(handler) {
        this.get_events().removeHandler("subjectchanged", handler);
    },

    set_autopostback: function(v) {
        this._autopostback = v;
    }
}
EduDesk.SubjectSelectorTest.registerClass('EduDesk.SubjectSelectorTest', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


function $del(obj, fn)
{
    return function() 
    {   fn.apply(obj, arguments);
    };
}


