﻿var purchased=new Array();
var totalprice=0;
var popWindow;
$(document).ready(function(){

	$('.pngfix').bind("click", function(event){
		addlist(this.id, this.alt, this.name);
		
	});
	
	$('#setregion').bind("change", function(event){
		changeIndex();
	});
	
	
	
	$(window).scroll(function(){
		if  ($(window).scrollTop() > $(".smartBannerIdentifier").offset({ scroll: false }).top){
		   $(".slide-box").css("position", "fixed");
		   $(".slide-box").css("top", "0");
		}
		
		if  ($(window).scrollTop() <= $(".smartBannerIdentifier").offset({ scroll: false }).top){
		   $(".slide-box").css("position", "relative");
		   $(".slide-box").css("top", $(".smartBannerIdentifier").offset);
		}
	}); 
	
	
		
		 var options = { 
		 	beforeSubmit:  showRequest,  // pre-submit callback 
      success:       showResponse  // post-submit callback 
		 	
		}
		//$('#filtrID').ajaxForm(options); 
		
		$('#filtrID').submit(function() { 
        $(this).ajaxSubmit(options); 
 				return false; 
    }); 
		
});

	function midWindow(winURL, w, h) {
			if (! popWindow || popWindow.closed) {
				popWindow = window.open(winURL, 'popWindow', 
					'width='+w+',height='+h+',left=150,top=100, scrollbars=1'); 
				} else {
				popWindow.focus();
				popWindow.location = winURL;
			}
	}
	
	
	function showRequest(formData, jqForm, options) { 
		var queryString = $.param(formData);
		//alert('About to submit: \n\n' + queryString);  
		//var testhtml = "<a href='/inc/modal_preview.php?"+queryString+"&filter=1'</a>"
		var testhtml = "/inc/modal_filter.php?"+queryString+"&filter=1";
		//$.fn.ceebox.overlay();
		//$.fn.ceebox.popup(testhtml,{onload:true,htmlWidth:950,htmlHeight:500});
		midWindow(testhtml, 900, 650);
		//$.fn.ceebox({unload:function(){$("body").css({background:"#ddf"})}});
		return false;	
	}
	
	function showResponse(responseText, statusText, xhr, $form)  { 
		
var result = "";
		 //alert('status: ' + $form + '\n\nresponseText: \n' + xhr + 
     //   '\n\nThe output div should have already been updated with the responseText.'); 
		
for (var i in xhr) 
			{
				result += i + " :: " + xhr[i];
				
			}
			alert('result: ' + result);
	}
	
function changeIndex()
{
	//$(".index-vis").css("display", "block");
	//alert ($('#setregion').val());
	if ($('#setregion').val() == 'Регионы'){
		$(".index-vis").css("display", "block");
	
	} else {
		$(".index-vis").css("display", "none");
		
		
	}
	/*
	индекс
	область/край
	город
	улица
	
	
	
	*/
}


function addlist(id, price, img)
{
	
	$.ajax({
	type: "POST",
	url: "/js/addtoCart.php",
	data: 'price='+encodeURIComponent(price)+'&id='+encodeURIComponent(id)+'&img='+encodeURIComponent(img),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');}, 
	success: function(msg){
		$('#ajax-loader').css('visibility','hidden');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			var check=false;
			var cnt = false;
			var result = "";
			/*for (var i in purchased) 
			{
				result += i + " = " + purchased[i] + "\n";
				for (var ii in purchased[i]) 
				{
					result += "	>> " + ii + " = " + purchased[i][ii] + "\n";
					
				}
			}
			alert(result);*/
			for(var i=0; i<purchased.length;i++)
			{
				if(purchased[i].id==msg.id)
				{
					check=true;
					cnt=purchased[i].cnt;
					
					break;
				}
			}
			
			if(!cnt)
				$('#item-list').append(msg.txt);
				
			if(!check)
			{
				purchased.push({id:msg.id,cnt:1,price:msg.price});
			}
			else
			{
				if(cnt>=10) return false;
				
				purchased[i].cnt++;
				$('#'+msg.id+'_cnt').val(purchased[i].cnt);
			}
			
			totalprice+=msg.price;
			update_total();

		}
		
		$('.tooltip').hide();
	
	}
	});
}
//////////////////////////////////////////////////////////
function addRefreshlist(id, price, count)
{
	$.ajax({
	type: "POST",
	url: "/js/addtoRefreshCart.php",
	data: 'price='+encodeURIComponent(price)+'&id='+encodeURIComponent(id)+'&count='+encodeURIComponent(count),
	dataType: 'json',
	beforeSend: function(x){$('#ajax-loader').css('visibility','visible');}, 
	success: function(msg){
		$('#ajax-loader').css('visibility','hidden');
		if(parseInt(msg.status)!=1)
		{
			return false;
		}
		else
		{
			$('#item-list').append(msg.txt);
			purchased.push({id:msg.id,cnt:msg.count,price:msg.price});
			$('#'+msg.id+'_cnt').val(msg.count);
			totalprice+=msg.price*msg.count;
			update_total();
			
		}
		
		$('.tooltip').hide();
	
	}
	});
}


//////////////////////////////////////////////////////////
function findpos(id)
{
	for(var i=0; i<purchased.length;i++)
	{
		if(purchased[i].id==id)
			return i;
	}
	
	return false;
}

function remove(id, all)
{
	$.ajax({
	type: "POST",
	url: "/js/delettoCart.php",
	data: 'id='+encodeURIComponent(id)+'&all='+all,
	dataType: 'json'});
	
	
	var i=findpos(id);

	totalprice-=purchased[i].price*purchased[i].cnt;
	purchased[i].cnt = 0;

	$('#table_'+id).remove();
	update_total();
}



function removeAll()
{
	for(var i=0; i<purchased.length;i++)
	{
		remove(purchased[i].id, 'all');
	}
	
}

function change(id)
{
	var i=findpos(id);
	
	totalprice+=(parseInt($('#'+id+'_cnt').val())-purchased[i].cnt)*purchased[i].price;
	
	purchased[i].cnt=parseInt($('#'+id+'_cnt').val());
	update_total();
	
	$.ajax({
	type: "POST",
	url: "/js/addtoChangeCart.php",
	data: 'id='+encodeURIComponent(id)+'&cnt='+purchased[i].cnt,
	dataType: 'json'});
}

function update_total()
{
	
	if(totalprice)
	{
		$('#total').html('всего: '+totalprice + ' руб.');
		if (totalprice >= 29900) $('a.button').css('display','block');
		else $('a.button').hide();
		//$('a.button').css('display','block');
	}
	else
	{
		$('#total').html('');
		$('a.button').hide();
	}
}
