$(document).ready(function(){
	
		
	$("#bv,.reg_form.p form, #prl").validationEngine('attach');
	
	
	$('.bg_city .r').click(function(){
		$('select[name=city]').change();
	});
	 $('select[name=city]').change(function(){var city_id  =  $(this).val();load_city_content(city_id);});
	$('form .reference').jqTransform();
	$('.menu td .r_bg:last, #container .box1 table td:last').css('background', 'none');
	$(".slider").easySlider({auto: true,continuous: true ,speed: 3000});
	
	$('input[name=search_header]').click(function(){
		if($(this).val()=='Поиск'){
			$(this).attr('value','');
		}
	});
	$('.refrash_tc').click(function(){
		$('.captha').attr('src', '/captcha?img='+Math.random());
		return false;
	});
	$('input[name=search_header]').blur(function(){
		if($(this).val()==''){
			$(this).attr('value','Поиск');
		}
	});
	
	
	$('#bud-v-kurse').click(function(){
		var windowWidth = $(window).width();
		var windowHeidht = $(window).height();
		$('.shadow').css({'width' : windowWidth,'height' : windowHeidht}).fadeTo('show','0.45');
		var top_reg = (windowHeidht/2)-295;
		var left_reg = (windowWidth/2)-215;
		$('.reg_form.b').css({'left' : left_reg,'top': top_reg}).fadeIn('show');
		
		return false;
	});
	
	$('.rem').click(function(){
		var windowWidth = $(window).width();
		var windowHeidht = $(window).height();
		$('.shadow').css({'width' : windowWidth,'height' : windowHeidht}).fadeTo('show','0.45');
		var top_reg = (windowHeidht/2)-295;
		var left_reg = (windowWidth/2)-215;
		$('.reg_form.remember').css({'left' : left_reg,'top': top_reg}).fadeIn('show');
		$('.reg_form.p, .reg_form.l').fadeOut();
		return false;
	});
	$('.enter').click(function(){
		var windowWidth = $(window).width();
		var windowHeidht = $(window).height();
		$('.shadow').css({'width' : windowWidth,'height' : windowHeidht}).fadeTo('show','0.45');
		var top_reg = (windowHeidht/2)-295;
		var left_reg = (windowWidth/2)-215;
		$('.reg_form.l').css({'left' : left_reg,'top': top_reg}).fadeIn('show');
		$('.reg_form.p, .reg_form.remember').fadeOut();
		return false;
	});
	
	$('.reg').click(function(){
		var windowWidth = $(window).width();
		var windowHeidht = $(window).height();
		//$('.shadow').css({'width' : windowWidth,'height' : windowHeidht}).fadeTo('show','0.45');
		var top_reg = (windowHeidht/2)-295;
		var left_reg = (windowWidth/2)-215;
		$('.reg_form.l').fadeOut();
		$('.reg_form.p').css({'left' : left_reg,'top': top_reg}).fadeIn('show');
		
		return false;
	});

	
	
	$('.reg_form.l form').submit(function(){
		if($('.reg_form.l input[name=p_login]').val()=='' && $('.reg_form.l input[name=p_pass]').val()==''){
			alert('Не все поля заполнены.');
			return false;
		}else{
			$(this).submit();
		}
		return false;
	});
	$('.reg_form.remember').submit(function(){
		if($('.reg_form.remember input[name=r_login]').val()=='' && $('.reg_form.remember input[name=r_email]').val()==''){
			alert('Не все поля заполнены.');
			return false;
		}else{
			$(this).submit();
		}
		return false;
	});
	
	$('.shadow').click(function(){ 
			$(this).fadeOut(); 
			$('.reg_form, .out_form').stop().fadeOut();
			$('#bv').validationEngine('hide');
			$('#prl').validationEngine('hide');
			$('.reg_form.p form').validationEngine('hide');
	});
	
	$('.minus').click(function(){ 
		var id = $(this).attr('rel'); 
		var val = $('input[name=item_'+id+']').val();
		
		if(is_numeric(val) && val>0){
			$('input[name=item_'+id+']').attr('value',--val);
			add_item_basket(id,val);
		}
		else $('input[name=item_'+id+']').attr('value',0);
		
		return false;
	});
	
	$('.plus').click(function(){ 
		var id = $(this).attr('rel'); 
		var val = $('input[name=item_'+id+']').val();
		
		if(is_numeric(val)){
			$('input[name=item_'+id+']').attr('value',++val);
			add_item_basket(id,val);
		}
		else $('input[name=item_'+id+']').attr('value',0);
		
		return false;
	});
	
	$('.del_item').click(function(){
		var id = $(this).attr('rel'); 
		del_item_basket(id);
		$(this).parent().parent().remove();
		return false;
	});
	$('.count_item').blur(function(){
		var id = $(this).attr('rel');
		var values = Math.round($(this).val()); // Кол-во
		if(is_numeric(values)){
		add_item_basket(id,values);
			$(this).attr('value',Math.round(values));
			var price = $('#price_'+id).attr('rel');
			$('#count_price_'+id).text((price*values)+' руб.');
		}else {
			$(this).attr('value',0);
			var values = $(this).val(); // Кол-во
			add_item_basket(id,values);
		}
		
	});
	
	$('.add').click(function(){
		var id = $(this).attr('id');
		var values = $('input[name=item_'+id+']').val(); // Кол-во
		
		if(is_numeric(values)) add_item_basket(id,values);
		else $('input[name=item_'+$(this).attr('id')+']').attr('value',0); // не число, обнилили
		
		return false;
	});
});
function add_item_basket(id,values){
	$.ajax({
	   type: 'POST',
	   url: '/ajax.php',
	   data: 'item_id='+id+'&count='+values+'&'
	 });
}function del_item_basket(id){
	$.ajax({
	   type: 'POST',
	   url: '/ajax.php',
	   data: 'del_item='+id
	 });
}
function is_numeric( mixed_var ) {
    return ( mixed_var == '' ) ? false : !isNaN( mixed_var );
}
function load_city_content(id){
	$.ajax({
		type: 'POST',
		url: '/ajax.php',
		data: 'city_id='+id+'',
		dataType: 'text',
		success: function(data){
			$('#city_content').html(data);
		}
	});
}
