/*__>[trim]_______________________________*/
function trim(text) {
	return text.replace(/^\s+|\s+$/g,"");
}
function ltrim(text) {
	return text.replace(/^\s+/,"");
}
function rtrim(text) {
	return text.replace(/\s+$/,"");
}

/*__>[removeItems from an array]_______________________________*/
//var theArray = [17,2,5,1];
//var toRemove = [17,3];
// var toRemove = 5;
//theArray.removeItems(toRemove);
// adding it as a prototype object enables it to be used from any array
Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}

/*__>[THUMB GUNCELLE]_______________________________*/
function thumbGuncelle(thumbID, thumbURL, type)
{
	if(type=="yeni"){
		tempFoto.push(thumbURL);
		$('#fileUploadThumb'+thumbID).html('<img src="fotoilan/'+ thumbURL + '" width="100" height="75" />');
		$('#uyeIlanEkleResimSil'+thumbID).show();
		$('#firstImage_'+thumbID).show();
	}else if(type=="sil"){
		$('#fileUploadThumb'+(thumbID)).html('<img src="images/fotoekle_buton.gif" width="100" height="75" />');
	}else{
		$('#fileUploadThumb'+(thumbID+1)).html('<img src="fotoilan/'+ thumbURL + '" width="100" height="75" />');
		$('#uyeIlanEkleResimSil'+(thumbID+1)).show();
		$('#firstImage_'+(thumbID+1)).show();
	}
	return false;
}

/*__>[JQUERY]_______________________________*/
$(document).ready(function(){
    /*
     * chainSelect for combo
     */
    $('#cKategori').chainSelect('#cMarka', 'JSON_yeniIlanKaydi4ChainedCombo.php', {
        before: function(target){
            $("#loading").css("display", "block");
            $(target).slideUp(150);
            $('#cModel').slideUp(100);
            $('#cTip').slideUp(50);
            $('#katSecimBitir').fadeOut(50, function() {             	            	 $(target).prev("div").show();            	            });                       
        },
        after: function(target){
            $("#loading").css("display", "none");$('.loading_text').hide();
            $(target).slideDown(200);						
        }
    });

    $('#cMarka').chainSelect('#cModel', 'JSON_yeniIlanKaydi4ChainedCombo.php', {
        before: function(target){
            $("#loading").css("display", "block");
            $(target).slideUp(150, function() {             	            	 $(target).prev("div").show();            	            });
            $('#cTip').slideUp(100);
            $('#katSecimBitir').fadeOut(50);
        },
        after: function(target){
            $("#loading").css("display", "none");$('.loading_text').hide();
            $(target).slideDown(200);
        },                		site: function() { return $("#cKategori :selected").val(); }
    });
    
    $('#cModel').chainSelect('#cTip', 'JSON_yeniIlanKaydi4ChainedCombo.php', {
        before: function(target){
            $("#loading").css("display", "block");
            $(target).slideUp(100);
            $('#katSecimBitir').fadeOut(50, function() {             	            	 $(target).prev("div").show();            	            });
        },
        after: function(target){
            $("#loading").css("display", "none");$('.loading_text').hide();
            $(target).slideDown(200);
        }
    });
    $('#cTip').click(function(){
        $('#katSecimBitir').fadeIn(300);
    });
    
	$("option:first", "#cKategori").attr( "selected", "selected" );
	$("#cKategori").change();
	/*
	$("option:first", "#cMarka").attr( "selected", "selected" );
	$("#cMarka").change();
*/
    /*
     * katSecimBitir
     */


// BUNLAR DA YENİ JQ - BASLA
// Boyama
	$('#boyama_r0').click(function()
	{
		$('#boyama1').fadeOut(100);
		$('#boyama_uyari').fadeOut(100);
	});

	$('#boyama_r2').click(function()
	{
//		$('#boyama1').val("");
		$('#boyama1').fadeOut(100);
		$('#boyama_uyari').fadeOut(100);
	});

	$('#boyama_r1').click(function()
	{
		$('#boyama1').fadeIn(100);
		$('#boyama1').focus();
		$('#boyama_uyari').fadeIn(100);
	});

// Değişen
	$('#degisen_r0').click(function()
	{
		$('#degisen1').fadeOut(100);
		$('#degisen_uyari').fadeOut(100);
	});

	$('#degisen_r2').click(function()
	{
//		$('#degisen1').val("");
		$('#degisen1').fadeOut(100);
		$('#degisen_uyari').fadeOut(100);
	});

	$('#degisen_r1').click(function()
	{
		$('#degisen1').fadeIn(100);
		$('#degisen1').focus();
		$('#degisen_uyari').fadeIn(100);
	});



// BUNLAR DA YENİ JQ - BİTİR

	/*
     * foto
     */
    $("#fileUploadAll").fileUpload({
        'uploader': 'images/jQuploadify.swf',
        'cancelImg': 'images/cancelUpload.png',
        'folder': 'fotoilan',
        'script': 'jQuploadFoto.php',
        'fileDesc': 'Resim Dosyalari',
        'fileExt': '*.jpg;*.jpeg;',
		'maxFileSize': 10485760,
        'multi': true,
        'auto': true,
		'simUploadLimit': 1,
		'sid':sid
    });


/*__>[toggle]_______________________________*/
	$("#togglePasifTable").click(function(){
		$("#pasifTable").slideToggle('normal');
	});

/*__>[uyeYeniAracEkle resim sil]_______________________________*/
    $(".uyeIlanEkleResimSil").click(function(){
		$(".uyeIlanEkleResimSil").fadeOut(100);
		$(".firstimage_con").fadeOut(100);
        var BoxId = $(this).attr("rel");
		var resimId= BoxId-1;
		var resimAd= tempFoto[resimId];
		
		
		$.ajax({
		  url: "uyeIlanResimSil.php",
		  cache: false,
		  type: "POST",
		  data: "rn="+resimAd,
		  success: function(html){
			if(trim(html)=='1'){ // php silindi
				thumbGuncelle(BoxId, '', 'sil');
				tempFoto.removeItems(resimAd);
				
				if(resimId<tempFoto.length){
					for(j=resimId;j<tempFoto.length;j++){
						thumbGuncelle(j, tempFoto[j], ' ');
					}
					thumbGuncelle(tempFoto.length+1, '', 'sil');
				}
				
				
				
			}else{
				alert('Resim silinemedi!');	
			}
			$(".uyeIlanEkleResimSil:lt("+tempFoto.length+")").fadeIn(200);
			$(".firstimage_con:lt("+tempFoto.length+")").fadeIn(200);
			$("#fileUpload").fileUpload();
			
		  }
		});
    });
/*__>[uyeYeniAracEkle resim ilk sira]_______________________________*/	
	$(".firstimage").click(function(){
        var BoxId = $(this).val();
		var resimId= BoxId-1;
		var _tempFoto = tempFoto;
		var resimAd= tempFoto[resimId];
		var old  = tempFoto[0];
		
		$.ajax({
		  url: "uyeIlanResimUpdate.php",
		  cache: false,
		  type: "POST",
		  data: "rn="+resimAd,
		  success: function(html){
			 tempFoto[0]=resimAd;
			 thumbGuncelle(0, resimAd , ' ');
			 for(j=1;j<tempFoto.length;j++){
				if(j==resimId){
					thumbGuncelle(resimId, old, ' ');
					tempFoto[resimId]=old;
				}else{
					thumbGuncelle(j, tempFoto[j], ' ');
				}
			 } 
			 
			$("#firstimage_c_1").attr("checked", 'checked'); 
			$("#fileUpload").fileUpload();
		  }
		});
    });

	
});

 
//__________________________________________________
