$(window).load(
  function () {
    global_autoAttach();
  }
);


function global_autoAttach() {
  window_popup_image_over();
  menu_markLastItem();
  iflowgallery_localization();
  iflowgallery_thumb_over();
  iflowgallery_thumb_path_popup();
  window_popup_resize();
  tooltips();
  filelist_edit();
  element_next_fadeOut();
  footer_position();
}



function footer_position() {

  number_height_head = Number($("div.head").outerHeight());
  number_height_menu_level_1 = Number($("div.menu_level_1").outerHeight());
  number_height_body = Number($("div.body").outerHeight());
  number_height_foot = Number($("div.foot").outerHeight());



  number_height_sum = number_height_head+number_height_menu_level_1+number_height_body+number_height_foot;
  //alert(Number($("html").height()) + " > " +  number_height_sum);
  if(Number($("body").outerHeight()) > number_height_sum) {
    //alert($("body").height() - number_height_head - number_height_menu_level_1 - number_height_foot);

    $("div.body").css("height", Number($("body").outerHeight()) - number_height_head - number_height_menu_level_1 - number_height_foot + "px");
    if ($.browser.mozilla) {
      $("div.body").css("height", Number($("body").outerHeight()) - number_height_head - number_height_menu_level_1 - number_height_foot - 36 + "px");
    }
    //alert(Number($("body").height()) - number_height_head - number_height_menu_level_1 - number_height_foot);
    //$("div.foot").css("position","absolute");
    //$("div.foot").css("bottom","0px");
  };

}

function window_popup_image_over() {


  $(".window_popup").hover(
    function(){

      $(this).find("div.over").css("display", "none");
    },function(){
      $(this).find("div.over").css("display", "block");
    }
  )
}

function element_next_fadeOut() {
  if ($(".element_next_fadeOut").length > 0) {
    $(".element_next_fadeOut").each(
      function () {
        $(this).next().css("display","none");
        $(this).click(
          function () {
            $(this).next().slideToggle("slow");
            $(this).toggleClass("slideUp");
          }
        )
      }
    )
  }
}

function filelist_edit() {
  $("table.csc-uploads td.csc-uploads-fileName p.csc-uploads-description").each(
    function () {
      //$(this).parent("td").prepend($(this));

      $(this).prev("p").addClass("float_right");
      $(this).prev("p").css("display", "block");
      $(this).prev("p").find("a").text("download");


    }
  )
  footer_position();

}

function tooltips() {
  if ($("a.two_dimensional_laser_engraving")) {
    $("a.two_dimensional_laser_engraving").cluetip({local: true, width: "200px", positionBy:"mouse"});
    $("a.color_print").cluetip({local: true, width: "215px", positionBy:"mouse"});
  }

}

function window_popup_resize() {
  $("body.window_popup").each(
    function () {
      var number_image_width = $(this).find(".image img").outerWidth();
      var number_image_height = $(this).find(".image img").outerHeight() + 30;
      window.resizeTo(number_image_width, number_image_height);

    }
  )
}

function menu_markLastItem() {
  var temp = $(".menu .content ul").each(
    function () {
      $(this).find("li").eq($(this).find("li").length-1).addClass("last");
    }
  )
}

function new_window(url, window_name, attributes){
  new_window_object = window.open(url, window_name, "width=300,height=400,left=100,top=200");
  new_window_object.focus();
}

function iflowgallery_thumb_path_popup() {
  $("div.iflowgallery div.iflow-image a").each(
    function () {

      $(this).removeAttr("onclick");
      var string_path_popup = String($(this).attr("href"));
      var number_position = string_path_popup.indexOf(".jpg&");
      if (number_position != -1) {
        string_text_before = string_path_popup.substr(0, number_position);
        string_text_after = string_path_popup.substring(number_position);
        array_text_before = string_text_before.split("%2F");
        var string_text_before = "";
        for (var i=0; i < array_text_before.length; i++) {
          if (i == array_text_before.length-1) {
            string_text_before += "%2Fdetail";
          }
          if (i>0) {
            string_text_before += "%2F";
          }
          string_text_before += array_text_before[i];
        }
        string_href = string_text_before + string_text_after;
        //$(this).attr("href", string_text_before.replace(/index.php/, "fileadmin/template/main/gallery_popup.php" ).replace(/openPic/,"new_window/")+string_text_after.replace(/width=104/, "width=700").replace(/height=204/, "height=500"));

        var string_url_image = string_href.substring(string_href.indexOf("file=") + 5, string_href.indexOf(".jpg&") + 4);
        $(this).removeAttr("href");
        $(this).click(
          function () {
            new_window('/fileadmin/template/main/gallery_popup.php?file=' + string_url_image,'LBC','');
          }
        )
      }
    }
  )
}

function iflowgallery_thumb_over() {
  $("div.iflowgallery div.iflow-image").mouseover(

    function() {

      $("div.iflowgallery div.description p.text_default").addClass("hidden");
      $("div.iflowgallery div.description").append( '<div class="text_description">' + $(this).find("div.iflow-image-description").html() + "</div>");
    }
  )
  $("div.iflowgallery div.iflow-image").mouseout(
    function() {
      $("div.iflowgallery div.description p.text_default").removeClass("hidden");
      $("div.iflowgallery div.description div.text_description").remove();
    }
  )
}

function iflowgallery_localization() {
  if ($("body.lang_gb").length > 0) {
    $(".de").each(
      function () {
        $(this).remove();
      }
    )
  } else {
    $(".gb").each(
      function () {
        $(this).remove();
      }
    )
  }
}
