(jQuery)(document).ready(function() {

	(jQuery)('#priceListSlideDown').click(function() {
		(jQuery)(this).hide();
		(jQuery)('#priceListSlideUp').show();
		(jQuery)('#pageHead div.priceDownload div.priceList').show();
	});

	(jQuery)('#priceListSlideUp').click(function() {
		(jQuery)(this).hide();
		(jQuery)('#priceListSlideDown').show();
		(jQuery)('#pageHead div.priceDownload div.priceList').hide();
	});
	
	(jQuery)('#pageHead').mouseleave(function() {
		(jQuery)('#priceListSlideUp').click();
	});

	/*
	 *
	 *	Callibri Scroll Engine
	 *
	 */

	var scroll = {};

	scroll.init = function() {

		this.locked = true;
		this.popupEnabled = false;

		this.holder = (jQuery)('#pageBanner');
		this.wrapper = (jQuery)(scroll.holder).find('div.wrapper');
		this.settings = {  // конфиг скролла
			speed: 500,
			paddingLeft: 0,
			paddingAfter: 1
		};

		this.moveProps = { // дефолтовый конфиг для движения
			num: 0,
			direction: 'left',
			callback: function() {
				scroll.clear();
				scroll.unlock();
			},
			dx: 0
		};

		this.obj = { // объекты
			first: (jQuery)(scroll.wrapper).find('.item:first'),
			last: (jQuery)(scroll.wrapper).find('.item:last'),
			amount: (jQuery)(scroll.wrapper).find('.item').size(),
			holderWidth: (jQuery)(scroll.holder).width()
		};

		//(jQuery)(this.obj.first).addClass('first');
		//(jQuery)(this.obj.last).addClass('last');

		var newX = scroll.settings.paddingLeft;
		var summaryW = 0;
		(jQuery)(scroll.wrapper).find('.item').each(function() {
			(jQuery)(this).css( 'position' , 'absolute' ).css( 'left' , newX +'px' );
			//newX = newX + (jQuery)(this).width() + scroll.settings.paddingAfter;
			//summaryW = summaryW + (jQuery)(this).width() + scroll.settings.paddingAfter;
			newX = newX + ( 265 * 1 ) + scroll.settings.paddingAfter;
			summaryW = summaryW + ( 265 * 1 ) + scroll.settings.paddingAfter;
		});
		summaryW = summaryW - scroll.settings.paddingAfter + scroll.settings.paddingLeft;

		if ( summaryW > scroll.obj.itemWidth ) {
			scroll.unlock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').show();
		} else {
			scroll.lock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').hide();
		}
		
		scroll.unlock();

	}
	
	
	scroll.onResize = function() {

		this.obj.itemWidth = (jQuery)(scroll.holder).width();
	
		var summaryW = 0;
		(jQuery)(scroll.wrapper).find('.item').each(function() {
			//summaryW = summaryW + (jQuery)(this).width() + scroll.settings.paddingAfter;
			summaryW = summaryW + ( 105 * 1 ) + scroll.settings.paddingAfter;
		});
		summaryW = summaryW - scroll.settings.paddingAfter + scroll.settings.paddingLeft;

		if ( summaryW > scroll.obj.itemWidth ) {
			scroll.unlock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').show();
		} else {
			scroll.lock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').hide();
		}
		

	}
	

	scroll.calculate = function() {

		var dx = 0;
		if ( 'right' == this.moveProps.direction ) { // to right
			for( var i =0; i < this.moveProps.num; i++ ) {
				dx = dx + (jQuery)(this.wrapper).find('.item:eq('+ i +')').width() + this.settings.paddingAfter;
			}
			//dx = dx + (this.moveProps.num - 1) * this.settings.paddingAfter;
			//dx = dx + scroll.settings.paddingLeft;
		} else { // to left
			for( var i =0; i < this.moveProps.num; i++ ) {
				dx = dx + (jQuery)(this.wrapper).find('.item:eq('+ i +')').width() + this.settings.paddingAfter;
			}
		}
		this.moveProps.dx = ( this.moveProps.direction == 'right' ) ? dx : -1 * dx;
		//scroll.settings.speed = scroll.settings.speed * 120 / dx * this.moveProps.num;
	}


	scroll.clear = function() {
		if ( 'right' == this.moveProps.direction ) { // to right
			scroll.clearAtEnd();
		} else { // left
			scroll.clearAtBegin();
		}
	}


	scroll.lock = function() {
		//(jQuery)('#msg').text( 'locked' );
		scroll.locked = true;
	}


	scroll.unlock = function() {
		//(jQuery)('#msg').text( 'unlocked' );
		scroll.locked = false;
	}
	
	
	scroll.isLocked = function() {
		//(jQuery)('#msg').html( '<b>locked!!!!!!</b>' );
		return scroll.locked;
	}


	scroll.move = function() {
		(jQuery)(this.wrapper).animate({
				left: '+='+ scroll.moveProps.dx
			},
			scroll.settings.speed
			,
			scroll.moveProps.callback
		);
	}


	scroll.addToEnd = function() {
		var tempX = (jQuery)(scroll.obj.last).position().left + (jQuery)(scroll.obj.last).width() + scroll.settings.paddingAfter;
		var tempObj = scroll.obj.first;

		(jQuery)(this.wrapper).find('.item:lt('+ this.moveProps.num +')').each(function() {
			(jQuery)(this).clone().css( 'left' , tempX ).appendTo( (jQuery)(scroll.wrapper) ); //.addClass('temp')
			tempX = tempX + (jQuery)(tempObj).width() + scroll.settings.paddingAfter;
			tempObj = (jQuery)(this).next();
		});
	}


	scroll.addToBegin = function() {
		var tempX = (jQuery)(scroll.obj.first).position().left - scroll.settings.paddingAfter - (jQuery)(scroll.obj.last).width();
		var tempObj = scroll.obj.last;

		for ( var i = scroll.obj.amount -1; i > scroll.obj.amount -1 -this.moveProps.num; i-- ) {
			(jQuery)(tempObj).clone().css( 'left' , tempX +'px' ).prependTo( (jQuery)(scroll.wrapper) ); //.addClass('temp')
			tempObj = (jQuery)(tempObj).prev();
			tempX = tempX - scroll.settings.paddingAfter - (jQuery)(tempObj).width();
		}
	}


	scroll.clearAtBegin = function() {
		(jQuery)(scroll.wrapper).find('.item:lt('+ (scroll.moveProps.num) +')').remove();
		//(jQuery)(this.wrapper).find('.item').removeClass('temp').removeClass('first').removeClass('last');
		this.obj.first = (jQuery)(this.wrapper).find('.item:first');
		this.obj.last = (jQuery)(this.wrapper).find('.item:last');
		//(jQuery)(this.obj.first).addClass('first');
		//(jQuery)(this.obj.last).addClass('last');
	}


	scroll.clearAtEnd = function() {
		(jQuery)(scroll.wrapper).find('.item:gt('+ (scroll.obj.amount -1) +')').remove();
		//(jQuery)(this.wrapper).find('.item').removeClass('temp').removeClass('first').removeClass('last');
		this.obj.first = (jQuery)(this.wrapper).find('.item:first');
		this.obj.last = (jQuery)(this.wrapper).find('.item:last');
		//(jQuery)(this.obj.first).addClass('first');
		//(jQuery)(this.obj.last).addClass('last');
	}


	scroll.scrollLeft = function(offset) {
		if ( scroll.isLocked() ) return;
		this.lock();
		this.moveProps.num = ( offset < 1 ) ? 1 : offset;
		this.moveProps.direction = 'left';
		this.addToEnd();
		this.calculate();
		this.move();
	}


	scroll.scrollRight = function(offset) {
		if ( scroll.isLocked() ) return;
		this.lock();
		this.moveProps.num = ( offset < 1 ) ? 1 : offset;
		this.moveProps.direction = 'right';
		this.addToBegin(offset);
		this.calculate();
		this.move();
	}


	scroll.getCurrent = function(obj) {
		return {
			x: (jQuery)(obj).position().left + (jQuery)(scroll.wrapper).position().left,
			w: (jQuery)(obj).width()
		}
	}

	scroll.init();

	//
	// -- arrows

	(jQuery)('#scroll-banner-arrow-left').click(function() {
		scroll.scrollRight(1);
	});


	(jQuery)('#scroll-banner-arrow-right').click(function() {
		scroll.scrollLeft(1);
	});
	
});
