
// sv Schoenmakerij Vording
sv = {
    header: {
        container: $('ul#header-itemHolder')
		, collection: $('ul#header-itemHolder li')
        , timer: null

        , nextSlide: function(event, el) {
            var nextId = null;
            var collection = sv.header.collection;
            var length = collection.length;
            var size = null;

            sv.header.clearTimer(sv.header.timer);
            sv.header.setTimer();

            collection.each(function(i) {
                var itm = $(this);

                if (itm.hasClass('active')) {
                    if (i + 1 == length) {
                        nextId = 0;
                        size = ((length * 945) - 945) * -1;
                    }
                    else {
                        nextId = i + 1;
                        size = 945;
                    }
                    itm.removeClass('active');
                }
            });
            $(collection[nextId]).addClass('active');
            sv.header.container.animate({ "marginLeft": "-=" + size }, "slow");
            return false;
        }

        , prevSlide: function(event, el) {
            var nextId = null;
            var collection = sv.header.collection;
            var length = collection.length;
            var size = null;

            sv.header.clearTimer(sv.header.timer);
            sv.header.setTimer();

            collection.each(function(i) {
                var itm = $(this);
                if (itm.hasClass('active')) {
                    if (i === 0) {
                        prevId = length - 1;
                        size = length * 945 - 945;
                    }
                    else {
                        prevId = i - 1;
                        size = -945;
                    }
                    itm.removeClass('active');
                }
            });
            $(collection[prevId]).addClass('active');
            sv.header.container.animate({ "marginLeft": "-=" + size }, "slow");
            return false;
        }

		, setTimer: function() {
		    var fnx = function() { sv.header.nextSlide(null, null); };
		    sv.header.timer = setInterval(fnx, 6000);
		}

		, clearTimer: function(timer) {
		    clearTimeout(timer);
		}
    },

    geschiedenis: {
        init: function() {
            $('a.ges-link').each(function(i) {
                var el = $(this);
                if (!el.hasClass('active')) {
                    el.click(function() {
                        sv.geschiedenis.clearActive();
                        var titel = el.attr('title');
                        $('#ges_txt').html(titel);
                        el.addClass('active');
                        el.blur();
                        return false;
                    });
                }
                else {
                    el.click(function() { return false;  })
                }

            });
        }
		, clearActive: function() {
		    $('a.ges-link').each(function(i) {
		        var el = $(this);
		        if (el.hasClass('active')) { el.removeClass('active'); }
		    });
		}

    }
}

$(document).ready(function() {
    // Dom ready Events
   	$('a#arrow_prev').click(function(e){
		sv.header.prevSlide(e, this);
	});
	
	$('a#arrow_next').click(function(e){
		sv.header.nextSlide(e, this);				 	
	});
	
	if ( $("#ges_txt").length > 0 ) {
		sv.geschiedenis.init();
	}
	
	sv.header.setTimer();
});


