$(document).ready(function(){
	$.each($('.spin'),function(){
		$(this).spin({
			imageBasePath: '/scripts/spin/img/spin1/',
			min: $(this).attr("min"),
			interval: 1,
			lock: true
		});			
	});	
	
	$(".addToBasket").click(function(){
		toBasket($(this).attr("idnom"),$(this));
		return false;	
	});
	
	showBasket();
})

function toBasket(id,elem){
	var elem = $("#addToBasket"+id);
	$("#loader").animate({"opasity":"show"},100);	 		
	$.post("/18/",{"toBasket": id, "count": $("#count_order_"+id).val() 
	},function(data){		
		$("#loader").animate({"opasity":"hide"},100);						
		
		if(data=="adding"){
			elemShow = $("#adding");
			showBasket();
		}else if(data=="earlier_add"){
			showBasket();
			elemShow = $("#earlier_add");		
		}else{
			elemShow = $("#error_add");		
		}
		var offs = elem.offset();
		elemShow.css("left",offs.left-30).css("top",offs.top);		
		elemShow.fadeIn(300);

		var timerHide = setTimeout(
			function ShowToolTip(event){
				clearTimeout(timerHide);	
				elemShow.fadeOut(400);				
			}
		, 1500);	
	});
	return false;
}

function showBasket(){		
	$.post("/18/view/1/?view=1",{},function(data){
		$("#basket").html(data);	
	});
}

function selectPhoto(i,s){
	fileNewPhoto = $("#photo"+i).attr("fp");
	fileOldPhoto = $("#photoNews").attr("fp");

	if($("#linkToBigPhoto")){$("#linkToBigPhoto").attr("href",dir+"b"+fileNewPhoto+"?v="+Math.random());}

	$("#photoNews").attr("src",dir+fileNewPhoto+"?v="+Math.random());
	$("#photo"+i).attr("src",dir+"s"+s+fileOldPhoto+"?v="+Math.random());		

	$("#photoNews").attr("fp",fileNewPhoto);
	$("#photo"+i).attr("fp",fileOldPhoto);
}

function MM_preloadImages(){ //v3.0
  var d=document; 
	if(d.images){ 
		if(!d.MM_p) d.MM_p=new Array();
	    	var i,j=d.MM_p.length, a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}
	}
}

function setFilterCatalog(part,cid){
	loc="";
	if($("#brand").attr("value")!='') loc +="/vendor/"+$("#brand").attr("value");
	location.href = "/"+part+"/cid/"+cid+loc+"/";
	return false;
}

function sendSearchForm(){
	if($("#word").attr("value")!=''){
	        $(document).attr("location","/13/search/"+$("#typeSearch").attr("value")+"/word/"+encodeURI($("#word").attr("value"))+"/");	
	}
}        
function ShowMessage(s){ 
	if(s!=''){
	        $("#messagerWindow #ms").html(s);
	        $("#messagerWindow")
			.show()
			.css("height",(document.getElementById("page").offsetHeight)+"px");

		$("#messagerWindow #messagerFront")
			.css("top",(document.body.scrollTop+(document.body['clientHeight'] - 80)/2))
			.css("left",(document.body['clientWidth'] - 300)/2);
	}
	return false;
}
function ShowSelectPriceList(){
	ShowMessage("<a href='/9/id/1/'>Экспортировать из каталога</a><br /><br /><a href='/9/id/0/'>Загрузить готовый</a><br /><br />");
}
