var Main={
	fok:function(inp,spn){$(spn).animate({'margin-left':($(inp).width()-$(spn).width())},500);},
	makeInput:function(){
		return;
			core=this;
			$('.input input,.input textarea').each(function(){
				var self=this;
				$(self).wrap('<div></div>');
				var span=$('<span>'+$(this).attr('title')+'</span>').insertBefore(this);
				$(self).bind('focus',function(){core.fok(this,span);}).
				bind('blur',function(){if(!$(this).val()) $(span).animate({'margin-left':'10px'},500);}).
				bind('keyup',function(){if($(this).val().length>6){$(span).hide();}else{$(span).show(300);}});
				$(span).bind('click',function(){core.fok(self,this);$(self).focus();});
				if($(self).hasClass('h')) $(self).parent().css('display','none');
		});
		if(!$.browser.msie){
		Cufon.replace(".btp span");
		Cufon.replace("h2");
			}
		},
	makeKol:function(obj,parent,price,type){
		var core=this;
		var id=$(obj).attr('id');
		$('<input type="text" id="'+id+'"  value="'+$(obj).html()+'" class="kol"/>').bind('change',function(){
			var val=$(this).val();
			$(obj).html(val).show();
			$(this).remove();
			$(parent).find('.sum b').html((val*price).toFixed(2));
			if(type)	core.reCalc(id,val);
		}).insertBefore(obj).focus();
		$(obj).hide();
	},	
	makeCalc:function(obj,type){
		var core=this;
				$(obj).each(function(){
			var self=this;
			
			var set=$(self).attr('lang').split("-");
			var price=parseFloat(str_replace(set[0], ',', '.'));
		
			var kol=$(self).find('.kol').click(function(){core.makeKol(this,self,price,type);}).get(0);
			$(self).find('.buy').bind('click',function(){
							buy($(this).attr('lang'),$(kol).html());
			});
			if(set[2]=='1') return;
				var id=$(kol).attr('id');	
			$('<span class="plus"></span>').insertAfter(kol).bind('click',function(){
					var val=parseInt($(kol).html());
						var sum=(parseFloat($('#totalsum').html())-val*price);
					val++;
					$(kol).html(val);
					$(self).find('.sum b').html((val*price).toFixed(2));
					if(type) core.reCalc(id,val);	
					
			});
			$('<span class="minus"></span>').insertBefore(kol).bind('click',function(){
					var val=parseInt($(kol).html());
					if(val==set[1]) return;
					var sum=(parseFloat($('#totalsum').html())-val*price);
					val--;
					$(kol).html(val);
					$(self).find('.sum b').html((val*price).toFixed(2));
					if(type) core.reCalc(id,val);	
			});
		});
},
reCalc:function(id,kol){
	var sum=0;
	var core=this;
	var i=id.split("_");
	$('#popupBasket .rowz span.sum b').each(function(){
		sum+=parseFloat($(this).text());
	});
		$('#totalsum').html(sum.toFixed(2));
		core.rewindBasket('/basket/?action=buy&type=show&tov_id='+i[1]+'&kol='+kol);
	
},
rewindBasket:function(url){
	var self=this;
	$.ajax({
		url:url,
		type:'GET',
		success:function(d){
		$('#basket').html(d);
		$('#basket .popup').prettyPopin({opacity:0.7,close:true});
			
		}});
},
remp:function(obj){
	$('#rowz'+obj).remove();
	$.get('/basket/?action=del&tov_id='+obj);
	var sum=0;
	$('.rowz .sum b').each(function(){
		sum+=parseInt($(this).html());
	});
	$('#totalsum').html(sum);
},
msg:'',
makeTip:function(obj){
	$(obj).poshytip({
	className: 'tip-darkgray',
	content:function(){return Main.msg},
	bgImageFrameSize: 11,
	offsetX: -25,
	showOn:'none',
	alignTo: 'target',
	alignX: 'inner-left',
	timeOnScreen:3000,
	offsetX: 0,
	offsetY: 6
	});
},
getUrl:function(url){
	$.getJSON(url,function(d){
		console.log(d);
		if(d.ok){
			
				if('url' in d) window.location.href=d.url;
				if('reload' in d) window.location.reload();
				if('popin' in d){PPcurrent(d.data); return;}
				if('close' in d){
					$('#overlay').remove();
					$('.prettyPopin').remove();
				}
			}
	});
},
send: function(form){
				core=this;
			//	core.makeTip(form+' .error');
		$.post($(form).attr('action'), $(form).serialize(),
		function(d){
			if(d.ok){
				if('url' in d) window.location.href=d.url;
				if('reload' in d) window.location.reload();
				if('popin' in d){PPcurrent(d.data); return;}
				if('data' in d) $(form).html(d.data);
				if('ajax' in d) $.ajax({url:d.ajax,success:function(data){PPcurrent(data);}});
			}else{
				if('err'in d){core.makeError(form,d.err); return;};
				core.msg=d.msg;
				console.log(core.msg);
				//core.makeTip(form+' .error');
//				$(form+' .error').poshytip('show');
				$(form+' .error').html(core.msg);
			}
		},'json');
	},
		makeError:function(obj,arg){
		console.log(arg);
		for(var key in arg){
			//console.log(key);
			var div=$(obj+' input[name="'+key+'"]').get(0);
			div.errorMsg=$('<span class="tiperr">'+arg[key]+'</span>').
			insertBefore(div).css({width:$(div).width()}).bind('click',function(){$(this).remove();});
			$(div).bind('focus',function(){$(this.errorMsg).remove();});
			
		}
	},
	validField:function(field,param){
		field = field.replace(/^\s+|\s+$/g, '');
		if('max' in param)
			if(field.length>param.max) return false;
		if('min' in param)
			if(field.length<param.min) return false;
			return true;
	},
	validEmail:function(email,strict){
 if ( !strict ) email = email.replace(/^\s+|\s+$/g, '');
 return (/^([a-z0-9_\-]+\.)*[a-z0-9_\-]+@([a-z0-9][a-z0-9\-]*[a-z0-9]\.)+[a-z]{2,4}$/i).test(email);
	},
resize_body:function(){
	var wh=window.innerHeight-$('body').height();
	//console.log("wh",wh);

	$('#inner').css({'height':wh+'px'});
},
popIn:function(obj){
	$(obj+' .popup').prettyPopin({opacity:0.7,close:true,onload:function(){
			if(!$.browser.msie){
		Cufon.replace('.h2');
		Cufon.replace(".but span",{textShadow:'0 -1px 1 #156CA9'});}
		}});
}
};
function buy(id,kol){
			//console.log(id,kol);
			$('.prettyPopin').remove();
		$('<a href="/order/?action=buy&target=1&tov_id='+id+'&kol='+kol+'"></a>').prettyPopin({
			opacity:0.7,close:true,onload:function(){
						if(!$.browser.msie){
							Cufon.replace(".btp span");
							Cufon.replace("h2");
						}
						Main.makeInput();
						Main.rewindBasket('/basket/?action=buy&type=show&tov_id='+id+'&kol='+kol);
			}}).trigger('click');
	
}

$(function(){
	//	$('.pop').prettyPopin({opacity:0.7,close:true});
	Main.popIn('');
		//$('.open').click(function(){$('.spoiler').slideToggle();});
});
function str_replace(haystack, needle, replacement) { 
	var temp = haystack.split(needle); 
	return temp.join(replacement); 
} 




