$(document).ready(function()
{
	/* init Events in content and bubbles */
	var thisParent = thisParent || $("body");
	initEvents(thisParent);
});


/* Init basic events */
function initEvents(thisParent)
{
	initSelects(thisParent);
	initFormCopy(thisParent);
}

// Select Felder ersetzen
function initSelects(thisParent)
{
  if(!thisParent)
  { var thisParent = $("body"); }
 
  thisParent.find("select").each(function()
  {
    var thisSelect = $(this);
	
	//* wr+ Klassenattribut des Html-Selects soll in das Javascript-Select übernommen werden
	var selectClass = " "+thisSelect.attr("class");
	
    // Init Fakeselect 
	// wr+ thisSelect.before("<div class=\"select\"><div class=\"value\"></div></div>");
    thisSelect.before("<div class=\"select"+selectClass+"\"><div class=\"value\"></div></div>");
    var thisFSelect = thisSelect.prev();
    
    // alert(thisSelect);
	
    thisFSelect.css({
      width:              thisSelect.width(),
      position:           "absolute",
      zIndex:             "1",
      display:            "inline",
      marginTop:          "-1",
      backgroundRepeat:   "no-repeat",
      backgroundPosition: "right top",
      cursor:             "default",
      MozUserSelect:      "none",
      KhtmlUserSelect:    "none"
    });
    if(document.all)
    { thisFSelect.attr("unselectable","on"); }
    
    var thisValue = thisFSelect.children(".value");
    thisValue.css({
      width:       thisValue.width() + "px", // Auto -> fixed
      height:      thisFSelect.css("height"),
      lineHeight:  thisFSelect.css("height"),
      position:    "relative",
      overflow:    "hidden",
      whiteSpace: "nowrap",
      textIndent:  "4px"
    });

    if(thisSelect.children("option[selected]").html())
    { thisValue.html(thisSelect.children("option[selected]").html()); }
    else
    { thisValue.html(thisSelect.children("option:first").html()); }
    thisValue.after("<div class=\"options\"></div>");
    thisSelect.css("visibility", "hidden");
    var thisOptions = thisValue.next();
    thisOptions.css({
      left:       "-" + thisFSelect.css("border-left-width"),
      position:   "absolute",
      zIndex:     "1",
      overflow:   "auto",
      whiteSpace: "nowrap",
      textIndent: "4px"
    });

    thisOptions.html( thisSelect.html().replace(/\n/gi, "").replace(/\r/gi, "").replace(/<option/gi, "<div class=\"option\"").replace(/<\/option>/gi, "</div>\n").replace(/ value=".*"/gi, "").replace(/<!--.*-->/gi, ""));
    thisOptions.children(".option").css({
      position: "relative",
      float: "left",
      clear: "left",
      overflow: "visible"
    });

    var longestOption = 0;
    thisOptions.children(".option").each(function(){
      var thisOption = $(this);
      if(longestOption < thisOption.width())
      { longestOption = thisOption.width(); }
    });

    thisOptions.children(".option").css("float", "none");
    
    var scrollWidth = 0;
    if(thisOptions.height() > 200)
    { 
      scrollWidth = 25;
      thisOptions.height(200); 
    }
    thisOptions.width("500%");
    if(thisOptions.width() > longestOption)
    { thisOptions.width(longestOption + scrollWidth); }
    if(thisOptions.width() < thisFSelect.width())
    { thisOptions.width("100%"); }
    thisOptions.hide(0);
    
    thisFSelect.click(function(){ fakeSelect($(this)); return false; });
    thisOptions.children(".option").click(function(){ fakeSelectChoice($(this)); return false; });
    $(document).click(function(){ $(".options").slideUp(100); });
  });
}

// Select Feld Dropdowns
function fakeSelect(thisFSelect)
{
  var thisOptions = thisFSelect.children(".options");
  var thisValue = thisFSelect.children(".value");
  if(thisOptions.is(":hidden"))
  {
    $(".options").slideUp(100);
  
    $(".select").css("z-index", 1);
    thisFSelect.css("z-index", 10);
    thisOptions.slideDown(100);
  }
  else
  { thisOptions.slideUp(100); }
    
}

// Select Feld Auswahl
function fakeSelectChoice(thisOption)
{
  var thisOptions = thisOption.parent(".options");
  var thisFSelect = thisOptions.parent(".select");
  var thisValue = thisOptions.siblings(".value");
  var thisSelect = thisFSelect.next();
  var thisOptionIndex = thisOptions.children(".option").index(thisOption);

  thisOptions.children(".option").removeClass("selected");
  thisOption.addClass("selected");
  thisValue.html(thisOption.html());
  thisSelect.get(0).selectedIndex = thisOptionIndex;
  thisSelect.trigger("change");
  thisSelect.trigger("onclick");
  thisOptions.slideUp(100);

}


function initFormCopy(thisParent)
{
  if(!thisParent)
  { var thisParent = $("body"); }
  else
  { var thisParent = thisParent; }
  
  thisParent.find("button.control.add").click(function()
	{
    var thisButton = $(this);
    var thisControl = thisButton.parent(".toggle");
    var thisPerson = thisParent.find(".person:last").clone();
    var newControl = thisControl.clone();
    var newButton = newControl.children("button");
    
    thisPerson.find("select,input").each(function()
    {
      $(this).removeAttr("selected");
      $(this).removeClass("selected");
      $(this).removeAttr("value");
      var thisCounter = parseInt($(this).attr("name").match(/\_(\d)/)[1]);
           
      $(this).attr("name", $(this).attr("name").replace(/_\d/, "_" + (thisCounter + 1)));      
      $(this).attr("id", $(this).attr("id").replace(/_\d/,  "_" + (thisCounter + 1)));         
    });
    
    thisPerson.find("label").each(function()
    { 
      var thisCounter = parseInt($(this).attr("for").match(/\_(\d)/)[1]);
      $(this).attr("for", $(this).attr("for").replace(/_\d/,  "_" + (thisCounter + 1)));
    });

    newButton.html("Person löschen");
    newButton.attr("class", "control delete");
    thisControl.before(thisPerson);
    initSelects(thisPerson); 
    
    thisPerson.before(newControl); 
    initFormCopy(newControl);
      		
		return false;
	});
  thisParent.find("button.control.delete").click(function()
	{
    var thisButton = $(this);
    var thisControl = thisButton.parent(".toggle");
    var thisPerson = thisControl.next(".person");
    
    thisPerson.remove();
    thisControl.remove();
      		
		return false;
	});
	
	$("#button_close").click(function(){
    window.self.close();
  });
}

