﻿
function TurnPhone(phones, howManyColume, phonePreFix, ContainerID) {
    this.phones = phones;
    this.howManyColume = howManyColume;
    this.phonePreFix = phonePreFix;
    this.ContainerID = ContainerID;
    this.setPhones();
}

TurnPhone.prototype.setPhones = function () {
    var start = this.getStartIndex();
    var container = document.getElementById(this.ContainerID);

    var phoneIndex = start;
    var innerText = "";
    var phoneCount = 0;
    for (var i = 0; i < this.phones.length; i++) {
        if (phoneIndex >= this.phones.length) {
            phoneIndex = 0;
        }
        innerText += this.phones[phoneIndex] + "/";
        phoneCount++;
        phoneIndex++;

        if (i < this.howManyColume) {
            if (phoneCount == this.howManyColume - 1 || i == this.phones.length - 1) {
                if (innerText != "") {
                    innerText = innerText.substring(0, innerText.length - 1);
                }
                var p = this.createP(this.phonePreFix + innerText, "");
                var childLength = container.childNodes.length;



                if (childLength > 0) {
                    container.insertBefore(p, container.childNodes[container.childNodes.length - 1]);
                }
                else {
                    container.appendChild(p);
                }

                innerText = "";
                phoneCount = 0;
            }
        }
        else {
            if (phoneCount == this.howManyColume || i == this.phones.length - 1) {
                if (innerText != "") {
                    innerText = innerText.substring(0, innerText.length - 1);
                }
                var p = this.createP(innerText, "texin");
                var childLength = container.childNodes.length;
                if (childLength > 0) {
                    container.insertBefore(p, container.childNodes[container.childNodes.length - 1]);
                }
                else {
                    container.appendChild(p);
                }
                innerText = "";
                phoneCount = 0;
            }
        }

    }


}



TurnPhone.prototype.createP=function(innerText, className) {
    var p = document.createElement("p");
    p.className = className;
    p.innerHTML = innerText;
    return p;
}

TurnPhone.prototype.getStartIndex = function() {
    var today = new Date();
    var birthTime = new Date();

    var y = "1900";
    var m = "1";
    var d = "1";

    birthTime.setFullYear(y, m - 1, d)
    var bms = Date.parse(birthTime.toString())
    var tms = Date.parse(today.toString())
    var distance = tms - bms
    dis_d = distance / (1000 * 60 * 60 * 24)
    dis_d = parseInt(dis_d);

    var start = 0;
    if (this.phones.length > 0) {
        start = dis_d % this.phones.length;
    }

    return start;
}



