var comsys = new Object();
comsys.comInit = comInit;
//comsys.loadScript = loadScript;
comsys.getCommentCount = getCommentCount;
comsys.getMostCommented = getMostCommented;
comsys.getCommentsJSON = getCommentsJSON;
//comsys.writeData = writeData;
comsys.mostCommentedOwners = '';
comsys.showComments = showComments;
comsys.version = 'v1.0';

//this.loadScript('http://comments.pif.office.net/static/js/jquery.postmessage.min.js');

function comInit(siteid, typeid, owner, ownerstamp) {
	this.comowner = owner;
	this.comtype = typeid;
	this.comsite = siteid;
	this.comownerstamp = ownerstamp;
}

function showComments() {
	var if_height;
	document.write('<iframe src=\'http://comments.webground.bg/comments/list/owner/'+this.comowner+'/ownerstamp/'+this.comownerstamp+'/type/'+this.comtype+'/site/'+this.comsite+'#'+encodeURIComponent(document.location.href)+'\' scrolling="no" id="commentsIFrame" style="border:0; width:620px; height:500px; margin-bottom:20px;"></iframe>');
	$.receiveMessage(function(e){
		dataarray = e.data.split('&');
		var h = Number(dataarray[0].replace( /.*if_height=(\d+)(?:&|$)/, '$1' ) );
		var ref = Number(dataarray[1].replace( /.*parent_refresh=(\d+)(?:&|$)/, '$1' ) );
		if (ref == 2){
			document.location.href = document.location.href;
		}
		if ( !isNaN( h ) && h > 0 && h !== if_height ) {
			$("#commentsIFrame").height( if_height = h );
		}
	}, 'http://comments.webground.bg' );
}	
/*
function writeData(data) {
	switch(data){
		case 'count':
			alert(comsys.comCount);
			document.write(comsys.comCount);
		break;	
	}
}


function writeData() {
	alert(comsys.comCount);
	document.write(comsys.comCount);
}


function loadScript(url, callback)
{
    // adding the script tag to the head as suggested before
   var head = document.getElementsByTagName('head')[0];
   var script = document.createElement('script');
   script.type = 'text/javascript';
   script.src = url;

   // then bind the event to the callback function 
   // there are several events for cross browser compatibility
   if (callback != undefined) {
	   script.onreadystatechange = callback;
	   script.onload = callback;
   }

   // fire the loading
   head.appendChild(script);
}
*/

function getMostCommented(interval, limit, callback){
	$.getScript('http://comments.webground.bg/comments/ajax/act/requestdata/datatype/mostcommented/interval/'+interval+'/limit/'+limit+'/site/'+this.comsite+'/type/'+this.comtype, function(){
		if (callback && typeof(callback) === "function") {
			callback(mostCommentedOwners);
		} else {
			return false;
		}
	});
}

function getCommentsJSON(ownerid, limit, callback){
	$.getScript('http://comments.webground.bg/comments/ajax/act/requestdata/datatype/getcommentsbyowner/site/'+this.comsite+'/type/'+this.comtype+'/owner/'+ownerid+'/limit/'+limit, function(){
		if (callback && typeof(callback) === "function") {
			//var commentsObj = eval(jsonStr);
			callback(jsonObj);
		} else {
			return false;
		}
	});
}

function getCommentCount(ownerid, containerid){
	//document.write('<span id="commentowner'+ownerid+'"></span>');
	//var newcontent = document.createElement('span');  
	//newcontent.id = 'commentowner'+ownerid;  
	//newcontent.appendChild(document.createTextNode('Here is some syndicated content.'));  
	//var scr = document.getElementById('commentowner'+ownerid);  
	//scr.parentNode.insertBefore(newcontent, scr); 

	$.getScript('http://comments.webground.bg/comments/ajax/act/requestdata/datatype/commentscount/site/'+this.comsite+'/type/'+this.comtype+'/owner/'+ownerid, function(){
		document.getElementById(containerid).innerHTML=comsys.comCount;
		//cObj.html(comsys.comCount);
	});
}
