var events_containers = 1;
var comments_containers = 1;
var nothing = 'Įrašų nėra';
var comments=new Array();
root = "http://mokinukai.lt"; 
function add_element(id){ 

	found = false;
	for(i=0; i<comments.length; i++){ //alert(comments[i][0]);
			if(comments[i][0]==id){comments[i][1]= comments[i][1]+1; found=true; break; }
	}
	if(found==false){ comments.push(new Array(2)); comments[i][0]=id; comments[i][1]=1; }
	//alert(id+' '+comments[i][1]);
}
function reduce_element(id){ //alert(id); 
	found = false;
	for(i=0; i<comments.length; i++){ //alert(comments[i][0]);
			if(comments[i][0]==id){comments[i][1]= comments[i][1]-1; found=true; break; }
	}
}
function check_elements(id){
	var i =0;
	for(i=0; i<comments.length; i++){
			//document.write(comments);
			if(comments[i][0]==id){ break; }
		}
		//document.write(comments[i]);
		//comments.splice(i,1);
		if(comments[i][1]=='undefined') return 0;
		return comments[i][1];
}
var loader = '';

function ajaxObject(layer, url) {                                    // This is the object constructor
   var that=this;                                                    // A workaround for some javascript idiosyncrocies
   var updating = false;                                             // Set to true if this object is already working on a request
   this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.

   this.update = function(passData) {                                // Initiates the server call.
      if (updating==true) { return false; }                          // Abort if we're already processing a call.
      updating=true;                                                 // Set the updating flag.
      var AJAX = null;                                               // Initialize the AJAX variable.
      if (window.XMLHttpRequest) {                                   // Are we working with mozilla?
         AJAX=new XMLHttpRequest();                                  //  Yes -- this is mozilla.
      } else {                                                       // Not Mozilla, must be IE
         AJAX=new ActiveXObject("Microsoft.XMLHTTP");                //  Wheee, ActiveX, how do we format c: again?
      }                                                              // End setup Ajax.
      if (AJAX==null) {                                              // If we couldn't initialize Ajax...
         alert("Klaida. Naršyklė nepalaiko AJAX. Reikia įjungti JavaScript palaikymą.");                // Sorry msg.                                              
         return false                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
      } else {
         AJAX.onreadystatechange = function() {                      // When the browser has the request info..
		 	if(loader!="") {LayerID.innerHTML = loader;}
            if (AJAX.readyState==4 || AJAX.readyState=="complete") { //   see if the complete flag is set.
               LayerID.innerHTML=AJAX.responseText;                  //   It is, so put the new data in the object's layer
               delete AJAX;                                          //   delete the AJAX object since it's done.
               updating=false;                                       //   Set the updating flag to false so we can do a new request
               that.callback();                                      //   Call the post-processing function.
            }                                                        // End Ajax readystate check.
         }                                                           // End create post-process fucntion block.
         var timestamp = new Date();                                 // Get a new date (this will make the url unique)
         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);   // Append date to url (so the browser doesn't cache the call)
         AJAX.open("GET", uri, true);                                // Open the url this object was set-up with.
         AJAX.send(null);                                            // Send the request.
         return true;                                                // Everything went a-ok.
      }                                                              // End Ajax setup aok if/else block                 
   }
      
   // This area set up on constructor calls.
   var LayerID = document.getElementById(layer);                     // Remember the layer associated with this object.
   var urlCall = url;                                                // Remember the url associated with this object.
}                                                                    // End AjaxObject


function get_comments(target, mod, limit, element_id, root, mo){
	// Comments function; Last update: 2010-12-03
	add_element(element_id);
	element_no = check_elements(element_id);
	new_field = ''+element_id+'_'+element_no;//comments_containers; //PROBLEMA	
	reduce_element(element_id);
	//alert(new_field + ' No: '+element_no);
	list = document.getElementById(element_id);
	if(document.getElementById(new_field)==null) list.innerHTML =list.innerHTML+ '<div id="'+new_field+'" style="margin:0px;"></div>';
	field = document.getElementById(new_field);
	//field.style.visibility="hidden";
	field.innerHTML = '<div align="center" style="width:100%;"><img src="'+root+'/images/loading1.gif" border=0></div>';
	
	// NEW //
	var commentObj = new ajaxObject(new_field, root+'/inc/request.php');
        commentObj.callback = 
			function() { 
				var rfield = document.getElementById(new_field); 
				var result = rfield.innerHTML; //
				if(rfield){  
					comments_containers++; //alert(result);
					if(result==0){ rfield.innerHTML = '<h2 align="center">'+nothing+'</h2>' ;}
					else{
						rfield.style.visibility="visible";
						rfield.innerHTML = ""+result;
						add_element(element_id);
					}
				  }
		  }// end of callback function
				  
				  
	// CALL Object
	commentObj.update("function=show_comments&target="+target+"&mod="+mod+"&limit="+limit+"&start="+(limit*(element_no-1))+"&modify="+mo);
	
}
function delete_comment(id, user_id, root, mo, created){
	item_id='comment_'+id;
	var rs = confirm('Ar tikrai norite ištrinti šį komentarą?');
	if(rs){
		var xmlhttp;  
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
				{
				if(xmlhttp.readyState==4)
				  {	
				  if(xmlhttp.status == 200){
					 // alert('test'+comments_containers);
					  
					  if(field){  
						comments_containers++; 
						if(xmlhttp.responseText==0){ alert('Klaida. Ištrinti nepavyko'); }
						else if(xmlhttp.responseText==1){
							//field.style.visibility="visible";
							//field.innerHTML = ""+xmlhttp.responseText;
							//alert(xmlhttp.responseText);
							hide(item_id);
							
						}else{ alert("Klaida: "+xmlhttp.responseText); }
					  }
				 }else{ alert('Klaida trinant įrašus. Bandykite dar kartą'); }
				  
				  }
				}	
		var url = root+"/inc/request.php";
		
		url = url + "?function=delete_comment&item_id="+id+"&user_id="+user_id+"&modify="+mo+"&created="+created;
		
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}//end if confirmed
	
			
}


function delete_event(id, user_id, root, mo, created){
	item_id='comment_'+id;
	var rs = confirm('Ar tikrai norite ištrinti šį komentarą?');
	if(rs){
		var xmlhttp;  
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
				{
				if(xmlhttp.readyState==4)
				  {	
				  if(xmlhttp.status == 200){
					 // alert('test'+events_containers);
					  
					  if(field){  
						events_containers++; 
						if(xmlhttp.responseText==0){ alert('Klaida. Ištrinti nepavyko'); }
						else if(xmlhttp.responseText==1){
							//field.style.visibility="visible";
							//field.innerHTML = ""+xmlhttp.responseText;
							//alert(xmlhttp.responseText);
							hide(item_id);
							
						}else{ alert("Klaida: "+xmlhttp.responseText); }
					  }
				 }else{ alert('Klaida trinant įrašus. Bandykite dar kartą'); }
				  
				  }
				}	
		var url = root+"/inc/request.php";
		
		url = url + "?function=delete&item_id="+id+"&user_id="+user_id+"&modify="+mo+"&created="+created;
		
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}//end if confirmed
	
			
}	
function get_events(target, limit, element_id, root){
	new_field = 'event_'+element_id+'_'+events_containers;
	list = document.getElementById(element_id);
	if(document.getElementById(new_field)==null) list.innerHTML =list.innerHTML+ '<div id="'+new_field+'" style="margin:0px;"></div>';
	field = document.getElementById(new_field);
	//field.style.visibility="hidden";
	field.innerHTML = '<div align="center" style="width:100%;"><img src="'+root+'/images/loading1.gif" border=0></div>';
	
	// NEW //
	var eventObj = new ajaxObject(new_field, root+'/inc/request.php');
        eventObj.callback = 
			function() { 
				var rfield = document.getElementById(new_field); 
				var result = rfield.innerHTML; //
				if(rfield){  
				    events_containers++; 
					if(result==0){ 
						events_containers=events_containers-1; 
						rfield.innerHTML = '<h2 align="center">'+nothing+'</h2>'; }
					else{
						rfield.style.visibility="visible";
						rfield.innerHTML = result;
					}
				  }
								
		  }// end of callback function
				  
				  
	// CALL Object
	eventObj.update("function=show_events&user_id="+target+"&limit="+limit+"&start="+(limit*(events_containers-1)));		
}
function rate(target1, target2, target_type, vote){
	item_id='rate_'+target1+'_'+target2+'_'+target_type;
	field = document.getElementById(item_id);
		var xmlhttp;  
		if (window.XMLHttpRequest)
		  {
		  // code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {
		  // code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
				{
				if(xmlhttp.readyState==4)
				  {	
				  if(xmlhttp.status == 200){
					  //alert(xmlhttp.responseText);
					  
						//events_containers++; 
						if(xmlhttp.responseText==0){ alert('Klaida. Bandykite dar kartą'); }
						else if(xmlhttp.responseText==1){
							//field.style.visibility="visible";
							field.innerHTML = "Ačiū. Jūsų balas: "+vote;
							//alert(xmlhttp.responseText);
							//alert('Ačiū');
							//hide(item_id);
							
						}else if(xmlhttp.responseText<0){ field.innerHTML = "Jūs jau balsavote: "+xmlhttp.responseText.replace('-',''); 
						}else{ alert("Klaida: "+xmlhttp.responseText); }
					  
				 }else{ alert('Klaida. Bandykite dar kartą'); }
				  
				  }
				}	
		var url = root+"/inc/request.php";
		
		url = url + "?function=add_rating&target1="+target1+"&target2="+target2+"&target_type="+target_type+"&vote="+vote;
		
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
			
}	
