var Sort = {
	elements : new Object(),
	price : null,
	sum : 0,
	total : 0,
	tmpsum : 0,
	changeItem : function(item)
	{
		// Get current sum
		if(this.price==null){
			this.price = parseInt($(".ProductInfoLeft > a > span").text().replace("'",""));
		}
		var id = $(item).attr("name");
		var val = $(item).children("option:selected").text();
		var re = /\(\+ (.*) руб.\)/;
		
		if(re.test(val))
		{
			var price = parseInt(re.exec(val)[1].replace("'",""));
			this.elements[id] = price;
		}
		else
		{
			this.elements[id]=0;
		}

		this.sum=0;
		$.each(this.elements,function(index,value){
			Sort.sum +=value;
		})
		//console.log(this.total);
		this.total =  this.sum+this.price;
		//console.log(this.total);

		this.tmpsum = this.total;
		
			while((parseInt(this.tmpsum/1000))!=0)
			{
				Sort.total = parseInt(this.tmpsum/1000);
				var modulo = parseInt(this.tmpsum%1000);
				if(modulo<100)
				{
					modulo='0' + modulo;
				}
				Sort.total = Sort.total + "'" + modulo;
				
				this.tmpsum=parseInt(this.tmpsum%1000);  
			}
									
		$(".ProductInfoLeft > a > span").html(this.total + ' руб.');
		
		if(Sort.sum!=0){
			$(".ProductInfoLeft > a > span").css('color','red');
		}
		else{
			$(".ProductInfoLeft > a > span").removeAttr('style');
		}
	}
}

