function fileQueueError(file, errorCode, message) {
	try {
		var imageName = "/_img/error.gif";
		var errorName = "";
		if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {errorName = "You have attempted to queue too many files.";}
		if (errorName !== "") {alert(errorName);return;}
		switch (errorCode) {
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:imageName = "/_img/zerobyte.gif";break;
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:	imageName = "/_img/toobig.gif";	break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			case -100:alert('En fazla bir adet resim gönderebilirsiniz!');break;
			default:alert(file +' '+ errorCode +' '+ message);break;
		}
		//addImage("/_img/" + imageName);
	} catch (ex) {this.debug(ex);}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {try {if (numFilesQueued > 0) {this.startUpload();}} catch (ex) {this.debug(ex);}}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.setProgress(percent);
		if (percent === 100) {progress.setStatus("thumbnail oluşturuluyor...");progress.toggleCancel(false, this);} 
		else {progress.setStatus("Yükleniyor...");progress.toggleCancel(true, this);}
	} catch (ex) {this.debug(ex);}
}

function uploadSuccess(file, serverData) {
	try {
		//addImage("/_ajax/thumbnail.php?id=" + serverData);
		AddHtml("/_ajax/thumbnail.php?id=" + serverData,serverData,this.customSettings.upload_thumbId,this.customSettings.swfu_name,this.customSettings.thumb_size,this.customSettings.form_nesne);
		var progress = new FileProgress(file,  this.customSettings.upload_target);
		progress.setStatus("Thumbnail Oluşturuldu.");
		progress.toggleCancel(false);
	} catch (ex) {this.debug(ex);}
}

function uploadComplete(file) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued > 0) {this.startUpload();} 
		else {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus("All images received.");
			progress.toggleCancel(false);
			// drag ozelligi ac
			//initGallery();
		}
	} catch (ex) {this.debug(ex);}
}

function uploadError(file, errorCode, message) {
	var imageName =  "/_img/error.gif";
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("Cancelled");
				progress.toggleCancel(false);
			}
			catch (ex1) {this.debug(ex1);}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				progress = new FileProgress(file,  this.customSettings.upload_target);
				progress.setCancelled();
				progress.setStatus("Stopped");
				progress.toggleCancel(true);
			}
			catch (ex2) {this.debug(ex2);}
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			imageName = "/_img/uploadlimit.gif";
			break;
		default:
			alert(message);
			break;
		}
		addImage("/_img/" + imageName);
	} catch (ex3) {this.debug(ex3);}
}

function AddHtml(src,idd,thumbsId,swfu,size,form_nesne){
	if(!thumbsId)thumbsId = 'thumbnails';
	divsource = document.getElementById(thumbsId).innerHTML;
	//yenikod = ' <div id="th'+idd+'" class="progressContainer blue" style="width:100px;margin:5px;float: left;"><img id="thi'+idd+'" src="'+src+'" border=0 ><br><a href="javascript:resimCikar(\''+idd+'\')">resmi sil</a></div>';
	//yenikod = '<div class="imageBox" id="'+idd+'"><div id="bg'+idd+'" class="imageBox_theImage" style="background-image:url(\''+src+'\')"></div> <div class="image_label"><span><a href="javascript:resim_duzenle(\''+idd+'\',\''+size+'\',\''+form_nesne+'\');">düzenle</a> | <a href="javascript:resim_sil(\''+idd+'\',\''+thumbsId+'\',\''+swfu+'\',\''+form_nesne+'\');">sil</a></span></div></div>';	
    yenikod = '<div class="imageBox" id="'+idd+'"><div id="bg'+idd+'" class="imageBox_theImage" style="background-image:url(\''+src+'\')"></div> <div class="image_label"><span><a href="javascript:resim_sil(\''+idd+'\',\''+thumbsId+'\',\''+swfu+'\',\''+form_nesne+'\');">sil</a></span></div></div>';   
	//document.getElementById(thumbsId).innerHTML = divsource + yenikod;
	document.getElementById(thumbsId).innerHTML = yenikod;
	$("#"+form_nesne).val('session:'+idd);	
}

function addImage(src) {
	var newImg = document.createElement("img");
	newImg.style.margin = "5px";
	document.getElementById("thumbnails").appendChild(newImg);
	if (newImg.filters) {
		try {newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;} 
		catch (e) {newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';}
	} else {newImg.style.opacity = 0;}
	newImg.onload = function () {fadeIn(newImg, 0);};
	newImg.src = src;
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;	// 15 fps
	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {opacity = 100;}
		if (element.filters) {
			try {element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;} 
			catch (e) {element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';}
		} else {element.style.opacity = opacity / 100;}
	}
	if (opacity < 100) {setTimeout(function () {fadeIn(element, opacity);}, rate);}
}

/* ******************************************
 *	FileProgress Object
 *	Control object for displaying file info
 * ****************************************** */

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress"+targetID;

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;
		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));
		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));
		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";
		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);
		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = file.name;
	}
	this.height = this.fileProgressWrapper.offsetHeight;
}

FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.style.display = 'block';
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";
	this.fileProgressElement.style.display = 'none';
};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
};
FileProgress.prototype.setStatus = function (status) {
	//this.fileProgressElement.childNodes[2].innerHTML = status;
};
FileProgress.prototype.SetOpen = function(status) {
	this.fileProgressElement.style.display = 'block';
};
FileProgress.prototype.SetClose = function(status) {
	this.fileProgressElement.style.display = 'none';
};
FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function () {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

function ajax(){
    var xmlHttp=null;
    try{xmlHttp=new XMLHttpRequest();}
    catch (e){try{xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');} catch (e){xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');}}return xmlHttp;
}

function resimEkle(idd){
	var formm = document.forms['frmForm'].frm_resimler;
	var resm = formm.value;
	if(resm.length>1)resm = resm +';'+idd;
	else resm = idd;
	formm.value = resm;	
}

function resim_sirala_form(idd){
	var formm = document.forms['frmForm'].frm_resimler;
	var resm = formm.value;
	var myArray = resm.split(";");
	var mystr = '';
	var ayrac = '';
	for (var i = 0; i < myArray.length; i++){
		if(myArray[i] != idd && myArray[i].length>1){
			mystr = mystr + ayrac + myArray[i];
			ayrac = ';';
		}
	}
	formm.value = mystr;
}

function resim_sil(idd,thumbsId,swfu){
	if(!thumbsId)thumbsId = 'thumbnails';
	xmlHttp=ajax();
	if(xmlHttp==null){alert ('Tarayıcınız Ajax Desteklemiyor!');return;}
	var rnd982g = Math.random(); 
	var url='/_ajax/img_del.php';
	var sc='id='+idd+'&rnd982g='+rnd982g;
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	xmlHttp.setRequestHeader('Content-length', sc.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4 && xmlHttp.status == 200){debug_sonuc(xmlHttp.responseText);}};
	xmlHttp.send(sc);
	//document.getElementById(idd).style.display="none";	
  	var d = document.getElementById(thumbsId);
  	var olddiv = document.getElementById(''+idd+'');
  	d.removeChild(olddiv);	
	//resim_sirala_form(idd);
	//eval("alert("+swfu+".getStats().successful_uploads)");
	eval("var newUploadLimit = "+swfu+".getStats().successful_uploads+1;");
	eval(swfu+".setFileUploadLimit("+newUploadLimit+");");
	eval(swfu+".setFileQueueLimit(1);");
    $('#frm_avatar_url').val('');
}

function resim_duzenle(idd,size,nesne){
	//jQuery.facebox(function() {jQuery.get('/_ajax/img_crop.php', function(data) {jQuery.facebox(data);})});
	//var img = document.getElementById('img_ana');
	//var thm = document.getElementById('thumbnail_view');
	//img.src = "/_ajax/img800x600.php?id=" + idd;
	//thm.src = "/_ajax/img800x600.php?id=" + idd;
	//jQuery.facebox("<IFrame Name = 'IFrame1' Id ='dynFrame' Width = '920' Height = '650' Src = '/_ajax/img_crop.php?id="+idd+"&size="+size+"' marginwidth = '0' marginheight ='0' frameborder = '0' vspace = '0' hspace = '0'></IFrame>");
	
	window.open("/_ajax/img_crop.php?id="+idd+"&size="+size+"&form_nesne="+nesne+"","rDuzenle","menubar=no,width=950,height=500,scrollbars=1,toolbar=no,fullscreen=yes");
	//window.open("win1.html","Window1","menubar=no,width=430,height=360,toolbar=no");
	//$('#img_ana').imgAreaSelect({ aspectRatio: '1:1.3333', onSelectChange: preview }); 	
}

function debug_sonuc(mesaj){	
	if(document.getElementById('debug')){
		var d = document.getElementById('debug');
		//d.innerHTML = mesaj;
	}
}

function gonder(data){
	var formm = document.forms['frmForm'].frm_resimler;
	//formm.value = data;
	xmlHttp=ajax();
	if(xmlHttp==null){alert ('Tarayıcınız Ajax Desteklemiyor!');return;}
	var rnd982g = Math.random(); 
	var url='/_ajax/img_order.php';
	var sc='ord='+data+'&rnd982g='+rnd982g;
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
	xmlHttp.setRequestHeader('Content-length', sc.length);
	xmlHttp.setRequestHeader('Connection', 'close');
	xmlHttp.onreadystatechange=function() { if (xmlHttp.readyState==4 && xmlHttp.status == 200){debug_sonuc(xmlHttp.responseText);}};
	xmlHttp.send(sc);	
}
