var url='';
$(document).ready(function(){
  $('#op-text-place').append($('#opttext'));
  $('#opttext').show();
  
  $("#mpp2").click(function(){return false;});
  $("#xs").width($(window).width()).bgiframe();

  $('#leftcolumn').height($('#mt').height());
  //$('#pdvl').height($('#mt').height() - $('#cntt').height());
  $('#cntt').height($('#mt').height() - $('#pdvl').height());

  if ($('.splash').length && !$.cookie('splash')) {
    $('.splash').modal({closeClass: "mbt"});
    $('#simplemodal-overlay').bgiframe();
    $.cookie('splash', '1', {
      path: '/'
    });
  }
  else
    $('.splash').remove();

$('.qz-delivery').change(function () {
  if ($(this).val())
    $.post(url+'/payment_delivery_ajax/', {'qz-delivery': $(this).val()}, function (data) {eval(data);});
});

$('#wm_submit').click(function(event){
  $($(this).parent()).submit();
  return false;
});
$("#opnx").click(function(event){								   
 if (!$('select#cities').length) {
     $.post(url+'/shops_ajax/', {}, function (data) {eval(data);});
 } 	 			  
 $("#xs").animate({opacity: "0.4", height: $("#tbl").height()}, 200);	
 $("#mpp").animate({opacity: "show"}, 1200);
});
function check_basket() {
    var res = 0;
    $(".basket_item").each(function() {
      if ($(this).val() > 0)
        res = 1;
    });
    $("#cart_submit, #quick_zakaz").parent().css({'background-color': (res ? '#f05876' : '#ADADAD')});
    return res;
}
$("#auth2").click(function () {
  $('#cart').submit();
  return false;
});

$(".basket_item").change(function () {
  check_basket();
});

$(".s_search").click(function(){
   if ($('input[name=s]').val())
    $(this).parent().submit();
});
$(".select_manufacture").change(function(){
  if ($(this).val())
    window.location = url+$(this).val();
});
$(".select_brand").change(function(){
  if ($(this).val())
    window.location = url+'/cat/brand/'+$(this).val()+'/';
});
$("#quick_zakaz").click(function(event){
   if (check_basket())
      $('#cart').attr('action', url+'/quick_zakaz/').submit();
   return false;
});
$("#cart_submit").click(function(event){
   if ($('#discount_card_referer').length && $('#discount_card_referer').val() != '')
   {
      $.post(url+'/discount_ajax/', $('#cart').serializeArray(), function (data) {eval(data);
         if ($('#referer').length)
            $('#cart').submit(); //Пересчитываем корзину
      });
   }
   else {
     if (check_basket()) {
       if ($('input[name=phone]').length)
          $('#cart').submit();
       else
         $.post(url+'/cart/', $('#cart').serializeArray(), function (data) {
           $('#cart').attr('action',url+'/profile/').submit();
         });
     }
   }
   return false;     
});	 
$("#cart_refresh").click(function(event){
   if ($('#discount_card_referer').length && $('#discount_card_referer').val() != '')
   {
      $.post(url+'/discount_ajax/', $('#cart').serializeArray(), function (data) {eval(data);
         if ($('#referer').length)
            $('#cart').submit();
      });
   }
   else {
     $('input[name=phone]').remove();
     $('#cart').submit();
   }
   return false;     
});	 

$("#discount_card_input").click(function(event){
   var t=$(this).offset();
   $.post(url+'/discount_ajax/', {'left': t.left, 'top': t.top}, function (data) {eval(data);});
   return false;
});

$(".menu").hover(function(){
    $(this).css({backgroundColor:"#f75876"});	
    $(this).css({color:"#ffffff"});				 
    $("#"+this.id+"z").css({color:"#ffffff"});				 			 
  },   
  function(){
    $(this).css({backgroundColor:"#ffffff"});	
    $(this).css({color:"#383838"});								
    $("#"+this.id+"z").css({color:"#383838"});				 			 			 
  }
);	
$("#cross_x").click(function () {
    $(".bl a").css({color:"#adadad" , fontWeight: "normal" , fontSize: "11px"});	            
    $(".bl").css({color:"#adadad"})		  
    $(".letter").css({backgroundColor:""})
 }
);
$(".letter").toggle(function () {			  
    $(this).css({backgroundColor:"#ffffff"});					
    $("#"+this.id+"_op").css({color:"#ffffff"});						
    $("#"+this.id+"_op a").css({color:"#ffffff", fontWeight:"bold", fontSize: "12"});	
 },
 function () {
    $(this).css({backgroundColor:"#424242"});
    $("#"+this.id+"_op").css({color:"#adadad"});				
    $("#"+this.id+"_op a").css({color:"#adadad", fontWeight:"normal", fontSize: "11"});
 }
);
$(".vote").click(function () {			  
    $(".voted").hide(1);					
    $(".vote").show(1);
    $(this).hide(1);
    $("#"+this.id+"z").show(1);						
    $("#vote_item_id").val(this.id);						
 }
);
$(".voted").click(function () {			  
    $(this).hide(1);    
    $("#"+this.id.replace('z', '')).show(1);						
    $("#vote_item_id").val(0);
 }
);
$(".heart").hover(function(){
    $("#"+this.id+"zz").show(1);							
    $("#"+this.id+"z").hide(1);	
  },   
  function(){
    $("#"+this.id+"z").show(1);				 				
    $("#"+this.id+"zz").hide(1);	 
  }
);
$(".heart").click(function () {
    if ($("#"+this.id+"zzz").is(":hidden")) {
        $("#"+this.id+"zzz").show(1);
        $.post(url+'/favorite_ajax/', {'addfavorite': this.id}, function (data) {});
    } else {
        $("#"+this.id+"zzz").hide(1);
        $.post(url+'/favorite_ajax/', {'delfavorite': this.id}, function (data) {});
    }
  }
);	
        $("#y1").click(function () {
             $("#y").animate({opacity: "hide"}, "slow");								 

             $("#y2").animate({opacity: "show"}, "slow");								 			 


		     }
		   );
		
        $("#arrow").click(function () {
             $('#sign').submit();
		     }
		   );	
		
        $("#arrow2").click(function () {
             remaind_pass('rp');								 			 
		     }
		   );			
        $("#reg").click(function () {
             $("#y").animate({opacity: "hide"}, "slow");								 

             $("#y2").animate({opacity: "hide"}, "slow");								 			 
			 
             $("#y3").animate({opacity: "show"}, "slow");								 			 			 


		     }
		   );		
        $("#rpzreg").click(function () {
             $("#yz2").animate({opacity: "hide"}, "slow");								 
             $("#yz3").animate({opacity: "show"}, "slow");								 			 
		     }
		   );		
		
        $("#check1").click(function () {
             $("#check1").hide(1);								 
             $("#check2").show(1);								 						 			 
		     }
		   );		
		
        $("#check2").click(function () {
             $("#check2").hide(1);								 
             $("#check1").show(1);								 						 			 
		     }
		   );
  $(".old-price").attr('title', 'цена без скидки');
  var discount = $('#udiscount').val();
  $(".dis-price").attr('title', 'ваша цена со скидкой '+discount+'%');
		
		
	});
	
function voting(id) {
    var t = $('.voted');
    var p=$('#votes'+id+'form').serializeArray();
    for (var i = 0; i < t.length; i++) {
        if (t[i].style.display == 'block') {p.push({name: 'vote_item_id[]', value: t[i].id.replace('rad', '').replace('z', '')});}
    }
    if (p.length <= 1)
        window.alert('Выберите вариант ответа');
    else
    {
        $('#votes'+id+'container').fadeOut(300, function() {
            $('#votes'+id+'container').css({display:'block', visibility:'hidden'});
            $.post('/vote_ajax/', p, function (data) {
                $('#votes'+id+'container').html(data);
                $('#votes'+id+'container').css({display:'none', visibility:'visible'}).fadeIn(300);
            });
        });
    }
}
function qu(id) {
  var t=$('#ib' + id).offset();
  var top = $(window).height()+(document.documentElement.scrollTop || document.body.scrollTop), left = $(window).width()+(document.documentElement.scrollLeft|| document.body.scrollLeft);  
  $.post(url+'/cart_ajax/', $('#cart' + id).serializeArray(), function (data) {eval(data);$('#lt').css({top: ($('#lt').height() > top-t.top ? top - $('#lt').height() : t.top), left: ($('#lt').width() > left-t.left ? left -  $('#lt').width() :t.left)})});
}

function badd(id) {
  $.post(url+'/cart_ajax/', $('#crt' + id).serializeArray(), function (data) {
    var temp=$('#id_cart span[_id=basket'+$('#id_cart').attr('_id')+']'),
    cnt = $('#crt'+id).attr('_count');    
    if (!(cnt > 0))
      cnt = 0;
    cnt++;
    $('#crt'+id).attr('_count', cnt);
    temp.text(parseInt(temp.text())+1);
    $('#crt'+id+' .add').text(' ('+cnt+')');
    $('#m'+id).addClass('addtocart');
  });
}
function qq() {
    $("#con").html('');
    return false;
}
function dadd() {
  var i=Math.abs($('#item_count').val() - $('#old_count').val());
  $.post(url+'/cart_ajax/', $('#can').serializeArray(), function (data) {eval(data);$('#old_count').val($('#item_count').val());if(i) $('#add_result').append(' '+i+' '+declension(i));});
  return false;
}
function dcancel() {
  $('#item_count').val(1);
  return dadd();
}
function go_to_cart() {
  $.post(url+'/cart_ajax/', $('#can').serializeArray(), function (data) {eval(data);$("#con").html('');window.location = url + '/cart/';});
  return false;
}
function declension(i)
{
  var t = i % 100, e='товар', e1='товара', e2='товаров';
  if (t>=5 && t<=20) {
    return e2;
  }
  else {
    var t1 = t % 10;
    if (t1==1)
      return e;
    else
      if (t1 >=2 && t1<=4)
        return e1;
    else
      return e2;
  }
}
function qqm() {
    $("#mpp2").html('');
    $("#mpp2").hide();
    return false;
}
function pd_change(src) {
    var p=$('#payment_delivery').serializeArray();
    p.push({name: 'changed', value: src});
    $('#sorder').click(function(){return false;});
    $.post(url+'/payment_delivery_ajax/', p, function (data) {
        eval(data);
        if ($('#xs').css('display') == 'none')
          $('#sorder').unbind('click');
    });
}
function tab(con, p) {
    if (!p) p={};
    $.post(url+'/'+con+'_ajax/', p, function (data) {$('#'+con).html(data).fadeIn(300)});
}

function df(src) {
    $.post(url+'/favorite_ajax/', {'delfavorite': src.replace('fav', '').replace('lt', '')}, function (data) {});
    $('#'+src).css({display:'block', visibility:'hidden'}).fadeOut(300);
}
function addOption(select, value, name)
{
  return select.options[select.options.length]=new Option(name, value, false, false);
}
function sc(id)
{
  $('select#cities').val(id);
  $.post(url+"/shops_ajax/", {"city": id}, function (data) {eval(data);});
}
function ss()
{
  var id = $('select#shops')[0].value;
  if (id>0) {    
    $("#xs").animate({opacity:"0.0",height:"0"},200);$("#mpp").animate({opacity:"hide"},1200);
    $.post('/shops_ajax/', {'shop': id}, function (data) {window.location=data;});
  }
}
function rate(prefix, newClass, count) {
    for (var i=count; i >0; i--) {
    	$('#'+prefix+i)[0].className=newClass;
    }
    return true;
};
function rating(id, rate) {
$.post(url+"/rate_ajax/", {"id": id, "rate":rate}, function (data) {eval(data);});
};

function remaind_pass(j) {
if(($('#'+j)[0].email.value.length!='')&&(/^([\w-~_]+\.)*[\w-~_]+@([\w-_]+\.){1,3}\w{2,4}$/.test($('#'+j)[0].email.value)))
{$.post("/password_remind_ajax/", $('#'+j).serializeArray(), function (data) {eval(data);});}
else{$('#e'+j).html('Неправильный e-mail');$('#e'+j).css({display:'block'}).fadeIn(300);}
};

function pd_submit(dm, pm) {
if ($("select#delivery").val()==0) {
  $("div#dcon").html('<div style="color:#F75876;padding-top: 8px; padding-left:10px;" class="font_11_2">'+dm+'</div>');$("div#pcon").css({display:'none', visibility:'visible'}).fadeIn(300);
}
if ($("select#payment").val()==0) {
  $("div#pcon").html('<div style="color:#F75876;padding-top: 8px; padding-left:10px;" class="font_11_2">'+pm+'</div>');$("div#pcon").css({display:'none', visibility:'visible'}).fadeIn(300);
}
if (($("select#payment").val()>0) && ($("select#delivery").val()>0)) { 
   $('#payment_delivery')[0].action=url+'/cart/step_4/';$('#payment_delivery').submit();
}
};
function sl(h) {
 $("#xs").animate({opacity: "0.4", height: $("#tbl").height()}, 200);	
 $("#mpp2").html(h);
 $("#mpp2").animate({opacity: "show"}, 1200);
};
function ims(i) {
 sl('<img class="nw" alt="" src="'+i+'" onclick="return imsh();"/>');
};
function imsh() {
$("#xs").animate({opacity:"0.0",height:"0"},200);
$("#mpp2").animate({opacity:"hide"},1200);
return false;
};
function dq() {
    $('#delivery, .qz-delivery').val(0);
    pd_change('delivery');
    imsh();
    $('#sorder').unbind('click');
    return false;
}
function modify(id) {
var t=$('#pink' + id).offset();
$.post(url+'/item_ajax/', {'id': id, 'modify': 1, 'left': t.left, 'top': t.top + $('#pink' + id).height()}, function (data) {$("#con").html(data).animate({opacity:"show"},200);});
}
function discount_form_send(){
   $.post(url+'/discount_ajax/', $('#discount_card_form').serializeArray(), function (data) {eval(data);});
   return false;
}
 $(document.body).click(function(e) {
      var offsetT = $("#lt").offset();
      if (offsetT)
      if (!((offsetT.left <= e.pageX) && (e.pageX <= offsetT.left + $("#lt").width()) 
            && (offsetT.top <= e.pageY) && (e.pageY <= offsetT.top + $("#lt").height()))
         )
         $("#con").html('');
      return true;
  });
  

