/* change category and use all other parameters in url */
function changeCategory(newCategoryPath, link) {
    var currUrl = window.location.href;
    var newUrl = getNewUrlByCategory(newCategoryPath, currUrl);
    if (newUrl != currUrl) {
        //alert(newUrl);
        link.href = newUrl;
    }
    return true;
}

function getNewUrlByCategory(newCategoryPath, url) {
    var decodedURI = decodeURIComponent(url);
    var slashIndex = decodedURI.lastIndexOf('/');
    newCategoryPath = (newCategoryPath.indexOf('/') == 0) ? newCategoryPath.substr(1) : newCategoryPath;
    newCategoryPath = (newCategoryPath.lastIndexOf('/') == newCategoryPath.length - 1) ? newCategoryPath.substring(0, newCategoryPath.length - 1) : newCategoryPath;

    if (decodedURI.indexOf(newCategoryPath) == -1) {
        var params = decodedURI.substr(slashIndex + 1);
        var within = (params.indexOf('within') != -1 ? getParameterValue(params, 'within') : '');
        var url = "/" + newCategoryPath + "/";
        if (within != '') url += 'within-' + within;
    }
    return url;
}

// Get a logical parameter from address string as part of a query parameter
function getParameterValue(url, name) {
    var decodedURI = decodeURIComponent(url);
    var slashIndex = decodedURI.lastIndexOf('/');
    var arr = decodedURI.substr(slashIndex + 1).split('-');
    var value = '';
    for (var i = 0, n = arr.length; i < n; i++) {
        if (arr[i] == name || (i == 0 && (arr[i].lastIndexOf(name) == arr[i].length - name.length))) {
            value = arr[i + 1];
            break;
        }
    }
    return value;
}

var categoriesMenuSelected="";
function toggleMenu (submenu, show, timeout) {
  if (show) {
    $("#"+submenu)[0].style.display = "block";
    categoriesMenuSelected = submenu;
  }
  else {
    if (!timeout) {
      categoriesMenuSelected="";
      setTimeout('toggleMenu ("'+ submenu +'", false, true)', 20);
    }
    else {
      if (categoriesMenuSelected=="" || categoriesMenuSelected != submenu)
        $("#"+submenu)[0].style.display = "none";
    }
  }
}
