/** 
    Searches for all elements loacted in a marquee tag.
    Creates a new div tag and moves the marquee elemets to that created div.
    Clones all elements and place them directly beneath the original elements in order to allow a seamless transition.
    Scrolls the articles with the defined scrollSpeed.   
   
    The marquee that contains the news articles to scroll must have assigned the class "newsArticles".
    If no Javascript is enabled the marquee tag itself scrolls its content (although it looks not so nice).

	Expects jQuery was assigned jQuery and jQuery is running in no conflict mode. So you can use
	this script in a heterogeneous environment

**/ 
    
jQuery(function(){                                    

    var scrollSpeed = 40;
    var delay = 3500;
    var marqueeHeight = jQuery("marquee.newsArticles").height();
    var scrollInterval;                                           
	var startTrigger;
	var articlesHeight;
	
	function installNewsScroller() {
	    jQuery("<div><!-- --></div>").addClass("newsArticles").css({"height" : (marqueeHeight + "px"), "position" : "relative", "overflow" : "hidden", "width" : "185px"}).appendTo("div.mediaNews");
	    jQuery("<div><!-- --></div>").addClass("articleScroller").css({"position": "absolute", "top": "0px"}).appendTo("div.newsArticles");

	    jQuery("marquee.newsArticles").children().appendTo("div.articleScroller");
	    articlesHeight = jQuery("div.articleScroller").clone().appendTo("div.newsArticles").height();
	    jQuery("div.articleScroller:eq(1)").css("top", articlesHeight);

	    jQuery("marquee.newsArticles").hide();

	    startTrigger = setTimeout(startScrolling,delay);

	    jQuery(".articleScroller").hover(function(){
	        clearTimeout(startTrigger); 
	        clearInterval(scrollInterval);
	    } , startScrolling);
	}

    // Scrolls both, the original articelScroller div and its clone
    function scroll(){
        jQuery(".articleScroller").each(function(){
            var y = parseInt(jQuery(this).css("top"));
            jQuery(this).css("top", y-1);
            if (y<(0-articlesHeight)) {
                resetPosition(jQuery(this));
            }
        });
    };

    function resetPosition(obj){
        obj.css("top", articlesHeight);
    };  

    function startScrolling(){
        scrollInterval = setInterval(scroll, scrollSpeed);
    }                           

	if ((jQuery("marquee.newsArticles").length) > 0) {
		installNewsScroller();
	}
});   

