function Carousel() {
	
    var carousel = this;
    var carouselElement = '#carouselWrapper';
    var elementBlock = carouselElement + ' .carouselItem';
    var animationSpeed = 350;
    var maxItems = '';
    var controlElement = '.jcarousel-control a';
    var imageIdCounter = '#carousel-image-'
    var intervalContainer = null;
    var imageWrapper = '#carouselImages';
    var carouselImage = imageWrapper + ' img';
    var changeToNext = 'a.rightArrow'
    var changeToPrevious = 'a.leftArrow'
    
    this.initCarousel = function () {
    	$(window).resize(function() {
    		Carousel.centerImage();
    	});
        $(controlElement).first().addClass('active');
        $(carouselImage).first().css('display', 'block');
        $(elementBlock).first().css('display', 'block');
        maxItems = $(carouselImage).length;
        Carousel.centerImage();
        $(controlElement).each(function () {
            $(this).click(function () {
                //if (intervalContainer != null) { //lamer click protection is off, if you click more than 100 times in 100 ms the script may crash in IE
                    currentControlItem = $(controlElement + '.active');
                    nextControlItem = $(this);
                    if ($(currentControlItem).attr('title') != $(nextControlItem).attr('title')) Carousel.go_to_position(currentControlItem, nextControlItem);
                //}
            });
        });
        $(changeToNext).click(function () {
            if (intervalContainer != null) {
                currentControlItem = $(controlElement + '.active');
                nextControlToChange = $(currentControlItem).next();
                if (nextControlToChange.attr('title') == null || nextControlToChange.attr('title') == 'undefined') {
                    nextControlToChange = $(controlElement).first();
                }
                Carousel.go_to_position(currentControlItem, nextControlToChange);
            }
        });
        $(changeToPrevious).click(function () {
            if (intervalContainer != null) {
                currentControlItem = $(controlElement + '.active');
                previousControlToChange = $(currentControlItem).prev();
                if (previousControlToChange.attr('title') == null || previousControlToChange.attr('title') == 'undefined') {
                    previousControlToChange = $(controlElement).last();
                }
                Carousel.go_to_position(currentControlItem, previousControlToChange);
            }
        });
        Carousel.startLoopAutoscroll();
    };
    this.startLoopAutoscroll = function () {
        if (intervalContainer == null) {
            intervalContainer = setInterval('Carousel.autoscroll()', 5500);
        }
    };
    this.stopLoopAutoscroll = function () {
        clearInterval(intervalContainer);
        intervalContainer = null;
    };
    this.autoscroll = function () {
        selectedControl = $(controlElement + '.active');
        currentElement = $(elementBlock + ':visible');
        Carousel.animateElements(currentElement, null, selectedControl, null);
    };
    this.animateElements = function (currentElement, nextElement, selectedControl, clickedControl) {
    	itemNumber = $(controlElement + '.active').attr('title');
    	
        $(elementBlock + '.current').removeClass('current');
        
        $(selectedControl).removeClass('active');
        currentImage = $(carouselImage + ':visible');
        nextImage = $(currentImage).next();
        $(currentImage).stop(true, true).fadeOut(animationSpeed);
        $(currentElement).stop(true, true).hide();
        
        if ((nextElement == null) || (clickedControl == null)) {
            if (itemNumber == maxItems) {
                $(elementBlock).first().stop(true, true).show();
                $(controlElement).first().addClass('active');
                $(carouselImage).first().fadeIn();
                $(carouselImage).first().css('display', 'block');
                $(elementBlock + ':visible').addClass('current');
            } else {
                $(currentElement).next().show();
                $(selectedControl).next().addClass('active');
                $(nextImage).fadeIn(animationSpeed);
                $(nextImage).css('display', 'block');
                $(elementBlock + ':visible').addClass('current');
            }
        } else {
            $(clickedControl).addClass('active');
            clickedImage = imageIdCounter + $(clickedControl).attr('title');
            $(clickedImage).fadeIn(animationSpeed);
            $(clickedImage).css('display', 'block');
            $(nextElement).show();
            $(elementBlock + ':visible').addClass('current');
        }
        
    };
    this.go_to_position = function (currentControlElement, clickedControlElement) {
    	Carousel.stopLoopAutoscroll();
        currentElement = $(elementBlock + ':visible');
        controlElementNumber = $(clickedControlElement).attr('title');
        var nextElementContainer = null;
        $(elementBlock).each(function () {
            if ($(this).attr('rel') == controlElementNumber) nextElementContainer = this;
        });
        Carousel.animateElements(currentElement, nextElementContainer, currentControlElement, clickedControlElement);
    };
    
    this.centerImage = function (){
    	// documentWidth = $(document).width(); - problems in gte IE 8
		
		// Measuring the viewport correctly
		documentWidth = document.documentElement.clientWidth;
    	
    	posLeft =  (documentWidth - 1600) / 2;
    	if(documentWidth < 1600){
    		$(carouselImage).css('left', posLeft);
    	}else{    		
    		$(carouselImage).css('left', posLeft);
    	}
    };
}
var Carousel = new Carousel();
