/*jslint browser: true */
/*global $ */

var Application = function (settings) {
    var stage,
    StageGallery = function (selector) {
        var stage = this,
            first = $(selector + ' li:first'),
            last = $(selector + ' li:last'),
            interval = 3000,
            timeOut,
        helper = function (direction) {
            clearTimeout(timeOut);
            var currentNode = $(selector + ' li.active'),
                sibling;
            if (direction === 'prev') {
                sibling = currentNode.prev();
                if (!sibling.length) {
                    sibling = last;
                }
            } else {
                sibling = currentNode.next();
                if (!sibling.length) {
                    sibling = first;
                }
            }
            sibling.fadeIn('slow').addClass('active');
            currentNode.removeClass('active').fadeOut('slow');
            if (timeOut) {
                stage.play();
            }
            return false;
        };
        this.play = function (newInterval) {
            if (newInterval) {
                interval = newInterval;
            }
            clearTimeout(timeOut);
            timeOut = setTimeout(this.next, interval);
        };
        this.stop = function () {
            clearTimeout(timeOut);
            timeOut = null;
        };
        this.next = function () {
            return helper('next');
        };
        this.prev = function () {
            return helper('prev');
        };
    },
    articleSelector = function () {
        var index = $(this).val();
        if (!index) {
            $('#articles .item').show('fast');
        } else {
            $('#articles .item').hide('fast').parent().find(':nth-child(' + index + ')').show('fast');
        }
    },
    initEventListeners = function () {
        $(settings.stageSelector + ' .prev').click(stage.prev);
        $(settings.stageSelector + ' .next').click(stage.next);
        //$('#articles .visual').fancybox();
        $('#article_selection select').change(articleSelector);
    };
    this.init = function () {
        stage = new StageGallery(settings.stageSelector);
        stage.play();
        initEventListeners();
    };
    $('.userinput[name|="slogin"]').focus();
    $('input[name|="company"]').focus();
    $('.userinput[name|="aUser[company]"]').focus();
 };

/**
 * Methode um Formularfelder, die einen Standardtext enthalten, bei "onFocus" leer zu machen.
 * Bei "onBlur" wird der Standardtext wieder eingetragen.
 * Der Standardtext muss im Attribut placeholder="" vorhanden sein.
 * Formularfelder kommasepariert der Variablen "placeHoldersSelector" zuweisen.
 */
var initPlaceholders = function() {
    var placeHoldersSelector = '#searchField';
    $(placeHoldersSelector).each(function() {
        var str = 'placeholder';
        if(!(str in document.createElement('input'))) {
            var placeholder = $(this).attr(str);
            $(this).addClass(str)
                .val(placeholder)
                .focus(function() {
//                    if($(this).val() == placeholder) {
                        $(this).val('');
                        $(this).removeClass(str);
//                    }
            }).blur(function() {
                if($(this).val() == '') {
                    $(this).addClass(str);
                    $(this).val(placeholder);
                }
            });
        }
    });
}


$(function () {
    (new Application({
        stageSelector: '#stage'
    })).init();

    initPlaceholders();

});
