var rollerTimeout;

$(document).ready(function()
{
  initEvents();
});

function initEvents()
{
  initForms();
  initNavi();
  initLangsel();
  initItemDetailPopups();
}

function initForms()
{
  initButtons();
  initSelects();
  
  $("input,select,.select").focus(function(){ $(this).removeClass("error"); $(this).parents("label").removeClass("error"); });
  $("input,select,.select").click(function(){ $(this).removeClass("error"); $(this).parents("label").removeClass("error"); });
}

// Button Resizer (IE)
function initButtons()
{
  if(document.all) // ONLY FOR IE //
  {
    for(var i = 0; i < $("button").size(); i++)
    {
      var thisButton = $("button:eq(" + i + ")");
      if(thisButton.children("strong").html())
      { 
        var thisArea = thisButton.children("strong");
        var outerWidth = thisArea.width() + parseInt(thisArea.css("padding-right").replace(/px/, "")) + parseInt(thisArea.css("padding-left").replace(/px/, ""));
        thisButton.css("width", outerWidth + "px"); 
      }
    }
  }
}

// Select Felder ersetzen
function initSelects(thisParent)
{
  if(!thisParent)
  { var thisParent = $("body"); }
 
  for(var i = 0; i < thisParent.find("select").size(); i++)
  {
    var thisSelect = thisParent.find("select:eq(" + i + ")");
    // Init Fakeselect 
    thisSelect.before("<div class=\"select\"><div class=\"value\"></div></div>");
    var thisFSelect = thisSelect.prev();
    
    thisFSelect.parent().css("z-index", "1");

    thisFSelect.css("width",               Math.round(thisSelect.width()) + 4);
    thisFSelect.css("position",            "absolute");
    thisFSelect.css("left",                Math.round(thisSelect.offset().left - thisSelect.parent().offset().left));
    thisFSelect.css("z-index",             "1");
    thisFSelect.css("display",             "inline");
    thisFSelect.css("margin-top",          "-1px");
    thisFSelect.css("background-repeat",   "no-repeat");
    thisFSelect.css("background-position", "right center");
    thisFSelect.css("cursor",              "default");
/*
    thisFSelect.css("border-top-color",        thisSelect.css("border-top-color"));
    thisFSelect.css("border-right-color",        thisSelect.css("border-right-color"));
    thisFSelect.css("border-bottom-color",        thisSelect.css("border-bottom-color"));
    thisFSelect.css("border-left-color",        thisSelect.css("border-left-color"));
*/
    
    thisFSelect.css("MozUserSelect","none");
    thisFSelect.css("KhtmlUserSelect","none");
    if(document.all)
    { thisFSelect.attr("unselectable","on"); }
    
    var thisValue = thisFSelect.children(".value");
    thisValue.css("width",       thisValue.width() + "px"); // Auto -> fixed
    thisValue.css("height",      thisFSelect.css("height"));
    thisValue.css("line-height", thisFSelect.css("height"));
    thisValue.css("position",    "relative");
    thisValue.css("overflow",    "hidden");
    thisValue.css("white-space", "nowrap");
//    thisValue.css("text-indent", thisFSelect.css("padding-left"));

    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("height",        "auto");
    thisOptions.css("left",        "-" + thisFSelect.css("border-left-width"));
    thisOptions.css("position",    "absolute");
    thisOptions.css("z-index",     "1");
    thisOptions.css("overflow",  "auto");
    thisOptions.css("white-space", "nowrap");
    thisOptions.css("padding", "0");

    var longestOption = 0;
    for(var j = 0; j < thisSelect.children("option").size(); j++)
    {
      
      thisOptions.html(thisOptions.html() + "<div class=\"option\">" + thisSelect.children("option:eq(" + j + ")").html() + "</div>");
      var thisOption = thisOptions.children(".option:last");
      thisOption.css("position", "relative");
      thisOption.css("float", "left");
      thisOption.css("clear", "left");
      thisOption.css("overflow", "visible");
      if(longestOption < thisOption.width())
      { longestOption = thisOption.width(); }
      thisOption.css("float", "none");
      thisOption.css("padding-left", thisFSelect.css("padding-left"));
    }
    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() + Math.round(thisFSelect.css("padding-left").replace(/px/, "")) )
    { thisOptions.width(thisFSelect.width() + Math.round(thisFSelect.css("padding-left").replace(/px/, ""))); }
    thisOptions.hide(0);
    
//    thisFSelect.css("padding", "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 initTextareas()
{
  $("textarea").each(function()
  {
    var thisString = "Bitte Text eingeben...";
    if($(this).html() == "")
    { $(this).html(thisString); }
    $(this).focus(function()
    {
      if($(this).html() == thisString)
      { $(this).html(""); }
    });
  });
}


function initNavi()
{
  /// IMGSWAP - METANAVI ///
  $("#metanavi ul li a img,#langsel ul li a img").each(function()
  {
    var thisImg = $(this);
    var thisSrc = thisImg.attr("src");
    var cacheImg = new Image();
    cacheImg.src = thisSrc.replace(/_off\./, "_on.");
    thisImg.parents("a").mouseover(function()
    { thisImg.attr("src", cacheImg.src); });
    thisImg.parents("a").mouseout(function()
    { thisImg.attr("src", thisSrc); });
    thisImg.removeAttr("title");
    thisImg.removeAttr("alt");
  });

  /// ROLLOVER-LAYER ///
  $("#navi>ul>li>a,#navi>ul>li>strong>a").each(function()
  {
    var thisLink = $(this);
    var thisParent = thisLink.parents("li");
    thisLink.removeAttr("onmouseover");
    thisLink.removeAttr("onmouseout");
    thisLink.unbind("onmouseover");
    thisLink.unbind("onmouseout");
    thisLink.unbind("mouseover");
    thisLink.unbind("mouseout");
    thisParent.append("<ul></ul>");
    var thisRoller = thisParent.children("ul");
    thisRoller.hide();
    thisRoller.load(thisLink.attr("href") + " #subnavi ul>*");
    thisLink.mouseover(function()
    { 
      var thisParent = $(this).parents("li");
      var thisRoller = thisParent.children("ul");
      thisLink.addClass("over");
      over(thisLink.children("img").attr("id"));
      if(rollerTimeout)
      { window.clearTimeout(rollerTimeout); }
      $("#navi ul li ul").not(thisRoller).css("z-index", 1);
      $("#navi ul li ul:visible").not(thisRoller).slideUp(400, function(){ if($(this).siblings("a").html()){ out($(this).siblings("a").children("img").attr("id")); $(this).siblings("a").removeClass("over"); } });
      thisRoller.css("z-index", 2);
      thisRoller.slideDown(400); 
    });
    thisParent.children("*").mouseover(function()
    { 
      if(rollerTimeout)
      { window.clearTimeout(rollerTimeout); }
    });
    thisLink.mouseout(function()
    { rollerTimeout = window.setTimeout(function(){ thisRoller.slideUp(300, function(){ if($(this).siblings("a").html()){ out($(this).siblings("a").children("img").attr("id")); $(this).siblings("a").removeClass("over"); } }); }, 400); });
    thisRoller.mouseout(function()
    { rollerTimeout = window.setTimeout(function(){ thisRoller.slideUp(300, function(){ if($(this).siblings("a").html()){ out($(this).siblings("a").children("img").attr("id")); $(this).siblings("a").removeClass("over"); } }); }, 400); });
  });
}

function initLangsel()
{
  $("#langsel").css("width", "65px");

  $("#langsel ul").css("position", "absolute");
  $("#langsel ul").css("height", "24px");
  $("#langsel ul").css("top", "-5px");
  $("#langsel ul").css("overflow", "hidden");
  $("#langsel ul").css("border", "0");
  
  $("#langsel ul li").css("float", "none");
  $("#langsel ul li").css("border", "none");
  $("#langsel ul li").css("width", "40px");
  $("#langsel ul li").css("height", "24px");
  $("#langsel ul li").css("overflow", "hidden");
  $("#langsel ul li").css("top", "0");
  $("#langsel ul li").css("margin", "0 0 0 10px");
  $("#langsel ul li").css("padding", "0 13px 0 10px");
  $("#langsel ul li").css("background", "#FFFFFF");

  $("#langsel ul li:first-child").css("border-left", "1px solid #FFFFFF");
  $("#langsel ul li:not(:first-child)").css("border-left", "1px solid #ACACAC");
  $("#langsel ul li:not(:first-child)").css("border-right", "1px solid #ACACAC");
  $("#langsel ul li:last-child").css("border-bottom", "1px solid #ACACAC");

  $("#langsel ul li img").css("margin", "0");
  $("#langsel ul li img").css("display", "inline");

  $("#langsel ul").after("<img src=\"/fileadmin/templates/img/dropdown.gif\" width=\"13\" height=\"24\" alt=\"\" border=\"0\">");
  thisToggle = $("#langsel ul").next();
  thisToggle.css("position", "absolute");
  thisToggle.css("z-index", "1");
  thisToggle.css("left", "60px");
  thisToggle.css("top", "-5px");
  thisToggle.click(function(){ return toggleLangsel(); });
  $("#langsel ul li:first-child a").click(function(){ return toggleLangsel(); });
  $("body>*:not(#langsel)").click(function()
  { $("#langsel ul").animate({height: "24px"}, 200); });
}

function toggleLangsel()
{
  openHeight = (24 * $("#langsel ul li a img").length + 1) + "px";

  if($("#langsel ul").css("height") == "24px")
  { $("#langsel ul").animate({height: openHeight}, 200); }
  else
  { $("#langsel ul").animate({height: "24px"}, 200); }
  return false; 
}

function initItemDetailPopups()
{
  $("#orderform .items a.intern").click(function(){ return detailWin($(this).attr("href")); });
}

function detailWin(URL)
{
  var thisWin = window.open(URL,'dricondetailfenster','height=300,width=500,top=100,left=200');
  thisWin.focus();
  return false;
}

