/**
 * Obiekt konfiguracji dla przewijaka
 *
 */
function Config() {
	this.configured   = 0; //czy skonfigurowano

	this.itemsOnPage  = 0; //ilosc wyswietlanych elementow
	this.itemsAll     = 0; //ilosc wszystkich elementow
	this.itemPosition = 0; //aktualna pozycja

	this.arrowRewindElementName  = ''; //element do przesuwania w lewo (id)
	this.arrowForwardElementName = ''; //element do przesuwania w prawo (id)

	this.itemElementName = ''; //nazwa elementu pojedynczgo (dodawany jest itemPosition)

	this.rotate = 0; //czy zawijac 1 - tak , 0 - nie

	this.configure    = configure;
	this.forward      = forward;
	this.rewind       = rewind;
	this.nextItem     = nextItem;
	this.previousItem = previousItem;

	this.prepareArrows = prepareArrows;

	this.configToString = configToString;

	return this;
}
/**
 * Metoda konfigurujaca
 *
 */
function configure( itemsonpage, itemsall, itemposition, arrowlname, arrowrname, elementname, rotate) {

	this.itemsOnPage  = itemsonpage;
	this.itemsAll     = itemsall;
	this.itemPosition =  itemposition;

	this.arrowRewindElementName  = arrowlname;
	this.arrowForwardElementName = arrowrname;
	this.itemElementName         = elementname;

	this.rotate = rotate;

	this.configured = 1;
	return 1;
}
/**
 * Metoda wyswietlajaca aktualny obiekt
 */
function configToString() {
	var strConfig = "Konfiguracja: \n";
	for ( var prop in this ) {
		strConfig += " " + prop + " = " + this[prop] + ";\n"
	}

	return strConfig;
}

/**
 * Metoda do przewijania w prawo
 */
function forward( ) {
	if ( 0 == this.configured ) {
		return;
	}

	$(this.itemElementName+this.itemPosition).hide();
	this.itemPosition++;

	if ( this.itemPosition == this.itemsAll ) {

		if ( 0 == this.rotate ) {
			$(this.arrowForwardElementName).hide();
		} else {
			this.itemPosition = 0;
		}
	}

	$(this.itemElementName+this.itemPosition).show();
}

/**
 * Metoda do przewijania w lewo
 */
function rewind() {
	if ( 0 == this.configured ) {
		return;
	}

	$(this.itemElementName+this.itemPosition).hide();
	this.itemPosition--;

	if ( -1 == this.itemPosition ) {
		if ( 0 == this.rotate ) {
			$(this.arrowRewindElementName).hide();
		} else {
			this.itemPosition = this.itemsAll - 1;
		}
	}

	$(this.itemElementName+this.itemPosition).show();
}


function nextItem( id ) {

	if ( this.itemPosition < ( this.itemsAll - this.itemsOnPage) ) {
		this.itemPosition++;
	}

	for ( i = 0; i < this.itemsOnPage; i++ ) {
		$(this.itemElementName+i).innerHTML = $(id + (this.itemPosition + i)).innerHTML;
	}

	if (this.itemPosition >= (this.itemsAll - this.itemsOnPage)) {
		$(this.arrowForwardElementName).hide();
	}
    
	if (0 < this.itemPosition) {
		$(this.arrowRewindElementName).show();
	}
}

function previousItem( id ) {

	if ( 0 < this.itemPosition ) {
		this.itemPosition--;
	}


	for (i = 0; i < this.itemsOnPage; i++) {
		$(this.itemElementName + i).innerHTML = $(id + (this.itemPosition + i)).innerHTML;
	}

	if (0 >= this.itemPosition) {
		$(this.arrowRewindElementName).hide();
	}

	if (this.itemPosition < ( this.itemsAll - this.itemsOnPage ) ) {
		$(this.arrowForwardElementName).show();
	}

}

function prepareArrows() {

	alert(this.itemPosition + 'prepareArrows ');
	if ( this.itemsAll <= this.itemsOnPage ) {
		$(this.arrowRewindElementName).hide();
		$(this.arrowForwardElementName).hide();
	}

	if ( 0 == this.itemPosition ) {
		alert(this.itemPosition + 'prepare ');
		$(this.arrowRewindElementName).hide();
	}

	if ( ( this.itemsAll - this.itemsOnPage ) == this.itemPosition ) {
		$(this.arrowForwardElementName).hide();
	}
}

