/* for top menu */
 
function showBg(id) {
    left = document.getElementById(id + 'left');
    bg = document.getElementById(id + 'bg');
    right = document.getElementById(id + 'right');
    
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        left.style.background = "url(/_tpl/images/menu/menu_hov_left.gif)";
        bg.style.background = "url(/_tpl/images/menu/menu_hov_bg.gif)";
        right.style.background = "url(/_tpl/images/menu/menu_hov_right.gif)";
    } else {
        left.style.background = "url(/_tpl/images/menu/menu_hov_left.png)";
        bg.style.background = "url(/_tpl/images/menu/menu_hov_bg.png)";
        right.style.background = "url(/_tpl/images/menu/menu_hov_right.png)";
    }
}

function hideBg(id) {
    left = document.getElementById(id + 'left');
    bg = document.getElementById(id + 'bg');
    right = document.getElementById(id + 'right');
    
    left.style.backgroundImage = "none";
    bg.style.backgroundImage = "none";
    right.style.backgroundImage = "none";
}

/* basic cookies functions */

function setCookie(name, value) {
      var valueEscaped = escape(value);
      var expiresDate = new Date();
      expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок - 1 год, но его можно изменить
      var expires = expiresDate.toGMTString();
      var newCookie = name + "=" + valueEscaped + "; path=/; expires=" + expires;
      if (valueEscaped.length <= 4000) document.cookie = newCookie + ";";
}

function getCookie(name) {
      var prefix = name + "=";
      var cookieStartIndex = document.cookie.indexOf(prefix);
      if (cookieStartIndex == -1) return null;
      var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
      if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
      return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
        
/* add/delete models to/from note */

function noteClick() {
    cur_id = $(this).attr("href").split("-")[1];
    cur_val = getCookie("note_models");
    curModelsCounter = $("#note_models");
    new_date = new Date("2030", "0", "1");
    if (cur_val)
        posit = cur_val.indexOf("," + cur_id + ",");
    else
        posit = -1;
    if (posit == -1) { // добавление
	    if (cur_val) {
	        new_val = cur_val + cur_id + ",";
	    } else {
	        new_val = "," + cur_id + ","; 
	    }
	    setCookie("note_models", new_val);
	    $(this).html("добавлено...").fadeOut(function(){
	        $(this).html("удалить из блокнота!").fadeIn();
	    });
	    curModelsCounter.html(curModelsCounter.html() * 1 + 1);
	} else { // удаление
	    cur_val = cur_val.substring(0, posit) + cur_val.substring(posit+cur_id.length+1, cur_val.length);
	    setCookie("note_models", cur_val);
        $(this).html("удалено...").fadeOut(function(){
            $(this).html("добавить в блокнот!").fadeIn();
        });
        total = curModelsCounter.html() * 1 - 1;
        if (!total)
            total = "0";
        curModelsCounter.html(total);
	}
    return false;
}

/* DOM is ready */

$(document).ready(function(){
    var curModels = getCookie("note_models");
    $(".note a.action").each(function(){
        cur_id = $(this).attr("href").split("-")[1];
        if (curModels && curModels.indexOf("," + cur_id + ",") != -1) {    
            $(this).html("удалить из блокнота!");
        } 
        $(this).click(noteClick);
    });
});
