(jQuery)(document).ready(function() {

	function showBaloon() {
		(jQuery)('#popup-holder').show(50);
	}
	
	
	function reserBaloon() {
		(jQuery)('div#pageContent div.newestBlock div.books div.holder').removeClass('holder-hovered').removeClass('holder-baloon');
		(jQuery)('#popup-holder').hide();
	}
	


	function showBaloonMarkers(obj) {

		var aObj = (jQuery)(obj).find('a');
		var dataObj = (jQuery)(aObj).attr('rel').toString();
		var link = (jQuery)(aObj).attr('href');
	
		dataObj = eval( "("+ dataObj +");" );
	
		(jQuery)('#popup-author').text( dataObj.bookData['author'] );
		(jQuery)('#popup-title').html( '<a href="'+ link +'">'+ dataObj.bookData['title'] +'</a>' );
		(jQuery)('#popup-short').text( dataObj.bookData['short'] );
		(jQuery)('#popup-long').html( dataObj.bookData['description'] );

		if ( dataObj.bookData['add2cart'] == '0' ) {
			(jQuery)('#popup-add2cart').hide();
		} else {
			(jQuery)('#popup-add2cart').show();
			(jQuery)('#popup-add2cart').attr( 'ref' , dataObj.bookData['add2cart'] );
		}

		(jQuery)('#popup-state').text( dataObj.bookData['available'] );
		(jQuery)('#popup-price').text( dataObj.bookData['price'] + ' P' );
	}

	/*
	 *
	 *	Callibri Scroll Engine
	 *
	 */
	 
	(jQuery)(window).bind( 'resize' , function() {
		scroll.onResize();
	});
	 

	var scroll = {};

	scroll.init = function() {

		this.locked = true;
		this.popupEnabled = false;

		this.holder = (jQuery)('#pageContent div.newestBlock div.books');
		this.wrapper = (jQuery)(scroll.holder).find('div.wrapper');
		this.settings = {  // конфиг скролла
			speed: 500,
			paddingLeft: 0,
			paddingAfter: 30
		};

		this.moveProps = { // дефолтовый конфиг для движения
			num: 0,
			direction: 'left',
			callback: function() {
				scroll.clear();
				scroll.unlock();
			},
			dx: 0
		};

		this.obj = { // объекты
			first: (jQuery)(scroll.wrapper).find('.holder:first'),
			last: (jQuery)(scroll.wrapper).find('.holder:last'),
			amount: (jQuery)(scroll.wrapper).find('.holder').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('.holder').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 + ( 105 * 1 ) + scroll.settings.paddingAfter;
			summaryW = summaryW + ( 105 * 1 ) + scroll.settings.paddingAfter;
		});
		summaryW = summaryW - scroll.settings.paddingAfter + scroll.settings.paddingLeft;

		if ( summaryW > scroll.obj.holderWidth ) {
			scroll.unlock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').show();
		} else {
			scroll.lock();
			(jQuery)('#pageContent div.newestBlock div.scrolling').hide();
		}

	}
	
	
	scroll.onResize = function() {

		this.obj.holderWidth = (jQuery)(scroll.holder).width();
	
		var summaryW = 0;
		(jQuery)(scroll.wrapper).find('.holder').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.holderWidth ) {
			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('.holder: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('.holder: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('.holder: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('.holder:lt('+ (scroll.moveProps.num) +')').remove();
		//(jQuery)(this.wrapper).find('.item').removeClass('temp').removeClass('first').removeClass('last');
		this.obj.first = (jQuery)(this.wrapper).find('.holder:first');
		this.obj.last = (jQuery)(this.wrapper).find('.holder:last');
		//(jQuery)(this.obj.first).addClass('first');
		//(jQuery)(this.obj.last).addClass('last');
	}


	scroll.clearAtEnd = function() {
		(jQuery)(scroll.wrapper).find('.holder:gt('+ (scroll.obj.amount -1) +')').remove();
		//(jQuery)(this.wrapper).find('.item').removeClass('temp').removeClass('first').removeClass('last');
		this.obj.first = (jQuery)(this.wrapper).find('.holder:first');
		this.obj.last = (jQuery)(this.wrapper).find('.holder: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)('div.newestBlock div.scrolling span.scrollLeft').click(function() {
		reserBaloon();
		scroll.scrollLeft(1);
	});

	(jQuery)('div.newestBlock div.scrolling span.scrollRight').click(function() {
		reserBaloon();
		scroll.scrollRight(1);
	});

	//
	// -- popup

	(jQuery)(scroll.wrapper).find('.holder').live( 'mouseover' , function() {
		(jQuery)(this).addClass('holder-hovered');
	}).live( 'mouseleave' , function() {

		if ( scroll.isLocked() ) return;
		if ( (jQuery)('#popup-holder').is(':visible') && (jQuery)(this).is('.holder-baloon') ) return;

		(jQuery)(this).removeClass('holder-hovered');

	}).live( 'click' , function() {

		if ( (jQuery)(this).is('.holder-baloon') ) {
			var aObj = (jQuery)(this).find('a');
			var link = (jQuery)(this).attr('href');
			window.location.href = link;
			return;
		}

		var popup = {};

		popup.xMin = 0;
		popup.xMax = scroll.obj.holderWidth -634 -25;
		popup.arrow = { x: 0, w: 50, xMax: 530, xMin: 55 };
		popup.x = 0;

		var currentObj = scroll.getCurrent(this);

		popup.arrow.x = currentObj.w / 2 + currentObj.x - popup.arrow.w ;
		
		(jQuery)('#popup-holder').hide();
		
		if ( popup.arrow.x > popup.arrow.xMax )	{
			popup.x = popup.arrow.x - popup.arrow.xMax;
			popup.arrow.x = popup.arrow.xMax;
		}
		if ( popup.arrow.x < popup.arrow.xMin ) {
			popup.x = popup.arrow.x - popup.arrow.xMin;
			popup.arrow.x = popup.arrow.xMin;
		}
		
		//alert( popup.xMin +' < '+ popup.x +' < '+ popup.xMax );

		if ( popup.x < popup.xMin || popup.x > popup.xMax ) {
			var dx = ( popup.x > popup.xMin ) ? popup.x - popup.xMax : popup.x - popup.xMin;

			if ( dx > 0 ) { // left
				scroll.moveProps.direction = 'left';
				for( var i = 0; i <= scroll.obj.amount; i++ ) {
					scroll.moveProps.num = i;
					scroll.calculate();
					if ( -1 * scroll.moveProps.dx > dx ) {
						dx = -1 * scroll.moveProps.dx;
						scroll.scrollLeft(i);
						break;
					}
				}
				popup.x = popup.x - dx;
			} else {
				scroll.moveProps.direction = 'right';
				for( var i = 0; i <= scroll.obj.amount; i++ ) {
					scroll.moveProps.num = i;
					scroll.calculate();
					if ( scroll.moveProps.dx > -1 * dx  ) {
						dx = scroll.moveProps.dx;
						//scroll.scrollRight(i);
						break;
					}
				}
				//popup.x = popup.x + dx;
			}
		}

		(jQuery)('div#pageContent div.newestBlock div.books div.holder').removeClass('holder-hovered').removeClass('holder-baloon');
		(jQuery)(this).addClass('holder-hovered');
		(jQuery)(this).addClass('holder-baloon');

		(jQuery)('#popup-holder').css( 'left' , popup.x +'px' );
		(jQuery)('#popup-holder div.arrow').css( 'left' , popup.arrow.x +'px' );

		if ( scroll.isLocked() ) {
			setTimeout( showBaloon , (scroll.settings.speed + 10) );
		} else {
			showBaloon();
		}

		showBaloonMarkers(this);

		return false;
	});

});

