var comments_global;

function Comments(parent_,type_) {
	this.parent;
	this.type;
	this.id;
	
	var xmlHttp;
	this.commentsXML;

	this.construct(parent_, type_);
	
	return (this);
}

Comments.prototype.construct = function (parent_, type_) { this.parent = parent_; this.type = type_; comments_global = this;}

Comments.prototype.xml_update = function () {
	var this_ = this;
	
	this.xmlHttp = GetXmlHttpObject();
	var url = urlreceivecomm+"?id="+this.id;

	this.xmlHttp.open("GET",url,true);
	this.xmlHttp.onreadystatechange = function () { this_.xml_updated(); };
	this.xmlHttp.send(null);
}

Comments.prototype.xml_updated = function () {
	if (this.xmlHttp.readyState == 4) {
		var xmlDoc = this.xmlHttp.responseXML;	
		
		this.commentsXML = xmlDoc.getElementsByTagName('comment');
		this.assign_comments();
	}
}

Comments.prototype.show_comments = function (updated) {	
	if (updated) {
		this.assign_comments();
	}
	else {
		this.xml_update();
	}
}

Comments.prototype.assign_comments = function () {
	var comment_holder = document.createElement('div');

	if (this.commentsXML) {
		for (x=0;x<this.commentsXML.length;x++) {
			var submitterId = this.commentsXML[x].getElementsByTagName('submitterId');
			submitterId = this.checkValueForEmpty(submitterId);
			var submitterName = this.commentsXML[x].getElementsByTagName('submitterName');
			submitterName = this.checkValueForEmpty(submitterName);
			var submitterCity = this.commentsXML[x].getElementsByTagName('submitterCity');
			submitterCity = this.checkValueForEmpty(submitterCity);
			var submitterProvince = this.commentsXML[x].getElementsByTagName('submitterProvince');
			submitterProvince = this.checkValueForEmpty(submitterProvince);
			var submitDate = this.commentsXML[x].getElementsByTagName('submitDate');
			submitDate = this.checkValueForEmpty(submitDate);
			var submitTime = this.commentsXML[x].getElementsByTagName('submitTime');
			submitTime = this.checkValueForEmpty(submitTime);
			var ttxt = this.commentsXML[x].getElementsByTagName('text');
			ttxt = this.checkValueForEmpty(ttxt);
			var videoId = this.commentsXML[x].getElementsByTagName('videoId');
			videoId = this.checkValueForEmpty(videoId);
			comment_holder.appendChild(this.comment_construct(submitterId,submitterName,submitterCity,submitterProvince,submitDate,submitTime,ttxt,videoId));
		}
	}
	else {
		comment_holder.innerHTML = '';
	}
	replaceChildren(this.parent,comment_holder);

	if (document.forms['comment_form']) {
		var f = document.forms['comment_form'];
		f.reset();
	}
}

Comments.prototype.comment_construct = function (smtid,smtname,citycomment,provcomment,datecomment,timecomment,txtcomment,videoId){
	var div = document.createElement('div');
	div.id = 'sp'+smtid;
	div.className = 'comment_item';

	var objinnerhtml;
	if (videoId != '') {
		var moviewidth = 256;
		var movieheight = 232;
		objinnerhtml = '<object width="'+moviewidth+'" height="'+movieheight+'" codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param name="movie" value="'+ smallplayer +'" /><param name="loop" value="false" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="scale" value="noborder" /><param name="wmode" value="transparent" /><param name="FlashVars" value="moviePath='+mediapath+'?id='+videoId+'&conn_msg='+connmsg+'" /><embed src="'+ smallplayer +'" loop="false" bgcolor="#ffffff" width="'+ moviewidth +'" height="'+ movieheight +'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" FlashVars="moviePath='+mediapath+'?id='+videoId+'&conn_msg='+connmsg+'" play="false" menu="false" quality="high" scale="noborder" wmode="transparent" /></object>';
		var object = document.createElement('div');
		object.innerHTML = objinnerhtml;
		div.appendChild(object);
	}
	else {
	}

	var p = document.createElement('p');
	//pTxt = document.createTextNode(txtcomment);
	p.innerHTML = txtcomment;
	var pcredit = document.createElement('p');
	pcredit.setAttribute('class', 'credit');
	citycomment = (citycomment != '')? fromm +' ' + citycomment + ', ': '';
	var span = '<span class="province_abbr">'+ provcomment +'</span> <a href="'+ urlp + '?id=' + smtid +'">'+ smtname +'</a> '+citycomment + datecomment +' '+ timecomment;
	pcredit.innerHTML = span;

	div.appendChild(p);
	div.appendChild(pcredit);

	return div;
}

Comments.prototype.checkValueForEmpty = function (htmlcollection){
	try { return htmlcollection[0].childNodes[0].nodeValue; } catch(err) { return ""; }
}

Comments.prototype.comment_submit = function (f) {
	var txt = f['txt_comment'].value;
	if (txt.trim() != "" || f['video_comment'].value) {
		var f = document.forms['comment_form'];
		f['txt_comment'].disabled = true;
		f['submit_button'].disabled = true;
		
		var submit_msg = objId('comment_submit_msg');
		submit_msg.innerHTML = pleasewait;
		
		return AIM.submit(txt, f, {'onStart' : iframeCommentStart, 'onComplete' : iframeCommentComplete});
	}
	else return false;
}

Comments.prototype.comment_submitted = function () {
	if (this.xmlHttp.readyState == 4) {
		this.show_comments(false);
	}
}

function iframeCommentStart() {

	return true;
}

function iframeCommentComplete(response) {
	var success_response = response.getElementsByTagName('success');
	try {
		if (success_response[0].childNodes[0].nodeValue == 'true') {
			if (comments_global != null) {
				comments_global.show_comments(false);
			}			
		}
		else {
			var error_response = response.getElementsByTagName('error');
			alert(error_response[0].childNodes[0].nodeValue);
		}
	}
	catch (err) { alert(err); }//comments_global.show_comments(false); }
	
	var f = document.forms['comment_form'];
	f['txt_comment'].disabled = false;
	f['submit_button'].disabled = false;
	f['txt_comment'].value = '';
	
	var submit_msg = objId('comment_submit_msg');
	submit_msg.innerHTML = '';
}

AIM = {

	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

    form : function(txt, f, name) {
        f.setAttribute('target', name);
		f['content'].value = txt;
		f['contentHTML'].value = 'false';
		f['language'].value = langu;
		f['subjectId'].value = comments_global.id;
		f['action'].value = 'add';		
    },

    submit : function(txt, f, c) {
        AIM.form(txt, f, AIM.frame(c));
        if (c && typeof(c.onStart) == 'function') {
            return c.onStart();
        } else {
            return true;
        }
    },

    loaded : function(id) {
        var i = document.getElementById(id);
        if (i.contentDocument) {
            var d = i.contentDocument;
        } else if (i.contentWindow) {
            var d = i.contentWindow.document;
        } else {
            var d = window.frames[id].document;
        }
        if (d.location.href == "about:blank") {
            return;
        }

        if (typeof(i.onComplete) == 'function') {
	        if (i.contentDocument) {
	            i.onComplete(d);
	        } else if (i.contentWindow) {
	            i.onComplete(d.XMLDocument);
	        } else {
	            i.onComplete(d);
	        }
        }
    }

}
