var clickelem="#fakeap1";

function changeOnPages()
{
	var f_m=$('#getform');
	var v=$('#onpageselect').val();
	var f=$('#dopfield');
	f.attr('name','onpagesinput');
	f.val(v);
	f_m.submit();

}

function changeOrder(name)
{
	var f_m=$('#getform');
	var v=$('#onpageselect').val();
	var f=$('#dopfield');
	f.attr('name','onpagesinput');
	f.val(v);
	f_m.submit();

}

function doWS()
{
	var sf=$("#sform");
	var sw=$("#sw");
	sf.attr("action","/clocks/");
	var v=sw.val();
	if (v!="")
	{
		sf.submit();
	}

}

function doAS()
{
	var sf=$("#sform");
	var sw=$("#sw");
	sf.attr("action","/asearch/");
	var v=sw.val();
	if (v!="")
	{
		sf.submit();
	}

}

function clientClose()
{
	var client=$("#client-menu");
	var aclient=$("#aclient-menu"); 
	 client.css("visibility", "hidden"); 
	 client.css("display", "none");
	  aclient.removeClass('arrow-up'); 
	  aclient.addClass('arrow-down'); 
	 aclient.attr("onClick",'clientOpen()');
}

function clientOpen()
{
	var client=$("#client-menu");
	var aclient=$("#aclient-menu"); 
	 client.css("visibility", "visible"); 
	 client.css("display", "block");
     aclient.removeClass('arrow-down'); 
	 aclient.addClass('arrow-up');
	 aclient.attr("onClick",'clientClose()');
}

function articleClose()
{
	var client=$("#article-menu");
	var aclient=$("#aarticle-menu"); 
	 client.css("visibility", "hidden"); 
	 client.css("display", "none");
	  aclient.removeClass('arrow-up'); 
	  aclient.addClass('arrow-down'); 
	 aclient.attr("onClick",'articleOpen()');
}

function articleOpen()
{
		var client=$("#article-menu");
	var aclient=$("#aarticle-menu"); 
	 client.css("visibility", "visible"); 
	 client.css("display", "block");
     aclient.removeClass('arrow-down'); 
	 aclient.addClass('arrow-up');
	 aclient.attr("onClick",'articleClose()');
}


function countryOpen(objid)
{
	var country=$("#country"+objid);
	var acountry=$("#acountry"+objid);
	
	 if (country.attr("class")=='sel')
	  {
		  country.removeClass('sel'); 
		  acountry.attr("title",'Нажмите, чтобы раскрыть');
	  }	 else
	  if (country.attr("class")=='hover')
	  {
		  country.removeClass('hover'); 
		  acountry.attr("title",'Нажмите, чтобы раскрыть');
	  }	  
	  else if (country.attr("class")=='')
	  {
		  country.addClass('hover'); 
		  acountry.attr("title",'Нажмите, чтобы закрыть');
	  } 
	 
	 
	

}

function switchMainPic(objid,picbig,picsuperbig)
{
	 var act_a=$("#"+objid);
	 var mainpic=$("#mainpic");
	 clickelem=$("#fake"+objid);
	 $(document).find(".thumb").each(function()
			  {
		      $(this).removeClass('active'); 
		      });
	 /*
	 $(document).find(".fake").each(function()
			  {
		      $(this).attr('onclick','return hs.expand(this)');
		      });
		       act_fakea.attr('onclick','');
	 */
	 act_a.addClass('active');
	 mainpic.attr({src:picbig});
	 
	

}

function SuperClick()
{
	
	 var act_fakea=$(clickelem);
	 act_fakea.click();
}


function orderBasketPre()
{
  var o_fio=$("#o_fio").val();
  var offset=$("#orderButton").offset();
  var o_phone=$("#o_phone").val();
  var o_email=$("#o_email").val();
  var o_town=$("#o_town").val();
  var o_index=$("#o_index").val();
  var o_address=$("#o_address").val();
  var o_date=$("#o_date").val();
  var o_prim=$("#o_prim").val();
  var error=false;
  var errortext='';
  
  if (o_fio.length<5)
  {		
	  errortext=errortext+'<li>Не введена фамилия<br/></li>';
	  error=true;
  }
  if (o_phone.length<4)
  {		
	  errortext=errortext+'<li>Не введен телефон</li>';
	  error=true;
  }
  
  if (error==true)
  {
	  offset.top=offset.top-200;
	  offset.left=offset.left-200;
	  show_modal_info(errortext,offset);
  }
  else
  {
	  $.post('/ajaxcheckout',
     { postorder : "ok",o_fio: o_fio,o_phone:o_phone,o_email:o_email,
	  o_town:o_town,o_index:o_index,o_address:o_address,o_date:o_date,o_prim:o_prim	  
     },orderBasketPost);
	  
  }	  
  
  	 return true; 
}

function orderBasketPost(xml)
	{
	 
	  $(xml).find("ordersuccess").each(function()
			  {
		  
			
			
			var order=$(".order");
			order.css("visibility", "hidden"); 
		 	order.css("display", "none");
		 	var bdiv=$("#bdiv"); 
			bdiv.html('<div class="basket-link">Корзина <a href="javascript:void(0)"><img src="/media/images/basket-icon2.png" alt="" /></a></div>')
		 
			  });
	  $(xml).find("errororder").each(function()
			  {
		  
		  alert("Ошибка при оформлении заказа");
			  });
}


function deleteFromBasket(id)
{
  var b_f=$("#b_f");
  var delete_but=$("#delete_but");
  var action_mode=$("#action_mode");
  	delete_but.attr("name","delete[]");
  	delete_but.val(id);
  	action_mode.attr("name","mode");
  	action_mode.val("basketdelete");
  	b_f.submit();
  	 return true; 
}
function recalcBasket(id)
{
  var b_f=$("#b_f");
  var delete_but=$("#delete_but"); 
  	delete_but.attr("name","recalc");
  	delete_but.val("yes");
  var action_mode=$("#action_mode");
    action_mode.attr("name","mode");
  	action_mode.val("basketrecalc");
  	b_f.submit();
  	 return true; 
}
function show_modal_info(html,offset)
{
	 
 var show_modal=$(".modal-form-info");
 var cont=$("#mfi-cont");
 	 cont.html(html);
     show_modal.css("top", offset.top+'px');
 	 show_modal.css("left", offset.left+'px');
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  
 	 return true;

}
function hide_modal_info()
{
  var show_modal=$(".modal-form-info"); 
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
 	
  	 return true; 
}
function hideQuestion()
{
  var show_modal=$(".modal-feedback"); 
  $("#w_q_n").val("");
  $("#w_q_ep").val("");
  $("#w_q_t").val("");
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}

function showQuestion(offset)
{
  var show_modal=$(".modal-feedback");
  var offset=$(".ask").offset();
	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
	 show_modal.css("top", offset.top+50+'px');
	 show_modal.css("left", offset.left-100+'px');
  	 return true; 
}

function submitQuestionPre()
{
 
 var id=$("#w_q_id").val();
 var name=$("#w_q_n").val();
 var emailphone=$("#w_q_ep").val();
 var msg=$("#w_q_t").val();
 
  if ((name!='') && (emailphone!='') && (id!='') && (msg!=''))
  {
	  $.post('/ajaxquestion',
		{ question : "ok",o_id:id,o_name: name,o_emailphone:emailphone,o_msg: msg	  
			     },submitQuestionPost);
	  
  }
  else
  {
	  alert('Не введены необходимые поля');
  }	  
   
}

function submitQuestionPost(xml)
{
	  $(xml).find("questionsuccess").each(function()
			  { 
			alert("Ваш запрос успешно отправлен");
			hideQuestion();
			  });
	  $(xml).find("questionerror").each(function()
			  { 
		  alert("Ошибка");
			  });
}

function show_modal_callback()
{
	 
 var show_modal=$(".modal-callback");
 var offset=$(".feedback").offset();
 	 show_modal.css("top", offset.top-24+'px');
 	show_modal.css("left", offset.left-120+'px');
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  
 	 return true;

}

 


function show_modal_callbackfooter()
{
	 
 var show_modal=$(".modal-callback");
 var offset=$(".footercallback").offset();
 	 show_modal.css("top", offset.top-100+'px');
 	 show_modal.css("left", offset.left-50+'px');
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  
 	 return true;

}


function submit_modal_callbackPre()
{
 
 var name=$("#mc_n").val();
 var phone=$("#mc_p").val();
  if ((name!='') && (phone!=''))
  {
	  $.post('/ajaxcallback',
		{ postcallback : "ok",o_name: name,o_phone:phone	  
			     },submit_modal_callbackPost);
	  
  }
  else
  {
	  alert('Ошибка');
  }	  
   
}

function submit_modal_callbackPost(xml)
{
	  $(xml).find("callbacksuccess").each(function()
			  { 
			alert("Ваш запрос успешно отправлен");
			hide_modal_callback();
			  });
	  $(xml).find("callbackerror").each(function()
			  { 
		  alert("Ошибка");
			  });
}



function hide_modal_callback()
{
	 var show_modal=$(".modal-callback"); 
	 $("#mc_n").val("");
	 $("#mc_p").val("");
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}


function switchFavPre(id)
{
	hide_modal_fav();
	$.post('/switchfav',{type: "fav",id: id},switchFavPost);
}

function switchFavWarePre(id)
{
	 $.post('/switchfav',{type: "fav",id: id},switchFavWarePost);
}

function switchOrderPre(id)
{

	$.post('/switchorder',{type: "order",id: id},switchOrderPost);
}
function switchOrderPost(xml)
{
	  $(xml).find("order").each(function()
			  {
		  
			var idware = $(this).attr('id'); 
			var count_ware = $(this).attr('count_ware');
			var count_price = $(this).attr('count_price'); 
		 	 
			var bor=$("#bor"+idware);
			var offset = bor.offset();
				
			 
			
		 	 bor.html('<img src="/media/images/basket-icon.png" alt="">Уже в корзине');
		  
		 	var bdiv=$("#bdiv"); 
			
			if (count_ware>0)
			{
				 
				bdiv.html('<div class="basket-link"><a href="/basket">Корзина</a><a href="/basket"><img src="/media/images/basket-icon.png" alt="" /></a></div>'
				+'<ul class="basket-info"><li> <span>'+count_ware+'</span>Товаров в корзине: </li>'
				+'<li> <span>'+count_price+' руб.</span>Сумма заказа: </li></ul>');
				show_modal_added(count_ware,count_price,offset);
				
			}
			else
			{
		 
			bdiv.html('<div class="basket-link">Корзина пуста<a href="javascript:void(0)"><img src="/media/images/basket-icon2.png" alt="" /></a></div>')
			}
			
			});
}


function show_modal_added(count_ware,count_price,offset)
{
	hide_modal_added();
 var show_modal=$(".modal-added");
 	 show_modal.css("top", offset.top-150+'px');
 	show_modal.css("left", offset.left-60+'px');
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
 var ma_count=$(".ma_count");
 	 ma_count.text(count_ware);	
 var ma_price=$(".ma_price");
 	 ma_price.text(count_price);
 	 return true;

}


function hide_modal_favadd_ware()
{
  var show_modal=$(".favorite-add"); 
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}

function show_modal_favadd_ware()
{
  var show_modal=$(".favorite-add"); 
	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  	 return true; 
}

function hide_modal_favdel_ware()
{
  var show_modal=$(".favorite-delete"); 
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}

function show_modal_favdel_ware()
{
  var show_modal=$(".favorite-delete"); 
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  	 return true; 
}


function hide_modal_added()
{
  var show_modal=$(".modal-added"); 
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}

function show_modal_fav(offset,flag)
{
	
 var show_modal=$(".popup-fav");
 var show_modal_text=$(".pf_t");
 if (flag)
 {
	 show_modal_text.html('Товар добавлен <br/>  в избранное');
 }
 else
 {
	
	 show_modal_text.html('Товар удален <br/>  из избранного');
 }
 	 show_modal.css("top", offset.top-50+'px');
 	show_modal.css("left", offset.left-160+'px');
 	 show_modal.css("visibility", "visible"); 
 	 show_modal.css("display", "block");
  
 	 return true;

}


function hide_modal_fav()
{
  var show_modal=$(".popup-fav"); 
 	 show_modal.css("visibility", "hidden"); 
 	 show_modal.css("display", "none");
  	 return true; 
}

function switchFavPost(xml)
{
	 
	 $(xml).find("fav").each(function()
			  {
		  
			var idfav = $(this).attr('id'); 
		 	var count = $(this).attr('count'); 
		 	
		 	var favlink=$("#favlink");
		 	
		 	if (count>0)
		 	{
		   favlink.text('('+count+' шт.'+')');
		 	}
		 	else
		 	{
		 	favlink.text('&#160;');
		 	}
		 	 
		 	var afav=$("#fav"+idfav);
		 	var offset = afav.offset(); 
		 	afav.attr('title','Удалить из избранного');
		 	afav.attr("class","favorite true");
		 	show_modal_fav(offset,true); 
	 
			  });
	 $(xml).find("favdel").each(function()
			  {
			var idfav = $(this).attr('id'); 
		 	var count = $(this).attr('count'); 
		 	 
			var favlink=$("#favlink");
		 	if (count>0)
		 	{
		 	favlink.text('('+count+' шт.'+')');
		 	}
		 	else
		 	{
		 	favlink.text('&#160;');
		 	}
		 	var afav=$("#fav"+idfav);
		 	afav.attr('title','Добавить в избранное');
		 	afav.attr("class","favorite");
		 	var offset = afav.offset(); 
		 	show_modal_fav(offset,false); 	
			  });
 
	 
 
}
function switchFavWarePost(xml)
{
	 
	 $(xml).find("fav").each(function()
			  {
		  
			var idfav = $(this).attr('id'); 
		 	var count = $(this).attr('count'); 
		 	
		 	var favlink=$("#favlink");
		 	
		 	if (count>0)
		 	{
		   favlink.text('('+count+' шт.'+')');
		 	}
		 	else
		 	{
		 	favlink.html('&#160;');
		 	}
		 	 
		 	var afav=$("#fav"+idfav);
		  	afav.attr('title','Удалить из избранного');
		 	afav.attr("class","favorite");
		 	var but=$(".favorite-add");
		 	var offset = but.offset(); 
		 	show_modal_fav(offset,true); 
		 	hide_modal_favadd_ware();
		 	show_modal_favdel_ware();
		 	return true;
	 
			  });
	 $(xml).find("favdel").each(function()
			  {
			var idfav = $(this).attr('id'); 
		 	var count = $(this).attr('count'); 
		 	 
			var favlink=$("#favlink");
		 	if (count>0)
		 	{
		 	favlink.text('('+count+' пїЅпїЅ.'+')');
		 	}
		 	else
		 	{
		 	favlink.html('&#160;');
		 	}
		 	var afav=$("#fav"+idfav);
		 	afav.attr('title','Добавить в избранное');
		 	afav.attr("class","nofavorite");
		 	var but=$(".favorite-delete");
		 	var offset = but.offset(); 
		 	show_modal_fav(offset,false);
		 	hide_modal_favdel_ware();
		 	show_modal_favadd_ware();	
			  });
 
	 
 
}

function dosimplesearch()
{
	$("#simplesearch").submit();
}
function doextsearch()
{
	$("#es_f").submit();
}


function openclose(open){
 var d=$("#open-close"); 
 var listcont=$("#listcont");  
 if (open==1)
 {
	
	 d.attr("class","sidebar left-bar");
	/*
	  listcont.css("margin-right", "10%"); 
	 listcont.css("margin-left", "10%"); 
	 */
 }else
  if (open==3)
 {
	
	 d.attr("class","sidebar left-bar");
	/*
	  listcont.css("margin-right", "3%"); 
	 listcont.css("margin-left", "3%"); 
	 */
 }
  else if (open==2)
 {
	
	 d.attr("class","sidebar left-bar closed");
	 /*
	  listcont.css("margin-right", "5%"); 
	 listcont.css("margin-left", "5%"); 
	*/
 }
 else
 {
if (d.attr("class")=="sidebar left-bar closed")
{
	d.removeClass('closed');
	 
}
else
{
	d.addClass('closed');
	/*
	 listcont.css("margin-right", "5%"); 
	 listcont.css("margin-left", "5%");  
*/
}
 }
 
 centerProductDiv();	
}

function resizeOpenClose()
{
 var w=$(document).width();
 
 if (w>1400)
{
	 
		openclose(3);
}
	else
	if (w>1300)
{
	 
		openclose(1);
}else
	if (w>1200)
{
	 
		openclose(3);
}
	else
	{
	
		openclose(2);
	}
	

}

function openBlock(btnOpen, boxOpen){
	var flag = true;
	$(btnOpen).click(function(){
		if(flag){
			flag = false;
			if ($(boxOpen).is(':hidden')) {
				$(this).removeClass('close-box');
				$(boxOpen).slideDown(500, function(){flag = true;});
			}
			else {
				$(this).addClass('close-box');
				$(boxOpen).slideUp(500, function(){flag = true;});
			}
		}
		return false;
	});
}
function addBlock(){
	var btnAdd = $('a.btn-add-box');
	var _blocks = $('div.box-hide > div.box');
	var deleteBox = $('a.delete-box');
	var _count = _blocks.index(_blocks.filter(':last'));
	var _active = _blocks.index(_blocks.filter('.active-box:eq(0)'));
	if (_active > -1) _active++;
	var _bug = true;
	
	btnAdd.click(function(){
		if (_active <= (_count)){
			_blocks.each(function(){
				var hol = _blocks.index($(this));
				if(($(this).is('.close-box')) && _bug){
					_blocks.eq(hol).animate({
						marginTop: 0
					}, 500).removeClass('close-box');
					_bug = false;
				}
			});
			deleteBox.eq(_active-1).hide();
			_active++;
			_bug = true;
		}
		return false;
	});
	deleteBox.click(function(){
		_active--;
		act = deleteBox.index($(this));
		_blocks.eq(act).animate({
			marginTop: -230 + "px"
		}, 500, function(){
			deleteBox.eq(_active-1).show();
		}).addClass('close-box');
		_bug = true;
		return false;
	});
}
$(document).ready(function(){
	openBlock('a.open-form-tools', 'div.form-tools');
	addBlock();
	$("input.datepick").datepicker({
		showOn: 'button',
		buttonImage: 'images/ico-calendar.gif', 
		buttonImageOnly: true,
		changeYear: true,
		yearRange: '2009:2011'
	});
	$(function() {
		$(".modal-callback").draggable();
		$(".modal-added").draggable();
		$(".popup-fav").draggable();
		$(".modal-feedback").draggable(); 
	});
	
	 
	
	resizeOpenClose();
	centerProductDiv();	
});
$(window).resize(function() {
	resizeOpenClose();
	centerProductDiv();	
	});

function centerProductDiv()
{
	 
	var lc=$("#listcont");
	lc.css("padding-left","0px"); 
	lc.css("padding-right", "0px"); 
	var w=lc.width();
	 
	var liw=230;
	var num=w%liw;
	//alert('w '+w+'liw '+liw+' num '+num);
	var pad=Math.floor((num)/2); 
	lc.css("padding-left", pad+"px"); 
	lc.css("padding-right", pad-40+"px"); 
	 

}


 
function escapeCyrillic(str)
{
	// пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ
	var trans = [];
	for (var i = 0x410; i <= 0x44F; i++)
	  trans[i] = i - 0x350; // пїЅ-пїЅпїЅ-пїЅ
	trans[0x401] = 0xA8;    // пїЅ
	trans[0x451] = 0xB8;    // пїЅ
	var ret = [];
  // пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ, пїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ пїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅпїЅ
  for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
     if (typeof trans[n] != 'undefined')
      n = trans[n];
    if (n <= 0xFF)
      ret.push(n);
  }
  return escape(String.fromCharCode.apply(null, ret));
}



hs.graphicsDir = '/js/highslide/graphics/';

hs.registerOverlay(
	{
		thumbnailId: null,
		overlayId: 'controlbar',
		position: 'bottom right',
		hideOnMouseOut: true
	});
	 hs.wrapperClassName='highslide-white';
	 hs.outlineType='rounded-white';
	 
	

hs.showCredits = false;

hs.wrapperClassName = 'wide-border';
hs.captionId = 'the-caption';
 