//ladnowane tylko w adminie /app


$(document).ready(function() {
		
   // Uses the new live method in jQuery 1.3+
    // $('a.delete').live('click', function(event) {
    //       if ( confirm("Czy na pewno?") )
    //           $('<form method="post" action="' + this.href.replace('/delete', '') + '" />')
    //               .append('<input type="hidden" name="_method" value="delete" />')
    //               .append('<input type="hidden" name="authenticity_token" value="' + AUTH_TOKEN + '" />')
    //               .appendTo('body')
    //               .submit();
    // 
    //       return false;
    //   });
    // 
    // 
    // $("input.text:first").each(function(i) {
    // 		$(this).focus();
    // });
    // 
    // $(".focus").focus();         

  // $("tr:odd").css("background-color", "#fff");  
  // 
  // $('form.remote').ajaxForm({
  // 	  target:        '#remote_container'
  // });      

  //$('a[rel*=facebox]').facebox(); 

	$("form:first").append('<img src="/images/progress_bar.gif" style="display:none;" alt="sending..."/>');
	$("form").submit( function () {
		//$(this).FormObserve_save();
	  $('.buttons').html('<img src="/images/progress_bar.gif" alt="sending..." title="sending..."/>');
	  return true;
	} );

    //kopiowanie buttonow
    // if ($(document).height() > $(window).height() + 150) {
    //   $('#options_bottom').html($('.options.top').html());  
    // } else {
    //   $('#options_bottom').hide();
    // }
	

	$.fn.extend({
		//plugin name 
		test1: function(options) {
			return this.each(function() {
				//Assign current element to variable, in this case is UL element
				var obj = $(this);
				
				obj.html('test1');

				// $("li ul", obj).each(function(i) {
				// 	$(this).css('top', $(this).parent().outerHeight());
				// })
				// 
				// $("li", obj).hover(
				// 	function () { $(this).addClass('over'); },
				// 	function () { $(this).removeClass('over'); }
				// );

			});
		}
	});
	
	$('li[data-hover]').live('mouseover', function () {
        $($(this).attr('data-hover')).show();
    }).live('mouseout', function () {
        $($(this).attr('data-hover')).hide(); 
    });
    
    
    //validacja
    
	$('div.formErrorSiteor').each(function(i) {
	    $.validationEngine.buildPrompt('#' + $(this).attr('rel'), $(this).html(),'error')
    });
    
    //autofocus
    
    function hasAutofocus() {
      var element = document.createElement('input');
      return 'autofocus' in element;
    }

    $(function(){
       if(!hasAutofocus()){
         $('input[autofocus=true]').focus();
       }
    });
    
    //placeholder
    
    $.fn.placeholder = function(){   

        function valueIsPlaceholder(input){
          return ($(input).val() == $(input).attr("placeholder"));
        }
        return this.each(function() {  

          $(this).find(":input").each(function(){

            if($(this).attr("type") == "password"){ 

              var new_field = $("<input type='text'>");
              new_field.attr("rel", $(this).attr("id"));
              new_field.attr("value", $(this).attr("placeholder"));
              $(this).parent().append(new_field);
              new_field.hide();

              function showPasswordPlaceHolder(input){
                if( $(input).val() == "" || valueIsPlaceholder(input) ){ 
                  $(input).hide();
                  $('input[rel=' + $(input).attr("id") + ']').show();
                };
              };

              new_field.focus(function(){
                $(this).hide();
                $('input#' + $(this).attr("rel")).show().focus();
              });

              $(this).blur(function(){
                 showPasswordPlaceHolder(this, false);
              });

              showPasswordPlaceHolder(this); 

            }else{

              // Replace the value with the placeholder text. 
              // optional reload parameter solves FF and IE caching values on fields.
              function showPlaceholder(input, reload){
               if( $(input).val() == "" || ( reload && valueIsPlaceholder(input) ) ){ 
                 $(input).val($(input).attr("placeholder"));
               }
              };

              $(this).focus(function(){
                if($(this).val() == $(this).attr("placeholder")){
                  $(this).val("");
                };
              });

              $(this).blur(function(){
                 showPlaceholder($(this), false)
              });


              showPlaceholder(this, true); 
            };
          });

          // Prevent forms from submitting default values
          $(this).submit(function(){  
            $(this).find(":input").each(function(){
              if($(this).val() == $(this).attr("placeholder")){
                $(this).val("");
              }          
            });
          });

        });  
      };
    
    
    $('a.l'+'m'+'a'+'i'+'l').each(function(i) {
        e = $(this).attr('rel');e = replaceAll(e, '.', '');e = replaceAll(e, '@', '');e = replaceAll(e, 'L'+'L'+'L', '@');e = replaceAll(e, 'L'+'L', '.');e = replaceAll(e, 'L', '');
	    $(this).attr('href', 'mai'+'lto:'+e).html(e);
    });
    
    
                            

});  

function set_focus() {
  $("input.text:first").each(function(i) {
		$(this).focus();
  }); 
};
                                                                           
function replaceAll(str, from, to) {   
	if (from == to) {
		return str;
	}                                                                   
  var idx = str.indexOf(from);    
   while ( idx > -1 ) {                                                                                    
      str = str.replace(from, to);                                                                      
      idx = str.indexOf(from);    
  }                                                                                                       
   return str;                                                                                             
};

/* funkcja zmienia nazwę na url - likwidujac spacje, kropke i polskie znaki */
function suggest_url(id_from, id_to) {   
   if ($('#'+id_to).val().length == 0 && $('#'+id_from).val().length > 0) {	  	
         var tmp = replaceAll($('#'+id_from).val().toLowerCase(), " ", "-"); 
         //tmp = replaceAll(tmp, ".", "-"); 
         var pl = new Array ("ą","ż","ś","ź","ę","ć","ń","ó","ł");                   
         var uni = new Array ("a","z","s","z","e","c","n","o","l");                        
         for (i = 0; i < pl.length; i++) tmp = replaceAll(tmp, pl[i], uni[i]);             
         tmp = replaceAll(tmp, /[^a-z1-9\+\-\.]/, "+");                                    
         $('#'+id_to).val(tmp);                                                        
   }       
} //suggest_url   

function window_close() {     
	window.parent.location.reload();  
    window.parent.$.fancybox.close();
	//window.parent.Shadowbox.close();
	return false;
};


//TERAZ NALEZY UZYWAC form_details_v2 - poniżej !!!!
function form_details(i) {
		var form_last_no = i; 
		var form_detail_content = $('#detail_'+i).html(); 
		if (i == 0) {            
			//jeśli nie ma jeszcze zadnych details to wyświetlamy pierszy
			$('#detail_'+i).remove();
	  	var form_detail_nr = i; 
    } else {                                                       
	  	var form_detail_nr = i;
		  $('#detail_'+i).remove();  
    }
		add_next_detail = function() { 			
			new_content = replaceAll(form_detail_content, '_'+form_last_no+'_', '_'+form_detail_nr+'_');
			new_content = replaceAll(new_content, 'detail_'+form_last_no, 'detail_'+form_detail_nr);
			new_content = replaceAll(new_content, '['+form_last_no+']', '['+form_detail_nr+']');				
			$('#add_next').before('<div id="detail_'+form_detail_nr+'" style="display:none;">'+new_content+'</div>');
			$('#detail_'+form_detail_nr).slideDown();
			form_detail_nr += 1;
		}                    
};      


//MS: funkcja do nested_form (przykład w articles/_form)
function form_details_v2(i) {
		var form_last_no = i; 
		var form_detail_content = $('#detail_'+i).html(); 

		$('#detail_'+i).remove();
  		var form_detail_nr = i; 

		add_next_detail = function(type) { 			
			new_content = replaceAll(form_detail_content, '_'+form_last_no+'_', '_'+form_detail_nr+'_');
			new_content = replaceAll(new_content, 'detail_'+form_last_no, 'detail_'+form_detail_nr);
			new_content = replaceAll(new_content, '['+form_last_no+']', '['+form_detail_nr+']');	
			new_content = replaceAll(new_content, '('+form_last_no+')', '('+form_detail_nr+')');	 			 

			$('#details').append('<li id="detail_'+form_detail_nr+'" style="display:none;">'+new_content+'</li>');

			$('#field_'+type+'_'+form_detail_nr+'_').show(); 
			$('.field_'+type+'_'+form_detail_nr+'_').show(); 

			$('#detail_'+form_detail_nr).slideDown();  
			//$('#details').height($('#details').height()+90);    

			$('#form_form_fields_attributes_'+form_detail_nr+'_field_type').val(type);

			//show_properties(form_detail_nr);

			form_detail_nr += 1;
		};

};  




         
          
//funkcja klonuje div "what" do diva "to"
function clone_buttons(to, what) {
	$(to).prepend('<div class="buttons buttons_top">' + $(what).html() + '</div>');
}





// function show_search() {		
// 	if ($('table').length > 0) {
// 		$('table tbody tr').quicksearch({
// 		  position: 'append',
// 			'loaderText': '',
// 			attached: '#top_bar_content',
// 			labelText: '',
// 			delay: 50,
// 			focusOnLoad: true,
// 			inputClass: "text search"					
// 		});			         
// 	}
//}




function show_index_options() {
    $('.right_box_hide').show();$('#right_box').show();$('#show_index_options_id').hide();$('#zwezacz').addClass('narrower');$.cookie('show_options', 'yes');
    return false;
}

function hide_index_options() {
    $('.right_box_show').show();$('#right_box').hide();$('#hide_index_options_id').hide();$('#zwezacz').removeClass('narrower');$.cookie('show_options', 'no');    
    return false;
}

