
	  //create the scratch
	  $('body').append('<div id="scratch" style="display:none;" />').find('#scratch').append('<div id="h1" />').append('<div id="h2" />');
	 
	  var urlVar = 'http://' + document.location.hostname + '/';
      
            $('#scratch #h1').load(urlVar + ' #top-articles');
			$('#scratch #h2').load(urlVar + ' #secondary-articles', function(){
      

$(document).ready(function(){
		
		$('#ticker').append('<ul />');

$('#scratch h1, #scratch h2').each(function(){

charLimit = 100; // set character limit

$$ = $(this);

hVar = $$.children('a').text(); // Get h1 as var

hVar = jQuery.trim(hVar);

h1Length = hVar.length; // Get length of h1 var

aVar = $$.children('a').attr('href'); // Get h1 A HREF as var

if ( $$.is('h1') ) { pVar = $$.next().text(); if (pVar=='') { pVar = $$.next().next('p').text(); } } // Get next P tag as var

else if ( $$.is('h2') ) { pVar = $$.parent().parent().find('p:eq(1)').text(); if (pVar=='') { pVar = $$.parent().parent().find('p:eq(2)').text(); } }

pVar = jQuery.trim(pVar);

pString = hVar + ' : ' + pVar; // Combine h1 and p var to string

if ( pString.length >= charLimit ) { // Only trim pString if total character length is equal to or more than charLimit

pString = jQuery.trim(pString);

pString = pString.slice(0, charLimit); // Trim string to 120 chars

}

indexVar = pString.lastIndexOf(' '); // Get lastIndexOf value for last white space in string... 

pString = pString.slice(0, indexVar); // ...and slice to this


pString = pString.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&');

var chars = [' ','!','\"','£','$','%','^','&','*','(',')','-','_','+','=','[',']','{','}',';',':','\'','@','#','~','\,','<','>','.','/','?','\\']; // create array of special characters

arLen = chars.length;

for ( var i=0; i < arLen; i++ ){// begin cycle through array

var pLength = pString.length;

var lastChar = pString.charAt(pLength-1);// get pString last char

if ( lastChar == chars[i] ) { pString = pString.slice(0, pLength-1); pString = jQuery.trim(pString); i = 0; } // reset i var to repeat cycle from beginning 

}

pString = pString.replace('<', '<').replace('>', '>').replace('&', '&');

strLength = pString.length - h1Length;

strLength *= -1; // convert var to negative value

pString = pString.slice(strLength) + '...</li>'; // Split pString at length of strLength, starting from the end

mainString = '<li><a href="' + aVar + '">' + hVar + '</a>' + pString; // create new string from h1 var, A HREF var, and pString

$('#ticker ul').append(mainString).find('li').hide();

});



var total = $('#ticker ul li').length;

m = 0;

function ticker() {

if ( m == total) { m = 0; }

$('#ticker ul li').fadeOut(1000);

$('#ticker ul li:eq(' + m + ')').fadeIn(1000);

m++;

}

ticker();

var t = setInterval(ticker,3000);

$('#ticker').hover(function(){ clearInterval(t); }, function(){ t=setInterval(ticker,3000); });

$('#scratch').remove();

});

}); 
