$(document).ready(function()
{
  
  /* init Events in content and bubbles */
  initEvents();
  
});

/* Init basic events an effects */
function initEvents()
{
  initButtons();
  initSelects();
  initUploads();
  initRoller();
  initImgScroller();
  initBubbles();
  initLogin();

  $("#navi>ul>li").hover(function(){ setNaviHighlight($(this)); }, function(){ unsetNaviHighlight(); });
  $("#navi ul li:has(ul)").hover(function(){ showNaviBubble($(this)); }, function(){ hideNaviBubble($(this)); });
  
  $(".preview:not(.productlist) .imgblock a").click(function(){ imgPopup($(this)); return false; });
  $(".shadowimage a").click(function(){ imgPopup($(this)); return false; });
  $(".shadowflash a").click(function(){ iframePopup($(this)); return false; });
  
    if(!$("#nl_order").is(":checked"))
  { $("#nl_lang").hide(); }
  $("#nl_order").click(function(){
    if($("#nl_order").is(":checked"))
    { $("#nl_lang").fadeIn(300); }
    else
    { $("#nl_lang").fadeOut(300); }
  });

  $("#filterbox input.clickbox").click(function()
  { $("#filterbox").submit(); });
  
  $("button[id^='nojssubmitbtn']").hide();
  
}

// Button Resizer (IE)
function initButtons(thisParent)
{
  if(document.all) // ONLY FOR IE //
  {
    if(!thisParent)
    { var thisParent = $("body"); }

    for(var i = 0; i < thisParent.find("button").size(); i++)
    {
      var thisButton = thisParent.find("button:eq(" + i + ")");
      var thisArea = thisButton.children("div");
      if(thisArea.html())
      { 
        var outerWidth = thisArea.width() + parseInt(thisArea.css("padding-left").replace(/px/, "")) + parseInt(thisArea.css("padding-right").replace(/px/, "")) + parseInt(thisButton.css("padding-left").replace(/px/, "")) + parseInt(thisButton.css("padding-right").replace(/px/, ""));
        thisButton.css("width", outerWidth + "px"); 
      }
    }
  }
}

// Select Felder ersetzen
function initSelects(thisParent)
{
  if(!thisParent)
  { var thisParent = $("body"); }
 
  thisParent.find("select").each(function()
  {
    var thisSelect = $(this);
    // Init Fakeselect 
    thisSelect.before("<div class=\"select\"><div class=\"value\"></div></div>");
    var thisFSelect = thisSelect.prev();
    
    thisFSelect.css({
      width:              thisSelect.width(),
      position:           "absolute",
      zIndex:             "1",
      display:            "inline",
      marginTop:          "-1px",
      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);

}

// Upload Felder ersetzen
function initUploads(thisParent)
{
  if(!thisParent)
  { var thisParent = $("body"); }

  for(var i = 0; i < thisParent.find("input[type=file]").size(); i++)
  {
    var thisUpload = thisParent.find("input[type=file]:eq(" + i + ")");
    thisUpload.before('<div class="fakeupload"><button type="button"><div><strong>Datei ausw&auml;hlen</strong></div></button></div>');
    var thisFUpload = thisUpload.prev();
    thisFUpload.hide();
    thisFUpload.attr("size", thisUpload.attr("size"));
    thisFUpload.css("position", "absolute");
    thisUpload.css("position", "relative");
    thisUpload.css("zIndex", "1");
    thisFUpload.css("zIndex", "0");
    if(document.all)
    { thisUpload.css("width", 0); }
    else
    { 
      thisUpload.css("width", thisFUpload.width()); 
      thisUpload.css("marginRight", "15px"); 
    }
    thisUpload.fadeTo(0, 0);
    thisFUpload.show(0);
  }
}

var naviTimeout = false;
function showNaviBubble(thisNavPoint)
{
  if(thisNavPoint.parents("li").html())
  { thisNavPoint = thisNavPoint.parents("li"); }
  
  thisNavPoint.children("a:first").addClass("on");
  thisNavPoint.children("ul").show();
}

function hideNaviBubble(thisNavPoint)
{
  $("#navi ul li a").removeClass("on");
  $("#navi ul li ul").hide(); 
}

function setNaviHighlight(thisNavPoint)
{
  $("#navi>ul>li:not(:eq(" + $("#navi>ul>li").index(thisNavPoint) + "))>strong").replaceWith("<span>" + $("#navi>ul>li>strong").html() + "</span>"); 
}

function unsetNaviHighlight()
{
  $("#navi ul li span").replaceWith("<strong>" + $("#navi ul li span").html() + "</strong>"); 
}

/* Roller function */
function initRoller()
{
  $(".roller .rollerbox .multicol").append("<div style=\"clear: both\"></div>"); // flickerfix
  $(".roller .rollerbox").after("<div class=\"toggle\"><span>auf-/einklappen</span></div>");
  $(".toggle span").hide();
  for(var i = 0; i < $(".roller .rollerbox").size(); i++)
  {
    var thisRoller = $(".roller .rollerbox:eq(" + i + ")");
    var thisToggle = thisRoller.next(".toggle");
    thisRoller.css("height", (thisRoller.height() + thisToggle.height()) + "px");
  }
  $(".roller .rollerbox").hide();
  $(".roller .toggle").click(function(){ rollerToggle($(this)); });
}

function rollerToggle(thisToggle)
{
  if(!thisToggle.is(":animated"))
  {
    thisToggle.prev(".rollerbox").slideToggle(700, function(){ thisToggle.toggleClass("open"); });
  }
}


/* IMGSCROLLER - SCROLLABLE LIST */
function initImgScroller()
{
  $(".imgscroller").each(function()
  {
    var thisScroller = $(this);

    var maxLoad = 20;
		thisScroller.children(".images").children("a").children("img").each(function(i)
		{
			var thisImg = $(this);
			thisImg.attr("savesrc", thisImg.attr("src"));
			thisImg.removeAttr("src");
    });
    
		var imgCount = 7;
	  if (thisScroller.parents("#content").html() && !thisScroller.parents("#scroller").html())
    { imgCount = 5; }

	  if(thisScroller.children(".images").children("a").size() > 0 && thisScroller.children(".images").children("a").size() <= imgCount)
    {
    	$(".scroller.left a").remove();
    	$(".scroller.right a").remove();
    }

		var baseImgCount = thisScroller.children(".images").children("a").length; 
		while(thisScroller.children(".images").children("a").size() > 0 && thisScroller.children(".images").children("a").size() <= (parseInt(imgCount / 2) + parseInt(baseImgCount / 2)))
    {
       thisScroller.children(".images").prepend(thisScroller.children(".images").children("a:first").clone());
       thisScroller.children(".images").children("a:first").css("visibility", "hidden");
       thisScroller.children(".images").children("a:first").removeAttr("href");
    }
  
		// move current to center
		if(baseImgCount > imgCount && thisScroller.children(".images").children("a.current").length > 0)
    {
	    while(thisScroller.children(".images").children("a").index(thisScroller.children(".images").children("a.current")) < parseInt(imgCount / 2))
	    {
	      thisScroller.children(".images").prepend(thisScroller.children(".images").children("a:last"));
	    }
	    while(thisScroller.children(".images").children("a").index(thisScroller.children(".images").children("a.current")) > parseInt(imgCount / 2))
	    { 
				thisScroller.children(".images").append(thisScroller.children(".images").children("a:first"));
	    }
	  }
	 
		thisScroller.children(".images").children("a").children("img:lt(" + maxLoad + ")").each(function(i)
		{
			var loadImg = $(this);
      if(loadImg.attr("savesrc"))
      { loadImg.attr("src", loadImg.attr("savesrc")); }
    });

		thisScroller.children(".images").children("a").children("img:gt(" + (thisScroller.children(".images").children("a").length - maxLoad) + ")").each(function(i)
		{
			var loadImg = $(this);
      if(loadImg.attr("savesrc"))
      { loadImg.attr("src", loadImg.attr("savesrc")); }
    });

    // hide non-visible images, init scroller
    thisScroller.children(".images").children("a:gt(" + (imgCount - 1) + ")").children("img").css("height", "0").css("top", "50%").css("margin", "0").hide();

		if(imgCount == 7)
    {
      thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "40%", top: "30%"});
      thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "60%", top: "20%"});
      thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "80%", top: "10%"});
      thisScroller.children(".images").children("a:eq(3)").children("img").animate({height: "100%", top:  "0%"});
      thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "80%", top: "10%"});
      thisScroller.children(".images").children("a:eq(5)").children("img").animate({height:  "60%", top: "20%"});
      thisScroller.children(".images").children("a:eq(6)").children("img").animate({height:  "40%", top: "30%"});
    }
    else if (imgCount == 5)
    {
      thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "30%", top: "35%"});
      thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "60%", top: "20%"});
      thisScroller.children(".images").children("a:eq(2)").children("img").animate({height: "100%", top:  "0%"});
      thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "60%", top: "20%"});
      thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "30%", top: "35%"});
    }
    else if (imgCount == 4)
    {
      thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "60%", top: "20%"});
      thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "80%", top: "10%"});
      thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "80%", top: "10%"});
      thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "60%", top: "20%"});
    }
    

    $(".scroller.left a").click(function()
    {
      var loadImg = thisScroller.children(".images").children("a").children("img:eq(" + (thisScroller.children(".images").children("a").length - maxLoad) + ")");
      if(loadImg.attr("savesrc"))
      { loadImg.attr("src", loadImg.attr("savesrc")); }
      scrollImgScroller($(this), "left", imgCount);
      return false;
    });
    $(".scroller.right a").click(function()
    {
      var loadImg = thisScroller.children(".images").children("a").children("img:eq(" + maxLoad + ")");
      if(loadImg.attr("savesrc"))
      { loadImg.attr("src", loadImg.attr("savesrc")); }
			scrollImgScroller($(this), "right", imgCount);
      return false;
    });    
  });
}

function scrollImgScroller(thisLink, direction, imgCount)
{
  var thisScroller = thisLink.parents(".imgscroller");
  if(!(thisScroller.children(".images").children("a").children("img:animated").is(":animated")))
  {
    if(direction == "left")
    {
      thisScroller.children(".images").prepend(thisScroller.children(".images").children("a:last"));
      thisScroller.children(".images").children("a:eq(" + imgCount + ")").children("img").animate({height:    "0", top: "50%", marginRight:  "0px"}, 400);
      if(imgCount == 7)
      {
        thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "40%", top: "30%", marginRight: "10px"}, 400);
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height: "100%", top:  "0%"}, 400);
        thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(5)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(6)").children("img").animate({height:  "40%", top: "30%"}, 400);
      }
      else if (imgCount == 5)
      {
        thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "30%", top: "35%", marginRight: "10px"}, 400);
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height: "100%", top:  "0%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "30%", top: "35%"}, 400);
      }
      else if (imgCount == 4)
      {
        thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:  "60%", top: "20%", marginRight: "10px"}, 400);
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "60%", top: "20%"}, 400);
      }
        
    }
    else if(direction == "right")
    {
      thisScroller.children(".images").children("a:eq(0)").children("img").animate({height:    "0", top: "50%", marginRight:  "0px"}, 400);
      if(imgCount == 7)
      {
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "40%", top: "30%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(4)").children("img").animate({height: "100%", top:  "0%"}, 400);
        thisScroller.children(".images").children("a:eq(5)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(6)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(7)").children("img").animate({height:  "40%", top: "30%", marginRight: "10px"}, 400);
      }
      else if (imgCount == 5)
      {
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "30%", top: "35%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height: "100%", top:  "0%"}, 400);
        thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(5)").children("img").animate({height:  "30%", top: "35%", marginRight: "10px"}, 400);
      }
      else if (imgCount == 4)
      {
        thisScroller.children(".images").children("a:eq(1)").children("img").animate({height:  "60%", top: "20%"}, 400);
        thisScroller.children(".images").children("a:eq(2)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(3)").children("img").animate({height:  "80%", top: "10%"}, 400);
        thisScroller.children(".images").children("a:eq(4)").children("img").animate({height:  "60%", top: "20%", marginRight: "10px"}, 400);
      }
      window.setTimeout(function(){ thisScroller.children(".images").append(thisScroller.children(".images").children("a:first")); }, 410);
    }
  }
}

function printPreview()
{
  var printStyle = $("link[media=print]:first");
  printStyle.attr("media", "screen, print");
  
  $("body").prepend(printCTRL);
  $("#printctrl").animate({top: 0}, 600);
  
  $("#print").click(function(){
    window.print();
    $("#printctrl").animate({top: "-60px"}, 600);
    window.setTimeout(function(){ printStyle.attr("media", "print"); $("#printctrl").remove(); }, 650);
  });

  $("#close").click(function(){
    $("#printctrl").animate({top: "-60px"}, 600);
    window.setTimeout(function(){ printStyle.attr("media", "print"); $("#printctrl").remove(); }, 650);
  });
  
  return false;
}

function imgPopup(thisLink)
{
  $("body").append("<div id=\"shader\"></div>");
  $("#shader").fadeTo(0.1, 0, styleShader);

  $("body").append("<div id=\"popup\"><img src=\"" + thisLink.attr("href") + "\" alt=\"\"/></div>");
  $("#popup").css("visibility", "hidden");
  $("#popup img").load(function(){
    $("#popup").fadeTo(0.1, 0, stylePopup);
  });
  $("#popup").css("z-index", "10001");

  $("#shader").click(function(){ closePopup(); return false; });
}

function iframePopup(thisLink)
{
  $("body").append("<div id=\"shader\"></div>");
  $("#shader").fadeTo(0.1, 0, styleShader);

  $("body").append("<div id=\"popup\"><iframe id=\"flashshadow\" src=\"" + thisLink.attr("href") + "\"></div>");
  
   $("#popup").css({
   padding: 0
     });
  
   $("#flashshadow").css({
    position: "fixed",
    zIndex: "10000",
    width: "340px",
    height: "340px",
    top: "10%",
    left: parseInt($("body").width() / 2 - 160 ) + "px",
    background: "#FFFFFF"
  });


  if($("#flashshadow").offset().left == 0) // position: fixed fehlgeschlagen
  { 
      $("#flashshadow").css({
        position: "absolute",
        height: parseInt($("body").height() * 0.9) + "px",
        top: parseInt(document.documentElement.scrollTop + $("body").height() * 0.05) + "px"
      });         
  }
  
  $("#shader").click(function(){ closePopup(); return false; }); 

}


function closePopup()
{
  $("#popup").fadeOut(200);
  $("#shader").fadeOut(200);
  window.setTimeout(function(){ $("#popup").remove(); $("#shader").remove(); }, 200);
}

function stylePopup()
{
  popupLeft = Math.round(($("body").width() - $("#popup").width()) / 2);
  
  if(window.innerHeight)
  { popupTop = window.pageYOffset + Math.round((window.innerHeight - $("#popup").height()) / 2); }
  else if(document.documentElement.clientHeight)
  { popupTop = document.documentElement.scrollTop + Math.round((document.documentElement.clientHeight - $("#popup").height()) / 2); }
  else if(document.body.clientHeight)
  { popupTop = document.body.scrollTop + Math.round((document.body.clientHeight - $("#popup").height()) / 2); }
  $("#popup").css("left", popupLeft);
  $("#popup").css("top", popupTop);
  $("#popup").css("visibility", "visible");
  $("#popup").fadeTo(200, 1);
}

function styleShader()
{
  $("#shader").css("position", "absolute");
  $("#shader").css("z-index", "10000");
  $("#shader").css("width", "100%");
  $("#shader").css("top", "0");
  if($("html").height() > $("#page").height())
  { $("#shader").css("height", $("html").height()); }
  else { $("#shader").css("height", $("#page").height()); }
  $("#shader").css("background", "#000000");
  $("#shader").fadeTo(200, 0.80);
}

function initBubbles()
{
  $("a.contact").click(function(){ showBubble($(this)); return false; });
}

var thisBubbleOpener = false;
function showBubble(thisLink)
{
  if(!thisBubbleOpener || thisLink.attr("href") != thisBubbleOpener.attr("href"))
  {
    thisBubbleOpener = thisLink;
    $("#bubble").remove();
    $("#page").append("<div id=\"bubble\"><div id=\"bubblecontent\"></div><div id=\"bubblearrow\"></div><div id=\"bubblefooter\"></div><div id=\"bubblecloser\"></div></div>");
    $("#bubble").openerURL = thisLink.attr("href");
    $("#bubblecontent").load(thisLink.attr("href") + " #content form", function(){ initBubble(thisLink); initButtons($("#bubble")); initSelects($("#bubble")); initUploads($("#bubble")); });
    
    $("#bubblearrow").css("top", thisLink.offset().top - $("#bubble").offset().top - Math.round($("#bubblearrow").height() / 2 - thisLink.height() / 2));

    $("#bubblecloser").click(function(){ $("#bubble").remove(); thisBubbleOpener = false; });
  }
}

function initBubble(thisLink)
{
  if($("#bubblecontent").height() < thisLink.offset().top - $("#bubble").offset().top - Math.round($("#bubblearrow").height() / 2 - thisLink.height() / 2) + 80)
  { $("#bubblecontent").css("height", thisLink.offset().top - $("#bubble").offset().top - Math.round($("#bubblearrow").height() / 2 - thisLink.height() / 2) + 80); }
  $("#bubble form").submit(function()
  {
    $("#bubblecontent").load($(this).attr("action") + " #content #formresult", $(this).serializeArray(), function(){ initBubble(thisLink); initButtons($("#bubble")); initSelects($("#bubble")); initUploads($("#bubble")); });
    return false;
  });
}

function initLogin()
{
  $("#page").append("<div id=\"quicklogin\"></div>");
  $("#quicklogin").hide();
  $("#quicklogin").load($("#login a.opener").attr("href") + " form.loginform", initLoginForm)
  $("#login a.opener").click(function(){ $("#quicklogin").show(); initButtons($("#quicklogin")); $("#quicklogin input:first").focus(); return false; });
}

function initLoginForm()
{
  $("#quicklogin").append("<div class=\"closer\">&nbsp;</div>");
  $("#quicklogin .closer").click(function(){ $("#quicklogin").hide(); return false; });
}

function getLocation(address) {
var addressData = unescape(address);
var adresses = addressData.split("#");

var name = adresses[0];
var address = adresses[1];
var phone = adresses[2];
var fax = adresses[3];
var mail = adresses[4];
var link = adresses[5];
var content = "";

//alert("addi: "+address + " phone: "+phone+ "fax: "+fax+ "mail: "+mail+ "link: "+link);

if (name == "" || name == "undefined") {
  $("#addressbox").css("display", "none");
}
  else {
          $("#addressbox").css("display", "block");
          
          if (link == "" || link == "undefined") {
            content = name + "<br />";
          } else {
            $("body").append(link);
            var thisLink = $("body").children("a:last").hide();
            var linkTextRegExp = new RegExp(thisLink.text(), "gi");
            content += "<strong>" + link.replace(linkTextRegExp, name + " ") + "</strong><br />";
            thisLink.remove();
          }
          
      	  content += address;
                
          if (phone == "" || phone == "undefined" || phone == " ") {
          } else { 
          content += '<br /><strong>Phone:</strong> ' + phone;
          };
           
          if (fax == "" || fax == "undefined") {
          } else {
          content += '<br /><strong>Fax:</strong> ' + fax;
          };         
          
          if (mail == "" || mail == "undefined") {
          } else {
          content += "<br />" + mail;
          };
          
          if (link == "" || link == "undefined") {
          } else {
          // content += "<br /><strong>" + link + "</strong>";
          };                                
          $("#address").html("<p>" + content + "</p>");
  }
}

function getAddress(id , country) {
getXML2('/en/locationsjsp.jsp?country='+ country);
saveId(id);
}

var savedid;

function saveId(id){
// alert ("saving id: " + id);
savedid = id;
}

function getXML( url ){
	    $.ajax({
	        url: url,
	        dataType: "xml",
	        encoding:"UTF-8",
	        beforeSend: xmlStart,
	        success: xmlSuccess,
	        error: xmlError,
	        complete: xmlComplete
	    });
	}
	
function getXML2( url ){
	    $.ajax({
	        url: url,
	        dataType: "xml",
	        encoding:"UTF-8",
	        beforeSend: xmlStart2,
	        success: xmlSuccess2,
	        error: xmlError,
	        complete: xmlComplete
	    });
	}	

	function xmlStart( xhrInstance ){
	    //Clear Any Previously Written HTML In Content DIV
	    $( "#locationdropdown .content" ).html( "" );
	    //Show Preloader GIF
	    $( "#locationdropdown .content" ).addClass( "ajaxloader" );
	}
	
		function xmlStart2( xhrInstance ){
	    //Clear Any Previously Written HTML In Content DIV
	}

	function xmlError( xhrInstance, message, optional ){
	    $("#locationdropdown .content").html("<option>There was an error loading the document.</option>");
	}

	function xmlComplete( xhrInstance, status ){
	    $( "#locationdropdown .content" ).removeClass( "ajaxloader" );
	}

	function xmlSuccess( data, status ){
	  // If Success Call Parsing Function
    parseXML( data );
	}
	
	function xmlSuccess2( data, status){
 	  // If Success Call Parsing Function
    // alert ("going to parsing new XML Tree, ID = " + savedid);
    parseLocations( data );
	}	

	function parseXML( xml ){
	    // XML Tags For Each Item
	    var locationTag = "location";
	    var nameTag = "cs_name";
	    var streetTag = "cs_street";
	    var numberTag = "cs_number";
	    var stateTag = "cs_state";
	    var plzTag = "cs_plz";   
	    var cityTag = "cs_city";
	    var phoneTag = "cs_phone";
	    var faxTag = "cs_fax";
	    var mailTag = "cs_mailto";	      	       
	    var boldTag = "cs_bold";
                                     	         
	    // HTML Content
	    var content = "<select onchange=\"getLocation(this.options[this.selectedIndex].value);\">";
          content += '<option value=\"undefined\">Please choose a location</option>';
	    //Parse the XML and build new <options>'s for each NODE
	    $( xml ).find( locationTag ).each(function(){    
	        var name = $( this ).find( nameTag ).text();
	        var street = $( this ).find( streetTag ).text();
	        var number = $( this ).find( numberTag ).text();	        
	        var state = $( this ).find( stateTag ).text();
	        var plz = $( this ).find( plzTag ).text();
	        var city = $( this ).find( cityTag ).text();
	        var address = (street+" "+number+"<br />"+state+"-"+plz+" "+city);
	        var phone = $( this ).find( phoneTag ).text();
	        var fax = $( this ).find( faxTag ).text();
	        var mail = $( this ).find( mailTag ).text();          	        
	        var link = $( this ).find( boldTag ).text();
	        
	        content  += "<option value=\""+ escape(name) +"#"+ escape(address)+"#"+ escape(phone)+"#"+ escape(fax)+"#"+ escape(mail)+"#"+ escape(link)+"\">" + name + "</option>";
	    });

	    content += "</select>";

	    // DIV With The Dropdown Box
		$("#locationdropdown").html(content);
    initSelects($("#locationdropdown"));
	}
	
	function parseLocations( xml ){
	
	    // XML Tags For Each Item
	    var locationTag = "location";	    
      var idTag = "cs_id";	    
	    var nameTag = "cs_name";
	    var streetTag = "cs_street";
	    var numberTag = "cs_number";
	    var stateTag = "cs_state";
	    var plzTag = "cs_plz";   
	    var cityTag = "cs_city";
	    var phoneTag = "cs_phone";
	    var faxTag = "cs_fax";
	    var mailTag = "cs_mailto";	      	       
	    var boldTag = "cs_bold";
                                     	         
	    // HTML Content
	    var content = "";
          content += "";
    
	    //Parse the XML and build locations
      i = 0;
	    $( xml ).find( locationTag ).each(function(){
        
      if ($( this ).find( idTag ).text() == savedid){
         
         i++;
         
            if (i > 1) {
            content += "<br /><br /><div class=\"hr\"><hr /></div>";
            }

            var name = $( this ).find( nameTag ).text();
  	        var street = $( this ).find( streetTag ).text();
  	        var number = $( this ).find( numberTag ).text();	        
  	        var state = $( this ).find( stateTag ).text();
  	        var plz = $( this ).find( plzTag ).text();
  	        var city = $( this ).find( cityTag ).text();
            var address = (street+" "+number+"<br />"+state+"-"+plz+" "+city);
  	        var phone = $( this ).find( phoneTag ).text();
  	        var fax = $( this ).find( faxTag ).text();
  	        var mail = $( this ).find( mailTag ).text();          	        
 	          var link = $( this ).find( boldTag ).text();
  	        
              //content += address;

              if (link == "" || link == "undefined") {
                content = name + "<br />";
              } else {
                $("body").append(link);
                var thisLink = $("body").children("a:last").hide();
                var linkTextRegExp = new RegExp(thisLink.text(), "gi");
                content += "<strong>" + link.replace(linkTextRegExp, name + " ") + "</strong><br />";
                thisLink.remove();
              }
    
      	      content += address;
                                    
              if (phone == "" || phone == "undefined" || phone == " ") {
              } else { 
              content += '<br /><strong>Phone:</strong> ' + phone;
              }
               
              if (fax == "" || fax == "undefined") {
              } else {
              content += '<br /><strong>Fax:</strong> ' + fax;
              };         
              
              if (mail == "" || mail == "undefined") {
              } else {
              content += "<br />" + mail;
              };
              
              if (link == "" || link == "undefined") {
              } else {
              //content += "<br /><strong>" + link + "</strong>";
              };               
	        }
	        
	    });

          if (content == "" || content == "undefined") {
            $("#addressbox").css("display", "none");
          }
           else {
            $("#addressbox").css("display", "block");
            $("#address").html(content);         
           }
                
	}	
	
  function getCountry(country) {
 	  getXML('/en/locationsjsp.jsp?country='+ country +"");
  }
 
function unloadCountry() { 
  $("#locationdropdown").html( "" );
}
 
function unloadCountry() { 
  $("#locationdropdown").html( "" );
}