PokerListings.modules.TableFinder =
{
	run: function()
	{
		PokerListings.modules.TableFinder.hijaxForm();
		PokerListings.modules.TableFinder.hijaxLinks();
	}, 

	hijaxForm: function()
	{
		var form = $('#tf-rttf form');

		form.submit(function()
		{
			var	room = $('select[name="room"]', form).val(), 
				game = $('select[name="game"]', form).val(), 
				type = $('select[name="type"]', form).val(), 
				num_players = $('select[name="num_players"]', form).val(), 
				you_what = $('select[name="you_what"]', form).val(), 
				sort = $('input[name="sort"]', form).val();

			$.post(
				'/modules/table-finder-mod.php', 
				{
					room: room, 
					game: game, 
					type: type, 
					num_players: num_players, 
					you_what: you_what, 
					sort: sort
				}, 
				function(data)
				{
			 		$('#tf-rttf').html(data);
					PokerListings.modules.TableFinder.run();
				}
			);

			return false;
		});
	}, 

	hijaxLinks: function()
	{
		$('#tf-rttf a[href^="?sort"]').click(function()
		{
			$.get('/modules/table-finder-mod.php' +$(this).attr('href'), function(data)
			{
		 		$('#tf-rttf').html(data);
				PokerListings.modules.TableFinder.run();
			});

			return false;
		});
	}
};/* used to display table finder results */

function showPage( page, urlStr, onFirstPage, design )
{

	$("div#extra-info").hide();	
	
	$("div#loader").show();
	
	if( ! urlStr )
	{
		urlStr = '';
				
		$("#table-finder select").each(function() {
			if( $(this).val() )
			{
				urlStr += '&' + $(this).attr('name') + '=' + $(this).val().replace( '+', '%2b' );
			}
		});
		
		if( ! page )
			page =  $("input[@name=page]").val();
		
		urlStr += '&page' + '=' +  page;
		
		urlStr += '&sort' + '=' +  $("input[@name=sort]").val();
		
		urlStr += '&advanced' + '=' +  $("input[@name=advanced]").val();
	}

	urlStr += '&design=' + design;

	//$("div#table-finder-content").fadeTo("slow", 0.01);

	
	ajaxUrl = '/ajax/table-finder/search';
	
	if ( onFirstPage == 'tool')
	{
		ajaxUrl += '-tool';
	}
	else if( onFirstPage )
	{
		ajaxUrl += '-fp';
	}
	
	$.get( ajaxUrl + "?" + urlStr, function( data ){
		$("div#table-finder-content").html( data );
		//$("div#table-finder-content").fadeIn("slow");
		$("div#loader").hide();
		
		$("th").mouseover(function() {
			
			oldTitle = '';
			
			if( $(this).attr('title') )
			{
				
				$('<div id="extra-info"></div>').appendTo('body').hide();
				
				$("div#extra-info").css( "min-height", "30px" );
				$("div#extra-info").css( "height", "30px" );
				$("div#extra-info").css( "top", (mouseY + 5) + "px" );
				$("div#extra-info").css( "left", (mouseX - 5) + "px" );
				$("div#extra-info").show();
				$("div#extra-info").html(  $(this).attr('title') );
				
				oldTitle =  $(this).attr('title');
				
				$(this).attr('title', '' );
			}
			
		});
		
		$("th").mouseout(function() {
			if( typeof(oldTitle) != 'undefined' && oldTitle )
			{
				$(this).attr( 'title', oldTitle );
				$("div#extra-info").hide();
			}
			
		});
		
		
		$("th").mousemove(function() {
			$("div#extra-info").css( "top", (mouseY + 5) + "px" );
			$("div#extra-info").css( "left", (mouseX + 15) + "px" );	
		});
		
	});
	
	return false;
}

function tableInfo( contentId, tableData, isTool )
{
	if($.browser.msie)
	{
		$('#tablefinder-page #content select').hide();
	}

	if(document.getElementById('table-finder-info'))
	{
		$('#table-finder-info').remove();
	}
	
	$('<div id="table-finder-info" class="stone-box">Loading...</div>').appendTo('body').hide();
	
	$("div#loader").show();
	
	ajaxUrl = '/ajax/table-finder/info';
	
	if( isTool )
	{
		ajaxUrl += '-tool';
	}
	
	$.get(ajaxUrl + "?content_id=" + contentId + "&table_data=" + tableData, function( data ){
		
	if( isTool )
	{	
		$("#table-finder-info").html( data ).fadeIn("slow");
	}
	else
	{
		$("#table-finder-info").html( data ).show().center();
	}

		$('div#loader').hide();

		$('a.table-finder-info-close').click(function()
		{
			$("#table-finder-info").remove();
			
			if($.browser.msie)
			{
				$('#tablefinder-page #content select').show();
			}

			return false;
		});
	});
	
	return false;
}

function nextPage()
{
	page = parseInt( $("input[@name=page]").val() );
	return showPage( page + 1 );
}

function prevPage()
{
	page = parseInt( $("input[@name=page]").val() );
	return showPage( page - 1 );	
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;

// Variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0
document.onmousemove = getMouseXY;
// Main function to retrieve mouse x-y pos.s

function getMouseXY(e)
{
  if( IE && document.documentElement && document.documentElement.scrollTop ) // Explorer 6 Strict
  {
    mouseX = event.clientX + document.documentElement.scrollLeft;
    mouseY = event.clientY + document.documentElement.scrollTop;
  }
  else if( IE ) // grab the x-y pos.s if browser is IE
  {
    mouseX = event.clientX + document.body.scrollLeft;
    mouseY = event.clientY + document.body.scrollTop;
  }
  else  // grab the x-y pos.s if browser is NS
  {
  	mouseX = e.pageX;
    mouseY = e.pageY;
  }
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0;}
  if (mouseY < 0){mouseY = 0;}

  return true;
}function calcage(secs, num1, num2)
{
	s = ((Math.floor(secs/num1))%num2).toString();
	
	if(s.length < 2)
	{
		s = "0" + s;
	}

	return s;
}

// Count down timers in "Live" Top-Lists
function countDown()
{
	var clock_time = new Date();
	var clock_hours = clock_time.getHours();
	var clock_minutes = clock_time.getMinutes();
	var clock_seconds = clock_time.getSeconds();

	var secs = parseInt(clock_hours, 10)*3600 + parseInt(clock_minutes, 10)*60 + parseInt(clock_seconds, 10);

	var hours = calcage(secs, 3600, 365);
	var minutes = calcage(secs, 60, 60);
	var seconds = calcage(secs, 1, 60);

	$(countDownWhere).html(hours +':' +minutes +':' +seconds);
	
	setTimeout("countDown()", 1000);
}var next = 0;
var changer;
var targetBox;

var nextPromo = function(i,obj) {
	
	targetBox = $("#fp-whats-hot").find('p');

	$("#fp-whats-hot").find('ol>li').each(function(j) {

		if ( this == obj )
		{
			next = j;
			i = j;
		} 
		
		if ( i == j ){
			$(this).addClass('sel');
			targetBox.html( $(this).html() );
			targetBox.find('img').show();
		} else {
			$(this).removeClass('sel');
		}
		
		var getId = function() {
			return j;
		}
	})
	
	next++;
	
	if( next > 6 ){
		next = 0;
	}
}

$(document).ready(function() {
	changer = setInterval( 'nextPromo(next)', 2000);
	
	$("#fp-whats-hot").find('p').mouseover(function(i) {
		clearInterval( changer );
    }).mouseout(function(i) {
		changer = setInterval( 'nextPromo(next)', 2000);
	})

	$("#fp-whats-hot").find('ol>li').mouseover(function(i) {
		clearInterval( changer );
		nextPromo( -1, this );
    }).mouseout(function(i) {
		changer = setInterval( 'nextPromo(next)', 2000);
	})
});$(document).ready(function() {
	
	var re = /([_\-\w]+$)/i;
	
	// Choose all .flaps exept the one with id #review-flaps
	$('ul.flaps').not('#review-flaps').each(function() {
		
		var container = this;

		var activateTab = function(clear,targetTab) {

			$(container).find('li a').each(function(i) {

				thisTab = re.exec(this.href)[1];

				var contentDiv = $('#' + thisTab );
			
				if( thisTab == targetTab || ( i==0 && clear == false ) ){
					firstParent = this.parentNode;
					$(firstParent.parentNode).addClass('sel');
					contentDiv.removeClass('hideContent');
				}else{
					firstParent = this.parentNode;
					$(firstParent.parentNode).removeClass('sel');
					contentDiv.addClass('hideContent');
				}
			
	        });
		}
		
		activateTab(false,false);
		
		$(this).find('li a').click(function(i) {
			
			activateTab(true,re.exec(this.href)[1]);

			return false;
			
		})
	})
});


$(document).ready(function() {
	toggleSpecificTabs('ul.n-flaps, ul.arrow-flaps, ul.n-s-flaps, ul.il-flaps')
});



function toggleSpecificTabs(tabString) {
	var re = /([_\-\w]+$)/i;
	
	$(tabString).each(function() {
		
		var container = this;

		var activateTab = function(clear,targetTab) {

			$(container).find('li a').each(function(i) {

				thisTab = re.exec(this.href)[1];
				
				var contentDiv = $('#' + thisTab );
			
				if( thisTab == targetTab || ( i==0 && clear == false ) ){
					firstParent = this.parentNode;
					$(firstParent).addClass('tabs-selected');
					contentDiv.removeClass('tabs-hide');
				}else{
					firstParent = this.parentNode;
					$(firstParent).removeClass('tabs-selected');
					contentDiv.addClass('tabs-hide');
				}
			
	        });
		}
		
		activateTab(false,false);
		
		$(this).find('li a').click(function(i) {
			
			activateTab(true,re.exec(this.href)[1]);

			return false;
			
		})
	})

}


// feature to toggle to a specific tab vi aa url call
// http://www.pokerlsitings/market-pulse/biggest-pots#tab_2
function evaluateTabRequest()
{
	var url = location.href;
	var fragment = url.split('#');
	if (fragment[1])
	{
		if (fragment[1].indexOf('tab_') != -1)
		{
			var tabIndex = fragment[1].replace('tab_', '');
			return parseInt(tabIndex);
		}
	}
	return false;
}

function displaySelectedTab ( selectedTab )
{
	$('.n-s-flaps > li').each(function(){ 
		$(this).removeClass();
	});
	$( "#" + selectedTab ).addClass("tabs-selected");
	
}


