(function($)
{	
    var $stop = false;
    var $last_id;

    $.fn.marquee = function($options)
    {
       /*
        * Jquery plugin fot marquee
        */
        var pixels_per_second = $options.pps;
        var delay = $options.delay;
        var direction = $options.direction;
        var style = $options.style;
        var $url = $options.url;

        var $list = $(this);
        var $divs = $list.children('div');
        var $current_div;
        var pixels_per_millisecond = pixels_per_second / 1000;
        var easing = (delay == 0) ? 'linear' : 'swing';

        $list.hover(function()
        {
            $stop = true;
        },
        function()
        {
            $stop = false;
        });

        if (style == "even") {
            $list.find('>div:even').attr('class', 'even');
        } else if (style == "first") {
            $list.find('>div:first').attr('class', 'first-marquee');
        }

        // Set position relative if position is currently static so top/left properties will affect it's position            
        if ('static' == $list.css('position')) {
            $list.css('position', 'relative');
        }
        
        
        // Remove text nodes between lis since they add spacing that 
        // will be removed when shifting lis from end to end
        if ('left' == direction || 'right' == direction) {
            var text = [];
            this.each(function() {
                var children = this.childNodes;
                for (var i = 0; i < children.length; i++) {
                    var child = children[i];
                    if (child.nodeType == 3) {
                        text.push(child);
                    }
                }
            });
            
            //$(this.pushStack(text)).remove(); //Commented because if we remove the childs events that was assigned to this child will be removed too.
        }
        
        if ('up' == direction || 'left' == direction) {
            $current_div = $divs.filter(':first');
            $last_div = $divs.filter(':last');
        }
        else {

            // If right or down, move the last li to the beginning and move the entire
            // list up/left to allow the next li to scroll into view rather than just appear.
            var $last_div = $divs.filter(':last');
            if ('down' == direction) {
                $list.css('top', '-' + $last_div.outerHeight() + 'px');
            }
            else {
                $list.css('left', '-' + $last_div.outerWidth() + 'px');
            }
            //$last_div.remove(); //Commented because if we remove the childs events that was assigned to this child will be removed too.
            $divs.filter(':first').before($last_div);
            $divs = $list.children('>div');
            $current_div = $divs.filter(':last');
            $last_div = $divs.filter(':first');
        }

        $last_id = $last_div.attr('id');
        
        function scrollListNext() {
            var distance, property;
            var css = {};

            if ('up' == direction || 'down' == direction) {
                property = 'top';             
                distance = $current_div.outerHeight();
            }
            else {
                property = 'left';             
                distance = $current_div.outerWidth();
            }

            switch (direction) {
                case 'up':   
                    css.top = '-' + distance + 'px';
                    break;
                    
                case 'right':                
                    css.left = 0;
                    break;

                case 'down':                
                    css.top = 0;
                    break;
                    
                case 'left':                
                    css.left = '-' + distance + 'px';
                    break;
            };

            if ($stop != true) {
                var duration = distance / pixels_per_millisecond;

                if (($current_div.attr('id') == $last_id) && ($url != "")) {

                    jQuery.ajax({
                        url: $url,
                        //cache: false,
                        //ifModified: true,
                        success: function(response){
                            $list.html(response);

                            if (style == "even") {
                                $list.find('>div:even').attr('class', 'even');
                            } else if (style == "first") {
                                $list.find('>div:first').attr('class', 'first-marquee');
                            }
                        }
                    });

                    /*jQuery.post($url, function(response)
                    {
			        	$list.html(response);

                        if (style == "even") {
                            $list.find('>div:even').attr('class', 'even');
                        } else if (style == "first") {
                            $list.find('>div:first').attr('class', 'first-marquee');
                        }
        			});*/
                }

                $list.animate(css, duration, easing, function() {
                    //$current_div.remove(); //Commented because if we remove the childs events that was assigned to this child will be removed too.
                    if ('up' == direction || 'left' == direction) {
                        $list.css(property, 0);
                        $divs.filter(':last').after($current_div);
                        $divs = $list.children('div');
                        $current_div = $divs.filter(':first');

                        if (style == "first") {
                            $list.find('>div:first').attr('class', 'first-marquee');
                            $list.find('>div:last').attr('class', 'marquee');
                        }
                    }
                    else {
                        $list.css(property, '-' + distance + 'px');
                        $divs.filter(':first').before($current_div);
                        $divs = $list.children('div');
                        $current_div = $divs.filter(':last');

                        if (style == "first") {
                            $list.find('>div:first').attr('class', 'first-marquee');
                            $list.find('>div:last').attr('class', 'marquee');
                        }
                    }
                });
            }

            setTimeout(scrollListNext, delay);
        };

        setTimeout(scrollListNext, delay);
        /* End of marquee plugin */ 
    };
})(jQuery);

