//var paaosoite="../../";
var paaosoite="http://www3.jkl.fi/kaavoitus/";
function toggleDiv(divid, sivuid, func) {
	if(func==1 && sivuid!="") { // jos pyydetään kommenttilistaa
		var osoite=paaosoite+"inc/inc_hae_kommentit.php?id="+sivuid;
		haetieto(divid, osoite);
	}
	if(document.getElementById(divid).style.display == 'none') {
		document.getElementById(divid).style.display = 'block';
	} else {
		document.getElementById(divid).style.display = 'none';
	}
}
function wedge(event){ 
	return false 
}
if (document.all) { Droppables.drops = [] }
window.onload = function() {
	// Make all the items dragable by creating Draggable objects
	var elements = [ 
   'kommentit',
   'uusikommentti'
   ];
	elements.each(
       function(item) {
           new Draggable(item, {});
		//   new Draggable(item, { onStart: function(){ if (document.all){ Event.observe(document.body, "drag", wedge, false); Event.observe(document.body, "selectstart", wedge, false); } }, revert: function(){ if (document.all){ Event.stopObserving(document.body, "drag", wedge, false); Event.stopObserving(document.body, "selectstart", wedge, false); } } })
		   // lähde: http://www.ruby-forum.com/topic/127550
       }
	);
	Event.observe('kommentinlisaysnappi', 'click', lisaa_kommentti);

}

function haetieto(id,url) {  
//	new Ajax.Updater(id, url);
	new Ajax.PeriodicalUpdater(id, url, { frequency:3, decay: 1 } );
}

function lisaa_kommentti() {
//	var x = $('kommentinlisays').serialize();
	var kentat=[ "kom2", "nimi2", "email2", "kaid2"];
	for(i=0; i<kentat.length; i++) {
		if($F(kentat[i])==null || $F(kentat[i])=="") {
			alert("kaikki kentät pitää täyttää!"); return false;
		}
	}

	var osoite=paaosoite+"inc/inc_lisaa_kommentti.php";
	new Ajax.Request(osoite, {
		method:'post',
		parameters: Form.serialize($('kommentinlisays'), true),
		onSuccess: toggleDiv('uusikommentti', $F(kentat[3]), 2),
		onComplete: toggleDiv('kommentit', $F(kentat[3]), 1), 
		onFailure: virhe
	});
}
function virhe(transport) {
  console.log(transport.statusText);
}

