Cielo={
	total:function(i){var t=0;$.each(i,function(k,v){t+=(parseFloat(v.price)||0)*v.count});$('#cart button').html(t+',- kr. Gå til kassen &raquo;');Cufon.refresh()},
	conditions:function(){return !$('#conditions').slideToggle(150)},
	add:function(items){
		var cart=$('#cart').removeClass('empty');
		$.each(items,function(k,v,c){if(c=v.count){
			var el=$('#cart img[src$='+v.img+']').siblings('i'),none=!el.length;
			if(none)$('<div><img alt="" src="'+v.img+'" />'+v.title+' - <i>'+c+'</i> stk.<a href="?remove='+v.file+'">-1</a></div>').prependTo(cart).fadeIn();
			else if(parseFloat(el.text())!=c)el.text(c).parent().stop().fadeTo(200,0).fadeTo(500,1);
		}});
		Cielo.total(items);
	},
	remove:function(items){
		$.each(items,function(k,v){
			var el=$('#cart img[src$='+v.img+']').siblings('i');
			if(!v.count)el.parent().find('a').remove().end().stop().fadeOut(function(){el.parent().remove()});
			else if(parseFloat(el.text())!=v.count)el.text(v.count).parent().stop().fadeTo(200,0).fadeTo(500,1);
		});
		Cielo.total(items,$('#cart a').length||setTimeout(function(){$('#cart').addClass('empty')},400));
	}
}

Validate={
	name:function(v){return v.indexOf(' ')>0},
	post:function(v){return v.match(/^\d\d\d\d+$/gi)},
	phone:function(v){return v.replace(/^(00|\+)47/,'').replace(/\D/gi,'').length==8},
	mail:function(v){return v.match(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi)}
}

$('#order input').bind('focus blur',$.placeholder).blur();
$('#order input[name=post]').keyup(function(e){
	var valid=this.value.replace(/\D/,'').substr(0,4);
	if(valid!=this.value){
		if(this.value.length>3)alert('KUN postnummer');
		this.value=valid;
	}
});
$('#order form').submit(function(e,fail){
	if($.validate('#order input.required',Validate))$('input',this).each(function(i,el){el.value=el.value==el.title?'':el.value});
	else{alert('Alle påbudte felter må fylles ut med gyldig info');return false}
});
$('a').live('click',function(e,fn){
	if(this.getAttribute('href',2).replace(window.location.hostname,'').indexOf('.')>0){window.open(this.href);return false}
	else if((fn=this.href.split('?')[1])&&(fn=Cielo[fn.split('=')[0]]))return !$.get(this.href,fn);
});
