function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
	// The index() method calculates the index from a
        // given index who is out of the actual item range.
        var idx = carousel.index(i, itemList.length);
//	for(var i = 0; i < itemList.length; i++) {
	        carousel.add(i, mycarousel_getItemHTML(itemList[idx - 1]));
//	        carousel.add(i, mycarousel_getItemHTML(itemList[i]));
//	}
}; 
            
function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
	carousel.remove(i);
}; 
            
function mycarousel_getItemHTML(item) {
    return '<div class="latestGalleryTeaserImage"><a href="' + item.link + '"><img height="75" width="130" src="' + item.url + '"/></a></div><div class="carouselHeadlineText"><a href="' + item.link + '">' + item.headline + '</a></div>';
//	return '<a href="' + item.link + '"><div class="latestGalleryTeaserImage"><img height="75" width="130" src="' + item.url + '"/></div><div class="carouselHeadlineText">' + item.headline + '</div></a>';
}; 
            
jQuery(document).ready(function() {
	if($('#newsIndexHolder').length > 0) {
		$('#brandingImageHolder').show();
		updateNewsIndex(fliIndexSet[GLOBAL_PAGE_BASE_PATH.substring(1, GLOBAL_PAGE_BASE_PATH.length)].startPageNo);
	}

	if($('#featuresCarousel').length > 0) {
		jQuery('#featuresCarousel').jcarousel({
			scroll: 3,
        	        wrap: "circular",
	                easing: "linear",
                	animation: 1000,
			auto: 8,
	                itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
                	itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback} 
		});
	}
});

var fliIndexSet = {
    // NEWS INDEX PAGES

        //'Phase2TestsNewsIndex' : {'catIds' : ['6', '12006119', '12006204', '12006205', '12006206', '12006208', '12006234', '12006817', '12006904', '12006923', '12006924', '12006925', '12006995', '12009871', '12009897', '12012473', '12012760', '12012933', '12012936', '12013445', '12014417'],
	   'Phase2TestsNewsIndex' : {'catIds' : ['12017346'],
                    'startPageNo' : '1'//,
          //          'isVideoArticle' : true
        },
        
        'News' : {'catIds' : ['1200276', '12006211', '12006856', '12006857', '12006952', '12006953', '12006954', '12006955', '12006956', '12006957', '12006958', '12007191', '12007193', '12013037', '12013444', '12015627'],
                      'startPageNo' : '0'
        },
        
        'OfficialCharity/HelptheHospices/Index' : {'catIds' : ['12019602','12019600'],
                        'startPageNo' : '0',
						 'orderBy' : 'detailInstanceId',
						 'sortd': 'asc'
        },
		
		'OfficialCharity/HelptheHospices/NewsIndex' : {'catIds' : ['12019601'],
                        'startPageNo' : '1'
        },
        
        'LatestNews' : {'catIds' : ['6','12006119','12006204','12006205','12006206','12006234','12006817','12006923','12006924','12006925','12006994','12006996','12012760','12012933','12013271','12013445'],
                        'startPageNo' : '1'
        },
        
        'Columnists' : {'catIds' : ['12006994'],
                        'startPageNo' : '1'
        
        },
        'Features' : {'catIds' : ['12006995'],
                      'startPageNo' : '1'
        },
        
        'Blogs' : {'catIds' : ['12013041', '12013042', '12013043', '12016156', '12019132'],
                   'startPageNo' : '0'
        },
        
        '/Blogs/Kilimanjaro' : {'catIds' : ['12019133'],
                   'startPageNo' : '1'
        },
        
        'Blogs/ChrisCharles' : {'catIds' : ['12016157'],
                   'startPageNo' : '1'
        },
        
        'Blogs/Championship' : {'catIds' : ['12013038'],
                   'startPageNo' : '1'
        },
        
        'Blogs/League1' : {'catIds' : ['12013039'],
                   'startPageNo' : '1'
        },
        
        'Blogs/League2' : {'catIds' : ['12013040'],
                   'startPageNo' : '1'
        },
        
        'News/ChampionshipNews' : {'catIds' : ['12006923', '12012483','6','12006119','12006204','12006205','12006206','12006234','12006817','12006923','12006924','12006925','12006994','12006996','12012760','12012933','12013271','12013445'],
                    'startPageNo' : '1'
        },
        
        'News/League1News' : {'catIds' : ['6','12006119','12006204','12006205','12006206','12006234','12006817','12006923','12006924','12006925','12006994','12006996','12012760','12012933','12013271','12013445','12006924', '12012483'],
                    'startPageNo' : '1'
        },
        
        'News/League2News' : {'catIds' : ['12006925', '12012483','6','12006119','12006204','12006205','12006206','12006234','12006817','12006923','12006924','12006925','12006994','12006996','12012760','12012933','12013271','12013445'],
                    'startPageNo' : '1'
        },
        
        'News/PlayOffs' : {'catIds' : ['12006205'],
                    'startPageNo' : '1'
        },
        
        'News/CarlingCupNewsIndex' : {'catIds' : ['12006119'],
                    'startPageNo' : '1'
        },
        
        'JPTNews' : {'catIds' : ['12006204'],
                    'startPageNo' : '1'
        },
        
        'FLTrustNews' : {'catIds' : ['12006817'],
                    'startPageNo' : '1'
        }, 
        
        'News/FLNews' : {'catIds' : ['12006206'],
                    'startPageNo' : '1'
        },
        
        'News/FLAwards' : {'catIds' : ['12006208'],
                    'startPageNo' : '1'
        },
        
        'FLAwardsSection' : {'catIds' : ['12006858', '12006859', '12006860', '12006861', '12006862', '12009871'],
                    'startPageNo' : '0'
        },
        
        'LeagueCup50th' : {'catIds' : ['12012745', '12012746', '12012747', '12012749', '12012750', '12012761', '12012762', '12012766', '12072767'],
                    'startPageNo' : '0'
        },
        
        'LeagueCup50th/LatestNews' : {'catIds' : ['12012760'],
                    'startPageNo' : '1'
        },
        
        'OfficialCharity/MarieCurie/Index' : {'catIds' : ['12015629', '12015631'],
                    'startPageNo' : '0'
        },
        
        'MarieCurie/NewsIndex' : {'catIds' : ['12015630'],
                    'startPageNo' : '1'
        },
        
        'OfficialCharity/HelpforHeroes' : {'catIds' : ['12012934', '12012935', '12012936', '12012938', '12012939', '12013014', '12013275', '12013396', '12013878', '12014459'],
                    'startPageNo' : '0'
        },
        
        'OfficialCharity/HelpforHeroes/News' : {'catIds' : ['12012933'],
                    'startPageNo' : '1'
        },
        
        'OfficialCharity/FootballForHeroesWeek' : {'catIds' : ['12014460'],
                    'startPageNo' : '0'
        },
        
        'News/Video' : {'catIds' : ['6', '12006119', '12006204', '12006205', '12006206', '12006208', '12006234', '12006817', '12006904', '12006923', '12006924', '12006925', '12006995', '12009871', '12009897', '12012473', '12012760', '12012933', '12012936', '12013445', '12014417'],
                    'startPageNo' : '1',
                    'isVideoArticle' : true
        },
        
        'Phase2TestSectionIndex' : {'catIds' : ['12006863'],
                      'startPageNo' : '0'
        },
        
        //  MATCH INDEX PAGES
        
        'MatchIndex' : {'catIds' : ['12006219', '12006926', '12006934', '12006970', '12007373', '12007374', '12007376', '12009514', '12009515'],
                    'startPageNo' : '0'
        },
        
        'LineUps' : {'catIds' : ['12006220'],
                    'startPageNo' : '1'
        },
        
        'MatchCentre' : {'catIds' : ['12006214', '12006215', '12006216'],
                    'startPageNo' : '0'
        },
        
        'Referees' : {'catIds' : ['12006217', '12006855', '12006864', '12006908', '12009872', '12013270'],
                    'startPageNo' : '0'
        },
        
        'LawsofFootballIndex' : {'catIds' : ['12006863'],
                    'startPageNo' : '0'
        },
        
        'Reserves' : {'catIds' : ['12006872', '12006874', '12006875', '12009885'],
                    'startPageNo' : '0'
        },
        
        'ReserveLeague' : {'catIds' : ['12006873'],
                    'startPageNo' : '0'
        },
        
        'MenInBlack' : {'catIds' : ['12013271'],
                    'startPageNo' : '0'
        },
        
        
        //  STATS INDEX PAGES
        
        'StatsIndex' : {'catIds' : ['49', '1200375', '12006222', '12006223', '12006224', '12006927', '12006971', '12014035'],
                    'startPageNo' : '0'
        },
        
        'RecordsSectionIndex' : {'catIds' : ['12006870', '12006928', '12006932', '12009880'],
                    'startPageNo' : '0'
        },
        
        'RecordsStatsIndex' : {'catIds' : ['12006221', '12006865', '12006866', '12006867', '12006868', '12006869', '12006895', '12006929', '12006930'],
                    'startPageNo' : '0'
        },
        
        'LeagueCupRecords' : {'catIds' : ['12006871', '12006931', '12009883'],
                    'startPageNo' : '0'
        },
        
        'RecordsLeagueTrophy' : {'catIds' : ['12006933', '12009884'],
                    'startPageNo' : '0'
        },
        
        // CLUBS INDEX PAGES
        
        'ClubsIndex' : {'catIds' : ['12006231', '12006935', '12012568', '12012569', '12012570', '12013114'],
                    'startPageNo' : '0'
        },
        
        'ChampionshipIndex' : {'catIds' : ['12006227'],
                    'startPageNo' : '0',
 		    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        'League1Index' : {'catIds' : ['12006228'],
                    'startPageNo' : '0',
		    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        'League2Index' : {'catIds' : ['12006229'],
                    'startPageNo' : '0',
	 	    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        // FANS INDEX PAGES
        
        'FansIndex' : {'catIds' : ['12006233', '12006235', '12006236', '12006880', '12006907', '12006935', '12006942', '12006976', '12007580', '12007583', '12007585', '12015180', '12016462'],
                    'startPageNo' : '0'
        },
        
        'Fans/GroundGuidesSection' : {'catIds' : ['12006939', '12006940', '12006941'],
                    'startPageNo' : '0'
        },
        
        'ChampionshipGroundGuideIndex' : {'catIds' : ['12006230'],
                    'startPageNo' : '0',
		    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        'LeagueOneGroundGuideIndex' : {'catIds' : ['12006937'],
                    'startPageNo' : '0',
		    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        'LeagueTwoGroundGuideIndex' : {'catIds' : ['12006938'],
                    'startPageNo' : '0',
		    'orderBy' : 'headline',
		    'sortd' : 'asc'
        },
        
        'YoungFans' : {'catIds' : ['12006876', '12006880', '12006881', '12009889', '12013443'],
                    'startPageNo' : '0'
        },
        
        'YoungFansIndex' : {'catIds' : ['12006877', '12006878', '12006879', '12009890'],
                    'startPageNo' : '0'
        },
        
        'FotFNews' : {'catIds' : ['12006234'],
                    'startPageNo' : '1'
        },
        
        'YoungFansPlayFootball' : {'catIds' : ['12009891'],
                    'startPageNo' : '0'
        },
        
        'EnjoyTheMatch' : {'catIds' : ['12006905', '12006906', '12006997', '12007258', '12007259', '12012473', '12012902'],
                    'startPageNo' : '0'
        },
        
        'EnjoytheMatch/VideoIndex' : {'catIds' : ['12006904'],
                    'startPageNo' : '0'
        },
        
        'EnjoyTheMatchNews' : {'catIds' : ['12006996'],
                    'startPageNo' : '1'
        },
        
        'OverseasFansIndex' : {'catIds' : ['12014923'],
                    'startPageNo' : '0'
        },
        
        'Games' : {'catIds' : ['12006993', '12013196'],
                    'startPageNo' : '0'
        },
        
        'Competitions' : {'catIds' : ['12006237'],
                    'startPageNo' : '1'
        },
        
        'Podcast' : {'catIds' : ['12007581'],
                    'startPageNo' : '1'
        },
       
        // ABOUT US INDEX PAGES
        
        'FLExplained' : {'catIds' : ['12006885'],
                    'startPageNo' : '1'
        },
        
        'Media/PressReleases' : {'catIds' : ['12006894'],
                    'startPageNo' : '1'
        },
        
        'PublicationsIndex' : {'catIds' : ['12006249'],
                    'startPageNo' : '1'
        },
        
        'AboutUs' : {'catIds' : ['12006239', '12006243', '12006245', '12006248', '12006250', '12006251', '12006252', '12006916', '12006917', '12006918', '12006919', '12006920', '12006922', '12016250'],
                    'startPageNo' : '0'
        },
        
        'FAQ/FAQs' : {'catIds' : ['12006886', '12006887', '12006888'],
                    'startPageNo' : '0'
        },
        
        'HistoryIndex' : {'catIds' : ['12006241', '12006889'],
                    'startPageNo' : '0'
        },
        
        'AboutUs/WhosWho' : {'catIds' : ['12006945', '12006946', '12007266', '12009894', '12014743'],
                    'startPageNo' : '0'
        },
        
        'BoardProfilesIndex' : {'catIds' : ['12006244'],
                    'startPageNo' : '0'
        },
        
        'SMTProfilesIndex' : {'catIds' : ['12006890'],
                    'startPageNo' : '0'
        },
        
        'Media' : {'catIds' : ['12006246', '12006891', '12006893', '12007195', '12009497'],
                    'startPageNo' : '0'
        },
        
        'Commercial' : {'catIds' : ['12006247', '12006248', '12006892', '12007196', '12009897'],
                    'startPageNo' : '0'
        },
        
        'RegulationsIndex' : {'catIds' : ['12016251'],
                    'startPageNo' : '0',
                    'orderBy' : 'detailInstanceId',
		    'sortd' : 'asc',
		    'sortType' : 'number'
        },
       
        'YouthDevelopmentIndex' : {'catIds' : ['12006278'],
                    'startPageNo' : '0'
        },
        
        // FL TRUST INDEX PAGES
        
        'CommunityIndex' : {'catIds' : ['12006279'],
                    'startPageNo' : '0'
        },
        
        'KidsCup/Index' : {'catIds' : ['12016620', '12016622'],
                    'startPageNo' : '0'
        },
        
        'KidsCupNewsIndex' : {'catIds' : ['12006280'],
                    'startPageNo' : '1'
        },
        
        'GirlsCup/Index' : {'catIds' : ['12016621', '12016623'],
                    'startPageNo' : '0'
        },
        
        'GirlsCupNewsIndex' : {'catIds' : ['12006281'],
                    'startPageNo' : '1'
        },
        
        'NewslettersIndex' : {'catIds' : ['12006822'],
                    'startPageNo' : '1'
        },
        
        'TrustPartners' : {'catIds' : ['12006823', '12007334', '12007335', '12007336'],
                    'startPageNo' : '0'
        },
        
        // YOUTH ALLIANCE INDEX PAGES
        
        'YouthAlliance' : {'catIds' : ['12007187', '12007188', '12007189', '12009898'],
                    'startPageNo' : '0'
        },
        
        'YouthAllianceIndex' : {'catIds' : ['12005825'],
                    'startPageNo' : '0'
        } 
};

function updateNewsIndex(pageNumber) {
    var luceneUrl = buildLuceneUrl(pageNumber);
    var newHtml = '';

    $.ajax({
        url:luceneUrl,
        type:"GET",
        dataType: "html", 
        success: function(html) {
            $('#newsIndexHolder').html(html);
            return true;
        }
    });
}

function buildLuceneUrl(pageNumber) {
    var indexItem = fliIndexSet[GLOBAL_PAGE_BASE_PATH.substring(1, GLOBAL_PAGE_BASE_PATH.length)];
    var perPage;

    var luceneUrl = '/siteSearch/siteSearch/0,,' + 
        GLOBAL_SITE_ID + 
        ',00.xml?iTypeIds=1&1filterIn_pageCategoryId=';


    if(indexItem.startPageNo == 0) {
	perPage = 25;
    }  else {
	perPage = 10;
    }
    
    $(indexItem.catIds).each(function(item) {
        luceneUrl = luceneUrl + indexItem.catIds[item] + ",";
    });
    
    luceneUrl = luceneUrl.substring(0, (luceneUrl.length-1));
    
    luceneUrl = luceneUrl + '&pageSize=' + perPage;
        
    if(pageNumber != 0) {
        luceneUrl = luceneUrl + '&npl=10&pageNumber=' + pageNumber;
    }
    
    if(indexItem.isVideoArticle) {
        luceneUrl = luceneUrl + '&1filterIn_isVideoArticle=Y';
    }
    
    if(indexItem.orderBy) {
        luceneUrl = luceneUrl + '&sortf=' + indexItem.orderBy;
	if(indexItem.sortd) {
		luceneUrl = luceneUrl + '&sortd=' + indexItem.sortd;
	}
	if(indexItem.sortType) {
		luceneUrl = luceneUrl + '&sortd=' + indexItem.sortType;
	}
    } else {
        luceneUrl = luceneUrl + '&sortf=publishedDate';
    }
    
    luceneUrl = luceneUrl + '&lastXDays=9999';
    luceneUrl = luceneUrl + '&gXsltId=1579';
    
    return luceneUrl;
}
