$(document).ready(function(){
	$('.videoTek').click(function(){
		$(".popup").fadeOut();
		var v_id = $(this).attr("id");
		v_id = v_id.substring(2);
		var ayframe = '<iframe width="640" height="480" src="http://www.youtube.com/embed/'+v_id+'" frameborder="0" allowfullscreen></iframe>';
		$("#videoPlayer").html(ayframe);
		$("#popup_videoPlayer").centerit().fadeIn();
	});
	$('.fotoTek').click(function(){
		$(".popup").fadeOut();
		$("#fotoGoster").html('<img src="'+$("#anasayfa_url").html()+'img/loading-black.gif" style="margin:30px;"/>');
		$("#popup_fotoGoster").fadeIn().centerit();
		var f_id = $(this).attr("id");					
		var ayframe = $("<img />").attr('src', 'http://img.kimdir.com/kimdir/'+$("#g0").html()+'/'+$("#g1").html()+'/'+f_id+'.jpg').addClass('popup_foto').load(function(r,s,x){
			if(s=='error'){
				setTimeout(function(){
					$("#fotoGoster").html("Hatalı Görsel");
				},3000);
			} else {
				$("#fotoGoster").html(ayframe);
				$("#popup_fotoGoster").centerit();
			}
		});
	});
	$('.popCloseWhite').click(function(){
		$("#fotoGoster").empty();
		$("#videoPlayer").empty();
	});
	$('#profil_bildir_link').click(function(){
		$("#popup_profilBildir").fadeIn().centerit();
	});
	$('.bildirRadio').click(function(){
		if($(this).attr("id")=="bildirR7"){
			$("#bildir_diger").fadeIn();
		} else {
			$("#bildir_diger").fadeOut();
			$("#bildirDiger").val("");
		}
	});
	$("#lokasyon_sehir_id").change(function(){
		$('#lokasyon_ulke_id').val("201");
	});
	$("#lokasyon_ulke_id,#lokasyon_sehir_id").change(function(){
		if($('#lokasyon_ulke_id').val()==201){
			$("#lokasyon_label_sehir").parent().fadeIn();
			lokasyon = $("#lokasyon_sehir_id").val();
			$("#kayit_lokasyon").val($("#lokasyon_sehir_id").val());
		}
		if($('#lokasyon_ulke_id').val()>201){
			$("#lokasyon_label_sehir").parent().fadeOut();
			lokasyon = $('#lokasyon_ulke_id').val();
			$("#kayit_lokasyon").val($('#lokasyon_ulke_id').val());
		}
		if($('#lokasyon_ulke_id').val()==0){
			lokasyon = 0;
			$("#kayit_lokasyon").val("0");
		}
	});
	$('#bildirGonder').click(function(){
		var formdata = $('#profilBildirForm').serialize();
		formdata = formdata.replace("bildirTip","t");
		formdata = formdata.replace("bildirDiger","d");
		$.ajax({
			type: "POST",
			url: "sorgu/unlu_profili_bildir.php",
			timeout: 10000,
			data: "u="+$('#unluid').html()+"&"+formdata,
			success: function(data){
				if(data==1){
					$("#bildirMesaj").html("Teşekkür ederiz.").css('color','green').fadeIn();
					setTimeout(function(){
						$("#popup_profilBildir").fadeOut();
						$("input:radio").removeAttr("checked");
						$("#bildirDiger").val("");
						$("#bildirMesaj").html("").css('color','black').fadeOut();
					},4000);
				}
				else if(data==2){ $("#bildirMesaj").html("Hata: 2").css('color','red').fadeIn(); }
				else if(data==3){ $("#bildirMesaj").html("Hata: 3").css('color','red').fadeIn(); }
				else if(data==4){ $("#bildirMesaj").html("Hata: 4").css('color','red').fadeIn(); }
				else{ $("#bildirMesaj").html("Hatta: "+data).css('color','red').fadeIn(); }
			},
			error: function(x,t,m){
				if(t==="timeout"){ $("#bildirMesaj").html("Lütfen tekrar deneyin!"+data).css('color','red').fadeIn(); }
			}
		});
	});
	$('#unluKayitFB').click(function(){
		$("#popup_loading").fadeIn().centerit();
		FB.login(function(yanit){
			if (yanit.authResponse){
				FB.api('/me',function(u){
					var anasayfa_url = $('#anasayfa_url').html();
					$('#kayit_facebook_id').val(u.id);
					$('#kayit_facebook_at').val(yanit.authResponse.accessToken);
					$("#popup_kayit_unlu").fadeIn(0).centerit();
					$("#kayit_username").focus();
					$("#popup_loading").fadeOut();
				});
			} else {
				$("#error_title").html("Hata!");
				$("#error_message").html("Giriş Yapılamadı.<br /><br />Lütfen tekrar deneyin.").css('color','red');
				$("#popup_loading").fadeOut(0);
				$("#popup_error").fadeIn(0).centerit();
			}
		}, {scope:'publish_stream,email'});
	});
	$('#kayit_submit').click(function(){
		$('#kayit_mesaj').html("<img src=\"img/loading.min.gif\" /> Kaydediliyor...").css('color','black').fadeIn();
		if(!$('#kayit_sozlesme').is(':checked')){
			$("#kayit_mesaj").html("Kullanım koşullarını kabul etmediniz.").css('color','red').fadeIn();
		} else {
			$.post(
				"sorgu/uye_fb_kayit.php",
				{
					facebook_id: $('#kayit_facebook_id').val(),
					kullanici_adi: $('#kayit_username').val(),
					davetiye: $('#kayit_sayfasi').val(),
					meslek: $('#kayit_meslek').val(),
					lokasyon: $('#kayit_lokasyon').val(),
					access_token: $('#kayit_facebook_at').val()
				},
				function(data){
					if(data==1){
						$("#error_title").html("Tebrikler!");
						$("#error_message").html("Sayfanız açıldı.<br /><br />Şimdi yönlendiriliyorsunuz....").css('color','green');
						$("#error_message").append(".");
						$(".popClose").remove();
						$("#popup_kayit_unlu").fadeOut(0);
						$("#popup_error").fadeIn(0);
						window.location.href = $("#anasayfa_url").html();
					} else if(data==3){
						$("#kayit_mesaj").html("Şu anda oturumu açık olan facebook kullanıcısı ile daha önce kayıt olunmuş.").css('color','red').fadeIn();
					} else if(data==2){
						$("#kayit_mesaj").html("Bir hata oluştu.<br /><br />Lütfen daha sonra tekrar deneyin.<br /><br />"+data).css('color','red').fadeIn();
					} else if(data==5){
						var beklemesaj ="Şu anda davetiyeniz olmadığı için sitenizi açamadık.<br /><br />"+
																	"Davetiyesiz kayıt süreci başladığında istediğiniz kullanıcı adını "+
																	"öncelikli olarak almak istiyorsanız "+
																	"<span class=\"beklesin\">tıklayın</span>. "+
																	"Davetiyeniz yakında <em>"+$('#kayit_eposta').val()+"</em> adresinize gönderilecek.";
						$("#error_message").html(beklemesaj);
						$('#kayit_mesaj').html("").css('color','black');
						$("#popup_kayit_unlu").fadeOut(0);
						$("#popup_error").fadeIn(0).centerit();
					} else if(data==6){
						$("#kayit_mesaj").html("Kullanıcı adınız 3 karakterden kısa olmamalıdır.").css('color','red').fadeIn();
					} else if(data==9){
						$("#kayit_mesaj").html("Kullanıcı adınız 30 karakterden uzun olmamalıdır.").css('color','red').fadeIn();
					} else if(data==7){
						$("#kayit_mesaj").html("Bu kullanıcı adı daha önce alınmış.").css('color','red').fadeIn();
					} else if(data==8){
						$("#kayit_mesaj").html("Kullanıcı adı geçerli değil.").css('color','red').fadeIn();
					} else if(data==10){
						$("#kayit_mesaj").html("Bu kullanıcı adı rezerve edilmiş.").css('color','red').fadeIn();
					} else if(data==11){
						$("#kayit_mesaj").html("Bu kullanıcı adı alınamaz.").css('color','red').fadeIn();
					} else if(data==12){
						$("#kayit_mesaj").html("Şu anda oturumu açık olan facebook kullanıcısı ile daha önce kayıt olunmuş.").css('color','red').fadeIn();
					} else if(data==13){
						$("#kayit_mesaj").html("Lütfen mesleğinizi seçin.").css('color','red').fadeIn();
					} else if(data==14){
						$("#kayit_mesaj").html("Lütfen şehir ve ülke seçin.").css('color','red').fadeIn();
					} else {
						$("#kayit_mesaj").html("Bilinmeyen hata ("+data+").<br />Lütfen daha sonra tekrar deneyin.").css('color','red').fadeIn();
					}
				}
			);
		}
	});
});
function textLimit(field) {
	if (field.value.length > 300)
		field.value = field.value.substring(0, 300);
}
