(function($){
   $.input = {
   	  setup: function(form, modus){
   	    var set = $("#"+form+" :input");
   	    f = $("#"+form);
   	    
   	    // Mouseover -out auf Formelemente
   	    /*
	    set.each(function(i,o){
	    	$(o).bind("mouseenter",function(){
  	          $(this).toggleClass("frmOver");
            }).bind("mouseleave",function(){
              $(this).toggleClass("frmActive");
          });
	    });
	    */
        
		//$(o).bind("mouseenter mouseleave", function(){$(this).toggleClass("frmOver");});
		//$(o).bind("focus blur", function(){$(this).toggleClass("frmActive");});
	    
	    
	    // Überprüfen Button einblenden
	    //$("#b1"+form).show();
	    
	    // Speichern
	    f.bind("submit", function(){    	
	    	if($.input.validate(set)){
	    	  $.input.setLoader(form);
	    	  return true
	    	}else{return false;}
	    	return false; 
	    	
	    });

	    // Vorschau (Check)
	    $("#b1"+form).bind("click", function(){
	    	var attr = f.attr("action");
	    	attr = attr.replace(/modus=save/, "modus=check");
	    	f.attr("action", attr);
	    	f.submit();
	    });
	    
	    
	    if(modus=="update"){
	        // Nur wenn Dasboard!
	        if(document.getElementById('actionNew')){
	        
	    	  $("#"+form+", .editTable, #actionNew, #actionBack").toggle();
	        }
	    };
	    
	    $.input.modus = modus; 
	    $.input.form  = f; 
	    
 
	   
   	  },
   	  
   	  showForm: function(id){
   	  	 var url = document.location.href; 
   	  	 if(url.match(/modus=update/)){
   	  	 	document.location.href=url.replace(/update&id=\d+/, "=neu");
   	  	 	return;
   	  	 }
   	  
   	  	 $("#"+id+", .editTable, #actionNew, #actionBack").toggle();
   	  	 $(".systemMessage, .systemError, .systemQuestion").hide();

   	  	 
   	  	 // Wenn in URL modus=update return true (Seite neu laden)
   	  	 
   	  },
   	  
   	  setLoader: function(form){
   	  	$("#"+form+" .button").hide();
   	    $("#"+form+" #loadingSpan").html('<img src="../_default/img/loading.gif" alt="Wait!" /> Bitte etwas Geduld...');
   	  },
   
   	  validate: function(set){
        var ok = 1;
        var f = $.input.form;
        
   	  	set.each(function(i,n){ 		
   	  	  o=$(n);
          var typ = o.data("typ");
         

          if(typ){

      	  // MUST Feld?
      	  var must = o.data("text");
      	  var val = $.trim(o.val());

      	  if(must){
      		if((val==""||val==0) && (typ != 'pw' || (typ=='pw' && $.input.modus !="update"))){
      			alert(must);
      			$.input.highlight(o);
      			ok=0;return false;
      		}
      	  }

      	  // Typ Switchen
      	  if(!val.match(/^\s*$/)){ // Nicht Leer

      	    switch(typ){

              case "checkbox":
      	      break;
      	    				//return this.optional(element) || /^\d+$/.test(value);
      		  case "int":
      		    if(o.type=='checkbox'){return true;}
      		      if(!val.match(/^-?((\d+\.?\d?)|(\.\d+))$/)){
      		      alert(f.data("int"));
      		      ok=0;return $.input.highlight(o);
      	        }
      	      break;
      	
      	      case "email":
      	        if(!val.match(/^[a-zA-Z0-9_\.-]{1,}@[a-z0-9-]{3,}\.[a-z]{2,4}|museum$/)){
      	          alert(f.data("email"));
      		      ok=0;return $.input.highlight(o);
                }
              break;
      
              case "date":
		        var t,m,j,o;
		        var c = val;
		        j = c.substring(6,10);
		        m = c.substring(3,5);
		        d = c.substring(0,2);
		        p = new Date(j,m-1,d);
		        if(p.getDate()==d && p.getMonth()+1==m && p.getFullYear()==j){

		        }else{
		        	alert(f.data("date"));
      		        ok=0;return $.input.highlight(o);
		        }  	      
		      break;
      
		      case "select":
                if(isNaN(val)){
                  alert(f.data("int"));
      		      ok=0;return $.input.highlight(o);
                }
      	      break;
      	      
      	      case "string":
      	        
      	      break;
      	      case "file":
      	        if(!val.match(/^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$/)){
                  alert(form.data("file"));
                  ok=0;return $.input.highlight(o);
                }
      	      break;
      	      
      	      case "pw":
      	    	    if(val !=  $("#"+n.name+"-1").val()){
      	        	alert(f.data("pass"));
      	        	ok=0; return $.input.highlight(o);
      	        }
      	      break;
      	      
      	      case "text":
      	      break;
      	      
      	      case "readonly":
      	      break;
      	      
      	      default:
      	        alert('unbestimmt!!!! "'+typ+'"');
      	      break;
           }
         }
       }
     });
     return (ok)? true:false;
   	 },
   	  
      deleteIt: function(url){
      	var c = confirm('Wollen Sie diesen Datensatz wirklich löschen?');
  	    if(c){window.location.href=url;}
      },
      
      
      highlight: function(o){
      	o.addClass("frmMissing");
      	o.focus();
      	return false; 

      },
      
      doSef: function(v){
    	v = v.toLowerCase();
    	// alles Kleinbuchstaben
    	v = v.replace(/ü/g, "ue"); 
    	v = v.replace(/ö/g, "oe");
    	v = v.replace(/ä/g, "ae");
    	v = v.replace(/ /g, "-");
    	v = v.replace(/[^a-z_\-0-9]/g, "_"); 
    	return v;  	
	}
  }
})(jQuery);


// Mouseover bei den edit Icons
/*
$(document).ready(function(){
  $("a img").bind("mouseenter",function(e){
  	  var a = e.target.src.replace(/0/, '1');
      e.target.src = a;
    }).bind("mouseleave",function(e){
      var a = e.target.src.replace(/1/, '0');
      e.target.src = a;
    });
});
*/
  


// Iframe dynamisch anpassen

// For other good browsers.
$('iframe').load(function()
    {
        this.style.height =
        this.contentWindow.document.body.offsetHeight + 'px';
    }
);

$(document).ready(function()
	{
		// Set specific variable to represent all iframe tags.
		var iFrames = document.getElementsByTagName('iframe');

		// Resize heights.
		function iResize()
		{
			// Iterate through all iframes in the page.
			for (var i = 0, j = iFrames.length; i < j; i++)
			{
				// Set inline style to equal the body height of the iframed content.
				iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
			}
		}

		// Check if browser is Safari or Opera.
		if ($.browser.safari || $.browser.opera)
		{
			// Start timer when loaded.
			$('iframe').load(function()
				{
					setTimeout(iResize, 0);
				}
			);

			// Safari and Opera need a kick-start.
			for (var i = 0, j = iFrames.length; i < j; i++)
			{
				var iSource = iFrames[i].src;
				iFrames[i].src = '';
				iFrames[i].src = iSource;
			}
		}
		else
		{
			// For other good browsers.
			$('iframe').load(function()
				{
					// Set inline style to equal the body height of the iframed content.
					this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
				}
			);
		}
	}
);

