﻿function submitLink(){
	var sLinkToShort = $("#tbLink").val();
	var re = new RegExp("^(http|https|ftp):\/\/","m");
	var http = sLinkToShort.search(re);
	if(http == -1)
		sLinkToShort = "http://"+sLinkToShort;
	var re2 = new RegExp("/$","m");

	var http = sLinkToShort.search(re2);
	if(http != -1)
		sLinkToShort = sLinkToShort.substring(0, sLinkToShort.length-1);
	var re3 = new RegExp("^(http|https|ftp):\/\/ufalt.net/","m");
	var http = sLinkToShort.search(re3);
	if(http != -1){
		$("#errorDisplay").stop().animate({top:'-60px'},200,function(){$("#errorDisplay").html("Bu Link Zaten Ufaltılmış.");});		
		$("#errorDisplay").animate({top:'0px'},400, function(){$("#errorDisplay").animate({top:'0px'},4000,function(){$("#errorDisplay").animate({top:'-60px'},400)})});
	}else if(sLinkToShort != ""){
		$.ajax({
			type: 'POST',
			url: 'ajax.php',				
			data: {sLink : sLinkToShort},
			success:function(data){
				var sResult = ""+data.result;
				if(sResult == "0"){
					$("#errorDisplay").stop().animate({top:'-60px'},200,function(){$("#errorDisplay").html("Hatalı Link");});		
					$("#errorDisplay").animate({top:'0px'},400, function(){$("#errorDisplay").animate({top:'0px'},4000,function(){$("#errorDisplay").animate({top:'-60px'},400)})});
				}else{
					$("#tbLink").val(sResult);
					$("#tbLink").focus();
					$("#tbLink").select();
					if($("#resultBox").is(":hidden")){
						$("#inputBoxBottom").slideUp('fast',function(){
							$("#resultBox").slideDown('fast');
							$("#resultBoxBottom").slideDown('fast');
						})
					}
					
					var htmlShort = '<div class="shortlink"><a target="_blank" href="'+sResult+'">'+sResult+'</a></div>';
					var htmlCopy = '<div class="copy"><a href="javascript:void(0)" id="copyLink'+count+'" title="'+sResult+'">Kopyala</a></div><div class="seperator"> | </div><div class="qr"><a href="javascript:void(0)" id="qrLink'+count+'" title="'+sResult+'">Karekod</a></div>';
					var htmlLong = '<div class="longlink"><a target="_blank" href="'+sLinkToShort+'">'+sLinkToShort.substring(0,38)+'</a></div>';
					var htmlItem = '<li>'+htmlShort+htmlCopy+htmlLong+'</li>';
					
					$("#resultBox").children("ul").prepend(htmlItem);
					$("#resultBox").children("ul").find("li:first").delay(200).fadeIn(200,function(){
						var bu = $("#resultBox").children("ul").find("li:first").children("div.copy").children("a");
						bu.xincopy(function(){
							$("#errorDisplay").stop().animate({top:'-60px'},200,function(){$("#errorDisplay").html("Link Kopyalandı: "+sResult);});		
							$("#errorDisplay").animate({top:'0px'},400, function(){$("#errorDisplay").animate({top:'0px'},4000,function(){$("#errorDisplay").animate({top:'-60px'},400)})});
							return bu.attr("title");
						});
						var qr = $("#resultBox").children("ul").find("li:first").children("div.qr").children("a");
						qr.click(function(){
							var qrImage = "<img src='"+sResult+"+qrP'>"
							$("#qrImage").html(qrImage);
							$("#qrImageB").attr("href",sResult+"+qr11");
							$("#qrImageO").attr("href",sResult+"+qr7");
							$("#qrImageK").attr("href",sResult+"+qr3");
							$("#popWrapper").fadeIn(200,function(){
								$("#popQR").fadeIn(100);
							});
						});
					});		
					count++;
				}
			},
			dataType: 'json'
		});
	}
}

function getFlashMovie(movieName) {   var isIE = navigator.appName.indexOf("Microsoft") != -1;   return (isIE) ? window[movieName] : document[movieName];  }
  function changetext(url) {
	 var flashMovie = getFlashMovie("copyflash");
	 if (flashMovie) {

		flashMovie.copyviaflash(url);
	 }
  }
$(window).load(function() {

});
function copybuttons(){
	$("#resultBox").children("ul").children("li").each(function(i){
		var bu = $(this).children("div.copy").children("a");
		bu.xincopy(function(){
			alert(bu.attr("title"));
			return bu.attr("title");
		});
	})
}
function copyProcess(){
	//Kopyalama
	$("#resultBox").children("ul").children("li").each(function(i){
		var bu = $(this).children("div.copy").children("a");
		bu.xincopy(function(){
			$("#errorDisplay").stop().animate({top:'-60px'},200,function(){$("#errorDisplay").html("Link Kopyalandı: "+bu.attr("title"));});		
			$("#errorDisplay").animate({top:'0px'},400, function(){$("#errorDisplay").animate({top:'0px'},4000,function(){$("#errorDisplay").animate({top:'-60px'},400)})});
			return bu.attr("title");
		});
	});	
}
function qrProcess(){
	//QRProcess
	$("#resultBox").children("ul").children("li").each(function(i){
		var bu = $(this).children("div.qr").children("a");
		bu.click(function(){
			var qrCookieImage = "<img src='"+bu.attr("title")+"+qrP'>"
			$("#qrImage").html(qrCookieImage);
			$("#qrImageB").attr("href",bu.attr("title")+"+qr11");
			$("#qrImageO").attr("href",bu.attr("title")+"+qr7");
			$("#qrImageK").attr("href",bu.attr("title")+"+qr3");
			$("#popWrapper").fadeIn(200,function(){
				$("#popQR").fadeIn(100);
			});
		});		
	});
}
function feedbackFormControl(){
	$("#mailError").fadeOut(100);
	var valid = true;
	var errorId = new Array();
	var i = 0;
	$(".required").each(function(){
		if($(this).val() == $(this).attr("title")){
			valid = false;
			errorId[i] = $(this).attr("id");
			i++;
		}
	});

	if(valid == false){
		$("#mailError").html("Lütfen Tüm Alanları Doldurunuz.");
		$("#mailError").fadeIn(400);
	}else{
		$.ajax({
			type: 'POST',
			url: 'ajaxmail.php',				
			data: {
				ad : $("#isim").val(),
				eposta : $("#email").val(),
				mesaj : $("#mesaj").val()
			},
			success:function(data){
				var sMailResult = ""+data.mailresult;
				if(sMailResult == "0"){
					$("#mailError").html("Lütfen Bilgilerinizi Kontrol Ederek Tekrar Deneyiniz.");
					$("#mailError").fadeIn(400);
				}else if(sMailResult == "1"){
					$("#popFeedback").slideUp(100,function(){
						$("#popMailConfirm").fadeIn(200);
					});
					$(".required").val("");
				}else{
					$("#mailError").html("Bir Sorun Oluştu. Lütfen Daha Sonra Tekrar Deneyiniz.");
					$("#mailError").fadeIn(400);
				}
			},
			dataType: 'json'
		});
	}
}
