/*
 * unbit.js - unbit sas - copyright 2010
 */

/*
 * Usare:
 * $(document).ready(function() {
 *   menuSetActive('ul#menu li')
 * });
 * I parametri:
 * - li_elem = stringa del selettore degli elementi li del menu
 * - active_class = la classe da dare agli elementi attivi, default 'active'
 * - follow_parent = booleano per segnare come attivo anche eventuali li padri, default true
 * - match_id = booleano per segnare di beccare anche eventuali cifre oltre al path, default false
 *   es. /progetti/1/ che segni attivo pure /progetti/
 * - cb = callback opzionale che prende come parametri l'href del link, il path chiamato e l'elementi li
 */
function menuSetActive(li_elem, active_class, follow_parent, match_id, cb) {
    var path = window.location.pathname;
    active_class = typeof(active_class) != 'undefined' ? active_class : 'active';
    follow_parent = typeof(follow_parent) != 'undefined' ? follow_parent : true;
    match_id = typeof(match_id) != 'undefined' ? match_id : false;

    $(li_elem).each(function() {
        var href = $(this).children('a').attr('href');
        var match_href_re = new RegExp('^(/it|/en|/fr)?'+href+'$')
        if (path.match(match_href_re)) {
            $(this).attr('class', active_class);
            if (follow_parent) {
              $(this).parent().parent('li').attr('class', active_class);
            }
        } else if (match_id) {
          var match_id_re = new RegExp('^'+href+'\\d+/$');
          if (path.match(match_id_re)) {
            $(this).attr('class', active_class);
          } else {
            $(this).attr('class', '');
          }
        } else {
          $(this).attr('class', '');
        }
        if (typeof(cb) != 'undefined') {
          cb(href, path, $(this));
        }
    });
}

/*
 * Usare:
 * redirect("http://unbit.it", 5000);
 * I parametri:
 * - url = url su cui redirigere
 * - timeout = millisecondi dopo i quali redirigere, default 0
 */
function redirect(url, timeout) {
    timeout = typeof(timeout) != 'undefined' ? timeout : 0;

    setTimeout("window.location.replace("+url+")", timeout);
}

