﻿
function AddElement(parent, tag, text, id, css) {
    var elem = document.createElement(tag);
    if (text) elem.innerHTML = text;
    parent.appendChild(elem);
    if (id) elem.setAttribute("id", id);
    if (css) elem.className = css;
    return elem;
}

var flag = false;
var select = {};
function initEventLine(num) {
    var blk = document.getElementById("View");
    blk.innerHTML = "";
    var line = document.getElementById("EventList");
    var linediv = line.getElementsByTagName("div");
    var el;
    for (var i = 0; i < EventList.length; i++) {
        el = EventList[i];
        if (i + 1 == num) break;
    }
    blk.innerHTML = "<img src='/admin/getFile.ashx?id=" + el.PhotoID + "' alt='' />";
    //var img = AddElement(blk,"img");
    //img.setAttribute("src", ("/admin/getFile.ashx?id=" + el.PhotoID));
    AddElement(blk, "div", "<a href='/" + el.DocHID + "/default.aspx'>" + el.Title + "</a><p>" + el.Spot + "</p><span>" + el.Date + "</span>");
    if (!flag) {
        for (var i = 0; i < linediv.length; i++) {
            var el = linediv[i];
            el.ind = i + 1;
            if (num == i + 1) {
                el.className = "A";
                select = el;
            }
            el.onmousedown = function() {
                initEventLine(this.ind);
                if (this.className != "A") {
                    this.className = "A";
                    select.className = "";
                    select = this;
                }
            }
        }
        flag = true;
    }
}

var items = [];
function loadData() {
    var data = this.req.responseXML;
    if (data.documentElement == null) return;
    items = DataBind(data.documentElement);
    update_list(0, "");
}
function update_list(coid, ciid) {
    if (ciid) searchcity(ciid);
    else if (coid != 0 && ciid == 0) searchcountry(coid);
    else if (coid == 0 && !ciid) getall();
}
function searchcity(ciid) {
    var container = document.getElementById("List");
    container.innerHTML = "";
    for (var i = 0; i < items.length; i++) {
        var city = items[i];
//        var flag = true;
//        var childs = city.child;
//        for (var j = 0; j < childs.length; j++) {
//            var city = childs[j];
            if (city.id == ciid) {
//                if (flag) {
//                    showInfoCountry(country, container);
//                    flag = false;
//                }
                showInfoCity(city, container);
            }
        //}
        //if (!flag) break;
    }
}
function searchcountry(coid) {
    var container = document.getElementById("List");
    container.innerHTML = "";
    for (var i = 0; i < items.length; i++) {
        var country = items[i];
        if (country.id == coid) {
            showInfoCountry(country, container);
            var childs = country.child;
            for (var j = 0; j < childs.length; j++) {
                var city = childs[j];
                showInfoCity(city, container);
            }
        }
    }
}
function getall() {
    var container = document.getElementById("List");
    container.innerHTML = "";
    for (var i = 0; i < items.length; i++) {
        var city = items[i];
        //showInfoCountry(country, container);
        //var childs = country.child;
        //for (var j = 0; j < childs.length; j++) {
        //    var city = childs[j];
            showInfoCity(city, container);
        //}
    }
}
function showInfoCountry(country, container) {
    var wco = document.createElement("div");
    container.appendChild(wco);
    wco.innerHTML = country.name;
}
function showInfoCity(city, container) {
    var wci = document.createElement("div");
    wci.innerHTML = ("<h2 class='CityName'>" + city.id + "</h2>");
    container.appendChild(wci);
    BindList(city.child, wci);
}
function BindList(arr, cont) {
    for (var i = 0; i < arr.length; i++) {
        var el = arr[i];
        cont.innerHTML += ("<table style='margin-bottom:20px;font-size:12px;color:#666;'><tr><td style='width:100px;'><b>" + el.title + "</b></td><td style='width:200px;'>" + el.address + "</td><td style='width:100px;'>" + el.phone + "</td><td>" + el.face + "</td></tr></table>");
    }
}
function DataBind(item) {
    var result = [];
    if (item == null && item == undefined) return null;
    var field = item.firstChild;
    for (field; field != null; field = field.nextSibling) {
        var output = {};
        if (field.nodeType == 1) {
            output.tag = field.nodeName;
            if (field.nodeName == "city") {
                output.id = field.getAttribute("name");
                //output.name = field.getAttribute("name");
                output.child = DataBind(field);
            }
            else {
                output.address = field.getElementsByTagName("address")[0].firstChild.nodeValue;
                output.title = field.getElementsByTagName("title")[0].firstChild.nodeValue;
                output.face = field.getElementsByTagName("face")[0].firstChild.nodeValue;
                output.phone = field.getElementsByTagName("phone")[0].firstChild.nodeValue;
            }
            result.push(output);
        }
    }
    return result;
}
