// Related Products
EnjoyEngland.RelatedProducts =
{
	data:[],
	sectors:{},
	timer:null,

	setup: function()
	{
		if ( EnjoyEngland.RelatedProducts.data.length )
		{
			jQuery( 'body' ).append( '<script type="text/javascript" src="http://www.ntopsearch.com/jsonapi"></script>' );
			
			// poll for api load
			EnjoyEngland.RelatedProducts.timer = setInterval( EnjoyEngland.RelatedProducts.load, 500 );
		}
	},
	
	load: function()
	{ 

		if ( window.eviivo )
		{
			clearTimeout( EnjoyEngland.RelatedProducts.timer );

			// iterate through modules and load data			
			jQuery.each( EnjoyEngland.RelatedProducts.data, function(index,item)
			{	
				if ( this.id && jQuery( '#' + this.id ).length )
				{
					var id = this.id;
					var type = this.type || 'simple';
					var items = this.items || 2;
					var urls = this.urls || { product:'http://search.enjoyengland.com/en-GB/Details.aspx?ContentID=' }
					var text = this.text || {};
					var industrySubTypes = EnjoyEngland.RelatedProducts.getIndustrySubTypesForSectors( this.params.Sectors );
					this.params.MaxResults = type == 'simple' ? items : items * 5;
					text.more = text.more || 'View more';
					text.all = text.all || 'View all';
					var params = { data:jQuery.toJSON( this.params ), success:function( json ) { EnjoyEngland.RelatedProducts.success( { id:id, type:type, industrySubTypes:industrySubTypes, items:items, urls:urls, text:text, data:json } ) } };

					// display loader
					jQuery( '#' + this.id ).addClass( 'loading' ).slideDown('slow');
					
					// call NTOP API
					var tim=50;
					setTimeout(function(){eviivo.wls.lightProductSearch( params )} , tim*index);
					
				}
			});

		}

	},

	success: function( data )
	{ 

		var list = jQuery( '<ul class="moduleNtopSubCollection clearfix"></ul>' );

		var j = 0;
		var total = 0;
		var categoryItems = {};
		var itemsDisplayed = {};
		jQuery.each( data.data, function(i)
		{	
			if ( data.type == 'simple' )
			{
				if ( i >= data.items ) return false;

				var alt = (i+1) == data.items ? 'moduleNtopCol last' : 'moduleNtopCol';

				var title = this.Name;
				if ( title.length > 31 )
				{
					var j = 28;
					while ( title.charAt( j ) != ' ' && j < title.length ) j++;
					title = title.substr( 0, j ) + '...';
				}

				var description = this.ShortDescription;
				if ( description.length > 80 )
				{
					var j = 77;
					while ( description.charAt( j ) != ' ' && j < description.length ) j++;
					description = description.substr( 0, j ) + '...';
				}

				// build up list
				var linkUrl = data.urls.product + this.ContentId;
				this.ThumbnailImageURL = this.ThumbnailImageURL.replace(/^(.+)-t(\.\w+)$/g, "$1$2");

				var item = jQuery( '<li class="' + alt + '"><div class="imgHolder"><a href="' + linkUrl + '" target="_blank"><img class="ntopThumb" height="156" width="206" src="' + this.ThumbnailImageURL + '" alt="' + this.name + '" /></a></div><h3><a href="' + linkUrl + '" target="_blank">' + title + '</a></h3><p>' + description + '</p></li>' );
			

				list.append( item );

				itemsDisplayed[ 'i_' + i ] = true;

				total++;
			}
			if ( total >= data.items ) return false
		} );
		// write list to page

		jQuery( '#' + data.id ).append( list );
		
		// if cateogory url, write more link
		if ( data.urls.category && data.type == 'simple' )
		{
			jQuery( '#' + data.id ).append( '<div class="textRight"><a href="' + data.urls.category + '" class="icon" target="_blank">' + data.text.all + '</a></div>' );
		}
		//$( ".ntopThumb" ).aeImageResize({ height: 156, width: 206 });
		$('.moduleNtopCollection li').children('div').removeClass('loading');
	},
	
	getIndustrySubTypesForSectors: function( sectors )
	{
		var subtypes = [];
	
		jQuery.each( sectors, function()
		{
			if ( this.IndustrySubType ) subtypes.push( this.IndustrySubType );
		} );
		
		return subtypes;
	}
}
jQuery( document ).ready( EnjoyEngland.RelatedProducts.setup );

