function turkishToUpper(gelen){
    var string = gelen;
    var letters = { "i": "Ý", "þ": "Þ", "ð": "Ð", "ü": "Ü", "ö": "Ö", "ç": "Ç", "ý": "I" };
    string = string.replace(/(([iþðüöçý]))+/g, function(letter){ return letters[letter]; })
    return string.toUpperCase();
}


function ilk(gelen){
    var string = gelen;
var ilki=gelen.substr(0,1)
var sonu=gelen.substr(1,gelen.length-1)
    var letters = { "i": "Ý", "þ": "Þ", "ð": "Ð", "ü": "Ü", "ö": "Ö", "ç": "Ç", "ý": "I" };
    string = ilki.replace(/(([iþðüöçý]))+/g, function(letter){ return letters[letter]; })
return  ilki.toUpperCase()+sonu;
}

function turkishToLower(gelen){
    var string = gelen;
    var letters = { "Ý": "i", "I": "ý", "Þ": "þ", "Ð": "ð", "Ü": "ü", "Ö": "ö", "Ç": "ç" };
    string = string.replace(/(([ÝIÞÐÜÇÖ]))+/g, function(letter){ return letters[letter]; })
    return string.toLowerCase();
}


function ilkrenkli(gelen){
if (gelen.indexOf('#0a7dbc">')==22)
{
var ilk=gelen.substr(0,21);
var son1=gelen.substr(31,1);
var son2=gelen.substr(32,gelen.length-31);

var letters = { "i": "Ý", "þ": "Þ", "ð": "Ð", "ü": "Ü", "ö": "Ö", "ç": "Ç", "ý": "I" };
    sondegis = son1.replace(/(([iþðüöçý]))+/g, function(letter){ return letters[letter]; })
return ilk+':#0a7dbc">'+sondegis.toUpperCase()+son2
}
else
return gelen
}







function explode( delimiter, string, limit ) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 810.114
    // discuss at: http://phpjs.org/functions/explode
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}




// global variables
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 500;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acResultsDiv  = null;

function setAutoComplete(field_id, results_id, get_url){

	// initialize vars
	acSearchId  = "#" + field_id;
	acResultsId = "#" + results_id;
	acURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acSearchField	= $(acSearchId);
	acResultsDiv	= $(acResultsId);

	// reposition div
	repositionResultsDiv();
	
	// on blur listener
	acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) });

	// on key up listener
	acSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();

		// check an treat up and down arrows
		if(updownArrow(keyCode)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27){
			clearAutoComplete();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoComplete(lastVal)}, acDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoComplete(lastValue)
{
	// get the field value
	var part = acSearchField.val();

	// if it's empty clear the resuts box and return
	if(part == ''){
		clearAutoComplete();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != part){
		return;
	}

	// get remote data as JSON
	$.getJSON(acURL + part, function(json){

		// get the total of results
		var ansLength = acListTotal = json.length;

		// if there are results populate the results div
		if(ansLength > 0){

			var newData = '';

			// create a div for each result

var gelenhtml=new Array();
			for(i=0; i < ansLength; i++) {

gelen=explode(";",json[i]);
//if (json[i].substr(json[i].length-1,1)=="1")
//json[i].substr(0,json[i].length-1)

if (gelen[1]=="1")
bol='style="font-weight:bold"'
else
bol=''
gelenhtml[i]=gelen[2]
				newData += '<div class="unselected" '+bol+'>' + ilkrenkli(ilk(turkishToLower(gelen[0])))+ '</div>';
			}

			// update the results div
			acResultsDiv.html(newData);
			acResultsDiv.css("display","block");
			
			// for all divs in results
			var divs = $(acResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divs.mouseover( function() {
				divs.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divs.click( function() {
				acSearchField.val(this.childNodes[0].nodeValue);
				clearAutoComplete();


//burada hýyar childnodesin index nosunu buluyoruz
var zz=0;
var no;
divs.each(function(){ 
if (this.className == "selected")
no=zz
zz++;
 });

document.location.href=gelenhtml[no];
			});

		} else {
			clearAutoComplete();
		}
	});
}

// clear auto complete box
function clearAutoComplete()
{
	acResultsDiv.html('');
	acResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDiv()
{
	// get the field position
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acSearchField.height();
	var sf_width  = acSearchField.width();

	// apply the css styles - optimized for Firefox
	acResultsDiv.css("position","absolute");
	acResultsDiv.css("left", sf_left - 2);
	acResultsDiv.css("top", sf_top + sf_height + 5);
	acResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acResultsDiv.children().each(function(i){
			if(i == acListCurrent){
				acSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acListCurrent = -1;
		return false;
	}
}



