TopList = {
	init:function() {
		this.removeRequirementsLink();
	},
	removeRequirementsLink:function(){
		$('p.requirements').remove();
		this.addRequirementsLink();
    },
	addRequirementsLink: function(){
		$('.high-roller span.requirements').each(
        function()
        {
			var box = $('div.requirements', this).slideUp();

			$( '<p class="requirements"><a href="#" class="requirements">View requirements</a></p>' ).insertBefore(box).find('a').toggle(function()
            {
				$(this).text('Hide requirements').addClass('hide-req');
				box.slideDown( 300 );
			}, function() {
				$(this).text('View requirements').removeClass('hide-req');
				box.slideUp( 300 );
			}
            );
		});
	}
};

/*
 * Changes the currency icon when the user clicks on that icon
 *
 */
function changeCurrencyIcon( link ){
	if( link == '' ){
		return false;
	}

    if( typeof $(link).parent().parent().attr('class') == 'undefined' ){
        return false;
    } else if( $(link).parent().parent().attr('class').indexOf("currency-selector") != -1 ){
        $('.currency-selector > .selected').removeClass( 'selected' );
        $(link).parent().addClass( 'selected' );
        return true;
    }
}

function addClickCountOnLink(){
	ClickTracker.init('#extraContent a');
}

$(document).ready(function(){

	TopList.init();

	initializePaginationClicks();

});


function showPage( urlStr, targetDivId, getParam, link, callbackOnDone ){
		if( link != undefined ) {
			changeCurrencyIcon( link );
		}

		$("div#" + targetDivId).fadeTo(100, 0.01);

		ajaxUrlString = urlStr;

		if( getParam ){
			ajaxUrlString = ajaxUrlString + "?" + getParam;
		}
		
		/* If the Currency was changed, change the selected icon */

		$.post( ajaxUrlString, function( data ){
			$("div#" + targetDivId).html( data );
			$("div#" + targetDivId).unbind( "ajaxComplete" );

			initializePaginationClicks();

			$("div#" + targetDivId).ajaxComplete( function( e, xhr, settings ){
				$("div#" + targetDivId).fadeTo(500, 1);
				TopList.init();
				/* Add sitecatalyst events on urls */
				if(  settings.url == ajaxUrlString ){
					addClickCountOnLink();
				}
				if(callbackOnDone){
					callbackOnDone();
				}
			});

		});
	}

	function initializePaginationClicks() {
		$('.paging a').click(function(){
			var link = $(this).attr('link');
			var params = $(this).attr('params');

			showPage(link, 'extraContent', params);
			return false;
		});
	}
