var terytID = 0;


/*
* Dodawanie nowej opcji do "selecta" 
* URL: /admin/reports/tabs1.html
*/
function cloneTabs()
{
	if($('#addnew').val()!='')
	{
		var obj = $('#first').clone().css('display','block').appendTo('.fakeSelect');
		obj.find('.text').html($('#addnew').val());
		obj.find('.value').val($('#addnew').val());
		$('#addnew').val('');
		$('#addnew').focus();
	}
}

function cloneTerytDiv()
{

	terytID++;
	$('#teryt').clone().appendTo('#teryty');

	var new_id = 'ter'+terytID;
	$('#teryty div:last').parent().attr('id',new_id);

	//podmiana nazw selectów
	$("#"+new_id+" select[name='wojewodztwa[]']").attr('id','wojewodztwo_ter'+terytID);
	$("#"+new_id+" select[name='powiaty[]']").attr('id','powiat_ter'+terytID);
	$("#"+new_id+" select[name='gminy[]']").attr('id','gmina_ter'+terytID);
	$("#"+new_id+" select[name='miejscowosci[]']").attr('id','miejscowosc_ter'+terytID);
	$("#"+new_id+" select[name='ulice[]']").attr('id','ulica_ter'+terytID);

	$("#"+new_id+" input[name='name[]']").attr('id','name_ter'+terytID);
	$("#"+new_id+" input[name='numer[]']").attr('id','numer_ter'+terytID);

	$("#"+new_id+" tr[id='ulicadiv']").attr('id','ulicadiv_ter'+terytID);
}



function teryt(type,objID)
{
	var prefix = objID.split('_');	

	$('#'+objID).parent().nextAll().children('select').html('<option value="0">-- Wybierz -- </option>').css('background-color','#eee');
	$.ajax({
		url: "/ajax/teryt.html",
		
		type: "POST",
		data: {
			typ: type,
			id_woj: $("#wojewodztwo"+'_'+prefix[1]).val(),
			id_pow: $("#powiat"+'_'+prefix[1]).val(),
			id_gmi: $("#gmina"+'_'+prefix[1]).val(),
			id_mie: $("#miejscowosc"+'_'+prefix[1]).val(),
			id_ulica: $("#ulica"+'_'+prefix[1]).val()
		},
		
		success: function(html)
		{
			var obj='';
			if(obj = $.parseJSON(html)) 
			{
				var options = '';
				options += '<option value="0">-- Wybierz --</option>';
				for (var i = 0; i < obj.length; i++) {
				options += '<option value="' + obj[i].id + '">' + obj[i].nazwa + ' (' + obj[i].nazdod +')</option>';
				}

        //ukrycie diva z ulicą jeśli taka w danej miejscowości nie występuje
        if(obj.length=='0' && type=='ulica'){
          $('#ulicadiv'+'_'+prefix[1]).hide('slow');
        } else {
          $('#ulicadiv'+'_'+prefix[1]).show('slow');
        }
        
        $('#'+type+'_'+prefix[1]).html(options);
        $('#'+type+'_'+prefix[1]).css('background-color','white');

			}
		}/*,
		error: function(xhr, ajaxOptions, thrownError) {
				$(".error").css("display","none");
				$("#_error_3").css("display","block");
				$("#loader").css({"visibility":"hidden"});
			
		}*/
	});
}

/*
* Pobiera dane z terytu dla danego projektu
* URL: /admin/projects/show.html
*/
function terytProject(type,objID)
{
	var prefix = objID.split('_');	

	$('#'+objID).parent().nextAll().children('select').html('<option value="0">-- Wybierz -- </option>').css('background-color','#eee');
	$.ajax({
		url: "/ajax/terytproject.html",
		
		type: "POST",
		data: {
			typ: type,
			id_log: $("#id_odl").val(),
			id_woj: $("#wojewodztwo"+'_'+prefix[1]).val(),
			id_pow: $("#powiat"+'_'+prefix[1]).val(),
			id_gmi: $("#gmina"+'_'+prefix[1]).val()
		},
		
		success: function(html)
		{
			var obj='';
			if(obj = $.parseJSON(html)) 
			{
				var options = '';
				options += '<option value="0">-- Wybierz --</option>';
				for (var i = 0; i < obj.length; i++) {
				options += '<option value="' + obj[i].id + '">' + obj[i].nazwa + '</option>';
				}

			$('#'+type+'_'+prefix[1]).html(options);
			$('#'+type+'_'+prefix[1]).css('background-color','white');

			}
		}
	});
}


function reloadFilesSelect()
{
	$.ajax({
		url: "/ajax/newsattachments.html",
		type: "POST",
		success: function(html)
		{
			var obj='';
			if(obj = $.parseJSON(html)) 
			{
				var options = '';
				for (var i = 0; i < obj.length; i++) {
				options += '<option value="' + obj[i] + '">' + obj[i] + '</option>';
				}
			
			$("select[name='attachment_select[]']").html(options);

			}
		}
	});

}

//nadaje zdarzenie umozliwiajace kadrowanie zdjecia
function addImgCropping(x1,x2,y1,y2,id) 
{
	$("img#" + id + "")
		.imgAreaSelect({
			remove: true, 
			handles: true
		});
	
	$("img#" + id + "").imgAreaSelect({
			x1: x1,
			y1: y1,
			x2: x2,
			y2: y2,
			resizable: false, 
			handles: true, 
			persistent: true, 
			borderWidth: 2, 
			borderColor1: '#FF0000', 
			instance: true, 
			onSelectEnd: function(img, selection) {
				$("input[name='x1']").val(selection.x1);
				$("input[name='x2']").val(selection.x2);
				$("input[name='y1']").val(selection.y1);
				$("input[name='y2']").val(selection.y2);
			} 
	});
	
	$('input[name=delThumb]').unbind('click').bind('click',function(){
		$("img#" + id + "").imgAreaSelect({
			remove: true
		});
		
		$("#logoMin").html("");
		$("input[name='imgUpload']").val("");
		$("input[name='error']").val("");
		
		return false;
	});
}

// Funkcja aktywuje tooltipy
function addTooltip() 
{
	$("a, div, img").each(function(i){
		var title = $(this).attr("title");

		if(title.length>0) {
			$(this)
				.unbind("mouseover")
				.bind("mouseover",function(){
					Tip(title);
				})
				.unbind("mouseout")
				.bind("mouseout",function(){
					UnTip();
				});
		}
});
	
	$("div.infoLine").each(function(i){
		var contentId = $(this).attr("id");
		
		if(contentId.length>0) {
			$(this)
				.unbind("mouseover")
				.bind("mouseover",function(){
					TagToTip("_" + contentId + "");
				})
				.unbind("mouseout")
				.bind("mouseout",function(){
					UnTip();
				});
		}
	});
}

function verifyMe(){
  $("#galleryFormSubmit").attr('disabled','disabled');
  msg = "";
  if($("#gallery_name").val() == "") msg += "Nazwa galerii\n";
  if($("#gallery_description").val() == "") msg += "Opis galerii\n";
  if(msg!=""){
    alert("Poniższe pola są wymagane:\n"+msg);
    $("#galleryFormSubmit").attr('disabled','');
    return false
  }else{
    if ($('.uploadifyQueueItem').length == 0) {
     /* if($("#gallery_link").val() == ""){
        msg += "Link do galerii lub zdjęcia z dysku\n";
        alert("Poniższe pola są wymagane:\n"+msg);
        return false
      }*/
      $("#gallery_form").submit();
    } else {
      $("#file_upload").uploadifyUpload();
    }
  } 
}
