$(document).ready(function()
{

  /* init Events in content and bubbles */
  initButtons();  
  
  initEvents();
    
});

function initEvents()
{
    $(".preview:not(.productlist) .imgblock a").click(function(){ iframePopup($(this)); return false; });
}


// 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("div").html())
      { 
        var thisArea = thisButton.children("div");
        var outerWidth = thisArea.width() + parseInt(thisArea.css("margin-right").replace(/px/, ""));
        thisButton.css("width", outerWidth + "px"); 
      }
    }
  }
}


/** IE < 7 Table Mouseover **/
function doMouseOver()
{
  this.className = "over";
}

function doMouseOut(obj)
{
  this.className = "";
}

function listen()
{
  if(navigator.appName == "Microsoft Internet Explorer")
  {
    navVersion = navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE "));
    navVersion = navVersion.substr(5, navVersion.indexOf(";")-5);
    if(navVersion < 7)
    {
      trs = document.getElementsByTagName("tr");
      for(i=0; i < trs.length; i++)
      {
        if(trs[i].parentNode.parentNode.className.search(/linklist/) > -1)
        {
          trs[i].onmouseover = doMouseOver;
          trs[i].onmouseout = doMouseOut;
        }
      }
    }
  }
}
/** /IE < 7 Table Mouseover **/

/** Project - Cardfunctions **/

function showProject(pUrl)
{
  document.getElementById("projectsearch").style.visibility="hidden";

  heightOffset = 0;
  if(document.documentElement.scrollTop)
  { heightOffset = document.documentElement.scrollTop;  }
  else if(document.body.scrollTop)
  { heightOffset = document.body.scrollTop;  }
  else if(window.pageYOffset)
  { heightOffset = window.pageYOffset; }
  jsDarkener = document.getElementById("darkener");
  
  totalHeight = "100%";
  totalWidth = "100%";
  if(document.all && document.documentElement.clientHeight)
  {
    totalHeight = document.documentElement.clientHeight;
    if(totalHeight < (document.getElementById("wrapper_page").offsetHeight+8))
    { totalHeight = (document.getElementById("wrapper_page").offsetHeight+8); }
    totalHeight += "Px";
    totalWidth = document.documentElement.clientWidth + "Px";
  }
  else if(document.all && document.body.clientHeight)
  {
    totalHeight = document.body.clientHeight;
    if(totalHeight < (document.getElementById("wrapper_page").offsetHeight+8))
    { totalHeight = (document.getElementById("wrapper_page").offsetHeight+8); }
    totalHeight += "Px";
    totalWidth = document.body.clientWidth + "Px";
  }
  else
  {
    jsDarkener.style.position = "fixed";
  }
  
  jsDarkener.style.height = (totalHeight);
  jsDarkener.style.width = (totalWidth);
  jsDarkener.style.display="block";

  $("#projects").load(pUrl+" #projectdetail");
  document.getElementById("projects").style.top = (heightOffset + 300) + "Px";
  return false;
}

function switchProject(pUrl)
{
  $("#projects").load(pUrl+" #projectdetail");
  return false;
}

function hideProjects()
{
  jsDarkener = document.getElementById("darkener");
  jsDarkener.style.display="none";
  $("#projects").empty();
  document.projectsearch.style.visibility="visible";
}
/** /Project - Cardfunctions **/

function iframePopup(thisLink)
{

  $("body").append("<div id=\"shader\"></div>");
  $("#shader").fadeTo(0.1, 0, styleShader);
  
  
  $("body").append("<div id=\"popup\"><iframe id=\"nlpreview\" src=\"" + thisLink.attr("href") + "\"></div>");
  
  $("#nlpreview").css({
    position: "fixed",
    zIndex: "10000",
    width: "920px",
    height: "90%",
    top: "5%",
    left: parseInt($("body").width() / 2 - 460) + "px",
    background: "#FFFFFF"
  });
     
  if($("#nlpreview").offset().left == 0) // position: fixed fehlgeschlagen
  { 
      $("#nlpreview").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 styleShader()
{
  $("#shader").css("position", "absolute");
  $("#shader").css("z-index", "10000");
  $("#shader").css("width", "100%");
  $("#shader").css("top", "0");
  if($("html").height() > $(document).height())
  { $("#shader").css("height", $("html").height()); }
  else 
  { $("#shader").css("height", $(document).height()); }
  $("#shader").css("background", "#000000");
  $("#shader").fadeTo(200, 0.80);
}

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);
}