var isIE = window.navigator.userAgent.indexOf("MSIE")>-1;
var GlassWindow=null;
var Dialog=null;

function jsSearch() {
	var quer=$('#imageSearch').val();
	quer=quer.replace(/[^a-zA-Zа-я-А-ЯёЁ0-9 ]+/ig," ");
	if (quer!="") location.href='/search/'+$.trim(quer);
}

function search_click(obj) {
  if (obj.value=="Поиск ") {
    obj.value="";
    obj.style.color="#d0d0d0";
  }
}

function goodsEvent(method,obj) {
  if (method=="over") obj.style.border = "1px solid #b06060";
  if (method=="out") obj.style.border = "1px solid #505050";
}

function checkform(arg,id) {
  if (arg.sizeop.value==-1) changePrice(arg.sizeop,id);
  else {
    $.prettyLoader.show();
	var str = $("#form"+id).serialize();
	$.post("/addgood.php", str, function(data) {
		$('#cart_mini').html(data);
		ShowGlassWindow(true);
		$("#process").slideDown(300);
		$("#headline").text("Товар добавлен в корзину");
		$.prettyLoader.hide();
	}
	);
  }
}

function checkform_mobile(arg,id,offset) {
  if (arg.sizeop.value==-1) changePrice(arg.sizeop,id);
  else {
    $.prettyLoader.show();
	var str = $("#form"+id).serialize();
	$.post("/addgood.php", str, function(data) {
		$('#cart_mini').html(data);
		ShowGlassWindow(true);
		$("#process").slideDown(300).css('top',offset.top+100+'px');
		$("#headline").text("Товар добавлен в корзину");
		$.prettyLoader.hide();
	}
	);
  }
}

function changePrice(obj,id) {
  var name="price"+id;
  if (obj.value==-1) $("#price"+id).fadeTo(150,0).queue(function(){$(this).html("Выберите размер!");$(this).dequeue();}).fadeTo(300,1);
  else {
    var price = $("#cusel-scroll-sel_size_"+id+" > span[value="+obj.value+"]").attr("class").split(' ');
	$("#price"+id).fadeTo(150,0).queue(function(){$(this).html(price[0] + " руб");$(this).dequeue();}).fadeTo(300,1);
  }
}

function ShowGlassWindow(show){
	if(GlassWindow==null){
	GlassWindow=document.createElement('DIV');
	$(GlassWindow).click( function() {$('#process').slideUp(300);ShowGlassWindow(false);});
	with(GlassWindow.style){
	  display='none';
	  position='fixed';
	  height=0; width=0;
	  zIndex=19;
	  if(isIE){
	    backgroundColor = '#000000';
	    filter="progid:DXImageTransform.Microsoft.Alpha(Opacity=50, Style=0)";
	  }
	  else //для остальных браузеров фон из полупрозрачного PNG
	    backgroundColor = '#000000';
	    opacity = 0.5;
	  }
	}
	if(show){
	  document.body.appendChild(GlassWindow);
	  with(GlassWindow.style){
	    left = top = 0;
	    width = '100%';
	    height = '100%';
	  }
	}
	GlassWindow.style.display=show?'block':'none';
}

function showMore(arg) {
  document.getElementById(arg).style.display="block";
}
function hideMore(arg) {
  document.getElementById(arg).style.display="none";  
}


function arrowCart(method,n,price,month,caller) {
  var obj="cInput"+n;
  var sum=0;
  var totalSum=$("#cTotal").html().split(",");
  var tSum=totalSum[0];
  
  if (month==1) sum=document.getElementById(obj).value*price;
  else sum=document.getElementById(obj).value*price;
  tSum=tSum-sum;
  
  if (method=="up") document.getElementById(obj).value=parseInt(document.getElementById(obj).value)+1;
  if (method=="down" && parseInt(document.getElementById(obj).value)!=0) document.getElementById(obj).value=parseInt(document.getElementById(obj).value)-1;

  if (month==1) sum=document.getElementById(obj).value*price;
  else sum=document.getElementById(obj).value*price;
  $("#cSum"+n).html(sum+",00 руб");
  
  tSum=tSum+sum;
  $("#cTotal").html(tSum+",00 руб");
}

function showPay(arg) {
  if (arg=="cash") {
    $('#PAY_NSK').hide(600);
	$('#PAY_SPB').hide(600);
    $('#PAY_CASH').show(600);
	$('#el_pay_btn').fadeIn(300);
	$('#rad_paycash').attr('disabled','disabled').animate({opacity: 0.3}).parent().find('label').animate({opacity: 0.3});
	$('#rad_electron').attr('checked','checked');
  }
  if (arg=="nsk") {
    $('#PAY_CASH').hide(600);
	$('#PAY_SPB').hide(600);
    $('#PAY_NSK').show(600);
	$('#el_pay_btn').fadeOut(300);
	$('#rad_paycash').removeAttr('disabled').animate({opacity: 1}).parent().find('label').animate({opacity: 1});
  }
  if (arg=="spb") {
    $('#PAY_CASH').hide(600);
	$('#PAY_NSK').hide(600);
    $('#PAY_SPB').show(600);
	$('#el_pay_btn').fadeOut(300);
	$('#rad_paycash').removeAttr('disabled').animate({opacity: 1}).parent().find('label').animate({opacity: 1});
  }
  $("#rad_"+arg).attr("checked","checked");
}

function suggGo(cid,name) {
	var loc=document.location+"";
	var loc_arr = new Array();
	loc_arr = loc.split('/');
	var domen = loc_arr[0]+"/"+loc_arr[1]+"/"+loc_arr[2]+"/";
	location.href=domen+"shop/cat"+cid+"/"+name+"/";
}


function recount() {
$("#hiddenorder").attr("value","no");
$("#cartForm").submit();
}

function cart_sbm() {
$("#cartForm").submit();
}


function makeOrder(arg) {
var name=$("#orderForm #name").val();
var email=$("#orderForm #email").val();
var phone=$("#orderForm #phone").val();
var city=$("#orderForm #city").val();
var ind=$("#orderForm #ind").val();
var address=$("#orderForm #address").val();
var payment=$('input:radio[name=payment]:checked').val()
var deliv=$("#orderForm [name='delivery']:checked").val();
if (deliv=="nsk" || deliv=="spb") {address="-"; ind="-"; city="-";}
if (payment!="paycash" && payment!="electron") alert ("Выберите способы доставки и оплаты!");
else if (name!="" && email !="" && phone!="" && city!="" && address!="" && ind!="" && deliv!="" && payment!="") $("#orderForm").submit(); //заказ отсылается тут
else alert("Заполните все поля!");
}

function goodsLoad(obj,link,add) {
	$.prettyLoader.show();
	$(obj).parent().children(".submenu").removeClass("sm_active").addClass("sm_inactive");
	$(obj).removeClass("sm_inactive").addClass("sm_active");
	var cut=link.split("/");var str="";
	for (var i=0;i<cut.length;i++) if (cut[i]=="shop") var n=i+1;
	for (var j=n;j<cut.length;j++) str=str+cut[j]+"/";
	$.get("/grid/"+str, function(data){
		if (add==0) $.get("/grid_menu/"+str, function(data){$("#gridMenu").fadeIn(300).html(data);});
		$("#goodCells").fadeOut(300).queue(function(){$(this).html(data);$(this).dequeue();}).fadeIn(300);;
		$.prettyLoader.hide();
	});
}

function hiddenLink(obj,method) {
	if (method=="show") {
		$(">h2>a",obj).fadeIn(200);
	}
	else if (method=="hide") {
		$(">h2>a",obj).fadeOut(200);
	}
}
