$("#eczaneTanimlaForm").submit(function(e){
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/uyeEczaneYonetim" , bu.serialize(), function(data) {
		if(data == 1)
		{
			alert("Üye eczane eklendi.");
			bu.get(0).reset();
		}
	});
});


$("#nobetciEczaneAra").keyup(function() {
	var bu = $(this);
	$.post("/homepage/uyeArama", {
		Ara : bu.val()
	}, function(data) {
		$("#nobetciEczaneAramaSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<img width='50' height='50' alt='' src='/img/logo.png' class='thumbnail'>";
			html = html + "<h5>" + item.Eczane + "</h5>" + item.Bilgi + " <button class='btn eczaneSec' id='"+ item.Id +"'>Seç</button> <br />";
			$("#nobetciEczaneAramaSonuc").append(html);
		});
	}, "json");

})

$(".eczaneSec").live("click",function(e){
	e.preventDefault();
	var bu = $(this);
	var id = bu.attr("id");
	$("#EczaneBilgi").val(id);
	bu.text("Seçildi");
	bu.attr("disabled", "true");
	$("#nobetciEczaneAra").val("");
	
})

$(".eczaGoster").click(function(){
	$("#nobetciEczaneYarin").slideToggle();
})

$(".eczaneKaldir").click(function(e){
	e.preventDefault();
	var bu = $(this)
	var id = bu.attr("id");
	$.post("/homepage/nobetciEczaneKaldir" , {id : id}, function(data) {
		bu.closest("li").fadeOut();
	})
})


$("#anketOlusturForm").formajaxer(function() {
	location.href = '/homepage/anketYonetim';
});

$("#haberEkleForm textarea").cleditor({
	width : 780
});

$("#uyeGirisForm").live("submit", function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/uyeGirisIslem", bu.serialize(), function(data) {
		if(data == 1) {
			location.href = "/";
		} else {
			$("#uyeGirisSonuc", bu).html("Hatalı giriş. Lütfen tekrar deneyin.");
		}
	})
})

/* yeni */

$("#haberDuzenleForm textarea").cleditor({
	width : 780
});

$(".ecz-list a").live("click", function() {
	var bu = $(this);
	var Id = bu.attr("id").slice(1);
	var Type = bu.attr("id").slice(0, 1);
	if(Type == "g") {
		location.href = '/haber-duzenle/' + Id;
	} else {
		var c = confirm("Silmek istediğine emin misin ?");
		if(c) {
			if(Type == "e") {
				$.post("/homepage/nobetciEczaneIslem", {
					EczaneId : Id
				}, function(data) {
					bu.closest("li").fadeOut(400);
				})
			} else if(Type == "k") {
				$.post("/homepage/kullaniciTanimlaIslem", {
					Sil : Id
				}, function(data) {
					bu.closest("li").fadeOut(400);
				})
			} else if(Type == "h") {
				$.post("/homepage/haberEkleIslem", {
					Sil : Id
				}, function(data) {
					bu.closest("li").fadeOut(400);
				})
			} else if(Type == "f") {
				$.post("/homepage/etkinlikEkleIslem", {
					Sil : Id
				}, function(data) {
					bu.closest("li").fadeOut(400);
				})
			}else if(Type == "a") {
				$.post("/homepage/uyeEczaneYonetim", {
					Sil : Id
				}, function(data) {
					bu.closest("li").fadeOut(400);
				})
			} else if(Type == "g") {
				alert(Id);
			}
		}
	}

})

$("#sifreDegistirForm").live("submit", function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/sifreDegistir", bu.serialize(), function(data) {
		$("#sifreDegistirSonuc", bu).html(data);
	})
})

$("#eczaneAra input").keyup(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/uyeArama", {
		Ara : bu.val()
	}, function(data) {
		$("#uyeListeAramaSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<li><img width='50' height='50' alt='' src='/img/logo.png' class='thumbnail'>";
			html = html + "<h5>" + item.Eczane + "</h5>" + item.Bilgi + "<br />";
			html = html + "<br></li>";
			$("#uyeListeAramaSonuc").append(html);
		});
	}, "json");
});

$("#eczaneAraAdmin input").keyup(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/uyeArama", {
		Ara : bu.val()
	}, function(data) {
		$("#uyeListeAramaSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<li><a id='a" + item.Id + "' class='btn danger'>Sil</a><img width='50' height='50' alt='' src='/img/logo.png' class='thumbnail'>";
			html = html + "<h5>" + item.Eczane + "</h5>" + item.Bilgi + "<br />";
			html = html + "<br></li>";
			$("#uyeListeAramaSonuc").append(html);
		});
	}, "json");
});

$("#haberAra input").keyup(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/haberEkleIslem", {
		Ara : bu.val()
	}, function(data) {
		$(".haberSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<li><a id='h" + item.HaberId + "' class='btn danger'>Sil</a> <a id='g" + item.HaberId + "' class='btn info'>Düzenle</a><img width='50' height='50' alt='' src='/img/logo.png' class='thumbnail'>";
			html = html + "<h5>" + item.HaberBaslik + "</h5></li>";
			$(".haberSonuc").append(html);
		});
	}, "json");
});
/* yeni */

$("#arama").submit(function(e) {
	e.preventDefault();
	var bu = $(this);
	var key = bu.find("input").val();
	if(key == "") {
		alert("Aranacak kelimeyi girin.");
	} else {
		location.href = "/arama/" + key;
	}
})

$("#oyVer").submit(function(e) {
	e.preventDefault();
	var bu = $(this);
	var session = $("input[name='SessionInput']").val();
	if(session) {
		$.post("/homepage/oyVer", bu.serialize(), function(data) {
			if(data == 1)
				alert("Bir cevap seçiniz.");
			else
				$(".anketAna").html(data);
		})
	} else {
		alert("Ankete oy vermek üyelik gerektirir.")
	}
})

$(".anketBilgi").live("click", function() {
	var bu = $(this);
	var id = bu.attr("rel");
	var state = $("#anketBilgi" + id).css("display");
	if(state == "none") {
		$.post("/homepage/anketSonucGoruntule", {
			AnketId : id
		}, function(data) {
			$("#anketBilgi" + id).fadeIn().html(data);
		})
	} else
		$("#anketBilgi" + id).css("display", "none");
})
jQuery(function($) {
	$("#telefon").mask("(999) 999-9999");
});
jQuery(function($) {
	$("#tarih").mask("99/99/9999");
});

$(".anketSil").live("click", function() {
	var bu = $(this);
	var Id = bu.attr("id").slice(1);
	var c = confirm("Silmek istediğine emin misin ?");
	if(c) {
		$.post("/homepage/anketOlusturIslem", {
			Sil : Id
		}, function(data) {
			bu.closest("li").fadeOut(400);
		})
	}
})

$("#kullaniciTanimlaForm").submit(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/kullaniciTanimlaIslem", bu.serialize(), function(data) {
		if(data == "1") {
			alert("Kullanıcı eklendi.");
			bu.get(0).reset();
		} else {
			alert("Zorunlu alanları doldurun.");
		}
	})
});

$("#kullaniciAra input").keyup(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/kullaniciTanimlaIslem", {
		Ara : bu.val()
	}, function(data) {
		$(".aramaSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<li><a id='k" + item.KullaniciId + "' class='btn danger'>Sil</a><img width='50' height='50' alt='' src='/img/user.png' class='thumbnail'>";
			html = html + "<h5>" + item.KullaniciAdSoyad + "</h5><span> E-Posta : " + item.KullaniciEposta + "<br />";
			html = html + "Tel : " + item.KullaniciTelefon + " </span><br></li>";
			$(".aramaSonuc").append(html);
		});
	}, "json");
});

$("#etkinlikAra input").keyup(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/etkinlikEkleIslem", {
		Ara : bu.val()
	}, function(data) {
		$(".etkinlikSonuc").html("");
		$.each(data, function(i, item) {
			var html = "<li><a id='f" + item.EtkinlikBilet + "' class='btn danger'>Sil</a><img width='50' height='50' alt='' src='/img/logo.png' class='thumbnail'>";
			html = html + "<h5>" + item.EtkinlikAdi + "</h5></li>";
			$(".etkinlikSonuc").append(html);
		});
	}, "json");
});

$("#nobetciEczaneForm").submit(function(e) {
	e.preventDefault();
	var bu = $(this);
	$.post("/homepage/nobetciEczaneIslem", bu.serialize(), function(data) {
		if(data.Valid == 1) {
			alert("Eczane eklendi.");
			$("#nobetciEczaneAramaSonuc").html("Seçili eczane yok.")
			bu.get(0).reset();
		} else {
			alert("Lütfen boş alan bırakmayınız.");
		}
	}, "json")
});

$("#cevapEkle a").click(function() {
	var bu = $("#cevapEkle");
	var cevap = bu.find("input").val().trim();
	if(cevap != "") {
		var cln = bu.clone();
		bu.after(cln);
		cln.find("a").remove();
		cln.find("small").html("<a id='cevapSil' class='btn danger'>Sil</a>");
		bu.find("input").val("");
	} else {
		alert("Cevap yazmayı unutun.");
	}

	$("#cevapSil").click(function() {
		var bu = $(this)
		bu.closest("li").remove();
	})
})
