﻿/// <reference path="modal.js"/>
/// <reference path="dataset.js"/>
/// <reference path="jquery-1.2.4.pack.js"/>
/// <reference path="ListItems.js"/>
var ds = new dataset("comment_list", false);
var c = {};
c.newsid = 0;
var websrv = "websrv/comments.ashx";
var vars = { name: '', email: '', site: '', text: '', code: '' };
var win_loading = new modal("win_loading", "درحال دریافت", "", "", "loading", 300, false);
var win_insert = new modal("win_insert", "اضافه کردن نظر", "Insert", "Cancel", "insert", 300, false);
var win_picture = new modal("win_picture", "تصاویر", "", "", "list_holder", 520, false);
win_picture.onclose = function(e) {
    return true;
};
win_insert.onclose = function(e) {
    if (e) {
        var d = document;
        vars.name = d.getElementById("name").value;
        vars.email = d.getElementById("email").value;
        vars.text = d.getElementById("txt").value;
        vars.site = d.getElementById("site").value;
        vars.code = d.getElementById("code").value;
        var valid = ((vars.name.length > 0) && (vars.email.length > 0) && (vars.text.length > 0));
        if (valid) {
            var myregexp = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if (vars.email.match(myregexp)) {
                myregexp = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/;
                if (vars.site.length > 0) {
                    if (!vars.site.match(myregexp)) {
                        alert("Error\r\nآدرس سایت اشتباه است");
                        return false;
                    }
                };
                $.ajax({
                    url: websrv,
                    type: "POST",
                    data: { mode: "insert", id: c.newsid, name: vars.name, email: vars.email, site: vars.site, code: vars.code, text: vars.text },
                    dataType: "json",
                    cache: false,
                    success: function(data) {
                        win_loading.onclose = function(e) {
                            return true;
                        };
                        win_loading.remove();
                        if (data.status == "insert") {
                            win_alert.show();
                        } else {
                            alert(data.id);
                        };
                    }
                });
                win_loading.onclose = function(e) {
                    return false;
                };
                window.setTimeout("win_loading.show()", 10);
                return true;
            } else {
                alert("Error\r\nایمیل اشتباه است");
            }
        } else {
            alert("Error:\r\nلطفا فیلدهای ستاره دار را پر کنید");
        }
        return false;
    }
    return true;
};
win_insert.onshow = function() {
    var d = document;
    d.getElementById("name").value = vars.name;
    d.getElementById("email").value = vars.email;
    d.getElementById("site").value = vars.site;
    d.getElementById("code_holder").innerHTML = vars.code;
};
var win_alert = new modal("win_alert", "نظر", "Ok", "", "result", 300, false);
win_alert.onclose = function(e) {
    return true;
};
c.getComments = function() {
    ds.startLoading();
    document.getElementById("comment_list").style.display = "block";
    $.ajax({
        url: websrv,
        cache: false,
        data: { mode: "list", id: c.newsid },
        dataType: "json",
        success: function(data) {
            ds.endLoading();
            ds.setData(data, true);
        },
        type: "POST"
    });
};
c.insertNew = function() {
    win_loading.onclose = function(e) {
        return false;
    };
    win_loading.show();
    $.ajax({
        url: websrv,
        dataType: "json",
        cache: false,
        success: function(data) {
            data = data[0];
            win_loading.onclose = function(e) {
                return true;
            };
            win_loading.remove();
            var op = ["", "+", "-", "x"];
            vars.code = data.id[1] + " " + op[eval(data.id[2])] + " " + data.id[0];
            vars.email = (data.email) ? data.email : "";
            vars.site = (data.site) ? data.site : "";
            vars.text = "";
            vars.name = (data.name) ? data.name : "";
            win_insert.show();
        }
    });
};
var g = {};
g.current = 0;
g.list = new generic.list();
g.setlist = function(ids) {
    g.list.addRange(ids.split(/,/));
};
g.next = function() {
    var index = g.list.find(g.current);
    if (index < (g.list.count() - 1)) {
        index++;
        g.current = g.list.getItem(index);
        g.picture(g.current);
    };
};
g.prev = function() {
    var index = g.list.find(g.current);
    if (index > 0) {
        index--;
        g.current = g.list.getItem(index);
        g.picture(g.current);
    };
};
g.picture = function(id) {
    g.current = id;
    var index = g.list.find(id);
    if (index != -1) {
        win_picture.show();
        var hasNext = (index < (g.list.count() - 1));
        var hasPrev = (index > 0);
        document.getElementById("list_next").style.display = (hasNext) ? "inline" : "none";
        document.getElementById("list_prev").style.display = (hasPrev) ? "inline" : "none";
        var imgb = document.getElementById("list_img");
        imgb.src = "style/load.gif";
        imgb.style.width = "16px";
        imgb.style.height = "16px";
        imgb.alt = "loading";
        $.ajax({
            url: websrv,
            data: { mode: "pic", id: id },
            dataType: "json",
            cache: false,
            type: "POST",
            success: function(data) {
                if (data.status != 'error') {
                    imgb.src = "getthumb.ashx?q=85&w=500&h=300&url=" + encodeURI(data.url);
                    imgb.style.width = "500px";
                    imgb.style.height = "300px";
                    imgb.alt = data.alt;
                    document.getElementById("list_text").innerHTML = data.name + ":&nbsp;" + data.desc + "<br />[دریافت نسخه اصلی عکس]";
                    document.getElementById("list_text").href = data.url;
                } else {
                    alert("Image not found");
                };
            }
        });
    } else {
        alert("Image not found");
    }
};