function createRequestObject(){
       if(navigator.appName=="Microsoft Internet Explorer"){return new
ActiveXObject("Microsoft.XMLHTTP");}else{return new XMLHttpRequest();}
}

var http_regions = createRequestObject();
var http_extraprice = createRequestObject();

function mili_time() {
       var d = new Date();
       return d.valueOf();

}

function gradselected(v) {
       http_regions.open('get', 'gradselected.php?v='+encodeURIComponent(v)+'&z='+mili_time());
       http_regions.onreadystatechange = regions_handle;
       http_regions.send(null);
}

function dobavki(v,total,k) {
       http_extraprice.open('get', 'extraprice.php?v='+encodeURIComponent(v)+'&total='+total+'&k='+k+'&z='+mili_time());
       http_extraprice.onreadystatechange = dobavki_handle;
       http_extraprice.send(null);
}

function regions_handle(){
      if(http_regions.readyState == 4 && http_regions.status==200) {
              var response = http_regions.responseText;
			  document.getElementById('raion').innerHTML= response ;
      }
}

function dobavki_handle(){
      if(http_extraprice.readyState == 4 && http_extraprice.status==200) {
              var response = http_extraprice.responseText;
              var temp = new Array();
			  temp = response.split('*');
			  document.getElementById('totalvalue'+temp[0]).innerHTML= temp[1];
      }
}

Event.observe(window, 'load', function() {
		var newsScrollerOptions = {
				trigger_class:			'news_scroller',
				moveObj_id:				'news_mover',
				backBtn_class:			'previous_button',
				back_disabled_class:	'previous_button_disabled',
				nextBtn_class:			'next_button',
				next_disabled_class:	'next_button_disabled',
				autoScroll:				true,
				autoScrollTimer:		6,
				animationSpeed:			0.6,
				numScrollItems:			1
		};
		var newsScroller = new scroller(newsScrollerOptions);
});

var scroller = Class.create();
scroller.prototype = {
	
	triggerObjName:		null,
	overflowObj:		null,
	movingObjName:		null,
	btnLeft:			null,
	btnLeftDisabled:	null,
	btnRight:			null,
	btnRightDisabled:	null,
	
	autoScroll:			true,
	autoScrollTimer:	5,
	
	animationSpeed:		1,
	numScrollItems:		1,

	itemWidth:			null,
	itemsWidth:			null,
	visibleWidth:		null,
	numItems:			0,
	visibleItems:		1,
	currItem:			1,
	currDirection:		1,
	animating:			false,
	iv:					null,
	
	initialize: function(opt) {
			if(opt) {
				if(opt.trigger_class) this.triggerObjName = opt.trigger_class;
				if(opt.moveObj_id) this.movingObjName = opt.moveObj_id;
				if(opt.backBtn_class) this.btnLeft = opt.backBtn_class;
				if(opt.back_disabled_class) this.btnLeftDisabled = opt.back_disabled_class;
				if(opt.nextBtn_class) this.btnRight = opt.nextBtn_class;
				if(opt.next_disabled_class) this.btnRightDisabled = opt.next_disabled_class;
				if(opt.autoScroll == true || opt.autoScroll == false) this.autoScroll = opt.autoScroll;
				if(opt.autoScrollTimer) this.autoScrollTimer = opt.autoScrollTimer;
				if(opt.animationSpeed) this.animationSpeed = opt.animationSpeed;
				if(opt.numScrollItems) this.numScrollItems = opt.numScrollItems;
			}
			if(!this.movingObjName.blank() && $(this.movingObjName)) {
					this.numItems = $$('.'+this.triggerObjName+' #'+this.movingObjName+' li').size();
					if($(this.movingObjName).down()) this.itemWidth = $(this.movingObjName).down().getWidth();
					this.itemsWidth = Math.round(this.itemWidth * this.numItems);
					this.overflowObj = $(this.movingObjName).up();
					this.visibleWidth = this.overflowObj.getWidth()+1;
					$(this.movingObjName).style.width = this.itemsWidth+'px';
					this.visibleItems = Math.round(this.visibleWidth / this.itemWidth);
					if(this.hasToScroll()) {
							this.disableLeftBtn();
							$$('.'+this.triggerObjName+' .'+this.btnLeft).each(function(e) {
								Event.observe(e, 'click', function(){
									if (!this.animating) this.moveScrollerFromButton(-1);
								}.bind(this));
							}.bind(this));
							$$('.'+this.triggerObjName+' .'+this.btnRight).each(function(e) {
								Event.observe(e, 'click', function(){
									if (!this.animating) this.moveScrollerFromButton(1);
								}.bind(this));
							}.bind(this));
							if (this.autoScroll) {
								var msSpeed = this.autoScrollTimer * 1000;
								this.iv = setInterval(function(){ this.moveScrollerFromAnimation(); }.bind(this), msSpeed);
							}
					} else {
						this.disableLeftBtn();
						this.disableRightBtn();
					}
			}
	},
	moveScrollerFromButton: function(direction) {
		this.moveScroller('button', direction);
	},
	moveScrollerFromAnimation: function() {
		this.moveScroller('animation', this.currDirection);
	},
	moveScroller: function(from, direction) {
			if(from == 'button') clearInterval(this.iv);
			var nextItem = this.currItem + (direction * this.numScrollItems);
	 		var rightPadding = this.numItems-this.visibleItems+1;
			if(nextItem > 1-this.numScrollItems && nextItem < rightPadding+this.numScrollItems) {
					if(nextItem > rightPadding) nextItem = rightPadding;
					if(nextItem < 1) nextItem = 1;
					var newPos = this.itemWidth - (nextItem * this.itemWidth);
					new Effect.Morph($(this.movingObjName), {
						duration: this.animationSpeed,
						beforeStart: function(){ this.animating = true; }.bind(this),
						afterFinish: function(){ this.animating = false; }.bind(this),
						style: 'margin-left: ' + newPos + 'px'
					});
					this.currItem = nextItem;
			} else {
					if(from == 'animation') {
						this.currDirection = (this.currDirection == 1) ? -1 : 1;
						this.moveScrollerFromAnimation();
					}
			}
			if(this.currItem < 2) this.disableLeftBtn(); else this.enableLeftBtn();
			if(this.currItem >= rightPadding) this.disableRightBtn(); else this.enableRightBtn();
		
	},
	disableLeftBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnLeft)[0].className = this.btnLeft+' '+this.btnLeftDisabled;
	},
	enableLeftBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnLeft)[0].className = this.btnLeft;
	},
	disableRightBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnRight)[0].className = this.btnRight+' '+this.btnRightDisabled;
	},
	enableRightBtn: function() {
		$$('.'+this.triggerObjName+' .'+this.btnRight)[0].className = this.btnRight;
	},
	hasToScroll: function() {
		if(this.numItems < 1) {
			$$('.'+this.triggerObjName)[0].up().hide();
			return false;
		} else if (this.numItems < 2) {
			$$('.'+this.triggerObjName+' div').each(function(e) { e.hide(); });
			return false;
		} else return (this.itemsWidth > this.visibleWidth) ? true : false;
	}

};