/*
 * Sozluk v0.1
 * Yazan  : Ertugrul Yildirim - HunTER
 * Ulasim : iletisim@eyildirim.org
*/

// Basla
// ---------------------------------------------
var _onload = window.onload;
var sozluk  = new Object;

window.onload = function(){
	if(typeof(_onload) == 'function') _onload();
	sozluk = new Sozluk();
	sozluk.basla();
}

// Sozluk
// ---------------------------------------------
function Sozluk(){
	var sozluk = {
		alan  : _nesne('alan'),
		ust   : _nesne('ust'),
		sol   : _nesne('sol'),
		sag   : _nesne('sag'),
		basla : function(){
			sozluk.alan.onkeyup = function(){
				sozluk.kelime(this.value);
			}
		},
		degistir : function(tablo){
			var xhr = new XHR('sozluk.php');
			xhr.talep('metot=degistir&tablo='+tablo);
			xhr.islem = function(){
				sozluk.kelime(sozluk.alan.value);
				sozluk.anlam(sozluk.alan.value);
			}
		},
		kelime : function(ifade){
			var xhr = new XHR('sozluk.php');
			xhr.talep('metot=kelime&ifade='+ifade);
			xhr.islem = function(){
				sozluk.sol.innerHTML = xhr.cevap();
			}
		},
		anlam : function(ifade){
			var xhr = new XHR('sozluk.php');
			xhr.talep('metot=anlam&ifade='+ifade);
			xhr.islem = function(){
				sozluk.sag.innerHTML = xhr.cevap();
			}
		}
	}
	return sozluk;
}

// XML
// ---------------------------------------------
function XML(hedef){
	var xml = {

		// Islem
		islem : function(){
			return true;
		},

		// Getir
		getir : function(){
			return this.xml;
		},

		// Yukle
		yukle : function(yukle){
			this.xml = new _xml(this);
			this.xml.load(yukle);
		},

		// Yarat
		yarat : function(metin){
			this.xml = new _xml(this);
			this.xml.loadXML(metin);
		},

		// Donustur
		donustur : function(xsl){
			return this.xml.transformNode(xsl);
		}
	}

	// Yukle ...
	if(hedef) xml.yukle(hedef);
	return xml;
}

// XHR
// ---------------------------------------------
function XHR(hedef){
	var xhr = {

		// Islem
		islem : function(){
			return true;
		},

		// Baglan
		baglan : function(hedef){
			this.xhr = new _xhr(this);
			this.xhr.open('POST',hedef,true);
			this.xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
		},

		// Talep
		talep : function(talep){
			this.xhr.send(talep);
		},

		// Deger
		deger : function(deger){
			var sonuc = /<script>(.*?)<\/script>/.exec(deger);
			if(sonuc){
				eval(sonuc[1]);
			}
		},

		// Cevap
		cevap : function(xsl,xml){
			var xml,rst;
			if(xsl){
				xml = new XML();
				xml.yarat(this.xhr.responseText);
				rst = xml.donustur(xsl);
			}
			else{
				if(xml){
					if(window.ActiveXObject) rst = this.xhr.responseXML;
					else{
						xml = new XML();
						xml.yarat(this.xhr.responseText);
						rst = xml.getir();
					}
				}
				else rst = this.xhr.responseText;
			}
			return rst;
		}
	}

	// Baglan ...
	if(hedef) xhr.baglan(hedef);
	return xhr;
}

// XML - Temel
// ---------------------------------------------
function _xml(nesne){
	var xml;

	if(window.ActiveXObject){
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async = true;
		xml.onreadystatechange = function(){
			if(xml.readyState == 4) nesne.islem();
		};
	}
	else if(document.implementation && document.implementation.createDocument){
		xml = document.implementation.createDocument('','',null);
		xml.onload = function(){
			nesne.islem();
		}
	}

	return xml;
}

// XHR - Temel
// ---------------------------------------------
function _xhr(nesne){
	var xhr;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
		try{ xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(e){ xhr = false; }
	}
	@else xhr=false; @end @*/

	if(!xhr && typeof XMLHttpRequest != 'undefined'){
		try{ xhr = new XMLHttpRequest(); }
		catch (e){ xhr = false; }
	}

	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200) nesne.islem();
	};

	return xhr;
}

// Nesne - Temel
// ---------------------------------------------
var _nesne_ = new Array();
function _nesne(no,zorla){
	if(!_nesne_[no] || zorla){
		if(typeof(document.getElementById(no)) != 'undefined') _nesne_[no] = document.getElementById(no);
		else _nesne_[no] = false;
	}
	return _nesne_[no];
}

// Mozilla Firefox icin ...
// ---------------------------------------------

// LoadXML
if(typeof DOMParser != 'undefined'){
	Document.prototype.loadXML = function(metin){
		var _doc = (new DOMParser()).parseFromString(metin,'text/xml');

		while(this.hasChildNodes()){
			this.removeChild(this.lastChild);
		}

		for(var i = 0; i < _doc.childNodes.length; i++) {
			this.appendChild(this.importNode(_doc.childNodes[i], true));
		}
	};
}

// transformNode
if(typeof XSLTProcessor != 'undefined'){
	Document.prototype.transformNode = function(xsl){
		var xtp, _doc;
		xtp = new XSLTProcessor();
		xtp.importStylesheet(xsl);

		_doc = xtp.transformToDocument(this);
		return _doc.xml;
	};
}

// xml
if(typeof XMLSerializer != 'undefined'){
	Document.prototype.__defineGetter__('xml', function(){
		return (new XMLSerializer()).serializeToString(this);
	});
}