﻿var sf = 0;
var sfS = 5;

Array.prototype._sortPrep = function ( field ) {
	if (!this.maximum) {
		this.maximum = this[0][field];
		var i = this.length;
		while(i--) {
			if (this.maximum < this[i][field]) {this.maximum = this[i][field];}
		}
	}
	if (this.maximum) {
		var fill = new Array();
		var length = this.maximum.toString().length;
		var i = length; var zs = "";
		while (i--) {
			fill.push(zs); zs += "0";
		}
		for (var i=0; i < this.length; i++ )
			this[i][field] = (this[i][field].toString().length < length ? fill[length - this[i][field].toString().length] : "") + this[i][field];
	}
}
var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/; 

Array.prototype._sortPrep1 = function ( field ) {
	
	if (!this.maximum) {
		this.maximum = this[0][field].replace(dateRE,"$3$2$1");
		var i = this.length;
		while(i--) {
			if (this.maximum < this[i][field].replace(dateRE,"$3$2$1")) {this.maximum = this[i][field].replace(dateRE,"$3$2$1");}
		}
	}
	if (this.maximum) {
		var fill = new Array();
		var length = this.maximum.toString().length;
		var i = length; var zs = "";
		while (i--) {
			fill.push(zs); zs += "0";
		}
		for (var i=0; i < this.length; i++ )
			this[i][field] = (this[i][field].toString().length < length ? fill[length - this[i][field].toString().length] : "") + this[i][field];
	}
}

 
 
Array.prototype._sortIntAsc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype._sortIntDesc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	this.reverse();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortAsc = function(field) {
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortDesc = function(field) {
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	this.reverse();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortDateAsc = function(field) {
	this._sortPrep1(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}

Array.prototype.sortDateDesc = function(field) {
	this._sortPrep(field);
	var saveO = Object.prototype.toString;
	var saveA = Array.prototype.toString;
	Object.prototype.toString = function(){ return this[field] };
	Array.prototype.toString = function(){ return this[field] };
	this.sort();
	this.reverse();
	Array.prototype.toString = saveA;
	Object.prototype.toString = saveO;
}


Array.prototype._parseInt = function(field) {
	for (var i=0; i < this.length; i++ ) {
		this[i][field] = this[i][field]*1;
	}
}

Array.prototype.sortIntAsc = function(field) {
	this._sortIntAsc(field);
	this._parseInt(field);
}

Array.prototype.sortIntDesc = function(field) {
	this._sortIntDesc(field);
	this._parseInt(field);
}


function yazdir() {
	   var temp = '';
	   sfSayisi = Math.round(etkinlik_list.length/sfS);
	   
	   if (sf>(sfSayisi-1)) {  sf=0;}
	   if (sf<0) {sf=(sfSayisi-1);}
	   sonS = (sf +1) * sfS;
	   
	   if (sf==0) { document.getElementById('sayfalama_left_arrow').style.backgroundPosition = 'bottom left';   document.getElementById('sayfalama_left_arrow').style.cursor ='default'; } else { document.getElementById('sayfalama_left_arrow').style.backgroundPosition  = 'top left';  document.getElementById('sayfalama_left_arrow').style.cursor ='pointer'; } 
	   if (sf==(sfSayisi-1)) { document.getElementById('sayfalama_right_arrow').style.backgroundPosition = 'bottom left'; document.getElementById('sayfalama_right_arrow').style.cursor ='default'; } else { document.getElementById('sayfalama_right_arrow').style.backgroundPosition  = 'top left'; document.getElementById('sayfalama_right_arrow').style.cursor ='pointer';} 

	   if (sonS>etkinlik_list.length) { sonS = etkinlik_list.length }
	   for (i=sf*sfS;i<sonS ;i++) {
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Macka","Maçka");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Izmir","İzmir");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Istanbul","İstanbul");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Karakoy","Karaköy");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Kazalay","Kızılay");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Ciftehavuzlar","Çiftehavuzlar");
		   etkinlik_list[i].sube = etkinlik_list[i].sube.replace("Kizilay","Kızılay");
		   
			   
	     temp = temp + '<p><span class="kahve bld t12">' + etkinlik_list[i].sube + '</span><br/>';
		 temp = temp + '' + etkinlik_list[i].bilgi + '</p>';
	   }
	   
	   document.getElementById('etkinlik_list_div').innerHTML = temp;
	   sayfalamaFunc(sfSayisi,sf)
}



function sfDegistir1(sf1) {
	 if ((sf + sf1)>=0 && (sf + sf1) <=(sfSayisi-1) ) { 
  	   sf = sf + sf1;
	   yazdir();
	 }
}

function sfDegistir(sf1) {
	 sf = sf1;
	 yazdir();
}
function sayfalamaFunc(sfSayisi,secili) {
	 basSf = sf - 5;
	 sonSf = sf + 5;
	 var temp = "";
	 if (basSf<0) {basSf = 0;}
	 if (sonSf>sfSayisi) {sonSf = sfSayisi;}
    
	 if (basSf>0) { temp = temp + ' ... ';}
	 for (i=basSf;i<sonSf; i++) {
		 temp = temp + "&nbsp;";
		 if (secili!=i) { temp =  temp + "<a class=\"blk\" onclick=\"sfDegistir(" + i + ")\" href=\"#\" title=\"" + i + "\">";} else { temp =  temp + "<b>"; }
  	     temp = temp + (i+1);
		 if (secili!=i) { temp =  temp + "</a>";} else { temp =  temp + "</b>"; }
	 }
	 if (sonSf<sfSayisi) { temp = temp + ' ... ';}
	 document.getElementById('sayfalama_page').innerHTML = temp;
 
	 if (sfSayisi==0) { document.getElementById('sayfalama_div').style.display='none';}
}

function butonkime (f,isim,durum) {
	   if (durum=='3') {
	     f.style.backgroundPosition='0px -40px';
		 secili = isim;
		 if (secili=='isme') {
		   etkinlik_list.sortAsc('baslik');
		   document.getElementById('tarih').style.backgroundPosition='0px 0px';
		 }
		 else {
		   etkinlik_list.sortDateDesc('tarih');
		   document.getElementById('isme').style.backgroundPosition='0px 0px';
		 }
		 
		 yazdir();
	   }
	   else {
  	     if (isim!=secili && durum=='1') {
		   f.style.cursor='pointer';
		   f.style.backgroundPosition='0px -20px';
	     }
		 else if  (isim!=secili && durum=='2') {
		   f.style.cursor='default';
		   f.style.backgroundPosition='0px 0px';
		 }
		 else if(isim==secili && durum=='1') {
			f.style.cursor='default'; 
		 }
	   }	 
	 }
