// Define Menu Array
closedMenu=new Array();

//--------------------------------
// START - Document Ready Functions
//--------------------------------
$().ready(function(){
  //ini menu
  iniMenu();
  
  //set right image and maybe close nav
  $("div.navi.klapp img.bottom").each(function(index) {
    var parent = $(this).parent("div.navi.klapp");
    var MenuID = parent.attr("id");
    
    if (parent.hasClass("closed")) {
      var newimg = "nl_bottom_closed.png";
      parent.find("div.content").toggle();
      
      if (getCookieInfo("HdRMenu") == false) {
        if ($.inArray(MenuID, closedMenu) == -1) {
          closedMenu.push(MenuID);
        }
      }
      
    } else {
      var newimg = "nl_bottom_open.png";
    }
    
    var oldsrc = $(this).attr("src");
    var newsrc = oldsrc.replace(/nl_bottom\.png/g, newimg);
    $(this).attr("src", newsrc);
  });
  
  // hover
  $("div.navi.klapp img.top, div.navi.klapp img.bottom").hover(function() {
    var parent = $(this).parent("div.navi.klapp");
    add_hover(parent.find("img.top"));
    add_hover(parent.find("img.bottom"));
  }, function() {
    var parent = $(this).parent("div.navi.klapp");
    rm_hover(parent.find("img.top"));
    rm_hover(parent.find("img.bottom"));
  });
  
  // click
  $("div.navi.klapp img.top, div.navi.klapp img.bottom").click(function() {
    var parent = $(this).parent("div.navi.klapp");
    var content = parent.find("div.content");
    var bottom = parent.find("img.bottom");
    var MenuID = parent.attr("id");
    
    //console.log("Clicked: "+MenuID);  
    
    // toggle content
    content.toggle();
    
    // set new image
    var oldsrc = bottom.attr("src");
    
    // closed
    if (content.is(':hidden')) {
      bottom.attr("src", oldsrc.replace(/nl_bottom_open(.+)\.png/g, "nl_bottom_closed$1.png"));
      if ($.inArray(MenuID, closedMenu) == -1) {
        closedMenu.push(MenuID);
      }
      
    // open
    } else {
      bottom.attr("src", oldsrc.replace(/nl_bottom_closed(.+)\.png/g, "nl_bottom_open$1.png")); 
      if ($.inArray(MenuID,closedMenu) > -1) {
        deleteArray(closedMenu,MenuID);
      }
    }
    
    // set cookie
    setCookie("HdRMenu", closedMenu.join("|"), 1000*60*60*24*30);
  });

});
//--------------------------------
// END - Document Ready Functions
//--------------------------------

// navi hover
function add_hover(ele) {
  var suffix = ".png";
  var oldsrc = ele.attr("src");
  var newsrc = basename(oldsrc, suffix )+"_hover"+suffix;
  ele.attr("src", newsrc);
}
function rm_hover(ele) {
  var oldsrc = ele.attr("src");
  var newsrc = oldsrc.replace(/(.+)_hover\.png$/g, "$1.png");
  ele.attr("src", newsrc);
}
                            
// basename
function basename (path, suffix, withpath) {
  if (withpath == true) {
    var b = path.replace(/^.*[\/\\]/g, '');
  } else {
    var b = path;
  }
  if (typeof(suffix) == 'string' && b.substr(b.length - suffix.length) == suffix) {
    b = b.substr(0, b.length - suffix.length);
  }
  return b;
}

//Initialize Menu
function iniMenu()
{    
    // Cookie exists?
    if (getCookieInfo("HdRMenu") != false) {
        closedMenu = getCookieInfo("HdRMenu");
      
        //inArray => closeMenu
        $("div.navi.klapp").each(function(index) {
            var MenuID = $(this).attr("id");
            if ($.inArray(MenuID, closedMenu) > -1) {
              $(this).addClass("closed");
            } else {
              $(this).removeClass("closed");
            }
        });      
      
    }
}

//Imitate deleteArray
function deleteArray (theArray,theValue)
{
    var i;
    for (i=0; i < theArray.length; i++) {
        // Matches identical (===), not just similar (==).
        if (theArray[i] === theValue) {
            theArray.splice(i,1);
            return true;
        }
    }
    return false;
}

  
//Set Cookie
function setCookie(cookieName, tMenuIDs, expiresIn)
{
    var expires = new Date();
    var expireDate = expires.getTime() + expiresIn;
    expires.setTime(expireDate);
    document.cookie=cookieName+"="+tMenuIDs+"; expires="+expires.toGMTString()+";";
}

//Get Information from Cookie
function getCookieInfo(cookieName)
{
    var menuData = new Array();
    if (document.cookie) {
        begin = document.cookie.indexOf(cookieName+"=");
        if (begin != -1) {
            begin += cookieName.length+1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) {
                end = document.cookie.length;
            }
            cookievalue = unescape(document.cookie.substring(begin, end));
        } else {
            return false;
        }
    } else {
        return false;
    }
    menuData = cookievalue.split('|');
    return menuData;
}


