/**
 * @author Developing
 */
function PictureScroller(container, itemObject, ribbonObject, leftButton, rightButton, ajaxScriptPath)
{
	this.container = container;
	this.containerWidth = container.width();
	this.itemWidth = itemObject.width();
	this.ribbon = ribbonObject;
	this.leftButton = leftButton;
	this.rightButton = rightButton;
	this.stopQueringForContent = false;
	this.ajaxScriptPath = ajaxScriptPath;
	
	this.refreshRibbonSize();
	var scroller = this;

	this.leftButton.click( function(){ scroller.leftClick();	} );
	this.rightButton.click( function(){ scroller.rightClick();	} );	
}


PictureScroller.prototype.checkAfterLeftClick = function(click)
{
	if ( typeof(click) == "undefined" || click != 1 ) { click = 0; }
	var ribbonMarginLeft = parseInt(this.ribbon.css('margin-left'));
	if ( ribbonMarginLeft >= -this.itemWidth * click) 
	{
		this.leftButton.hide();
	}
	
	if ( (this.ribbon.width() + ribbonMarginLeft) > this.containerWidth - this.itemWidth * click) 
	{
		this.rightButton.show();
	}
}

PictureScroller.prototype.checkAfterRightClick = function(click)
{
	var ribbonMarginLeft = parseInt(this.ribbon.css('margin-left'));
	if ( ribbonMarginLeft == 0 )
	{
		this.leftButton.show();
	}

	if ( (this.ribbon.width() + ribbonMarginLeft) <= this.containerWidth + this.itemWidth * click )
	{
		this.rightButton.hide();
	}
}

PictureScroller.prototype.refreshRibbonSize = function()
{
	var ribbonWidth = 0;
	this.ribbon.children().each( function()
	{
		ribbonWidth += $(this).width();
	} );
	this.ribbon.width( ribbonWidth );
	this.checkAfterRightClick(0);		
	this.checkAfterLeftClick(0);
}

PictureScroller.prototype.rightClick = function()
{
	this.checkAfterRightClick(1);		
	var scroller = this;
	this.ribbon.animate(
	{ 
        marginLeft: "-=" + scroller.itemWidth + "px"
    }, 
	"slow"  );
	
	setTimeout(function(){scroller.checkContentNeedless();}, 800);   
}

PictureScroller.prototype.leftClick = function()
{
	this.checkAfterLeftClick(1);
	var scroller = this;
	this.ribbon.animate(
	{ 
        marginLeft: "+=" + scroller.itemWidth + "px"
    }, 
	"slow"  );
}

PictureScroller.prototype.backToBegining = function()
{
	var scroller = this;
	this.ribbon.animate(
	{ 
        marginLeft: "0px"
    }, 
	"fast"  );
	this.leftButton.hide();
}

PictureScroller.prototype.checkContentNeedless = function()
{
	if ( this.stopQueringForContent == false 
		 && ( (this.ribbon.width() + parseInt(this.ribbon.css('margin-left'))) <= this.containerWidth + this.itemWidth * 2 )  )
	{
		var scroller = this;
		$.ajax({
			metod: "post",
			url: scroller.ajaxScriptPath,
			cache: false,
			data: "startfrom="+scroller.ribbon.children().size(),
			success: function(xml){
				scroller.handleServerResponse(xml);
			}
		});
	}
}

PictureScroller.prototype.handleServerResponse = function(xml)
{	
	alert('You should do here something');
}


