var comments = new Array();
suinfo_comments = function(dir_, category_id_, event_id_, image_, on_, table_) {
    this.dir = dir_;
    this.category_id = category_id_;
    this.event_id = event_id_;
    this.image = image_;
    this.on = on_;
    this.table = table_;
    this.comment_request = null;
    this.obj_id = this.table == 'press' ? this.image : '0';
    if (this.table == 'press') {
        this.comment_refresh();
    }
}
suinfo_comments.prototype.comment_on_off = function() {
    this.on =  1-this.on;
    SetCookie('www_comments', this.on, 3650);
    this.comment_refresh_on();
}
suinfo_comments.prototype.comment_refresh_on = function() {
    var don = this.on ? 'inline' : 'none';
    var doff = this.on ? 'none' : 'inline';
    var h;
    if (h = find_handle('comment_list'+this.obj_id)) {
        h.display= don;
    }
    if (h=find_handle('comment_on'+this.obj_id)) {
        h.display = don;
    }
    if (h=find_handle('comment_off'+this.obj_id)) {
        h.display = doff;
    }
}
suinfo_comments.prototype.comment_add_event = function(id) {
    winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=770,height=550";
    ShowFrame = window.open("comment_add.php?table=event&event_id="+id, 'pwi', winopts);
}

suinfo_comments.prototype.comment_add = function(image_id) {
    var winopts = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=770,height=501';
    var url;
    if (image_id == undefined) {
        url = 'comment_add.php?dir='+this.dir+'&event_id='+this.event_id+'&image='+this.image+'&table='+this.table;
    } else {
        url = 'comment_add.php?image_id='+this.image_id;
    }
    ShowFrame = window.open(url, 'pwi', winopts);

}
suinfo_comments.prototype.x_comment_add = function() {
    var request;
    var formname = 'comment_add'+this.obj_id;
    var comment = encodeURIComponent(document.forms[formname].comment.value);
    var request = 'comment_add.php?x=1&dir='+this.dir+'&event_id='+this.event_id+'&image='+this.image+'&table='+this.table+'&comment='+comment;
    var s = this;
    this.comment_request = new xmlhttp_request(request, function() {s.comment_response();});
}
suinfo_comments.prototype.comment_friend = function(image_id) {
    var winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=675,height=401";
    var url;
    if (image_id == undefined) {
        url = 'comment_friend.php?dir='+this.dir+'&event_id='+this.event_id+'&image='+this.image+'&table='+this.table;
    } else {
        url = 'comment_friend.php?image_id='+this.image_id;
    }
    ShowFrame = window.open(url, 'pwi', winopts);
}
suinfo_comments.prototype.comment_added = function() {
    opener.location.reload();
}
suinfo_comments.prototype.comment_added_event = function() {
    this.location.reload();
}
suinfo_comments.prototype.comment_friend_event = function(id) {
    winopts = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=675,height=401";
    ShowFrame = window.open("comment_friend.php?table=event&event_id="+id, 'pwi', winopts);
}
suinfo_comments.prototype.comment_refresh = function(t) {
    if (t != undefined) this.table = t;
    if (this.event_id != undefined) {
        var request = 'x_comment.php?dir='+this.dir+'&event_id='+this.event_id+'&image='+this.image+'&on='+this.on+'&table='+this.table;    
        var s = this;
        this.comment_request = new xmlhttp_request(request, function() {s.comment_response();});
    }
}
var delim = '_::_';
suinfo_comments.prototype.comment_response = function() {
    var cresp;
    var resp = '';
    try {
        resp = this.comment_request.response;
    } catch (e) {}
    try {
        cresp = decodeURIComponent(resp);
    } catch (e) {
        cresp = unescape(resp);
    }
    var o;
    var aresp = cresp.split(delim);
    if ((aresp[0] != undefined) && (aresp[0] != 'undefined') && (o = find_object('comment_block'+this.obj_id))) {
        o.innerHTML = aresp[0]; 
    }
    this.comment_refresh_on();
    FB.XFBML.Host.parseDomTree(); 
    if (aresp.length > 1) {
    alert(aresp[1]);
        eval(aresp[1]);
    }
}
suinfo_comments.prototype.comment_track = function(on) {
    var s = this;
    var conf = on ? 'Bi\u0107ete obave\u0161teni mailom ako neko doda komentar na ovaj izve\u0161taj. Nastavite dalje?' : '\u017delite da prekinete pra\u0107enje komentara?';
    if (confirm(conf)) {
        var request = 'comment_add.php?action=track&event_id='+this.event_id+'&image='+this.image+'&table='+this.table + '&on='+on;    
        this.comment_request = new xmlhttp_request(request, function() {s.comment_track_response(on);});
    }
}
suinfo_comments.prototype.comment_track_response = function(on) {
    var cresp;
    var resp = '';
    try {
        resp = this.comment_request.response;
    } catch (e) {}
    try {
        cresp = decodeURIComponent(resp);
    } catch (e) {
        cresp = unescape(resp);
    }
    if (cresp == 'ok') {
        var o = find_object('comment_track'+this.obj_id+'_'+on); 
        if (o) {
            o.style.display = 'inline';
        }
        if (o = find_object('comment_track'+this.obj_id+'_'+(1-parseInt(on)))) {
            o.style.display = 'none';
        }
        if (on) {
            alert('Uspe\u0161no ste se prijavili na pra\u0107enje komentara');
        } else {
            alert('Uspe\u0161no ste odjavili pra\u0107enje komentara');
        }
    } else {
        alert(cresp);
    }
}
