// JavaScript Document

var newsPageURL = "http://www.mcginley.co.uk/p/h/Home/McGinley_News/4/?lang=";
//var newsPageURL = "page.htm?cpi_id=4";

var initHomepage = function(local) {
	
	if(local == true)
	{
		var reader = new RSSReader({className: "rssWrapper", xmlPath: "rss.xml", scrollbar: false, thumbMaxHeight: 80, scrollbarWidth: 14, titleLength: 42, truncationDots: true});
		
		$("#searchButton").click(function(event){
			event.preventDefault();
			$("#searchText").val($("#keywordInput").val() + " " + $("#location").val() + " " + $("#sector").val());
			$('#searchform').submit();
		});
	}
	
	var slideScroll1 = new slideScroll({divID: "scroll1"});	
	
	var mainGallery = new Gallery({wrapper: "galleryWrapper", xmlPath: "storage/xml/slideshow-1.xml", layout: "arrowsOnly", imagesClickable: false, transition: "fade", fadeButtons: true, pauseOnHover: true, slideInterval: 3500 });
	
	$(".scrollBtnLeft").click(function(event){
		event.preventDefault();
		$(".marketsMover").stop().animate({marginLeft: '0px'}, 300, 'easeOutExpo', function(){$(".moreInfoBox").css({'display' : 'none'});});
	});
	
	$('.newsReportsContent').load(newsPageURL + ' #newsItems', function() {
		newsLoadComplete();
	});
	
	$(".moreInfoBox").css({'display' : 'none'});

}

var getMoreInfo = function(i) {
	
	$(".moreInfoBox").css({'display' : 'none'});
	$("#moreInfo"+i).css({'display' : 'inline'});
	
	var iconWidth = $('.iconGridWrapper').width() + 15;
	
	$(".marketsMover").stop().animate(
			{marginLeft:  '-'+iconWidth+'px'},
			{duration: 300,
			easing: 'easeOutExpo'});
}

var newsLoadComplete = function()
{
	var replaceHTML = $('.newsReportsContent #newsItems').html();
	replaceHTML = '<div class="singleItem">' + replaceHTML + '</div>';
	replaceHTML = replaceHTML.replace(/\<hr\>/gi, '</div><div class="singleItem">');
	$('.newsReportsContent #newsItems').html(replaceHTML);
	
	var newHTML = '';
	var i = 0;
	$('.newsReportsContent .singleItem').each( function() {
		if(i != 0)
		{
			newHTML+='<hr/>';
		}
		newHTML+='<p>';
		var pText = $('p', this).first().html();
		newHTML+=pText.trunc(120, true);
		newHTML += '...';
		newHTML += '<a href="'+newsPageURL+'#newsItem'+i+'">more</a>';
		newHTML+='</p>';
		i++;
	});
	
	
	/* old method
	html = html.split(/\<hr\>/i);
	var newHTML = '<div id="newsItems">';
	var i = 0;
	for(i = 0; i < html.length; i++)
	{
		newHTML += '<p>';
		var newText = html[i];
		newText = newText.replace("<div id=\"newsItems\">", "");
		newText = newText.replace(/\<p\>/i, "");
		newText = newText.split(". ");
		newText = newText[0];
		newText = newText.split(".<");
		newText = newText[0];
		newText = newText.split(" <");
		newText = newText[0];
		newText += '...';
		newText += '<a href="'+newsPageURL+'#newsItem'+i+'">more</a>';
		newHTML += newText;
		newHTML += '</p>';
		newHTML += '<hr>';
	}
	
	newHTML += '</div>';
	$('.newsReportsContent').html(newHTML)
	*/
	
	$('.newsReportsContent #newsItems').html(newHTML)
	
	var slideScroll2 = new slideScroll({divID: "scroll2",scrollPXAmount: 100});
}

String.prototype.trunc =
     function(n, useWordBoundary){
		 
		 var tooLong = false;
		 if(this.length > n)
		 {
			 tooLong = true;
		 }
		 
		 var newString = this;
		 
		 if(tooLong)
		 {
			 newString = this.substr(0,n-1);
		 }
		 
		 if(useWordBoundary && tooLong)
		 {
			 newString = newString.substr(0, newString.lastIndexOf(' '));
		 }
		 
         return  newString;
      };

