/**
 * GENIOS JavaScript core
 *
 * @author  Stefan Vogt <s.vogt@edelweiss72.de>
 *
 * Conventions:
 * Variables in camelCase
 * Classes in PascalCase
 * Function/Methods in camelCase
 */
 

/**
 * Bild-Skiper im Start-Teaser
 *
 * @static
 */
GENIOS.ImageSkiper = {

    /**
     * Pfad zu den Bildern
     * @type  {Array}
     */
    imagePaths: new Array(),

    /**
     * aktuelle Bildposition
     * @type  {Integer}
     */
    imagePosition: 0,

    /**
     * springt ein Bild vor oder zurück
     *
     * @static
     *
     * @param  {String}     direction           'forward' oder 'backward'
     */
    skip: function(direction){
        var imageNumber = this.imagePaths.length;
        if (direction == 'forward'){
            if (this.imagePosition < imageNumber - 1){
                this.imagePosition += 1;
            }else{
                this.imagePosition = 0;
            }
        }else{
            if (this.imagePosition > 0){
                this.imagePosition -= 1;
            }else{
                this.imagePosition = imageNumber - 1;
            }
        }
        document.getElementById('teaser_skip_image').src = this.imagePaths[this.imagePosition];
    }
};


/**
 * Class GENIOS.Teaser
 * controlls all teaser-skip functions
 */
GENIOS.Teaser = function() {

    // show skiper buttons
    $("teaser_skip_left").style.visibility = 'visible';
    $("teaser_skip_right").style.visibility = 'visible';
    // show pager status
    $("teaser_pager_status").show();

	this.teaserArray = new Array();		// contains all teaser-htmls
	this.teaserPos = 0;					// current teaser-position
	this.firstBox = 1;					// shows the number of the firt current-visible teaser (teaser "1" of 3)
	this.lastBox = 3;					// shows the number of the last current-visible teaser (teaser 1 of "3")

	/**
	 * void  writePos()
	 * shows the current teaser-positions (teaser "2-4" for example)
	 */
	this.writePos = function(pFirst, pFrom) {
		$("teaser_first").innerHTML = pFirst;
		$("teaser_from").innerHTML = pFrom;
	}

	/**
	 * void  __construct()
	 * constructor for initialisig the theaser-functions
	 */
	this.__construct = function() {

		var c=0; // counter for the "teaserArray" -container

		// 1. get all Teasers in Array
		for (var i=0; i<$("TeaserParentBox").childNodes.length; i++) {
			if ($("TeaserParentBox").childNodes[i].nodeType == 1) {
				this.teaserArray[c] = $("TeaserParentBox").childNodes[i];
				c++;
			}
		}

		// 2. display the first visible teasers
		for (i=1; i<=this.lastBox; i++) {
			$("PrintTeaserBox"+i).innerHTML = this.teaserArray[i-1].innerHTML;
		}

		// 3. display position
		this.writePos(this.firstBox, this.lastBox);
		$("teaser_all").innerHTML = this.teaserArray.length;
		this.setButtonCssClass();
	}

	/**
	 * void  setButtonCssClass()
	 * controls the css-class for the navigaton-buttons (disabled or enabled)
	 */
	this.setButtonCssClass = function() {
		if (this.teaserPos == 0) {
			$("teaser_skip_left").className = "adisabled";
		} else {
			$("teaser_skip_left").className = "";
		}

		if (this.teaserPos >= this.teaserArray.length-this.lastBox) {
			$("teaser_skip_right").className = "adisabled";
		} else {
			$("teaser_skip_right").className = "";
		}
	}

	/**
	 * void  skip()
	 * skip-function for showing and hiding the teasers
	 */
	this.skip = function(mode) {

		switch (mode) {
			case 1:
				if (this.teaserPos >= this.teaserArray.length-this.lastBox) {return;}
				this.teaserPos++;
				break;

			default:
				if (this.teaserPos <= 0) {return;}
				this.teaserPos--;
				break;
		}

		this.setButtonCssClass();
		this.writePos(this.teaserPos+1, this.teaserPos+this.lastBox);

		// display Teasers
		for (var i=1; i<=this.lastBox; i++) {
			$("PrintTeaserBox"+i).innerHTML = this.teaserArray[this.teaserPos+(i-1)].innerHTML;
		}
	}

	/* constructor */
	this.__construct();

}

