//Set global variables
var XmlPath = '';
var NextListAppended = false;
var GlobalAllList = new Array();
var Sport1ItemsCacheList = new Array();
var VimediaItemsCacheList = new Array();
var ViradioItemsCacheList = new Array();
var EuropaleagueItemsCacheList = new Array();
var GemistItemsCacheList = new Array();
var lastPlayer = '';
var CurrentDomain = 'http://'+document.domain;

//Function to set the xmlPath (for test/live environment)
function setXMLPath()
{
	if(CurrentDomain.toLowerCase().indexOf("test.vi.nl.kpnis.nl") < 0)
	{
		//It's the LIVE environment
		xmlPath = '/files/xmlcache/';
	}
	else
	{
		//It's the TEST environment
		xmlPath = '/static/vi/xml/';
	}
}

function getCustomBackground()
{
	//Function to set a custom background for the page. This function creates two divs that are placed left and right of the page
	//The two divs are 'stuck' to the left and right side 
	
	//Set the width of the sidebanners/divs, the backgroundcolor of the body (in case of small banners and the link when the banners are clicked
	bannerWidth = '142';
	bannerHeight = '800';
	backgroundColor = '#000';
	s_link = 'http://www.rtl7.nl/';

	//If takeover is flash, put source here (like http://www.vi.nl/static/vi/images/rtl/rtl_bg_left.swf)
	swfSrcLeft = '';
	swfSrcRight= '';

	//If takeover is JPG, put source here (like http://www.vi.nl/static/vi/images/rtl/rtl_bg_left.png)
	jpgSrcLeft = '/static/vi/images/rtl/rtl_bg_left.png';
	jpgSrcRight = '/static/vi/images/rtl/rtl_bg_right.png';

	//Place the two divs when the page is loaded
	$(document).ready(function(){
		takeover();
	});

	//When the user resizes his browserwindow, 'reposition' the divs again to create the 'sticky effect'
	$(window).resize(function(){
	   replaceTakeoverSides();
	});
}

//Function to create a lightbox
function loadFancyBox()
{
	$("a.iframe").fancybox({
		'width'		:	500,
		'height'	:	550,
		'padding'	:	0
	});	
}

function loadImagesLightbox()
{
	if ($('a.lightboxImage').length)
	{
		$("a.lightboxImage").fancybox({
			'cyclic'	:	true,
			'titlePosition' :	'inside',
			'transitionOut' :	'none'
		});
	}
}
function takeover()
{
	var position = placeTakeoverPosition();	
	//If banners are Flash
	if(swfSrcLeft != '' && swfSrcRight != '')
	{
		var divLeft = '<div id="takeOverLeftBar" style="height: ' + bannerHeight + 'px; background: ' + backgroundColor + '; width: ' + bannerWidth + 'px; margin: 0; left: ' + position + 'px; overflow:hidden; padding: 0;position: fixed; top: 0;"><a onclick="javascript:pageTracker._trackEvent(\'Clicktracker\', \'Nike background\', \'click\');" href="'+s_link+'" target="_blank"><embed src="' + swfSrcLeft + '"  scale="noscale" salign="LT" quality="high" wmode="transparent" id="" name="" swliveconnect="true" allowscriptaccess="always" allowfullscreen="true" menu="false" base="" type="application/x-shockwave-flash" width="'+ bannerWidth +'" height="'+ bannerHeight +'"></a></div>';
		var divRight = '<div id="takeOverRightBar" style="height: ' + bannerHeight + 'px; background: ' + backgroundColor + '; width: ' + bannerWidth + 'px; margin: 0; right: ' + position + 'px; overflow:hidden; padding: 0;position: fixed; top: 0;"><a onclick="javascript:pageTracker._trackEvent(\'Clicktracker\', \'Nike background\', \'click\');" href="'+s_link+'" target="_blank"><embed src="'+ swfSrcRight +'" scale="noscale" salign="LT" quality="high" wmode="transparent" id="" name="" swliveconnect="true" allowscriptaccess="always" allowfullscreen="true" menu="false" base="" type="application/x-shockwave-flash" width="'+ bannerWidth +'" height="'+ bannerHeight +'"></a></div>'; 
	}
	else
	{
		var divLeft = '<div id="takeOverLeftBar" style="height: ' + bannerHeight + 'px; background: ' + backgroundColor + ' url(' + jpgSrcLeft + ') right 0 no-repeat; width: ' + bannerWidth + 'px; margin: 0; left: ' + position + 'px; overflow:hidden; padding: 0;position: fixed; top: 0;"><a onclick="javascript:pageTracker._trackEvent(\'Clicktracker\', \'Nike background\', \'click\');" href="'+s_link+'" target="_blank" style="display: block; height: 100%; width: 100%;">&nbsp;</a></div>';
		var divRight = '<div id="takeOverRightBar" style="height: ' + bannerHeight + 'px; background: ' + backgroundColor + ' url(' + jpgSrcRight + ') left 0 no-repeat; width: ' + bannerWidth + 'px; margin: 0; right: ' + position + 'px; overflow:hidden; padding: 0;position: fixed; top: 0;"><a onclick="javascript:pageTracker._trackEvent(\'Clicktracker\', \'Nike background\', \'click\');" href="'+s_link+'" target="_blank" style="display: block; height: 100%; width: 100%;">&nbsp;</a></div>'; 		
	}

	$('body').css('background-image', 'none');
	$('body').css('background-color', backgroundColor);
	$('#wrapper').append(divLeft);
	$('#wrapper').append(divRight);
}

function placeTakeoverPosition(width)
{
	//Function to place the divs
	windowWidth = $(window).width();
	sideSpaces = windowWidth - 980;
	positionLeft = (sideSpaces / 2) - bannerWidth;
	return positionLeft;
}

function replaceTakeoverSides()
{
	var position = placeTakeoverPosition();	
	$('#takeOverLeftBar').css('left', position);
	$('#takeOverRightBar').css('right', position);
}

function loadTVGuide()
{
	//Function to load the TV-Guide for RTL pages (based on XML-feeds from RTL)
	
	$("#tvGuideHolderBlock").empty();

	//Browser check to set the datatype for ajax-call (xml or text)
	if ($.browser.webkit)
	{
		dataTypeVar = 'text';
	}
	else
	{
		dataTypeVar = 'xml';
	}

	itemsArray = new Array();
	var guideItemsCounter = 0;
	var xmlArray =new Array(); 

	//Array with all the needed XML-feeds
	xmlArray[0]=	xmlPath+'tvgids_vi.xml';       
	xmlArray[1]=	xmlPath+'tvgids_jupiler_league_match_of_the_week.xml'; 
	xmlArray[2]=	xmlPath+'tvgids_jupiler_league_live.xml';       
	xmlArray[3]=	xmlPath+'tvgids_jupiler_league.xml'; 
	xmlArray[4]=	xmlPath+'tv_gids_europa_league_samenvattingen.xml';       
	xmlArray[5]=	xmlPath+'tv_gids_europa_league.xml'; 

	for(i=0;i<xmlArray.length;i++)
	{
		xmlFile =  xmlArray[i];
		
		$.ajax({
			type: "GET",
			url: xmlFile,
			dataType: dataTypeVar,
			async: false, 
			success: function(xml) 
			{
				$(xml).find('item').each(function()
				{
					abstractKey = $(this).attr('abstract_key');
					seasonKey = $(this).attr('season_key');
					episodeKey = $(this).attr('episode_key');
					var episodeName = '';
					var episodeSyn = '';

					pubDate = $(this).find('localtime').text();
					var pubDatePublic = Date.parse(pubDate);					
					var pubDatePublicDate = new Date(pubDatePublic).toString("dd-MM");
					var pubDatePublicTime = new Date(pubDatePublic).toString("HH:mm");
					pubDatePublic = pubDatePublicDate + ' om ' + pubDatePublicTime;
					unixTime = $(this).find('unixtime').text();
									
					$(xml).find('library>s4m\\:abstracts').each(function()
					{
						episodeName = $(this).find('abstract[key=\''+abstractKey+'\']>name').text();
					});

					$(xml).find('library>s4m\\:episodes').each(function()
					{
						episodeSyn = $(this).find('episode[key=\''+episodeKey+'\']>s4m\\:synopsis').text();
						if(episodeSyn.length > 55)
						{
							episodeSyn = episodeSyn.substring(0, 60);
							episodeSyn += '...';
						}
					});

					itemsArray[guideItemsCounter] = new Array();
					
					if(episodeSyn == '')
					{
						itemsArray[guideItemsCounter][0] = '<li><p><strong>'+episodeName+'</strong><br /><strong>'+pubDatePublic+', RTL7</strong></p></li>';
					}
					else
					{
						itemsArray[guideItemsCounter][0] = '<li><p><strong>'+episodeName+'</strong><br />'+episodeSyn+'<br /><strong>'+pubDatePublic+', RTL7</strong></p></li>';
					}
					itemsArray[guideItemsCounter][1] = unixTime;

					guideItemsCounter++;
				});	
			}
		});
	}

	//Sort the array-items on date
	itemsArray = itemsArray.sort(sortNumber);

	var nrOfItems = itemsArray.length;

	if(nrOfItems < 4)
	{
		var itemsToShowCounter = nrOfItems;
	}
	else
	{
		var itemsToShowCounter = 3;
	}

	if(itemsArray[0] != null)
	{
		tvGuideList = '<ul>';
	
		for(j=0;j<itemsToShowCounter;j++)
		{
			tvGuideList += itemsArray[j][0];
		}
		
		tvGuideList += '</ul>';
		$('#tvGuideHolderBlock').empty();
		$('#tvGuideHolderBlock').append(tvGuideList);
	}
	else
	{
		//If there are no programs this week, don't display the TV-guide block
		$('#tvGuideHolder').css('display','none');
	}
}

//Function to load and show the latest newsarticles of RTL
function loadRtlNews(numberOfItems)
{
	//Var to set the max number of news items that are showed on the RTL-homepage
	var numberOfItems = 3;
	newsItemCounter = 0;
	newsList = '<ul>';
	dataTypeVar = 'xml';
	xmlFile =  xmlPath+'rtl_news.xml';
	
	$.ajax({
		type: "GET",
		url: xmlFile,
		dataType: dataTypeVar,
		async: false, 
		success: function(xml) 
		{
			$(xml).find('item').each(function()
			{
				title = $(this).find('title').text();
				imgUrl = $(this).find('images>image>url').text();
				articleUrl = $(this).find('link').text();
				articleContent = $(this).find('description').text();
				articleContent += $(this).find('content').text();
				articleContent = setHtmlLink(articleContent);
				
				//Shorten the text (not used now)
				/*if(articleContent.length > 320)
				{
					articleContent = articleContent.substring(0, 320);
					articleContent += '...';
				}*/

				if(numberOfItems != null)
				{
					if(newsItemCounter < numberOfItems)
					{
						if(newsItemCounter == 0)
						{
							//The first item has another layout
							newsList += '<li class="firstItem"><img src="'+imgUrl+'" alt="'+title+'" /><h2 class="rtlHeader">'+title+'</h2>'+articleContent+'</li>';
						}
						else
						{
							newsList += '<li class="normalItem"><h2 class="rtlHeader">'+title+'</h2><img src="'+imgUrl+'" alt="'+title+'" />'+articleContent+'</li>';
						}
					}
				}
				else
				{
					if(newsItemCounter == 0)
					{
						//The first item has another layout
						newsList += '<li class="firstItem"><img src="'+imgUrl+'" alt="'+title+'" /><h2 class="rtlHeader">'+title+'</h2>'+articleContent+'</li>';
					}
					else
					{
						newsList += '<li class="normalItem"><h2 class="rtlHeader">'+title+'</h2><img src="'+imgUrl+'" alt="'+title+'" />'+articleContent+'</li>';
					}
				}
				newsItemCounter++;
			});	
		}
	});

	newsList += '</ul>';

	//Place the newsitem in the div
	$('#rtlNewsBlock').empty();
	$('#rtlNewsBlock').append(newsList);
}


//Function to rewrite the links in the RTL articles
function setHtmlLink(articleContent)
{
	var newarticleContent = articleContent.replace(/<emphasis><link to=/g, '<a href='); 
	var newarticleContent = newarticleContent.replace(/<emphasis><link target=\"_blank\" to=/g, '<a target="_blank" href='); 
	var newarticleContent = newarticleContent.replace(/<\/link><\/emphasis>/g, '</a>'); 
	var newarticleContent = newarticleContent.replace(/<\/emphasis>/g, '</a>'); 
	return newarticleContent;
}

/**************************************************************/
/*********** VIDEO ARCHIVE PAGE FUNCTIONS ********************/
/************************************************************/

//Function to get the URL vars of the page (used @ the video archive page)
function getUrlVars()
{
	var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    
	//Return an array with vars that are used in the url (after the ?)
	return vars;
}

//Function for paging on video archive page
function nextPage()
{
	//Get the current left position of the complete list
	var currentMargin =	$('#itemsHolderContainer').css('left');
		
	if(parseInt(currentMargin) == maxMargin || parseInt(currentMargin) < maxMargin)
	{
		//Last page is reached, so no more animation/moving of the list
		return false;	
	}
	else
	{
		//Move the list to the left (next page)
		moveList('left', currentMargin);
	}
}

//Function for paging on video archive page
function previousPage()
{
		//Get the current left position of the complete list
	var currentMargin =	$('#itemsHolderContainer').css('left');
		
	if(parseInt(currentMargin) < 0)
	{
		//Move the list to the right (previous page)
		moveList('right', currentMargin);
	}
	else
	{
		//First page is reached, so no more animation/moving of the list
		return false;
	}
}

//Function to give the clicked thumbnail/item in the media list an active style
function clickedItem(id)
{
	$('.itemsHolder>ul>li').removeClass('activeItem');
	$('#'+id).addClass('activeItem');
}

//Function to load all the media from all types/providers (RTL, Sport1, VI)
function loadAllMedia(rtlShort, custom, playVideo,firstVisit, optimalLoad, selectedVideo)
{
	// PART 1 OF THE FUNCTION
	// Load the XML items or the 'cache' array
	foundList_next = '';
	
	if (NextListAppended == true)
	{
		NextListAppended  = false;
	}
	
	if ($('#videoArchiveNav').hasClass('videoViNav') && custom != 'rtlgemist') 
	{
		$('.videoViNav>li').removeClass('active');
		$('.videoViNav>li#left_link_allmedia').addClass('active');
	}
	else if($('#videoArchiveNav').hasClass('rtlnav') && custom != 'rtlgemist')
	{
		$('.rtlnav>li').removeClass('active');
		$('.rtlnav>li#left_link_allmedia').addClass('active');
	}
	
	
	
	//The 'Gemist' tab also uses this function, give this tab an active style when clicked
	if(custom == 'rtlgemist')
	{
		$('.mediaTabs>li>a').removeClass('active');
		$('#link_gemist').addClass('active');
	}
	else
	{
		//Give the 'all media/latest media' tabs an active style
		$('.mediaTabs>li>a').removeClass('active');
		$('#link_allmedia').addClass('active');
	}

	if(optimalLoad == true)
	{
		$("#foundInfo").empty();
		$('#itemsHolderContainer').empty();
		$('#pagingHolder').empty();
		$('#loaderImage').css('display','block');	
	}

	var linktarget = '';
	activePage = 1;	
	var itemsCounter = 0;
	
	//If the list is allready loaded before, use the array with all the items and do not load the XML-files again
	if(GlobalAllList == '' || (custom == 'rtlgemist' && GemistItemsCacheList == ''))
	{
		if(optimalLoad == true)
		{
			//Set the number of items to load first
			maxNumberPerCategory = 12;
		}
		else if(custom == 'rtlgemist')
		{
			//Set the max number per category to be loaded complete
			maxNumberPerCategory = 60;
		}
		else
		{
			maxNumberPerCategory = 12;
		}

		//Set the max date of how old items may be
	//Set the max date of how old items may be
		var earlier = (2).months().ago();

		//Create Arrays
		itemsArray = new Array();
		var xmlArray = new Array(); 

		if(custom == 'rtlgemist')
		{
			//When the tab 'Gemist' is clicked, get all the RTL shows
			xmlArray[0]=	xmlPath+'rtl_jupilerleague.xml'; 
			xmlArray[1]=	xmlPath+'rtl_vi_gemist.xml';
			xmlArray[2]=	xmlPath+'rtl_europaleague.xml'; 
		}
		else if(rtlShort == 'true')
		{
			//When movies are loaded on the RTL-homepage, use these XML-files
			xmlArray[0]=	xmlPath+'rtl_jupilerleague.xml'; 
			xmlArray[1]=	xmlPath+'rtl_vi_gemist.xml'; 
			xmlArray[2]=	xmlPath+'rtl_europaleague.xml'; 
			
			if(xmlPath == '/static/vi/xml/')
			{
				xmlArray[3]=	xmlPath+'vimediatest.xml'; 
			}
			else
			{
				xmlArray[3]=	xmlPath+'vimedia.xml'; 
			}
		}
		else
		{
			//Get all the XML-files
			if(xmlPath == '/static/vi/xml/')
			{
				//When using testsite
				xmlArray[0]=	xmlPath+'vimediatest.xml';
				xmlArray[4]=	xmlPath+'viradiotest.xml'; 
			}
			else
			{
				//When using LIVE site
				xmlArray[0]=	xmlPath+'vimedia.xml';
				xmlArray[4]=	xmlPath+'viradio.xml'; 
			}
			xmlArray[1]=	xmlPath+'rtl_jupilerleague.xml'; 
			xmlArray[2]=	xmlPath+'rtl_vi_gemist.xml'; 
			xmlArray[3]=	xmlPath+'rtl_europaleague.xml'; 
			xmlArray[5]=	xmlPath+'sport1.xml';
			
			//Array to check if there are double items in the Sport1 feed
			var sport1DoubleArray = new Array();
		}

		if(rtlShort == 'true')
		{
			//Other class is used on RTL homepage
			foundList = '<div class="itemsHolderFront"><ul>';
		}
		else
		{
			foundList = '<div class="itemsHolder"><ul>';
		}
		var counter = 0;
		pageCounter = 0;
		activeCircle = 0;
		
		for(i=0;i<xmlArray.length;i++)
		{
			xmlFile =  xmlArray[i];
		
			//Browser check to set the datatype for ajax-call (xml or text)
			if ($.browser.webkit)
			{
				if(xmlFile != xmlPath+'sport1.xml' && xmlFile != xmlPath+'vimedia.xml' && xmlFile != xmlPath+'viradio.xml')
				{
					dataTypeVar = 'text';
				}
				else
				{
					dataTypeVar = 'xml';
				}
			}
			else
			{
				dataTypeVar = 'xml';
			}
			
			$.ajax({
				type: "GET",
				url: xmlFile,
				dataType: dataTypeVar,
				async: false, 
				success: function(xml) 
				{
					stopEachCounter = 0;
					var mediaType = '';
					//When RTL-items are loaded
					if(xmlFile != xmlPath+'sport1.xml' && xmlFile != xmlPath+'vimedia.xml' && xmlFile != xmlPath+'viradio.xml' && xmlFile != xmlPath+'vimediatest.xml')
					{
						if(xmlFile == xmlPath+'rtl_jupilerleague.xml')
						{
							var showName = 'Jupiler League<br />';
							var mediaType = 'rtl_gemist';
						}
						else if(xmlFile == xmlPath+'rtl_vi_gemist.xml')
						{
							var showName = 'Voetbal International<br />';
							var mediaType = 'rtl_gemist';
						}
						else if(xmlFile == xmlPath+'rtl_europaleague.xml')
						{
							var showName = 'Europa League<br />';
							var mediaType = 'europaleague';
						}
						else
						{
							var showName = '';
						}
						
						$(xml).find('item').each(function()
						{
							if($(this).find('>media\\:thumbnail').attr('url') != null)
							{
								thumbnailUrl = $(this).find('>media\\:thumbnail').attr('url');
							}
							else
							{
								thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
							}

							videoType = $(this).find('>media\\:category ').text();
							title = $(this).find('>title').text();
							title = showName+' '+title;
							//title = delquote(title);
							title2 = title;
							desc = $(this).find('>description').text();
							pubDate = $(this).find('>pubDate').text();
							guid = $(this).find('>guid').text();
							var itemId = guid.substring(guid.lastIndexOf("/") + 1, guid.length);
							
							
							var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+mediaType;

							var pubDatePublic = Date.parse(pubDate);
							
							if(pubDatePublic < earlier)
							{
								return false;
							}
							
							var pubDatePublic = new Date(pubDatePublic).toString("dd/MM/yyyy HH:mm");

							sourceUrl = $(this).find('>media\\:embed').attr('url');

							if(rtlShort == 'true')
							{
								if(xmlFile == xmlPath+'rtl_vi_gemist.xml' && videoType == 'eps_fragment')
								{
									//We don't want videos with type 'eps_fragment' so skip it.
								}
								else
								{
									if(xmlFile == xmlPath+'rtl_europaleague.xml' && videoType != 'uitzending')
									{
										mediaType = 'europaleague';
									}
									else
									{
										mediaType = 'rtl_gemist';
									}
									
									var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+mediaType;								
									
									var normalTitle = title;
									//Shorten the title if needed (35+ characters)
									if(title.length > 35)
									{
										title = title.substring(0, 35);
										title += '...';
									}
									
									//Add this item to the itemsArray (including the needed htmlcode)
									itemsArray[itemsCounter] = new Array();
									itemsArray[itemsCounter][0] = '<li class="mediaListItem"><a href="'+permLinkCode+'&rtlPage=true"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" title="'+normalTitle+'" class="extIcon" /><img src="'+thumbnailUrl+'" /></a><br /><a class="itemTitle" href="'+permLinkCode+'&rtlPage=true">'+title+'</a></li>';
									itemsArray[itemsCounter][1] = pubDate;
									itemsArray[itemsCounter][2] = '<a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_play_button.png" class="rtlPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
									itemsArray[itemsCounter][3] = thumbnailUrl;
								}
							}
							else if(custom == 'rtlgemist')
							{
								if(xmlFile == xmlPath+'rtl_europaleague.xml' && videoType != 'uitzending')
								{
									//The 'gemist' tab only shows complete episodes. So when the item isn't an episode, skip it
								}
								else
								{
									//Add this item to the itemsArray (including the needed htmlcode)								
									itemsArray[itemsCounter] = new Array();
									
									if(itemId == selectedVideo)
									{
										itemsArray[itemsCounter][0] = '<li class="mediaListItem activeItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');" /></a><br /><a class="itemTitle" href="#" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
										loadMediaItemRTL(thumbnailUrl,guid);
										//window.open(guid);
									}
									else
									{
										itemsArray[itemsCounter][0] = '<li class="mediaListItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');" /></a><br /><a class="itemTitle" href="#" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
									}
																
									
									
									
									itemsArray[itemsCounter][1] = pubDate;
									itemsArray[itemsCounter][2] = '<a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_play_button.png" class="rtlPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
									itemsArray[itemsCounter][3] = thumbnailUrl;
								}
							}
							else
							{
								if(xmlFile == xmlPath+'rtl_vi_gemist.xml' && videoType == 'eps_fragment')
								{
									//We don't want videos with type 'eps_fragment' so skip it.
								}
								else
								{
									if(linktarget != 'blank')
									{
										//Add this item to the itemsArray (including the needed htmlcode)
										itemsArray[itemsCounter] = new Array();
																			
										if(guid == playVideo)
										{
											//Give the item a selected style
											itemsArray[itemsCounter][0] = '<li class="mediaListItem activeItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\'); "><a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');" /></a><br /><a class="itemTitle" href="#" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
										}
										else
										{
											itemsArray[itemsCounter][0] = '<li class="mediaListItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');" /></a><br /><a class="itemTitle" href="#" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
										}
										itemsArray[itemsCounter][1] = pubDate;
										itemsArray[itemsCounter][2] = '<a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_play_button.png" class="rtlPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
										itemsArray[itemsCounter][3] = thumbnailUrl;
									}
									else
									{
										//Add this item to the itemsArray (including the needed htmlcode)
										itemsArray[itemsCounter] = new Array();
										itemsArray[itemsCounter][0] = '<li class="mediaListItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'" onclick="loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');" /></a><br /><a class="itemTitle" href="'+guid+'" target="_blank">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
										itemsArray[itemsCounter][1] = pubDate;
										itemsArray[itemsCounter][2] = '<a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_play_button.png" class="rtlPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
										itemsArray[itemsCounter][3] = thumbnailUrl;
									}
								}
							}
							
							//If the video is selected at the RTL-homepage, open a new tab with the selected video
							if(guid == playVideo)
							{
								loadMediaItemRTL(thumbnailUrl,guid);
								window.open(guid);
							}	
							
							if(xmlFile == xmlPath+'rtl_vi_gemist.xml' && videoType != 'uitzending')
							{
								//Skip the items that aren't a full episode
							}
							else
							{
								itemsCounter++;
							}
							
							if(rtlShort == 'true')
							{
								stopEachCounter++;

								//There are max 9 items at the RTL-homepage, so stop collecting items > 9
								if(stopEachCounter == 9)
								{
									return false;
								}
							}
							else
							{
								stopEachCounter++;

								//Stop collection items when the max per category is reached
								if(stopEachCounter == maxNumberPerCategory)
								{
									return false;
								}
							}
						});
					}
					//When VImedia/VIradio items are loaded
					else if(xmlFile == xmlPath+'vimedia.xml' || xmlFile == xmlPath+'viradio.xml' || xmlFile == xmlPath+'vimediatest.xml' || xmlFile == xmlPath+'viradiotest.xml')
					{
						if( xmlFile == xmlPath+'viradio.xml')
						{
							//Add a radio symbol to the thumbnail in case of a radio-item
							var itemIcon = '<img src="/static/vi/images/rtl/radio_item_icon.png" alt="" class="radioIcon" />';
							var mediaType = 'viradio';
						}
						else
						{
							var itemIcon = '<img src="/static/vi/images/rtl/tv_item_icon.png" alt="" class="tvIcon" />';
							var mediaType = 'vimedia';
						}
						
						$(xml).find('article').each(function()
						{
							var guid = $(this).find('head>url').text();
							var title = $(this).find('head>headline').text();
							
							//Remove quotes from title (prevent IE7 bug)
							//title = delquote(title);

							var itemId = $(this).find('head>id').text();
							var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+mediaType;

							var pubDate = $(this).find('head>date').text() + ' ' + $(this).find('head>time').text();
							var pubDatePublic = Date.parse(pubDate);
							
							if(pubDatePublic < earlier)
							{
								//Item is too old so skip it
								return false;
							}
							
							//Generate a read-friendly date that's used in the overview
							var pubDatePublic = new Date(pubDatePublic).toString("dd/MM/yyyy HH:mm");

							//Check if thumbnail is available, otherwise, use standard thumb (no_media_thumb.jpg)
							if($(this).find('media>media-reference').attr('src') != null)
							{
								var thumbnailUrl = $(this).find('media>media-reference').attr('src');
							}
							else
							{
								var thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
							}
												
							if(rtlShort == 'true')
							{
								//Only VI-RTL-items are usefull for the RTL-homepage
								if(guid.toLowerCase().indexOf("webtv-rtl") > 0)
								{
									var normalTitle = title;
									//Shorten the title if needed (35+ characters)
									if(title.length > 35)
									{
										title = title.substring(0, 35);
										title += '...';
									}
									
									//Add this item to the itemsArray (including the needed htmlcode)
									itemsArray[itemsCounter] = new Array();
									itemsArray[itemsCounter][0] = '<li class="mediaListItem RTLItemOnFront"><a href="'+permLinkCode+'&rtlPage=true"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" title="'+normalTitle+'" class="extIcon" /><img src="'+thumbnailUrl+'" style="height: 66px;" /></a><br /><a class="itemTitle" href="'+permLinkCode+'&rtlPage=true">'+title+'</a></li>';
									itemsArray[itemsCounter][1] = pubDate;
									itemsArray[itemsCounter][2] = '<a href="#" onclick="loadViMediaItem(\''+guid+'\'); return false;"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'"></a>';
								}
							}
							else
							{
								//Use all the items for the media overview
								itemsArray[itemsCounter] = new Array();
								
								//If an item is clicked at the RTL homepage, give it an active-state/class at the archive page, otherwise use standard style
								if(guid == playVideo)
								{
									itemsArray[itemsCounter][0] = '<li class="mediaListItem activeItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\'); loadViMediaItem(\''+guid+'\');"><a href="#videoplayerTop">'+itemIcon+'<img src="'+thumbnailUrl+'" onclick="loadViMediaItem(\''+guid+'\');"></a><br /><a class="itemTitle" href="#videoplayerTop" onclick="loadViMediaItem(\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
								}
								else
								{
									itemsArray[itemsCounter][0] = '<li class="mediaListItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\'); loadViMediaItem(\''+guid+'\');"><a href="#videoplayerTop">'+itemIcon+'<img src="'+thumbnailUrl+'" onclick="loadViMediaItem(\''+guid+'\');"></a><br /><a class="itemTitle" href="#videoplayerTop" onclick="loadViMediaItem(\''+guid+'\');">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
								}
								
								//Add this item to the itemsArray (including the needed htmlcode)
								itemsArray[itemsCounter][1] = pubDate;
								itemsArray[itemsCounter][2] = '<a href="#" onclick="loadViMediaItem(\''+guid+'\'); return false;"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'"></a>';
								itemsArray[itemsCounter][3] = thumbnailUrl;
							}

							//Load the clicked item at the RTL-homepage directly
							if(guid == playVideo)
							{
								loadViMediaItem(guid);					
							}

							itemsCounter++;

							//Stop loading items at 9 items when the function is used on RTL-homepage
							if(rtlShort == 'true')
							{
								stopEachCounter++;

								if(stopEachCounter == 9)
								{
									return false;
								}
							}
							else
							{
								stopEachCounter++;

								//Stop collection items when the max per category is reached
								if(stopEachCounter == maxNumberPerCategory)
								{
									return false;
								}
							}
						});
					}
					//When Sport1 items are loaded
					else
					{
						$(xml).find('Video').each(function()
						{
							//We only use the items from the category 'voetbal', no tennis, golf, etc
							if($(this).find('>Category').text() == 'Voetbal')
							{
								var title = $(this).find('>Title').text();
								var itemId  = $(this).find('>ProviderVideoId').text();	
								var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory=sport1';
								
								//Check if item is allready loaded (double in Sport1 feed)
								var titleCheck = $.inArray(title, sport1DoubleArray);
								
								//If item is not allready in feed
								if(titleCheck < 0)
								{
									//Add title to the array which is used to check double items (items with the same title get skipped)
									sport1DoubleArray[itemsCounter] = title;
								
									//Remove quotes from title (prevent IE7 bug)
									//var title = delquote(title);

									//Don't use items with interviews, quotes, etc. Just match highlights only!
									if(title.toLowerCase().indexOf("interview") < 0 && title.toLowerCase().indexOf("voorbeschouwing") < 0 && title.toLowerCase().indexOf("stand up") < 0 && title.toLowerCase().indexOf("stand-up") < 0 && title.toLowerCase().indexOf("standup") < 0 && title.toLowerCase().indexOf("quote") < 0 && title.toLowerCase().indexOf("eindclip") < 0 && title.toLowerCase().indexOf("voetbal dit weekend") < 0 && title.toLowerCase().indexOf("item vooraf") < 0 && title.toLowerCase().indexOf("vooraf") < 0)
									{
										var xmlPubDate = $(this).find('>PublicationDate').text();
										var pubDate = Date.parse(xmlPubDate);

										if(pubDate < earlier)
										{
											return false;
										}

										var pubDate = new Date(pubDate).toString("ddd, dd MMM yyyy HH:mm");
										var pubDatePublic = new Date(pubDate).toString("dd/MM/yyyy HH:mm");
										var guid = $(this).find('>Url').text();
										var videoId = $(this).find('>ProviderVideoId').text();
										
										//Check if thumbnail is available, otherwise, use standard thumb (no_media_thumb.jpg)
										if($(this).find('formats>format[type="image"]').text() != undefined)
										{
											if($(this).find('formats>format[type="image"]').text() != null && $(this).find('formats>format[type="image"]').text() != '')
											{
												var thumbnailUrl = $(this).find('formats>format[type="image"]').text();
											}
											else
											{
												var thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
											}
										}
										else
										{
											var thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
										}

										var sourceUrl = $(this).find('>Url');
										
										if(rtlShort == 'true')
										{
											//Shorten the title if needed (35+ characters)
											if(title.length > 35)
											{
												title = title.substring(0, 35);
												title += '...';
											}
											
											//Add this item to the itemsArray (including the needed htmlcode)
											itemsArray[itemsCounter] = new Array();
											itemsArray[itemsCounter][0] = '<li class="mediaListItem"><a href="/RTL-1/Videoarchief.htm?videoToPlay='+videoId+'"><img src="/static/vi/images/rtl/sport1_logo.png" alt="" class="sport1Icon" /><img src="'+thumbnailUrl+'" /></a><br /><a class="itemTitle" href="href="/RTL-1/Videoarchief.htm?videoToPlay='+videoId+'">'+title+'</a></li>';
											itemsArray[itemsCounter][1] = pubDate;
										}
										else
										{
											//Add this item to the itemsArray (including the needed htmlcode)
											itemsArray[itemsCounter] = new Array();
											
											if(videoId == playVideo)
											{
												itemsArray[itemsCounter][0] = '<li class="mediaListItem activeItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><a href="#"><img src="/static/vi/images/rtl/sport1_logo.png" alt="" class="sport1Icon" /><img src="'+thumbnailUrl+'" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"></a><br /><a class="itemTitle" href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\');return false;">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
											}
											else
											{
												itemsArray[itemsCounter][0] = '<li class="mediaListItem" id="mediaListItem_'+itemsCounter+'" onclick="clickedItem(\'mediaListItem_'+itemsCounter+'\');windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><a href="#"><img src="/static/vi/images/rtl/sport1_logo.png" alt="" class="sport1Icon" /><img src="'+thumbnailUrl+'" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"></a><br /><a class="itemTitle" href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\');return false;">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';			
											}
											

											itemsArray[itemsCounter][1] = pubDate;
											if(thumbnailUrl != '')
											{
												itemsArray[itemsCounter][2] = '<a href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'"></a>';
												itemsArray[itemsCounter][3] = thumbnailUrl;
											}
										}

										if(videoId == playVideo)
										{
											//Open a popup with the selected video (when clicked on other page, i.e. RTL-homepage)
											windowObj = top.open(guid,'new428717','width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');if (windowObj != null){windowObj.focus()};openSport1Item(thumbnailUrl);
										}

										itemsCounter++;

										if(rtlShort == 'true')
										{
											stopEachCounter++;
											
											//Stop loading items at 9 items when the function is used on RTL-homepage
											if(stopEachCounter == 9)
											{
												return false;
											}
										}
										else
										{
											stopEachCounter++;
											
											//Stop collection items when the max per category is reached
											if(stopEachCounter == maxNumberPerCategory)
											{
												return false;
											}
										}
									}
								}
								else
								{
									//Item is allready in array so skip this item
								}
							}
						});	
					}
				}
			});
		}
	}
	else
	{
		//The clicked tab is allready loaded once, so use the 'cache' array		
		$("#foundInfo").empty();
		$('#itemsHolderContainer').empty();
		$('#pagingHolder').empty();		
		itemsArray = '';
		
		if(custom == 'rtlgemist')
		{
			itemsArray = GemistItemsCacheList;
		}
		else
		{
			itemsArray = GlobalAllList;			
		}
		

		pageCounter = 0;
		foundList = '';
		foundList = '<div class="itemsHolder"><ul>';
	}

	// PART 2 OF THE FUNCTION
	// All items are ready/loaded, now order them at date and create the presentation

	//Sort the array-items on date
	itemsArray = itemsArray.sort(custom_sort);
	
	//Show only 9 items at the RTL Homepage	
	if(rtlShort == 'true')
	{
		for(j=0;j<9;j++)
		{
			foundList += itemsArray[j][0];
		}
		foundList += '</ul></div>';

		$('#lastMediaItems').empty();
		$('#lastMediaItems').append(foundList);
	}
	else
	{
		numberOfItemsInArray = itemsArray.length;
		
		//Cut down the number of items to a max of 200
		if(numberOfItemsInArray > 200)
		{
			numberOfItemsInArray = 200;
		}
		
		if(custom == 'rtlgemist')
		{
			//Create the first list
			for(j=-1;j<=numberOfItemsInArray;j++)
			{
				if(itemsArray[j] != null)
				{
					foundList += itemsArray[j][0];

					if (j%12 == 0 && j != 0)
					{
						foundList += '</ul></div><div class="itemsHolder"><ul>';
						pageCounter++;
					}
				}
			}
			pageCounter++;
		}
		else
		{
			if(GlobalAllList != '')
			{
				//Create the complete list
				for(j=-1;j<=11;j++)
				{
					if(itemsArray[j] != null)
					{
						foundList += itemsArray[j][0];

						if (j%12 == 0 && j != 0)
						{
							pageCounter++;
							foundList += '</ul></div><div class="itemsHolder holderNumber_'+pageCounter+'"><ul>';

						}
					}
				}
			}
			else
			{
				//Create the first list
				for(j=-1;j<=12;j++)
				{
					if(itemsArray[j] != null)
					{
						foundList += itemsArray[j][0];

						if (j%12 == 0 && j != 0)
						{
							pageCounter++;
							
							if(j != 12)
							{
								foundList += '</ul></div><div class="itemsHolder holderNumber_'+pageCounter+'"><ul>';
							}
							else
							{
								foundList += '</ul></div>';
							}
						}
					}
				}
			}
		}
		foundList += '</ul></div>';

		if (custom == 'rtlgemist')
		{
			foundList_next = '';
		}
		else
		{
			// If the OptimalLoad Boolean is set to false, that means the function is runned for the second time to get the
			// other items. Load them and create the variable 'foundList_next' which is appended later in this function
			//THIS PART IS NOT ACTIVE NOW, ONLY 12 ITEMS ARE LOADED AT THE FIRST PAGE
			if(optimalLoad != true && GlobalAllList == '')
			{
				//Create second list
				if(numberOfItemsInArray > 12)
				{
					foundList_next = '<div class="itemsHolder holderNumber_'+pageCounter+'"><ul>';

					for(j=j;j<=numberOfItemsInArray;j++)
					{
						if(itemsArray[j] != null)
						{
							foundList_next += itemsArray[j][0];
							
							preload = new Image(); 
							preload.src= itemsArray[j][3]; 					
							
							if (j%12 == 0 && j != 0)
							{
								pageCounter++;
								foundList_next += '</ul></div><div class="itemsHolder holderNumber_'+pageCounter+'"><ul>';
							}
						}
					}
				}
				pageCounter++;
			}
		}

		numberOfItemsFound = itemsArray.length;
		
		if(numberOfItemsFound > 200)
		{
			numberOfItemsFound = 200;
		}

		if (custom == 'rtlgemist')
		{
			numberOfItemsFound = 0;

			for(j=-1;j<=numberOfItemsInArray;j++)
			{
				//Only count them items in the array that have a value
				if(itemsArray[j] != null)
				{
					numberOfItemsFound++;
				}
				else
				{
					//No value, so do nothing
				}
			}
		}

		if(GlobalAllList != '')
		{
			//pageCounter++;
		}
		
		//Create the string that is place at the top of the media-items overview (which show how many items are found)
		if(numberOfItemsFound > 12 )
		{
			var foundInfoText = numberOfItemsFound+' items gevonden (<strong>'+pageCounter+' pagina\'s</strong>)';
		}
		else if(numberOfItemsFound > 0  && numberOfItemsFound < 13)
		{
			if(numberOfItemsFound == 1)
			{
				var foundInfoText = numberOfItemsFound+' item gevonden (<strong>1 pagina</strong>';
			}
			else
			{
				var foundInfoText = numberOfItemsFound+' items gevonden (<strong>1 pagina</strong>';
			}
		}
		else
		{
			var foundInfoText = 'Geen items gevonden';
		}

		if(optimalLoad == true)
		{
			//Remove the load-image
			$('#loaderImage').css('display','none');	
			$('#previous').css('visibility','hidden');
			$('#next').css('visibility','hidden');
			$("#foundInfo").empty();
		}
		else if(custom == 'rtlgemist')
		{
			//Remove the load-image and place the found text
			$('#loaderImage').css('display','none');	
			$("#foundInfo").append(foundInfoText);
		}
		else if(pageCounter <= 0)
		{
			$('#buttonHolder').empty();
			$('#previous').css('visibility','hidden');
			$('#next').css('visibility','hidden');
			$("#foundInfo").empty();
		}
		else
		{
			$("#foundInfo").empty();
			$("#foundInfo").append(foundInfoText);
		}
		
		// Place the list with items that's created (in case of optimalLoad = true, only the first 12 items of all items are placed
		// (the rest of the items is loaded at the bottom of this function)
		if(optimalLoad == true || custom == 'rtlgemist' || GlobalAllList != '')
		{
			$('#itemsHolderContainer').append(foundList);
		}

		//Calculate the maxMargin for the css propertie 'left' (used for paging)
		maxMargin = pageCounter * -720;

		//Start at the first page, so the position left = 0
		$('#itemsHolderContainer').css('left', '0');

		//Create the paging circles at top
		var pagingCirclesTop = '<ul>';
		var startNumber = parseInt(activePage) - 7;
		var endNumber = parseInt(activePage) + 7;
		
		if(custom == 'rtlgemist')
		{
			pageCounter--;
			for(i=-1;i<pageCounter;i++)
			{
				liClassCounter = i + 1;
				pageNumberCounter = i + 2;
				liId = 'top_circle_' + liClassCounter;

					
				if(pageNumberCounter > startNumber && pageNumberCounter < endNumber)
				{
					
					if(pageNumberCounter == activePage)
					{
						 pagingCirclesTop += '<li class="active" id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
					}
					else
					{
						pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
					}
				}
			}
			pageCounter++;
		}
		else
		{
			for(i=-1;i<pageCounter;i++)
			{
				liClassCounter = i + 1;
				pageNumberCounter = i + 2;
				liId = 'top_circle_' + liClassCounter;

					
				if(pageNumberCounter > startNumber && pageNumberCounter < endNumber)
				{
					
					if(pageNumberCounter == activePage)
					{
						 pagingCirclesTop += '<li class="active" id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
					}
					else
					{
						pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
					}
				}
			}
		}

		pagingCirclesTop += '</ul>';


		$('#pagingHolderTop').empty();

		//No paging needed, so hide the previous and next buttons
		if(pageCounter <= 1)
		{
			$('#buttonHolder').empty();
			$('#previous').css('visibility','hidden');
			$('#next').css('visibility','hidden');
			$("#foundInfo").empty();
		}
		else
		{
			if(optimalLoad != true)
			{
				$('#pagingHolder').empty();
				$('#pagingHolderTop').append(pagingCirclesTop);
			}
		
			//Hide the previous button when on page 1
			if(activePage != '1')
			{
				$('#previous').css('visibility','visible');
			}
			else
			{
				$('#previous').css('visibility','hidden');
			}
			$('#next').css('visibility','visible');
		}
	}

	//If the user comes from another page, show the most recent item as a thumb in the videoplayer div
	if(firstVisit == true && custom != 'placeNoPicture')
	{
		//If the thumbnail from the first item isn't found, use the second item
		if(itemsArray[0][2] != undefined)
		{
			var firstItemThumb = itemsArray[0][2];
			$('#playerHolder').empty();
			$('#playerHolder').append(firstItemThumb);
		}
		else if(itemsArray[1][2] != undefined)
		{
			var firstItemThumb = itemsArray[1][2];
			$('#playerHolder').empty();
			$('#playerHolder').append(firstItemThumb);
		}
	}

	if(optimalLoad == true)
	{
		//Do nothing
		//Load the second part of the items (only the first are loaded now)
	}
	else
	{
		//The second part is loaded, so append it (not using right now)
		if(NextListAppended == false && foundList_next != '')
		{
			$("#itemsHolderContainer").append(foundList_next);
			NextListAppended = true;
		}
	}

	if (custom != 'rtlgemist' && optimalLoad != true && GlobalAllList == '')
	{
		// Set a 'cache' array with all the items that are loaded. When a user clicks the 'all' tab again, this array is used so XML doesn't needed to be
		// to be loaded again  (better performance)
		GlobalAllList.length = 0;
		GlobalAllList = itemsArray;
	}

	if(optimalLoad == true && GlobalAllList == '')
	{
		// Set a 'cache' array with all the items that are loaded. When a user clicks the 'all' tab again, this array is used so XML doesn't needed to be
		// to be loaded again  (better performance)
		GlobalAllList.length = 0;
		GlobalAllList = itemsArray;

	}
	else if(GemistItemsCacheList == '' && custom == 'rtlgemist')
	{
		GemistItemsCacheList = itemsArray;
	}
}

//Function to load the items of one type (tab is clicked)
function loadMediaXml(type, tabId, firstVisit, selectedVideo)
{
	dataTypeVar = 'xml';
	foundList_next = '';
	var earlier = (5).months().ago();

	$('.mediaTabs>li>a').removeClass('active');
	$('#'+tabId).addClass('active');
	
	if ($('#videoArchiveNav').hasClass('rtlnav')) 
	{
		$('.rtlnav>li').removeClass('active');
		$('.rtlnav>li#left_'+tabId).addClass('active');
	}
	else
	{
		$('.videoViNav>li').removeClass('active');
		$('.videoViNav>li#left_'+tabId).addClass('active');
	}
	
	
	

	$("#foundInfo").empty();
	$('#itemsHolderContainer').empty();
	$('#pagingHolder').empty();
	$('#loaderImage').css('display','block');


	if(type == 'vimedia')
	{
		if(xmlPath == '/static/vi/xml/')
		{
			xmlFile = xmlPath+'vimediatest.xml';
		}
		else
		{
			xmlFile = xmlPath+'vimedia.xml';
		}
	}
	else if(type == 'jupiler' || type == 'gemist')
	{
		loadAllMedia('','rtlgemist','','',false);
		return false;
	}
	else if(type == 'europaleague')
	{
		if ($.browser.webkit)
		{
			dataTypeVar = 'text';
		}
		
		xmlFile = xmlPath+'rtl_europaleague.xml';
	}
	else if(type == 'sport1')
	{
		xmlFile = xmlPath+'sport1.xml';
		var sport1DoubleArray = new Array();
	}
	else
	{
		if(xmlPath == '/static/vi/xml/')
		{
				xmlFile = xmlPath+'viradiotest.xml';
		}
		else
		{
			xmlFile = xmlPath+'viradio.xml';
		}	
	}
	
	
	var foundList = '<div class="itemsHolder"><ul>';
	var counter = 0;
	pageCounter = 0;
	activeCircle = 0;
	activePage = 1;
	pageNumber = null;
	
	
	if((type == 'sport1' && Sport1ItemsCacheList == '') || (type == 'vimedia' && VimediaItemsCacheList == '') || (type == 'viradio' && ViradioItemsCacheList == '') || (type == 'europaleague' && EuropaleagueItemsCacheList == ''))
	{
		$.ajax({
			url: xmlFile,
			type: "GET",
			async: false, 
			dataType: dataTypeVar,
			success: function(xml) 
			{
				//RTL ITEMS
				if(type != 'sport1' && type != 'vimedia' && type != 'viradio')
				{
					$(xml).find('item').each(function()
					{
						var guid = $(this).find('>guid').text();
						var itemId = guid.substring(guid.lastIndexOf("/") + 1, guid.length);
						title = $(this).find('title').text();
						title2 = title;
						desc = $(this).find('>description').text();
						//title = delquote(title);
						pubDate = $(this).find('>pubDate').text();
						videoType = $(this).find('>media\\:category ').text();
						var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+type;
						
						var pubDatePublic = Date.parse(pubDate);					
						
						if(pubDatePublic < earlier)
						{
							return false;
						}
						
						var pubDatePublic = new Date(pubDatePublic).toString("dd/MM/yyyy HH:mm");

						

						if($(this).find('>media\\:thumbnail').attr('url') != null)
						{
							thumbnailUrl = $(this).find('>media\\:thumbnail').attr('url');
						}
						else
						{
							thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
						}
						//var thumbnailUrl = $(this).find('media\\:thumbnail').attr('url');
						var sourceUrl = $(this).find('>media\\:embed').attr('url');
					
						if(xmlFile == xmlPath+'rtl_europaleague.xml' && videoType == 'uitzending')
						{		
							//Do Nothing
						}
						else
						{
							//The item is linked in the url, so play it and give the item an active class in the list
							if(itemId == selectedVideo)
							{
								foundList += '<li class="mediaListItem activeItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'");" /></a><br /><a class="itemTitle" href="#videoplayerTop">'+title2+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
								loadMediaItemRTL(thumbnailUrl,guid);
								//window.open(guid);
							}
							else
							{
								foundList += '<li class="mediaListItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');loadMediaItemRTL(\''+thumbnailUrl+'\',\''+guid+'\');window.open(\''+guid+'\');"><a href="#videoplayerTop"><img src="/static/vi/images/rtl/rtl_xl_icon.png" alt="" class="extIcon" /><img src="'+thumbnailUrl+'");" /></a><br /><a class="itemTitle" href="#videoplayerTop">'+title2+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
							}
							
							
							
							if(counter == 0 && firstVisit == 'true')
							{
								firstItemContent = '<a href="'+guid+'" target="_blank"><img src="/static/vi/images/rtl/rtl_play_button.png" class="rtlPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
								$('#playerHolder').empty();
								$('#playerHolder').append(firstItemContent);
							}
							
							counter++;

							if (counter%12 == 0)
							{
								foundList += '</ul></div><div class="itemsHolder"><ul>';
								pageCounter++;
							}
						}	
						
					});

					if(type == 'europaleague' && EuropaleagueItemsCacheList == '')
					{
						pageCounter++;
						EuropaleagueItemsCacheList[0] = foundList;
						EuropaleagueItemsCacheList[1] = counter;
						EuropaleagueItemsCacheList[2] = pageCounter;
					}					
				}
				//VI ITEMS
				else if(type == 'vimedia' || type == 'viradio')
				{
					if(type == 'viradio')
					{
						var itemIcon = '<img src="/static/vi/images/rtl/radio_item_icon.png" alt="" class="radioIcon" />';
					}
					else
					{
						var itemIcon = '<img src="/static/vi/images/rtl/tv_item_icon.png" alt="" class="tvIcon" />';
					}
					
					
					$(xml).find('article').each(function()
					{
						var guid = $(this).find('head>url').text();
						var title = $(this).find('head>headline').text();
						//title = delquote(title);
						var itemId = $(this).find('head>id').text();
						var pubDate = $(this).find('head>date').text();
						var pubTime = $(this).find('head>time').text();
						var pubDateToParse = pubDate + ' ' + pubTime;
						var pubDatePublic = Date.parse(pubDateToParse);
						var pubDatePublic = new Date(pubDatePublic).toString("dd/MM/yyyy HH:mm");

						var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+type;
						
						if($(this).find('media>media-reference').attr('src') != null)
						{
							var thumbnailUrl = $(this).find('media>media-reference').attr('src');
						}
						else
						{
							var thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
						}

						if(itemId == selectedVideo)
						{
							foundList += '<li class="mediaListItem activeItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');loadViMediaItem(\''+guid+'\');"><a href="#videoplayerTop">'+itemIcon+'<img src="'+thumbnailUrl+'"></a><br /><a class="itemTitle" href="#videoplayerTop">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
							loadViMediaItem(guid);
						}
						else
						{
							foundList += '<li class="mediaListItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');loadViMediaItem(\''+guid+'\');"><a href="#videoplayerTop">'+itemIcon+'<img src="'+thumbnailUrl+'"></a><br /><a class="itemTitle" href="#videoplayerTop">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
						}

						if(counter == 0 && firstVisit == 'true')
						{
							firstItemContent = '<a href="#videoplayerTop" onclick="loadViMediaItem(\''+guid+'\');"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'"></a>';
							$('#playerHolder').empty();
							$('#playerHolder').append(firstItemContent);
						}
						
						
						counter++;
						
						if (counter%12 == 0)
						{
							foundList += '</ul></div><div class="itemsHolder"><ul>';
							pageCounter++;
						}
					});

					if(type == 'vimedia' && VimediaItemsCacheList == '')
					{
						VimediaItemsCacheList[0] = foundList;
						VimediaItemsCacheList[1] = counter;
						VimediaItemsCacheList[2] = pageCounter;
					}
					else if(type == 'viradio' && ViradioItemsCacheList == '')
					{
						ViradioItemsCacheList[0] = foundList;
						ViradioItemsCacheList[1] = counter;
						ViradioItemsCacheList[2] = pageCounter;
					}
				}
				//SPORT 1 ITEMS
				else
				{
					$(xml).find('Video').each(function()
					{
						if($(this).find('>Category').text() == 'Voetbal')
						{
							var title = $(this).find('>Title').text();
							var titleCheck = $.inArray(title, sport1DoubleArray);
							var itemId  = $(this).find('>ProviderVideoId').text();	
							var permLinkCode = CurrentDomain+'/VI-TV-1/VI-Media.htm?linkedVideo='+itemId+'&linkedCategory='+type;

							if(titleCheck < 0)
							{
								sport1DoubleArray[counter] = title;
							
								//var desc = $(this).find('>Description').text();
								//var title = delquote(title);
								if(title.toLowerCase().indexOf("interview") < 0 && title.toLowerCase().indexOf("voorbeschouwing") < 0 && title.toLowerCase().indexOf("stand up") < 0 && title.toLowerCase().indexOf("stand-up") < 0 && title.toLowerCase().indexOf("standup") < 0 && title.toLowerCase().indexOf("quote") < 0 && title.toLowerCase().indexOf("eindclip") < 0 && title.toLowerCase().indexOf("voetbal dit weekend") < 0 && title.toLowerCase().indexOf("item vooraf") < 0 && title.toLowerCase().indexOf("vooraf") < 0)
								{
								
									var xmlPubDate = $(this).find('>PublicationDate').text();
									var pubDate = Date.parse(xmlPubDate);
									var pubDate = new Date(pubDate).toString("ddd, dd MMM yyyy HH:mm");
									var pubDatePublic = new Date(pubDate).toString("dd/MM/yyyy HH:mm");


									var guid = $(this).find('>Url').text();
									
									if($(this).find('formats>format[type="image"]').text() != null)
									{
										var thumbnailUrl = $(this).find('formats>format[type="image"]').text();
									}
									else
									{
										var thumbnailUrl = '/static/vi/images/no_media_thumb.jpg';
									}

									var sourceUrl = $(this).find('>Url');
									
									if(itemId == selectedVideo)
									{
										foundList += '<li class="mediaListItem activeItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><a href="#"><img src="/static/vi/images/rtl/sport1_logo.png" alt="" class="sport1Icon" /><img src="'+thumbnailUrl+'" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"></a><br /><a class="itemTitle" href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\');return false;">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
										windowObj = top.open(guid,'new428717','width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
										if (windowObj != null)
										{
											windowObj.focus();
										}
										
										openSport1Item(thumbnailUrl,guid);
									}
									else
									{
										foundList += '<li class="mediaListItem" id="mediaListItem_'+counter+'" onclick="clickedItem(\'mediaListItem_'+counter+'\');windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><a href="#"><img src="/static/vi/images/rtl/sport1_logo.png" alt="" class="sport1Icon" /><img src="'+thumbnailUrl+'" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"></a><br /><a class="itemTitle" href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\');return false;">'+title+'</a><span class="itemDate">'+pubDatePublic+'</span></li>';
									}
									
									if(counter == 0 && firstVisit == 'true')
									{
										firstItemContent = '<a href="#" onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};openSport1Item(\''+thumbnailUrl+'\',\''+guid+'\');return false;"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'"></a>';
										$('#playerHolder').empty();
										$('#playerHolder').append(firstItemContent);
									}
										
									counter++;
									
									if (counter%12 == 0)
									{
										foundList += '</ul></div><div class="itemsHolder"><ul>';
										pageCounter++;
									}
								}
							}
							else
							{
								//Item is allready in stream, so do nothing!
							}	
						}
					});
					if(Sport1ItemsCacheList == '')
					{
						if (counter%12 != 0)
						{
							pageCounter++;
						}		
						
						Sport1ItemsCacheList[0] = foundList;
						Sport1ItemsCacheList[1] = counter;
						Sport1ItemsCacheList[2] = pageCounter;
					}
				}
			
				foundList += '</ul></div>';
				
				if (counter%12 != 0 && pageCounter > 2 && (type == 'sport1' || type == 'vimedia' || type == 'viradio'))
				{
					pageCounter++;
				}
				

				if(counter > 12)
				{
					var foundInfoText = counter+' items gevonden (<strong>'+pageCounter+' pagina\'s</strong>)';
				}
				else if(counter > 0 && counter < 13)
				{
					if(counter == 1)
					{
						var foundInfoText = counter+' item gevonden (<strong>1 pagina</strong>)';
					}
					else
					{
						var foundInfoText = counter+' items gevonden (<strong>1 pagina</strong>)';
					}
				}
				else
				{
					var foundInfoText = 'Geen items gevonden';
				}
			
				$('#loaderImage').css('display','none');
				$("#foundInfo").append(foundInfoText);
				$('#itemsHolderContainer').append(foundList);
				maxMargin = pageCounter * -720;
				$('#itemsHolderContainer').css('left', '0');
				
				var pagingCirclesTop = '<ul>';
				var startNumber = parseInt(activePage) - 7;
				var endNumber = parseInt(activePage) + 7;
		
				for(i=-1;i<pageCounter;i++)
				{
					liClassCounter = i + 1;
					pageNumberCounter = i + 2;

					liId = 'top_circle_' + liClassCounter;
					
					if(pageNumberCounter > startNumber && pageNumberCounter < endNumber && pageNumberCounter <= pageCounter)
					{
						if(pageNumberCounter == activePage)
						{
							 pagingCirclesTop += '<li class="active" id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
						}
						else
						{
							pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
						}
					}
				}
		
				pagingCirclesTop += '</ul>';

				$('#pagingHolder').empty();
				$('#pagingHolderTop').empty();
				
				if(pageCounter <= 1)
				{
					$('#buttonHolder').empty();
					$('#previous').css('visibility','hidden');
					$('#next').css('visibility','hidden');
				}
				else
				{
					$('#pagingHolderTop').append(pagingCirclesTop);
					if(activePage != '1')
					{
						$('#previous').css('visibility','visible');
					}
					else
					{
						$('#previous').css('visibility','hidden');
					}
					$('#next').css('visibility','visible');
				}
			}
		});
	}
	else if((type == 'sport1' && Sport1ItemsCacheList != '') || (type == 'vimedia' && VimediaItemsCacheList != '') || (type == 'viradio' && ViradioItemsCacheList != '') || (type == 'europaleague' && EuropaleagueItemsCacheList != ''))
	{
		$('#loaderImage').css('display','none');
		
		if(type == 'sport1')
		{
			foundList = Sport1ItemsCacheList[0];
			counter = Sport1ItemsCacheList[1];
			pageCounter = Sport1ItemsCacheList[2];
		}
		else if(type == 'vimedia')
		{
			foundList = VimediaItemsCacheList[0];
			counter = VimediaItemsCacheList[1];
			pageCounter = VimediaItemsCacheList[2];
		}
		else if(type == 'viradio')
		{
			foundList = ViradioItemsCacheList[0];
			counter = ViradioItemsCacheList[1];
			pageCounter = ViradioItemsCacheList[2];
		}
		else if(type == 'viradio')
		{
			foundList = ViradioItemsCacheList[0];
			counter = ViradioItemsCacheList[1];
			pageCounter = ViradioItemsCacheList[2];
		}
		else if(type == 'europaleague')
		{
			foundList = EuropaleagueItemsCacheList[0];
			counter = EuropaleagueItemsCacheList[1];
			pageCounter = EuropaleagueItemsCacheList[2];			
		}

		foundList += '</ul></div>';
				
		if(counter > 12)
		{
			var foundInfoText = counter+' items gevonden (<strong>'+pageCounter+' pagina\'s</strong>)';
		}
		else if(counter > 0 && counter < 13)
		{
			if(counter == 1)
			{
				var foundInfoText = counter+' item gevonden (<strong>1 pagina</strong>)';
			}
			else
			{
				var foundInfoText = counter+' items gevonden (<strong>1 pagina</strong>)';
			}
		}
		else
		{
			var foundInfoText = 'Geen items gevonden';
		}
	

		$("#foundInfo").append(foundInfoText);
		$('#itemsHolderContainer').append(foundList);
		maxMargin = pageCounter * -720;
		$('#itemsHolderContainer').css('left', '0');
		
		var pagingCirclesTop = '<ul>';
		var startNumber = parseInt(activePage) - 7;
		var endNumber = parseInt(activePage) + 7;

		for(i=-1;i<pageCounter;i++)
		{
			liClassCounter = i + 1;
			pageNumberCounter = i + 2;

			liId = 'top_circle_' + liClassCounter;
			
			if(pageNumberCounter > startNumber && pageNumberCounter < endNumber && pageNumberCounter <= pageCounter)
			{
				if(pageNumberCounter == activePage)
				{
					 pagingCirclesTop += '<li class="active" id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
				}
				else
				{
					pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
				}
			}
		}

		pagingCirclesTop += '</ul>';

		$('#pagingHolder').empty();
		$('#pagingHolderTop').empty();

		if(pageCounter <= 1)
		{
			$('#buttonHolder').empty();
			$('#previous').css('visibility','hidden');
			$('#next').css('visibility','hidden');
		}
		else
		{
			$('#pagingHolderTop').append(pagingCirclesTop);
			if(activePage != '1')
			{
				$('#previous').css('visibility','visible');
			}
			else
			{
				$('#previous').css('visibility','hidden');
			}
			$('#next').css('visibility','visible');
		}
	}
	else
	{
		//Do nothing
	}
}

//Function that loads the 9 most recent items at the RTL-homepage
function loadAllMediaRTLHome()
{
	loadAllMedia('true','','','',false);
}

//Function that loads the most recent items and place the most recent item as a thumbnail
function loadAllMediaFirstVisit(placePic)
{
	if(placePic == 'placeNoPicture')
	{
		loadAllMedia('','placeNoPicture','',true, true);
	
		//Fix for IE7
		if ($.browser.msie)
		{
			if($.browser.version < 9)
			{
				loadAllMedia('','placeNoPicture','',true, true);
			}
		}		
	}
	else
	{
		loadAllMedia('','','',true, true);
		
		//Fix for IE7
		if ($.browser.msie)
		{
			if($.browser.version < 9)
			{
				loadAllMedia('','','',true, true);
			}
		}
	}
}


function loadAllMediaFirstVisitSecond()
{
	loadAllMedia('','','',true, false);
}


//Sort functions
function custom_sort(a, b) {
    return new Date(b[1]).getTime() - new Date(a[1]).getTime();
}

function sortNumber(a,b)
{
	return a[1] - b[1];
}

//Function to remove slashes
function removeSlashes(str) {
	str=str.replace(/\\/g,'');
	str=str.replace(/\'/g,'');
	str=str.replace(/\"/g,'');
	str=str.replace(/\0/g,'');
	return str;
}

//Function to remove quotes from a string
function delquote(str){return (str=str.replace(/["']{1}/gi,""));} 

//Function that provides the animation when the user clicks the previous/next buttons or one of the paging circles
function moveList(direction, currentMargin)
{
	if(direction == 'left')
	{
		if(activeCircle + 1 < pageCounter)
		{
			activeCircle = activeCircle + 1;
			marginLeft = activeCircle * -720;

			//As long the last page isn't reached, move the list left (next page)
			if(marginLeft > maxMargin)
			{
				$('#itemsHolderContainer').stop().animate({
				left: marginLeft
				}, 750, function(){});
			}
		}
	}
	else
	{
		if(activeCircle > 0)
		{
			activeCircle = activeCircle - 1;
			marginLeft = activeCircle * -720;
		}
		else
		{
			marginLeft = 0;
		}				
		
		$('#itemsHolderContainer').stop().animate({
		left: marginLeft
		}, 750, function(){});		
	}
	
	var pagingCirclesTop = '<ul>';
	
	//Place max number of pagenumbers next to the active page
	var startNumber = parseInt(activeCircle) - 7;
	var endNumber = parseInt(activeCircle) + 7;
	
	for(i=-1;i<pageCounter;i++)
	{
		liClassCounter = i + 1;
		pageNumberCounter = i + 2;
		liId = 'top_circle_' + liClassCounter;

		if(pageNumberCounter > startNumber && pageNumberCounter < endNumber && pageNumberCounter <= pageCounter)
		{
			pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
		}
	}
	
	pagingCirclesTop += '</ul>';


	$('#pagingHolderTop').empty();
	$('#pagingHolderTop').append(pagingCirclesTop);
	
	//If user isn't on page 1, show the 'previous' button, otherwise hide the button
	if(activeCircle != '0')
	{
		$('#previous').css('visibility','visible');
	}
	else
	{
		$('#previous').css('visibility','hidden');
	}

	//If user is on the last page, hide the 'next' button, otherwise show the button
	if(activeCircle == (parseInt(pageCounter) -1))
	{
		$('#next').css('visibility','hidden');
	}
	else
	{
		$('#next').css('visibility','visible');
	}


	$('#pagingHolder>ul>li').removeClass('active');
	$('#pagingHolderTop>ul>li').removeClass('active');
	$('#circle_'+activeCircle).addClass('active');
	$('#top_circle_'+activeCircle).addClass('active');
}

//Function for the pagenumber blocks
function moveListWithCircles(pageId, pageNumber)
{
	//Catch if null
	if(pageNumber == null)
	{
		pageNumber = 1;
	}

	/*if(pageNumber > 1 && NextListAppended == false && foundList_next != '')
	{
		$("#itemsHolderContainer").append(foundList_next);
		NextListAppended = true;
	}*/
	
	movingDistance = 0;
	numberOfPage = pageCounter;
	activePage = pageNumber;

	
	//Calculate the moving distance for the list
	if (pageId == -1)
	{
		movingDistance = 0;
		activeCircle = parseInt(pageId) + 1;
	}
	else
	{
		pageId = parseInt(pageId) + 1;
		movingDistance = (pageId * -720);

		activeCircle = pageId;
	}

	var pagingCirclesTop = '<ul>';

	var startNumber = parseInt(activePage) - 7;
	var endNumber = parseInt(activePage) + 7;
	
	for(i=-1;i<pageCounter;i++)
	{
		liClassCounter = i + 1;
		pageNumberCounter = i + 2;
		liId = 'top_circle_' + liClassCounter;

		if(i > -2 && i < pageCounter)
		{
			if(pageNumberCounter > startNumber && pageNumberCounter < endNumber)
			{
				//Give the active page's block an active style 
				if(pageNumberCounter == activePage)
				{
					 pagingCirclesTop += '<li class="active" id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
				}
				else if(pageNumberCounter <= numberOfPage)
				{
					pagingCirclesTop += '<li id="'+liId+'"><a onclick="moveListWithCircles(\''+i+'\',\''+pageNumberCounter+'\')">'+pageNumberCounter+'</a></li>';
				}
			}
		}
	}

	pagingCirclesTop += '</ul>';

	//Place the pagenumberblocks
	$('#pagingHolderTop').empty();	
	$('#pagingHolderTop').append(pagingCirclesTop);


	//Animate to the selected page
	$('#itemsHolderContainer').stop().animate({
		left: movingDistance
		}, 750, function(){});

	
	//Show/hide the previous/next buttons when needed
	if(activePage != '1')
	{
		$('#previous').css('visibility','visible');
	}
	else
	{
		$('#previous').css('visibility','hidden');
	}

	if(activePage == pageCounter)
	{
		$('#next').css('visibility','hidden');
	}
	else
	{
		$('#next').css('visibility','visible');
	}


	$('#pagingHolder>ul>li').removeClass('active');
	$('#pagingHolderTop>ul>li').removeClass('active');

	$('#circle_'+activeCircle).addClass('active');
	$('#top_circle_'+activeCircle).addClass('active');
}

//Function when a RTL-item is clicked. This function places a thumbnail of the RTL-item at the players place
function loadMediaItemRTL(thumbnailUrl, videoUrl)
{
	var content = '<a href="'+videoUrl+'" target="_blank"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';
	
	$('#playerHolder').empty();
	$('#playerHolder').append(content);
}

//Function when a Sport1-item is clicked. This function places a thumbnail of the Sport1-item at the players place
function openSport1Item(thumbnailUrl, guid)
{
	var content = '<a onclick="windowObj = top.open(\''+guid+'\',\'new428717\',\'width=700,height=630,left=10,top=10,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no\');if (windowObj != null){windowObj.focus()};return false;" href="#"><img src="/static/vi/images/play_button.png" class="thumbPlayButton" alt="speel video af" title="speel video af"/><img src="'+thumbnailUrl+'" alt="" title="" /></a>';

	$('#playerHolder').empty();
	$('#playerHolder').append(content);
}

//Function when a VI-item is clicked. This function uses the loadVideo function to load/place the player
function loadViMediaItem(artId)
{
	var movieXmlFile = artId+'?cachetimeout=300&contenttype=xml&flashxml=true&pagepartid=471545&presentationid=471544&webid=26098';
	$('#playerHolder').empty();	
	
	loadVideo(movieXmlFile, '720', '405', 'true', 'true', 'playerHolder');		
			
	//Function for RTL STIR
	function sitestat(u){var d=document;ns_pixelUrl=u+"&ns__t="+(new
			Date().getTime());u=ns_pixelUrl+"&ns_c="+((d.characterSet)?d.characterSet:d.defaultCharset)+"&ns_ti="+escape(
			d.title)+"&ns_jspageurl="+escape(d.URL)+"&ns_referrer="+escape(d.referrer);(d.images)?new
			Image().src=u:d.write('<'+'p><img src="'+u+'" height="1" width="1" alt="*"/><'+'/p>');};
			sitestat("http://nl.sitestat.com/rtl/rtlvoetbal/s?vi_tv.videoarchive.videoPlayed["+artId+"]");
			sitestat("http://nl.sitestat.com/rtl/rtlvoetbal/s?vi_tv.videoarchive.videoPlayed["+artId+"]&amp;ns_channel=sport/rtlvoetbal&amp;ns_context=rtl7");
}




function playVideoFromLink(linkedVideo,linkedCategory)
{
	if(linkedCategory != 'rtl_gemist')
	{
		if(linkedCategory == 'europaleague')
		{
			tabId = 'link_el';
		}
		else if(linkedCategory == 'sport1')
		{
			tabId = 'link_sport1';
		}
		else if(linkedCategory == 'vimedia')
		{
			tabId = 'link_vimedia';
		}
		else if(linkedCategory == 'viradio')
		{
			tabId = 'link_radio';
		}

		loadMediaXml(linkedCategory, tabId, true, linkedVideo);
	}
	else if(linkedCategory == 'rtl_gemist')
	{
		tabId = 'link_gemist';
		loadAllMedia('', 'rtlgemist', '','', true, linkedVideo);
	}
}


/**************************************/
//Breaking news function @ homepage not used right now
function toggleBreakingNews(animated)
{
	$('#closeBreakingNewsbutton').mousedown(function(){
		toggleBreakingNews();	
	});
	
	if(animated == true)
	{
		$('ul#breakingNewsTicker').newsticker(3700);
	}
	
	if($('#breakingNewsBar').css('display') == 'none')
	{
		$('#breakingNewsBar').css('display','block');
	}
	else
	{
		 $('#breakingNewsBar').css('display','none');
	}
}
