//SCROLL HEADLINES BY JOEL GRANNAS
hlSpeed=50000;
outerContainer = "div.hp.column.three";
//scrollHeadlines();


function scrollHeadlines(){
	try {
		if ((top.location.href !== window.location.href)  && (mode != "frame")){
			var mode = "frame";
		}
	} catch (e) {    
		// if you're in an iframe in a different domain, the top.location check
		// results in a security exception
		mode = "frame";
	}
     if(($("div.ui-widget.app.headlines h1.ui-article-title").length > 0 ) && (mode != "frame")){

          moveRSS();
          initVars();
          getHeights();
          gatherAndStack();
          animate();
          onHover();
     }
}

function moveRSS(){
	rssCode = $("div.ui-widget.app.headlines .ui-widget-footer").html();
	$("div.ui-widget.app.headlines .ui-widget-footer").remove();
	
	

   
}

function initVars(){
     headline_count = 0;
     headline_interval = 0;
     old_headline = 0;
     current_headline= 0;
     headline_height = 0;
     headlinesContent ="";
     //$("#hp-headlines-body table").addClass("headline");
     headline_count = $("div.ui-widget.app.headlines li .ui-article").size();
     $("div.ui-widget.app.headlines li .ui-article").each(function(){
		// $("div.ui-widget.app.headlines .ui-article").remove();
		 $("div.ui-widget.app.headlines .ui-widget-footer").remove();												
          headlinesContent += $(this).html();
		 		  
     });
     currentlyAtTop = 0;
}

function getHeights() {   
     headlinesTempWrapper = "<div id='headlinesTempWrapper'>" + headlinesContent + "</div>"
     $("div.ui-widget.app.headlines .ui-widget-detail .ui-articles").append(headlinesTempWrapper);
     headline_height = $("#headlinesTempWrapper").height();
     $("#headlinesTempWrapper").remove();
}

function gatherAndStack(){
     containerHeight = $("div.ui-widget.app.headlines .ui-widget-detail .ui-articles").height();
     divideBy = Math.ceil(containerHeight / headline_height)+1;
     for(j=0;j<divideBy;j++){
          headlinesWrapped = "<div class='stacked-ui-article' id='stack"+j+"'>" + headlinesContent + "</div>";
          $("div.ui-widget.app.headlines .ui-widget-detail .ui-articles").append(headlinesWrapped);
     }
     //$("#hp-headlines-body table").remove();
     $("div.ui-widget.app.headlines .ui-widget-detail .ui-articles  > *").wrapAll("<div id='scrollPane'></div>");
     $("#scrollPane").css({position:"absolute", top:"0px", left:"0px"});
	 $(outerContainer).append("<div id='rssFeed'>" + rssCode);
}

function animate(){
     moveDistance = headline_height*-1;
     $("#scrollPane").animate({ top: moveDistance }, {easing: "linear", duration:hlSpeed, queue: false, complete: function(){ andAgain() } });
}

function andAgain(){
     $("#scrollPane").css("top", "0");
     animate();     
}

function onHover(){
    $("div.ui-widget.app.headlines .ui-widget-detail .ui-articles").hover(function () {
      $("#scrollPane").stop();
    }, function () {
        percentMoved = ($("#scrollPane").position().top*-1) / headline_height;
        durationSet = hlSpeed * (1-percentMoved);
        $("#scrollPane").animate({ top: moveDistance }, {easing: "linear", duration:durationSet, queue: false, complete: function(){ andAgain() } });
      });    
}

