$(function() {

// VERY IMPORTANT, disable default onclick method defined in HTML to make sure
// google analytics tags only fire once!
$('#games_sort_method a').attr('onclick', '');

var gfdos = $('#games_full_details').children('.game_object');
if (gfdos.length) {
	var gfdo = gfdos.eq(0);
	var gfdoh = parseFloat(gfdo.height()) + parseFloat(gfdo.css('margin-bottom'));
	//console.log(gfdos.length);
	//console.log(gfdoh);
}

/**
var gfdoh = $('#games_full_details').children().eq(0).height();
 * get current sort order
 */
function getSortOrder() {
	var lhsr = $('#link_highest_star_rating');
	if (lhsr.length && lhsr.hasClass('inactive')) return 'weight';
	return 'rating';
}

/**
 * get current sort dir
 */
function getSortDir() {
	var sortingby = $('#arcadeSort a[class!=inactive]').attr('id');	// non inactive sort option
	if (sortingby == 'link_newest_to_oldest' || sortingby == 'link_highest_star_rating') return 'DESC';
	return 'ASC';
}

/**
 * get current full listing status
 */
function isFullListing() {
	return $('#games_full_details').length ? 1 : 0;
}

/**
 * get current titles listing status
 */
function isTitlesListing() {
	return $('#games_titles_only').length ? 1 : 0;
}

/**
 * flip newest to oldest / highest lowest
 */
function flipSortLink(li, new_id, new_text) {
	$('#' + li + ' a').attr('id', new_id).text(new_text);
}

/**
 * reload the div with new query
 */
function switchList(full, titles, sort, dir) {
	$('#games_listing').load('gamesListing.cfm', {
		'full_listing' : full,
		'titles_listing' : titles,
		'sort' : sort,
		'dir' : dir
	}, function() {
		if (gfdos.length) {
			if (full) {
				//console.log ('using full height');
				$(this).css({'height' : gfdoh * gfdos.length + 100});	// 13 is number of games, 350 is game height + margin
			}
			else {
				//console.log ('using titles height');
				$(this).css({'height' : $('#games_titles_only').height() + 100});
			}
			$.scrollTo('500px', 1000);
		}
	});
}

/**
 * capture full details link + tracking tag
 */
$('#link_games_full_details').attr('href', '').click(function(e) {
	if ($(this).hasClass('inactive')) {
		$(this).removeClass('inactive');
		$('#link_games_titles_only').addClass('inactive');
		//$.scrollTo('500px', 500);
		switchList(1, 0, getSortOrder(), getSortDir());
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/Full_Details');
	}
	e.preventDefault();
	return false;
});

/**
 * capture titles only link + tracking tag
 */
$('#link_games_titles_only').attr('href', '').click(function(e) {
	if ($(this).hasClass('inactive')) {
		$(this).removeClass('inactive');
		$('#link_games_full_details').addClass('inactive');
		//$.scrollTo('500px', 500);
		switchList(0, 1, getSortOrder(), getSortDir());
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/Titles_Only');
	}
	e.preventDefault();
	return false;
});

/**
 * capture sort by highest
 */
$('#link_highest_star_rating').attr('href', '').live('click', function(e) {
	if ($(this).hasClass('inactive')) {
		$(this).removeClass('inactive');					// turn on high2low
		flipSortLink('sortAge', 'link_newest_to_oldest', 'Newest to Oldest');	// turn on new2old
		$('#link_newest_to_oldest').addClass('inactive');						// turn off new2old
		switchList(isFullListing(), isTitlesListing(), 'rating', 'DESC');		// sort high2low
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/High_Star');	// track high2low
	}
	else { // active, switch to lowest to highest
		flipSortLink('sortRate', 'link_lowest_star_rating', 'Lowest Star Rating');	// turn on low2high
		switchList(isFullListing(), isTitlesListing(), 'rating', 'ASC');			// sort low2high
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/Low_Star');	// track low2high
	}
	e.preventDefault();
	return false;
});

/**
 * capture sort by lowest
 */
$('#link_lowest_star_rating').attr('href', '').live('click', function(e) {
	flipSortLink('sortRate', 'link_highest_star_rating', 'Highest Star Rating');	// turn on high2low
	switchList(isFullListing(), isTitlesListing(), 'rating', 'DESC');				// sort high2low
	pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/High_Star');	// track high2low
	e.preventDefault();
	return false;
});

/**
 * capture sort by newest to oldest
 */
$('#link_newest_to_oldest').attr('href', '').live('click', function(e) {
	if ($(this).hasClass('inactive')) {
		flipSortLink('sortRate', 'link_highest_star_rating', 'Highest Star Rating');	// turn on high2low
		$('#link_highest_star_rating').addClass('inactive');							// turn off high2low
		$('#link_newest_to_oldest').removeClass('inactive');				// turn on new2old
		switchList(isFullListing(), isTitlesListing(), 'weight', 'DESC');	// sort new2old
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/New_Old');		// track new2old
	}
	else {	// already active, switch to oldest to newest
		flipSortLink('sortAge', 'link_oldest_to_newest', 'Oldest to Newest');	// turn on old2new
		switchList(isFullListing(), isTitlesListing(), 'weight', 'ASC');		// sort old2new
		pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/Old_New');		// track old2new
	}
	e.preventDefault();
	return false;
});

/**
 * capture sort by oldest to newest
 */
$('#link_oldest_to_newest').attr('href', '').live('click', function(e) {
	flipSortLink('sortAge', 'link_newest_to_oldest', 'Newest to Oldest');	// turn on new2old
	switchList(isFullListing(), isTitlesListing(), 'weight', 'DESC');		// sort new2old
	pageTracker._trackPageview('/DYHWIT_2009/Event/Games/Arcade_Sort/New_Old');	// track new2old
	e.preventDefault();
	return false;
});

});
