// JavaScript Document
function global_position(){
	var brouser_width = $(document).width();
	var brouser_height = $(document).height();
	$("#main_block").css({left:brouser_width / 2 - 500});
}
function view_products(id){
	var url_section_full = "view_products.php?id=" + id;
	$("#prod_content").load(url_section_full);
}
function view_spare_parts(id){
	var url_section_full = "view_spare_parts.php?id=" + id;
	$("#spare_parts_content").load(url_section_full);
}

function setCookie(name, value) {
      var valueEscaped = escape(value);
      var expiresDate = new Date();
      expiresDate.setTime(expiresDate.getTime() + 1 * 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));
}

function replace_string(txt,cut_str,paste_str){ 
	var f=0;
	var ht='';
	ht = txt;
	f=ht.indexOf(cut_str);
	while (f!=-1){ 
		//цикл для вырезания всех имеющихся подстрок 
		f=ht.indexOf(cut_str);
		if (f>0){
			ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);
		};
	};
	return ht;
};

var now;

function vis(id,cat_id,mod_name,clicker){
	if(getCookie(mod_name)==null){
		setCookie(mod_name,id);
	}
	$("#"+now).removeClass("active").addClass("not_active");	
	now = clicker;
	setCookie(mod_name+"_active",now);
	$("#"+clicker).removeClass("not_active").addClass("active");
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display == "none"){
			$('#'+id).animate({opacity:'0.1'},1);
			$('#'+id).slideDown('fast',function(){
				$('#'+id).animate({opacity:'1.0'},300);
				var sp_p = getCookie(mod_name);
				var sp_p_a = sp_p.split(" ");
				if($.inArray(id, sp_p_a) != "-1"){
					//alert("Он уже есть");
				}else{
					setCookie(mod_name,sp_p+" "+id);
				}
			});
		}else{
			$('#'+id).animate({opacity:'0.1'},300,function(){
				$('#'+id).slideUp('fast');
				if(getCookie(mod_name)!=null){
					var sp_p = getCookie(mod_name);
					if(sp_p.indexOf(id)=="0"){
						sp_p = sp_p.substring(id.length);
					}else{
						sp_p = replace_string(sp_p,id,'.');
					}
					setCookie(mod_name,sp_p);
				}
			});
		}
	}
	var url_section_full = "view.php?id=" + cat_id + "&mod_name=" + mod_name;
	$("#vrem").load(url_section_full,function(){							  
		if($("#vrem").html()=="false"){
			$("#vrem").empty();
		}else{
			$("#"+mod_name+"_content").html($("#vrem").html());
			$("#vrem").empty();
		}
	});
}