 /**
  * Движение вертикального меню
  *
  */

function ActionVerticalMenu(id) {


  var element_id = 'menuv_' + id;
  var contant_id = 'menuv_content_' + id;
  //var params = $(contant_id).getDimensions();

  if ($(contant_id).style.display == 'none') {
  //$(contant_id).style.height = '0px;'
  $(contant_id).show();
   /* var attributes = {
       height: { from: 0, to: params.height },
    };
    var anim = new YAHOO.util.Anim(contant_id, attributes);
    anim.animate();*/

  } else {
    $(contant_id).hide();
  }

}


function contacts(id)
{
	  var req = new Ajax.Request('/contacts/city', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
      //alert(t.responseJSON.map);
        document.getElementById("map_img").src=t.responseJSON.map;
        document.getElementById("cont").innerHTML=t.responseJSON.body;;
        $$('#horiz-menu li').each(function(s){
          s.removeClassName('act-item');
        });
        ($$('#horiz-menu li')[id-1]).addClassName('act-item');
      }
    });
}


function libshow(alias)
{
	  var req = new Ajax.Request('/library/show', {
      method: 'post',
      parameters: {alias: alias },
      onSuccess: function (t) {
      //alert(t.responseJSON.map);
        document.getElementById("lib").innerHTML=t.responseJSON.body;
        $$('#horiz-menu li').each(function(s){
          s.removeClassName('act-item');
        });
        $('lib_li_'+alias).addClassName('act-item');
      }
    });
}

function addbasket(id)
{
	  var req = new Ajax.Request('/store/addbasket', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
      //alert(t.responseJSON.map);
        document.getElementById("bas_in_" + id).style.display='none';
        addDopStat();        
      }
    });
}

function addbasketsubscribe(id)
{
    var req = new Ajax.Request('/subscribe/addbasket', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
        document.getElementById("bas_in_" + id).style.display='none';
        addDopStat();
      }
    });
}

var summ=0;
function setBasketAmount(amount,id)
{
  summ = all_summ;
  
  if (!amount) {
    amount = 0;
  }

  //alert(summ);
	  var req = new Ajax.Request('/basket/setamount', {
	 
      method: 'post',
      parameters: {id: id , amount: amount},
      onSuccess: function (t) {
        summ = summ - (basket[id + '_am']*basket[id + '_pr']);
        summ = summ + amount*basket[id + '_pr']; 
        basket[id + '_am'] = amount;
        
        all_count = 0;
        for(i=0;i<ids.length;i++){ 
          all_count = parseInt(basket[ids[i]+ '_am']) + parseInt(all_count);
        }
        
        document.getElementById("summ").innerHTML=summ;       
        document.getElementById("basket_count").innerHTML = all_count;
        addDopStat();
        all_summ = summ;
      }
    });
}

function basketDel(id)
{
	  var req = new Ajax.Request('/basket/delete', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
      //alert(t.responseJSON.map);
     summ = all_summ;
      summ = summ - (basket[id + '_am']*basket[id + '_pr']);
      document.getElementById("summ").innerHTML=summ;
        document.getElementById("item_" + id).style.display='none';
        all_summ = summ;
        
         reAmount(id);
         addDopStat();
      }
    });
}

function subscribeDel(id)
{
    var req = new Ajax.Request('/subscribe/delete', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
      summ = all_summ;
      summ = summ - (basket[id + '_am']*basket[id + '_pr']);
        document.getElementById("summ").innerHTML=summ;
        document.getElementById("item_" + id).style.display='none';
        document.getElementById("item_" + id).innerHTML='';
         
        reAmount(id);
        addDopStat();
      }
    });
}

function reAmount(id) {
  
  basket[id + '_pr'] = '0';
  basket[id + '_am'] = '0';
  
  all_count = 0;
  for(i=0;i<ids.length;i++){
   all_count = parseInt(basket[ids[i]+ '_am']) + parseInt(all_count);
  } 
  document.getElementById("basket_count").innerHTML = all_count;
  addDopStat();
}

var menu_opened = new Array;
function ArticleMenuAjax(id){
  if(menu_opened[id] == 1){
    document.getElementById("artcont_" + id).style.display="none";
    menu_opened[id] = 2;
  }else{
    if(menu_opened[id] == 2){
      document.getElementById("artcont_" + id).style.display="";
      menu_opened[id] = 1;
    }else{
  	  var req = new Ajax.Request('/articles/ajax', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
        document.getElementById("artcont_" + id).innerHTML=t.responseJSON.htmlResult;
      }
      });
      menu_opened[id] = 1;
    }
  }
}



function checkKey(evt,id)
{
	var key;
	//pref = '';
	var el=document.getElementById(id);
	var cur_pos=getCaretPosition(el);
	var prev_val=el.value;
	var chk0=0

	if(brows=='Firefox'||brows=='Netscape'||brows=='Mozilla')
	{
		key=evt.charCode;
		if(key==0)
		{
			key=evt.keyCode;
		}
	}
	else
	{
		key=evt.keyCode;
	}

	if((key<48&&key!=46&&key!=8&&key!=37&&key!=39)||key>57||(key==48&&prev_val==''&&!chk0)||(cur_pos==0&&key==48&&!chk0))
	{
		return false;
	}
  
	//setPrice(val,id);
	return true;
}

function getCaretPosition(el)
{
	if (document.selection)
	{
		// IE
		var range = document.selection.createRange();
		range.moveStart('textedit', -1);
		return range.text.length;
	}
	else if (el.selectionStart)
	{
		// Gecko
		return el.selectionStart;
	}
	return el.value.length-1;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{

		string: navigator.vendor,

		subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
var brows=BrowserDetect.browser;

var default_search = 'Поиск';
function login_fild()
{
  var login_fild = document.getElementById('top-search');
  if (login_fild.value == default_search) {
    login_fild.value = '';
  }
}
function login_blur()
{
  var login_fild = document.getElementById('top-search');
  if (!login_fild.value) {
    login_fild.value = default_search;
  }  
}


function catbigimg(url)
{
  //alert(url);
  document.getElementById('img_big').src = url;  
}

function podp(csd)
{
  switch(csd){
    case 'ur': //document.getElementById('id__lico').value='ur'; 
    document.getElementById('lico_ch').style.display='none'; 
    document.getElementById('lico_ur').style.display=''; 
    break;
    case 'ch': //document.getElementById('id__lico').value='ch'; 
    document.getElementById('lico_ur').style.display='none'; 
    document.getElementById('lico_ch').style.display=''; 
    break;
  }
    $$('#horiz-menu li').each(function(s){
          s.removeClassName('act-item');
        });
        $(csd).addClassName('act-item');    
  
}

function confirmOrder(id)
{
   var req = new Ajax.Request('/basket/confirm', {
      method: 'post',
      parameters: {id: id },
      onSuccess: function (t) {
      //alert(t.responseJSON.map);
     
        document.getElementById("buttons").style.display='none';
      }
    });
  
}

function addDopStat() {
//alert('@@');
  	  var req = new Ajax.Request('/basket/setamountall', {
	 
      method: 'post',
      parameters: {},
      onSuccess: function (t) {
       //alert('#');
       //document.getElementById("basket_inform").innerHTML = 'ваш заказ (0 шт.)';
        var count = t.responseJSON.count;
        //alert(count);
    
   var word;   
    if ((count%10 == 1 && count > 20) || count == 1) {
      word = "наименование";
    }
    if (count%10 < 1 || count%10 >= 5) {
      word = "наименований";
    }
    if ((count%10 >= 2 && count%10 <= 4) && ( count < 10 || count > 20)) {
      word = "наименования";
    }
    if (!word) {
      word = 'наименований';
    }
    
    
        if (parseFloat(count) > 0) {
          document.getElementById("basket_inform").innerHTML = 'ваш заказ (' + count +' '+ word +')';
        } else {
          document.getElementById("basket_inform").innerHTML = '';
        }
      }
    });  
}
function tabsReplace(p) {
	if('1' == p||'2' == p) {
		var f = p==2?1:2;
		document.getElementById("link"+f).className='';
		document.getElementById("list"+f).className='';
		document.getElementById("link"+p).className='act';
		document.getElementById("list"+p).className='act';
	}
	else {
		document.getElementById("list"+p).className=document.getElementById("list"+p).className=='act'?'':'act';
	}
}

function addEmail(email)
{
    var req = new Ajax.Request('/spam/addemail', {
      method: 'post',
      parameters: { email: email },
      onSuccess: function (t) {
        //alert(t.responseJSON.body);
        
        if (t.responseJSON.body == '1') {
          $("success1").style.display = 'block';
          $("subscribe1").style.display = 'none';
          $("subscribe2").style.display = 'none';
          $("subscribe3").style.display = 'none';
         
        }
        if (t.responseJSON.body == '-1') {
          $("success2").style.display = 'block';
          
          $("subscribe1").style.display = 'none';          
          $("subscribe3").style.display = 'none';
        }
        if (t.responseJSON.body == '-2') {
          $("success3").style.display = 'block';
                   
          $("subscribe2").style.display = 'none';
          $("subscribe3").style.display = 'none';
          
        }
      }
    });
}

function addCard()
{
    var email = $('emailrtext6').value;
    var secondname = $('secondnamertext2').value;
    var name = $('namertext3').value;
    var fathername = $('fathernamertext4').value;
    var telephone = $('telephonertext5').value;
    var card = $('cardrtext7').value;

    //alert(email + secondname + name + fathername + telephone + card);
    var req = new Ajax.Request('/spam/addcard', {
      method: 'post',
      parameters: { 
	      email:email, 
	      name:name, 
	      secondname:secondname, 
	      fathername:fathername, 
	      telephone:telephone,
	      card:card 
      },
      onSuccess: function (t) {
        if (t.responseJSON.body) {
        tabsReplace('4');
        alert('Спасибо!');
        }
      }
    });
}

function reSummCard() {
  
  all_summ = 0;
  all_count = 0;
  var cardids = '';
  
  for(i=0;i<pricecard.length;i++){
   
   if ($('add_' + ids[i]).checked == true) {        
     all_summ += parseInt(pricecard[i])*parseInt($('itemam_' + ids[i]).value);
     all_count += parseInt($('itemam_' + ids[i]).value);
     
     cardids = ids[i]+ '&' +parseInt(pricecard[i]) +'&'+ parseInt($('itemam_' + ids[i]).value) +';' + cardids; 
   }
  }
  //alert(all_summ); 
  
  document.getElementById("basket_count").innerHTML = all_count;
  document.getElementById("summ").innerHTML = all_summ;
  
  $('id__summh').value = all_summ;
  $('id__basket_counth').value = all_count;
  $('id__cardids').value = cardids;
  //alert(cardids);
}
