﻿/// <reference path="pcal.js"/>
v.picker = {};
v.picker.format = function(src, param) {
    var rme = src;
    var Regex = new RegExp();
    var k = 0;
    for (k = 0; k < param.length; k++) {
        Regex.compile("\\{" + k + "\\}", "g");
        rme = rme.replace(Regex, param[k]);
    };
    return rme;
};
v.picker.position = function(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return { x: curleft, y: curtop };
};
v.picker.tb = {
    body: '<table id="{0}" cellspacing="0" cellpadding="0" class="{1}">{2}</table>',
    th: '<th id="{0}" class="{1}" {2}>{3}</th>',
    span: '<span id="{0}" class="{1}">{2}</span>',
    td: '<td id="{0}" class="{1}">{2}</td>',
    a: '<a id="{0}" href="javascript:{1}">{2}</a>',
    dayLink: '<a id="{0}" class="{1}" href="javascript:{2}">{3}</a>',
    css: {
        table: "picker-table",
        th: "picker-th",
        td: "picker-td",
        title: "picker-title",
        span: "picker-span",
        nav: "picker-nav",
        day: "picker-day",
        today: "picker-today",
        selected: "picker-selected",
        bottom: "picker-bottom"
    },
    txt: {
        py: '&lt;&lt;',
        ny: '&gt;&gt;',
        pm: '&lt;',
        nm: '&gt;',
        format: '%dddd %d %MMMM %yyyy',
        today:'Today:'
    }
};
function RenderEvent(day, month, year, lid, link, isToday, isSelected) {
    this.day = day;
    this.month = month;
    this.year = year;
    this.link = link;
    this.lid = lid;
    this.isToday = isToday;
    this.isSelected = isSelected;
};
v.picker.config = function(cnf) {
    this.c = {
        cal: cnf.cal || new v.cal(),
        firstday: cnf.firstday || 0,
        day: cnf.day || false
    };
    this.c.today = new v.date('', '', '', '', '', '', this.c.cal);
};
v.picker.daysIndex = [];
v.picker.render = function(target) {
    var i = 0;
    var content = "<thead><tr>";
    content += this.format(this.tb.th, [target + "_pyc", this.tb.css.th + " " + this.tb.css.nav, "", this.format(this.tb.a, [target + "_pyl", "v.picker.prevYear('" + target + "')", this.tb.txt.py])]);
    content += this.format(this.tb.th, [target + "_pmc", this.tb.css.th + " " + this.tb.css.nav, "", this.format(this.tb.a, [target + "_pml", "v.picker.prevMonth('" + target + "')", this.tb.txt.pm])]);
    content += this.format(this.tb.th, [target + "_titlec", this.tb.css.th + " " + this.tb.css.title, 'colspan="3"', this.format(this.tb.span, [target + "_title", "", ""])]);
    content += this.format(this.tb.th, [target + "_nmc", this.tb.css.th + " " + this.tb.css.nav, "", this.format(this.tb.a, [target + "_nml", "v.picker.nextMonth('" + target + "')", this.tb.txt.nm])]);
    content += this.format(this.tb.th, [target + "_nyc", this.tb.css.th + " " + this.tb.css.nav, "", this.format(this.tb.a, [target + "_nyl", "v.picker.nextYear('" + target + "')", this.tb.txt.ny])]);
    content += "</tr></thead><tbody><tr>";
    var j = v.picker.c.firstday;
    var tmp;
    for (i = 0; i < 7; i++) {
        tmp = (v.picker.c.day) ? v.picker.c.cal.strings.ldays[j] : v.picker.c.cal.strings.sdays[j];
        content += this.format(this.tb.td, [target + "_dayname_" + i, this.tb.css.td, tmp]);
        v.picker.daysIndex[i] = j;
        j++;
        if (j > 6)
            j = 0;
    };
    content += '</tr><tr id="' + target + '_row_1">';
    for (i = 1; i <= 42; i++) {
        content += this.format(this.tb.td, [target + "_day_" + i, this.tb.css.td, "", ""]);
        if ((i % 7) == 0) {
            content += "</tr>";
            if (i != 42)
                content += '<tr id="' + target + '_row_' + ((i / 7) + 1) + '">';
        };
    };
    content += "<tr>";
    content += this.format('<td id="{0}" class="{1}" colspan="7"></td>', [target + "_bottom_selected", this.tb.css.bottom]);
    content += "</tr><tr>";
    content += this.format('<td id="{0}" class="{1}" colspan="7"></td>', [target + "_bottom_today", this.tb.css.bottom, 'colspan="7"', ""]);
    content += "</tr></tbody>";
    content = this.format(this.tb.body, [target + "_tb", this.tb.css.table, content]);
    document.getElementById(target).innerHTML = content;
};
v.picker.isInit = false;
v.picker.init = function() {
    if (!this.isInit) {
        var divEl = document.createElement("DIV");
        var rnd = Math.round(Math.random() * 100);
        divEl.id = "picker_div_" + rnd;
        divEl.style.position = "absolute";
        divEl.style.display = 'none';
        this.id = divEl.id;
        document.body.appendChild(divEl);
        this.render(this.id);
        this.isInit = true;
    };
};
v.picker.detextDate = function(txt) {
    var rgx = /(\d{4})\/(\d{1,2})\/(\d{1,2})/;
    found = rgx.exec(txt);
    if (found) {
        day = eval(found[3]);
        month = eval(found[2]);
        year = eval(found[1]);
        return new v.date(year, month, day, 0, 0, 0, this.c.cal);
    } else {
        return new v.date('', '', '', '', '', '', this.c.cal);
    };
};
v.picker.$get = function(eid) {
    return document.getElementById(eid);
};
v.picker.renderCell = function(e) {
    var css = this.tb.css.day;
    if (e.isSelected) {
        css += " " + this.tb.css.selected;
    } else if (e.isToday) {
        css += " " + this.tb.css.today;
    };
    e.css = css;
    if (this.onRender) {
        return this.onRender(e);
    } else {
        return this.format(this.tb.dayLink, [e.lid, css, e.link, e.day]);
    };
};
v.picker.proccessDate = function(target) {
    var tg = this.$get(target);
    var cDate = tg.current;
    var sDate = tg.selected;
    this.$get(target + "_title").innerHTML = cDate.toString("%MMMM %yyyy");
    var doStart = false;
    var firstMonthDay = new v.date(cDate.getYear(), cDate.getMonth(), 1, 0, 0, 0, this.c.cal);
    var monthLength = cDate.getMonthLength();
    var proccess = 1;
    var tmp = "";
    var evt;
    var i = 0;
    for (i = 0; i < 42; i++) {
        if (this.daysIndex[i] == firstMonthDay.getDay()) {
            doStart = true;
        };
        tmp = this.$get(target + "_day_" + (i + 1));
        if (doStart) {
            evt = new RenderEvent(proccess, cDate.getMonth(), cDate.getYear(), target + "_lday_" + (i + 1), "v.picker.select('" + target + "'," + proccess + ")");
            evt.isSelected = ((sDate.getDate() == proccess) && (sDate.getMonth() == cDate.getMonth()) && (sDate.getYear() == cDate.getYear()));
            evt.isToday = ((this.c.today.getDate() == proccess) && (this.c.today.getMonth() == cDate.getMonth()) && (this.c.today.getYear() == cDate.getYear()));
            tmp.innerHTML = this.renderCell(evt);
            proccess++;
            doStart = (proccess <= monthLength);
        } else {
            tmp.innerHTML = "";
        };
    };
    proccess = 42 - i;
    proccess = parseInt(proccess / 7);
    for (i = 6; i > 0; i--) {
        if (i > (6 - proccess)) {
            this.$get(target + "_row_" + i).style.display = "none";
        } else {
            this.$get(target + "_row_" + i).style.display = "";
        };
    };
    tmp = this.$get(target + "_bottom_selected");
    tmp.innerHTML = this.format(this.tb.a, [target + "_bsl", "v.picker.gotoSelected('" + target + "')", sDate.toString(this.tb.txt.format)]);
    tmp = this.$get(target + "_bottom_today");
    tmp.innerHTML = this.format(this.tb.a, [target + "_btl", "v.picker.gotoToday('" + target + "')", this.tb.txt.today + this.c.today.toString(this.tb.txt.format)]);
};
v.picker.setDate = function(target) {
    var tg = this.$get(target);
    var fd = this.$get(tg.field);
    target = (tg.isFix) ? target : this.id;
    var st = fd.value;
    var cDate = v.picker.detextDate(st);

    var tg = this.$get(target);
    tg.selected = cDate;
    tg.current = cDate;
    v.picker.proccessDate(target);
};
v.picker.isShow = [false, ""];
v.picker.showPickder = function(el) {
    var showThisBox = (v.picker.isShow[0] && v.picker.isShow[1] == el.field);
    if (!this.isInit) {
        this.init();
    };
    var box = this.$get(this.id);
    box.field = el.field;
    if (!showThisBox) {
        var pos = this.position(this.$get(el.field));
        box.style.top = (el.offsetHeight + pos.y) + "px";
        box.style.left = pos.x + "px";
        box.style.display = "block";
        v.picker.isShow[0] = true;
        v.picker.isShow[1] = el.field;
        v.picker.setDate(el.id);
    } else {
        v.picker.isShow[0] = false;
        box.style.display = 'none';
    };
};
v.picker.attach = function(fieldId, triggerId) {
    var c = this.$get(triggerId);
    c.field = fieldId;
    c.isFix = false;
    c.onclick = function() {
        v.picker.showPickder(this);
    };
};
v.picker.show = function(fieldId, targetId) {
    var c = this.$get(targetId);
    c.field = fieldId;
    c.isFix = true;
    this.$get(fieldId).onchange = function() {
        v.picker.setDate(targetId);
    };
    v.picker.render(targetId);
    v.picker.setDate(targetId);
};
v.picker.prevYear = function(tid) {
    var tg = this.$get(tid);
    var ny = tg.current.getYear() - 1;
    tg.current.setFullYear(ny,'','');
    v.picker.proccessDate(tid);
};
v.picker.nextYear = function(tid) {
    var tg = this.$get(tid);
    var ny = tg.current.getYear() + 1;
    tg.current.setFullYear(ny,'','');
    v.picker.proccessDate(tid);
};
v.picker.prevMonth = function(tid) {
    var tg = this.$get(tid);
    var pm = tg.current.getMonth() - 1;
    var y = tg.current.getYear();
    if (pm == 0) {
        pm = 12;
        y--;
    };
    tg.current.setFullYear(y, pm, '');
    v.picker.proccessDate(tid);
};
v.picker.nextMonth = function(tid) {
    var tg = this.$get(tid);
    var pm = tg.current.getMonth() + 1;
    var y = tg.current.getYear();
    if (pm > 12) {
        pm = 1;
        y++;
    };
    tg.current.setFullYear(y, pm, '');
    v.picker.proccessDate(tid);
};
v.picker.gotoToday = function(tid) {
    var tg = this.$get(tid);
    tg.current = new v.date('', '', '', '', '', '', this.c.cal);
    v.picker.proccessDate(tid);
};
v.picker.gotoSelected = function(tid) {
    var tg = this.$get(tid);
    tg.current = tg.selected;
    v.picker.proccessDate(tid);
};
v.picker.select = function(tid, day) {
    var tg = this.$get(tid);
    tg.selected = new v.date(tg.current.getYear(), tg.current.getMonth(), day, '', '', '', this.c.cal);
    this.$get(tg.field).value = tg.selected.toString("%yyyy/%M/%d");
    v.picker.proccessDate(tid);
    if (!tg.isFix) {
        tg.style.display = 'none';
        this.isShow[0] = false;
    };
};
