﻿Type.registerNamespace("Explorica");

Explorica.Calendar = function () {
    Explorica.Calendar.initializeBase(this);
    this._calDiv = null;
    this._cal = null;
    this._hlOther = false;
    this._days = [];
    this._months = [];
    this._dates = [];
    this._text = { "cl": "close", "sel": "Selected Date", "sch": "Scheduled Date", "gar": "Guaranteed Date", "sale": "On Sale Date" };
    this._tdMonth = null;
    this._tblDays = null;
    this._lgnd = null;
    this._curDate = new Date();
    this._vMonth = this._curDate.getMonth();
    this._vYear = this._curDate.getFullYear();
    this._min = null;
    this._max = null;
    this._cb = null;
    this._dlgOnCloseClick = null;
}

Explorica.Calendar.prototype = {
    initialize: function () { },
    init: function (days, months, text, dates) {
        if (!this.get_isInitialized()) {
            var $cr = function (t) { return document.createElement(t); }, $ct = function (t) { return document.createTextNode(t); }, tbl, tr, td, div, html;
            div = $cr("div");
            div.style.display = "none";
            div.style.position = "absolute";
            div.style.backgroundColor = "#ffffff";
            div.style.zIndex = 10;
            div.id = "mycal";
            this._days = days;
            this._months = months;
            if (text) this._text = text;
            if (dates) this._dates = dates;

            html = "<table cellpadding=\"0\" cellspacing=\"0\"><tr><td><table class=\"calendarNavigation\">" +
				"<tr><td class=\"a\">&lt;&lt;</td><td class=\"a rgt\">&lt;</td><td style=\"width:100%;text-align:center;\"></td>" +
				"<td class=\"a\">&gt;</td><td class=\"a\">&gt;&gt;</td></tr></table></td></tr>" +
				"<tr><td><table class=\"calendarBody\">";

            for (var i = 0; i < 7; i++) {
                html += "<tr>";
                for (var j = 0; j < 7; j++) {
                    html += "<td class=\"";
                    if (i == 0) {
                        html += "wkHd\">" + this._days[j].substr(0, 1);
                    } else {
                        if (j == 0 || j == 6) html += "wknd\">";
                        else html += "wkdy\">";
                    }
                    html += "</td>";
                }
                html += "</tr>";
            }

            html += "</table></td></tr><tr><td><table class=\"calendarFooter\"><tr><td><div class=\"footerLegend\">" +
				"<table class=\"vmid\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\">" +
				"<tr><td><img src=\"/images/cal_1.gif\" width=\"18px\" height=\"18px\" /></td><td>- " + this._text.sel + "</td></tr>" +
				"<tr><td><img src=\"/images/cal_4.gif\" width=\"18px\" height=\"18px\" /></td><td>- " + this._text.sch + "</td></tr>" +
				"<tr><td><img src=\"/images/cal_2.gif\" width=\"18px\" height=\"18px\" /></td><td>- " + this._text.gar + "</td></tr>" +
				"<tr><td><img src=\"/images/cal_3.gif\" width=\"18px\" height=\"18px\" /></td><td>- " + this._text.sale + "</td></tr>" +
				"</table></div></td></tr><tr><td class=\"a\" style=\"text-align:center;\">" + this._text.cl + "</td></tr></table>";
            div.innerHTML = html;
            html = div.childNodes[0].getElementsByTagName("table");
            tbl = html[0];
            tr = tbl.rows[0];
            tr.cells[0].onclick = Function.createDelegate(this, this.navPrevYear);
            tr.cells[1].onclick = Function.createDelegate(this, this.navPrevMonth);
            tr.cells[3].onclick = Function.createDelegate(this, this.navNextMonth);
            tr.cells[4].onclick = Function.createDelegate(this, this.navNextYear);
            this._tdMonth = tr.cells[2];
            this._tblDays = html[1];
            tbl = html[2];
            this._lgnd = tbl.rows[0];
            if (!this._hlOther) this._lgnd.style.display = "none";
            tr = tbl.rows[1];
            this._dlgOnCloseClick = Function.createDelegate(this, this._onCloseClicked);
            tr.cells[0].onclick = this._dlgOnCloseClick;
            html = this._tblDays.getElementsByTagName("td");
            for (var i = 0; i < html.length; i++)
                html[i].calobj = this;
            this._calDiv = div;
            document.body.appendChild(div);
            Explorica.Calendar.callBaseMethod(this, "initialize");
        }
    },
    _configCal: function () {
        var tbl = this._tblDays, tr, td, date = new Date(), first;
        date.setFullYear(this._vYear, this._vMonth, 1);
        date.setHours(0, 0, 0, 0);
        first = date.getDay();
        this._tdMonth.innerHTML = this._months[this._vMonth] + " " + this._vYear;
        if (this._lgnd) {
            if (this._hlOther) this._lgnd.style.display = "";
            else this._lgnd.style.display = "none";
        }

        for (var i = 0; i < tbl.rows.length - 1; i++) {
            tr = tbl.rows[i + 1];
            for (var j = 0; j < tr.cells.length; j++) {
                td = tr.cells[j];
                td.innerHTML = "";
                if ((i == 0 && first > j) || date.getMonth() != this._vMonth) {
                    td.onmouseover = td.onmouseout = td.onclick = null;
                    td.removeAttribute("date");
                    td.className = "";
                } else {
                    td.innerHTML = date.getDate();
                    if ((this._min == null || this._min <= date) && (this._max == null || this._max >= date)) {
                        td.onmouseover = Function.createDelegate(td, this._dOver);
                        td.onmouseout = Function.createDelegate(td, this._dOut);
                        td.onclick = Function.createDelegate(td, this._selDate);
                        td.className = "a";
                        td.setAttribute("date", String.format("{0}/{1}/{2}", date.getMonth() + 1, date.getDate(), date.getFullYear()));
                        this._hlDate(date, td);
                    } else {
                        td.onmouseover = td.onmouseout = td.onclick = null;
                        td.removeAttribute("date");
                        td.className = "";
                    }
                    date.setDate(date.getDate() + 1);
                }
            }
        }
    },
    _hlDate: function (d, td) {
        var c = "", dt = this._dates;
        if ((this._curDate - d) == 0) { c += " selectedDate "; }
        if (this._hlOther && dt) {
            for (var i = 0; i < dt.length; i++) {
                if ((dt[i][0] - d) == 0) {
                    if (dt[i][1] == true) c += " guaranteed";
                    else c += " offer";
                    if (dt[i][2] > 0) c += " discount";
                    break;
                }
            }
        }
        if (c != "") { td.className += c + "Date"; }
    },
    _dOver: function () { Sys.UI.DomElement.addCssClass(this, "on"); },
    _dOut: function () { Sys.UI.DomElement.removeCssClass(this, "on"); },
    _selDate: function () {
        var sDate = this.getAttribute("date"), date = new Date(sDate), obj = this.calobj, dts = obj._dates;
        obj.close();
        obj.set_date(date);
        if (obj._cb) {
            if (dts) {
                for (var i = 0; i < dts.length; i++) {
                    if ((dts[i][0] - date) == 0) {
                        var args = { g: dts[i][1], d: dts[i][2] };
                        obj._cb(sDate, args);
                        return;
                    }
                }
            }
            obj._cb(sDate);
        }
    },
    navPrevMonth: function () { this._vMonth--; if (this._vMonth < 0) { this._vMonth = 11; this._vYear--; } this._configCal(); },
    navNextMonth: function () { this._vMonth++; if (this._vMonth > 11) { this._vMonth = 0; this._vYear++; } this._configCal(); },
    navPrevYear: function () { this._vYear--; this._configCal(); },
    navNextYear: function () { this._vYear++; this._configCal(); },
    close: function () { this._calDiv.style.display = "none"; },
    _onCloseClicked: function () {
        this.close();
        var h = this.get_events().getHandler("OnClickCloseButton");
        if (h) {
            var eArgs = new Sys.EventArgs();
            h(this, eArgs);
        }
    },
    show: function (btn, callback) {
        if (typeof (btn) == "string") btn = $get(btn);
        var b = Sys.UI.DomElement.getBounds(btn), div = this._calDiv;
        this._configCal();
        div.style.left = b.x + "px";
        div.style.top = (b.y + b.height) + "px";
        div.style.display = "";
        this._cb = callback;
    },
    showAndAppend: function (left, top, holderElement, callback) {
        var div = this._calDiv;
        this._configCal();
        div.style.left = left + "px";
        div.style.top = top + "px";
        div.style.display = "";
        this._cb = callback;
        holderElement.appendChild(div);
    },
    get_isVisible: function () { return (this._calDiv.style.display != "none"); },
    get_highlightOtherDates: function () { return this._hlOther; },
    set_highlightOtherDates: function (v) { if (this._hlOther !== v) { this._hlOther = v; this.raisePropertyChanged("highlightOtherDates"); } },
    get_minDate: function () { return this._min; },
    set_minDate: function (d) { if (this._min !== d) { this._min = d; this.raisePropertyChanged("minDate"); } },
    get_maxDate: function () { return this._max; },
    set_maxDate: function (d) { if (this._max !== d) { this._max = d; this.raisePropertyChanged("maxDate"); } },
    get_date: function () { return this._curDate; },
    set_date: function (d) { if (this._curDate !== d) { this._curDate = d; this._vMonth = d.getMonth(); this._vYear = d.getFullYear(); this.raisePropertyChanged("date"); } },
    get_dates: function () { return this._dates; },
    set_dates: function (d) { if (this._dates !== d) { this._dates = d; this.raisePropertyChanged("dates"); } },
    add_onCloseButtonClick: function(handler){ this.get_events().addHandler("OnClickCloseButton", handler); }
}

Explorica.Calendar.registerClass("Explorica.Calendar", Sys.Component);

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
