function flashAnimComplete() {

}

function NewsAggregator() {

    $(function() {
        var pageLang = ($('body').attr('class') == "lang-en") ? "en" : "fr";

        /* Ping database for number of records in the current language. */
        $.ajax({
            data: {
                lang: pageLang
            },
            dataType: "html",
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                //console.log(XMLHttpRequest, textStatus, errorThrown);
            },
            success: function(data, textStatus) {
                Scrollable(parseInt(data) + 3, pageLang);
            },
            url: "/UserControls/Internal/GetFeeds.aspx"
        });


        $("div.scrollable div.items div.item a[rel], div.scrollable div.items div.item span.ytwrapper span").live("click", function() {
            overlay.getConf().trigger = $(this);
            overlay.load();
            return false;
        });

        var overlay = $('div.overlay').overlay({
            absolute: true,
            api: true,
            expose: {
                color: "#ffffff",
                opacity: 0.9
            },
            fadeInSpeed: 1,
            speed: 1,
            trigger: null,
            top: 120,
            onBeforeLoad: function() {
                //var targetItem = this.getTrigger();
                var targetItem = this.getConf().trigger;

                this.getContent().append('<div class="wrapper"><div class="loader"><img src="/App_Themes/GreenBin/_images/ajax-loader.gif" /></div></div>');

                // Make AJAX call to grab content for this overlay.
                $.ajax({
                    beforeSend: function() {
                        
                    },
                    complete: function(XMLHttpRequest, textStatus) {

                    },
                    data: {
                        id: targetItem.parents('div.item').children('input[type="hidden"]').val(),
                        lang: pageLang
                    },
                    dataType: "html",
                    error: function(XMLHttpRequest, textStatus, errorThrown) {

                    },
                    success: function(data, textStatus) {
                        $('#overlay div.wrapper div.loader').remove();
                        $('#overlay div.wrapper').append(data);

                        $('#overlay div.wrapper a').click(function() {
                            if($(this).attr('target'))
							{
								var newWindow = window.open($(this).attr('href'), '_blank');
								newWindow.focus();								
							}
							else
							{
								document.location = $(this).attr('href');								
                            }
                            
                            return true;
                            
                        });
                    },
                    url: "/UserControls/Internal/GetFeeds.aspx"
                });
            },
            onBeforeClose: function() {
                $("#overlay div.wrapper").remove();
            }
        });

    });
	
}

function Scrollable(numItems, pageLang)
{
	// initialize scrollable
	var scrollable = $("div.scrollable").scrollable({
		api: true,
		clickable: false,
		keyboard: false,
		size: 4,
		nextPage: ".fauxNextPage",
		prevPage: ".fauxPrevPage",
		
		lang: pageLang,
		maxItems: numItems,
		maxPages: Math.floor(numItems / 4) + parseInt(((numItems % 4) > 0) ? 1 : 0),
		start: 1,
		stop: 17
	});

	scrollable.onBeforeSeek(function(e){
		//return false;
	});
	
	$(document).keydown(function(e){
		switch(e.keyCode) {
			case 37:
				initScroller('prev');
				break;
			
			case 39:
				initScroller('next');
		}
	});
	
	$('#carousel a.prevPage').click(function() { initScroller('prev'); return false; });
	$('#carousel a.nextPage').click(function() { initScroller('next'); return false; });

	function initScroller(scrollDirection) {
		// Current page being viewed in the carousel
		var currPageIndex = scrollable.getPageIndex() + 1;
		var numPages = scrollable.getPageAmount();

		// Requested scroll direction (next or previous)
		var currScrollDirection = scrollDirection;

		// Status
		/*
		console.log(
			'Current Page: ' + currPageIndex,
			'Cached Pages: ' + numPages,
			'Total Items: ' + scrollable.getConf().maxItems,
			'Total Pages: ' + scrollable.getConf().maxPages,
			'Scroll Direction: ' + currScrollDirection
		);	
		*/

		/* Determine course of action based on the direction the user has requested
		to move in. */
		if(currScrollDirection == "prev")
		{
			if(currPageIndex == 1)
			{
				//console.log('Beginning');
			}
			else{
				scrollable.prevPage();
			}
		}
		else if(currScrollDirection == "next")
		{
			// Cached view of the carousel
			if(currPageIndex < numPages)
			{
				//console.log('Cached next page request');
				scrollable.nextPage();
			}
			
			// End of the filmstrip, period.
			else if(numPages < scrollable.getConf().maxPages) 
			{
				//console.log('Request pages from DB');
				PollDB(currPageIndex);
				scrollable.reload();
				scrollable.nextPage();
			}		
			
		}
		
		
		/* Fire off events to clean up cache and update next/prev links, if 
		necesssary. */
		NavigationState();
				
		return false;
	}
	
	
	function PollDB(currPageIndex)
	{
		$.ajax({
			async: false,
			beforeSend: function(){
				scrollable.getRoot().append('<div class="loader"><img src="/App_Themes/GreenBin/_images/ajax-loader.gif" /></div>');
			},
			complete: function(XMLHttpRequest, textStatus){

			},
			data: {
				start: (currPageIndex * 4) - 2,
				stop: scrollable.getConf().maxItems,
				lang: scrollable.getConf().lang
			},
			dataType: "html",
			error: function(XMLHttpRequest, textStatus, errorThrown){

			},
			success: function(data, textStatus){
				//console.log('done');
				$('div.scrollable div.loader').remove();

				/* Tell scrollable that we are done fetching data. */
				if(data == '')
				{
					return false;
				}
				else
				{
					scrollable.getItemWrap().append(data);
					scrollable.reload();
					return true;
				}
			},
			url: "/UserControls/Internal/GetFeeds.aspx"
		});
	
	}
	
	
	function NavigationState()
	{
		var currPageIndex = scrollable.getPageIndex() + 1;
		
		if(currPageIndex >= scrollable.getConf().maxPages) $('#carousel a.nextPage').addClass('disabled');
		else $('#carousel a.nextPage').removeClass('disabled');
		
		if(currPageIndex <= 1) $('#carousel a.prevPage').addClass('disabled');
		else $('#carousel a.prevPage').removeClass('disabled');
	}	
}