/* Called after DOM has been generated */
$(document).ready(function(){
	// for the /news/ and /featured/ pages	
	setupPostViewer();
	
	// for the awards page
	setupAwardWinnerForm();	
	
	/* Sidebar Archive Section Special Formatting */
	setupSidebar();
});
	
	
function setupPostViewer()
{
	remove_no_js_fallbacks();

        // get the querystring data from the post viewer
        window.readPosts = {
                        "cat":40, "page":1,
                        "sort":"", "search":""
        };

        // overwrite defaults with values in the filter form
        $('#filter-form').children("input,select").each(function(){
                window.readPosts[$(this).attr("name")] = $(this).attr("value");
        });

        // Post Viewer Filter Form
        $('#filter-form').attr("target", "/ajax/read_posts.php");
        $('#filter-form').children("select").each(function(){
                $(this).change( function(e){
			// update the readPosts data with value in the field
	                window.readPosts[$(this).attr("name")] = $(this).attr("value");
                				
			// request a new page of posts using the new data
	                ajax_readPosts( window.readPosts, $('#read-posts-cont') );
                });
        });
        $('#filter-form').submit( function(e){
                e.preventDefault();
		ajax_readPosts( window.readPosts, $('#read-posts-cont') );
                return false;
        });

        // rig the Older/Newer entries links
        setupLinks();
}

function setupAwardWinnerForm()
{
	remove_no_js_fallbacks();
        window.awardWinner = {"y":1947, "type":"foty", "player":"", "choice":"y"};
        // rig the previous winners form on the awards page
        $('#award-winner-form').children("input,select").each( function(){
                window.awardWinner[$(this).attr("name")] = $(this).attr("value");
        });

        $('#award-winner-form').attr("target", "/ajax/awards.php");
        $('#award-winner-form').children("select").each(function(){
                $(this).change( function(e) {
			window.awardWinner["choice"] = $(this).attr("name");
                        window.awardWinner[$(this).attr("name")] = $(this).attr("value");
                        ajax_awardWinner( window.awardWinner, $('#award-winner-cont') );
                });
        });
        $('#award-winner-form').submit( function(e){
                e.preventDefault();
                ajax_awardWinner(window.readPosts, $('#award-winner-form') );
                return false;
        });
}

function setupSidebar()
{
	window.darrow = $("<span>&darr;</span>").text();
	window.uarrow = $("<span>&uarr;</span>").text();
	
	// initialise the twitter feed, first one is homepage only, second one is everywhere else
        $('#twitter-homepage').twitter({username:["theofficialfwa"], list:"football-writers", count:7});
        $('#twitter-container').twitter({username:["theofficialfwa"], count:6});

	/* Sidebar Archive Section Special Formatting */
        var curYear = (new Date()).getFullYear();
        $('.archive-year-header').each( function(){

                $(this).children(".archive-arrow").click( function() {
                        if( $(this).hasClass("expanded") )
                        {
                                $(this).parent().children("ul").hide();
                                $(this).removeClass("expanded");
                                $(this).text( darrow ); // <- that is a down arrow
                                $(this).attr("title", "Expand");
                        }
                        else
                        {
                                $(this).parent().children("ul").show();
                                $(this).addClass("expanded");
                                $(this).text( uarrow ); // <- that is an up arrow
                                $(this).attr("title", "Collapse");
                        }
                } );

                // expand the current year automatically
                if( $(this).attr("rel") == curYear )
                        $(this).children(".archive-arrow").removeClass("expanded");

                // click the all once to fully initialise them
                $(this).children(".archive-arrow").click();
        });
}

function ajax_readPosts( data, container )
{
	readPosts.lockdown = true;
	$.ajax({
		url:"/ajax/read_posts.php", 
		data: data,
		success: function( html ){
			$(container).replaceWith(html);
			// rig the Older/Newer entries links
        		setupLinks();
			
		},
		complete: function( html, state ){
			readPosts.lockdown = false;
			window.scrollTo(0, 120);
		}
	});
}

function ajax_awardWinner( data, container )
{
	$.ajax({
		url:"/ajax/awards.php",
		data:data,
		success: function( html ){
			
			// reset some fields depending on which were used
			// this is to prevent confusing combinations of year,award and player
			// being present in the drop down boxes
			var mode = $(html).attr("rel");			
			if( mode == 1 ) // year + award
			{
				$("#award-winner-player").val("0");
			}
			else // player
			{
				$("#award-winner-year").val("0");
				$("#award-winner-type").val("foy");
			}

			// insert the new content
			$(container).replaceWith(html);
		}
	});
}

function remove_no_js_fallbacks() {
	// Remove all no-js-fallback's
        $('.no-js-fallback').each(function(){
                $(this).remove();
        });
}

function setupLinks()
{
	$('.posts-prev').click(function(e) {
                e.preventDefault();
                if( !readPosts.lockdown ){
                        readPosts.page--;
                        ajax_readPosts( readPosts, $('#read-posts-cont') );
                }
        });

        $('.posts-next').click(function(e) {
                e.preventDefault();
                if( !readPosts.lockdown ){
                        readPosts.page++;
                        ajax_readPosts( readPosts, $('#read-posts-cont') );
                }
        });
}

