window.project = (window.project) ? window.project : {};

(function($)
{

project.SliderBehaviour = function(dom_element, options)
{
    var that = this;
    this.dom_element = dom_element;
    this.content_elements = this.dom_element.find(".slider > li");
    this.rotate_timer = null;

    this.ul_element = this.dom_element.find('.slider');
    this.position = 0;
    this.stage_size = {
        "x": 0,
        "y": 0
    };

    options = options || {};

    this.options = {
        "speed": parseInt(options.speed || 1000, 10),
        "step": parseInt(options.step || 1, 10),
        "rotate": options.rotate === "1" ? true : false,
        "duration": options.duration ||1000,
        "navigator": options.navigator ||false
    };

    if (this.options.rotate)
    {
        this.dom_element.bind({
            "mouseenter": function()
            {
                that.disableRotation();
            },
            "mouseleave": function()
            {
                that.enableRotation();
            }
        });

        that.enableRotation();
    }
    
    this.dom_element.find('.skip_left').bind("click", function()
    {
        that.stopVideos();
        that.previous();
    });
    this.dom_element.find('.skip_right').bind("click", function()
    {
        that.stopVideos();
        that.next();
    });

    if(this.options.navigator)
    {
        this.nav = this.dom_element.find('.slider_nav');
        this.nav_elements = this.nav.children('li');
    }

    this.initialize();
};

project.SliderBehaviour.prototype.initialize = function()
{
    var that = this;
    if (this.content_elements.length > 1)
    {
        this.stage_size.x = $(this.content_elements[0]).outerWidth();
        this.stage_size.y = $(this.content_elements[0]).outerHeight();
        this.ul_element.css({
            "width": this.stage_size.x * this.content_elements.length,
            "height": this.stage_size.y,
            "overflow": "hidden"
        });
        this.content_elements.each(function(index, element)
        {
            $(element).css({
                "position": "absolute",
                "top": 0,
                "left": that.stage_size.x * index
            });
        });
        
        if(this.options.navigator && this.options.step < 2)
        {
            this.nav_elements.each(function(index, li_element)
            {
                jQuery(li_element).click( {position: index}, function(event)
                {
                    that.setPosition(event.data.position);
                });
            });
        }
    }
};

project.SliderBehaviour.prototype.enableRotation = function()
{
    var that = this;
    this.options.rotate = true;
    clearTimeout(this.rotate_timer);
    this.rotate_timer = setInterval(function() {
        that.next();
    }, this.options.speed);
};

project.SliderBehaviour.prototype.disableRotation = function()
{
    this.options.rotate = false;
    clearTimeout(this.rotate_timer);
    this.rotate_timer = null;
};

project.SliderBehaviour.prototype.addEvents = function()
{
    var that = this;

    // prevent container scrolling on tab-key-navigation
    this.dom_element.bind("scroll", function()
    {
        that.dom_element.scrollTo(0);
    });

    // when an a-tag gets the focus, scroll to that container
    this.li_elements.each(function(index, li_element)
    {
        var a_elements = $(li_element).find("a");
        if (a_elements.length)
        {
            a_elements.bind("focus",function()
            {
                that.setPosition(index);
            });
        }
    });
};

project.SliderBehaviour.prototype.stopVideos = function()
{
	var videos_length = 0;
	if(typeof videoplayerCounter!='undefined'){
		videos_length = videoplayerCounter;
	}	

    //var videos_length = project.video_players.length;
	if(videos_length)
    {
        jQuery(project.video_players).each(function() {
		
           this.stop(videos_length);
        });
    }
}

project.SliderBehaviour.prototype.previous = function()
{
    var new_position = this.position - this.options.step;
    
    if (this.position > 0 && new_position < 0)
    {
        new_position = 0;
    }
    else if (new_position < 0)
    {
        new_position = this.content_elements.length - this.options.step;
    }
    this.setPosition(new_position);
};

project.SliderBehaviour.prototype.next = function()
{
    var new_position = this.position + this.options.step;
    if (new_position > this.content_elements.length - 1)
    {
        new_position = 0;
    }
    this.setPosition(new_position);
};

project.SliderBehaviour.prototype.setPosition = function(position)
{
    if(this.options.navigator && this.options.step < 2)
    {
        jQuery(this.nav_elements[this.position]).removeClass('active');
        jQuery(this.nav_elements[position]).addClass('active');
    }
    this.position = position;
    for (var i = 0; i < this.content_elements.length; i++)
    {
        jQuery(this.content_elements[i]).animate({
            'left': this.stage_size.x * i - this.stage_size.x * this.position
        }, {
            'duration': this.options.duration
        });
    }
};

JsBehaviourToolkit.registerHandler("slider", project.SliderBehaviour);

})(jQuery)

