var global_curcat;

function loadnav( id, url, s, m, c )
{
	var mode = url.lastIndexOf( '=' ) == url.length - 1 ? false : true;
	var oe = document.getElementById( id );
	for( var x = 1; x <= s*m; x++ )
	{
		var oo = document.createElement('option');
		oo.value = url;
		if( mode && x > 0 )
			oo.value += '/index' + x + '.html';
		else if( !mode )
			oo.value += x;
		oo.innerHTML = x;
		if( x == c )
			oo.selected = true;
		oe.appendChild( oo );
	}
}

function selectLoadedCatetory( cat )
{
	global_curcat = cat;
}

function load_cat_pt()
{
	if(document.getElementById('cat_player')/* && document.getElementById('cat_team')  && document.getElementById('player_team_or')*/)
	{
		var cat = global_curcat;
		
		// code for Mozilla, etc.
		if (window.XMLHttpRequest)
		{
			catpt_xmlhttp=new XMLHttpRequest()
		}
		// code for IE
		else if (window.ActiveXObject)
		{
			catpt_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		
		if (catpt_xmlhttp!=null)
		{
			catpt_xmlhttp.onreadystatechange=cat_pt_state_chg
			catpt_xmlhttp.open("GET",'/cat_players_and_teams.php?keep_https=true' + (cat==undefined?'':'&cate=' + cat),true)
			catpt_xmlhttp.send(null)
		}
		else
		{
			alert("Your browser does not support XMLHTTP.")
		}
	}
}

function cat_pt_state_chg()
{
	if (catpt_xmlhttp.readyState==4 && catpt_xmlhttp.status==200)
	{
		//var a = catpt_xmlhttp.responseText.split( /---------------------------------------------------/ );
		//document.getElementById('cat_player').innerHTML += a[0];
		//document.getElementById('cat_team').innerHTML +=  a[1];
		eval( catpt_xmlhttp.responseText );
/*
		if(document.getElementById('cat_player') && document.getElementById('cat_team')  && document.getElementById('player_team_or'))
		{
			var dropdown_threshold = 1;
			if(document.getElementById('cat_player').length<=dropdown_threshold && document.getElementById('cat_team').length<=dropdown_threshold && document.getElementById('player_team_narrow_box'))
			{
				document.getElementById('player_team_narrow_box').style.display = 'none';
			}
			else
			{
				if (document.getElementById('cat_player').length<=dropdown_threshold)
				{
					document.getElementById('cat_player').style.display = 'none';
					document.getElementById('player_team_or').style.display = 'none';
				}
				if(document.getElementById('cat_team').length<=dropdown_threshold)
				{
					document.getElementById('cat_team').style.display = 'none';
					document.getElementById('player_team_or').style.display = 'none';
				}
			}
			
		}*/
	}
}

function load_cat_pt_main()
{
	var cat = global_curcat;
	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
	{
		catpt_xmlhttp2=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject)
	{
		catpt_xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if (catpt_xmlhttp2!=null)
	{
		catpt_xmlhttp2.onreadystatechange=cat_pt_state_chg2
		catpt_xmlhttp2.open("GET",'/cat_players_and_teams.php?keep_https=true',true)
		catpt_xmlhttp2.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
}

function fill_drop_scroll( drop_scroll_elements, json_data, url_prefix, selected_option_element_name, group_by_cat )
{
	if( drop_scroll_elements.length > 0 && json_data.length > 0 )
	{
		var lastCat = '';
		$.each( json_data, function(name, value) {
			if( !this.show || this.show & 1 )
			{
				var name = this.name ? this.name : this.category;
				var url = this.search ? this.search : this.cdseo_url;
				var pcat = this.pcategory ? this.pcategory : this.category;
				var html = [];
				if( group_by_cat )
				{
					if( lastCat != pcat )
						html.push( "<a href='javascript: void(0);' style='color: #0000dd; font-weight:bold;'>--",
						pcat.replace( "'", "\\'" ),
						"--</a>" );
					lastCat = pcat;
				}
				html.push( "<a href=\"/",
				url_prefix,
				"/",
				encodeURIComponent( url ),
				"\" onclick=\"$('#",
				selected_option_element_name,
				"').html('",
				name.replace( "'", "\\'" ),
				"');\">",
				name,
				"</a>" );
				html = html.join( "" );
				drop_scroll_elements.each( function( ind, ele ) { $(this).append( html ); } );
			}
		});
	}
}

function cat_pt_state_chg2()
{
	if (catpt_xmlhttp2.readyState==4 && catpt_xmlhttp2.status==200)
	{
		var my_to_load = JSON.parse(catpt_xmlhttp2.responseText);
		fill_drop_scroll( $('#player_drop_scroll, #player2_drop_scroll'), my_to_load[0], player_path_name, 'player_selected_option', false );
		fill_drop_scroll( $('#team_drop_scroll, #team2_drop_scroll'), my_to_load[1], team_path_name, 'team_selected_option', true );
		fill_drop_scroll( $('#item_drop_scroll, #item2_drop_scroll'), my_to_load[2], cdseo_cat, 'item_selected_option', true );
		if( $('#MLB_drop').length > 0 )
		{
			//filling the header menus on the home page
			//teams
			$.each(my_to_load[1], function(index, value) {
				if( this.show & 2 )
				{
					var cat = this.category.replace( " Memorabilia", "" );
					var menu = $('#' + cat + '_drop');
					var lists = $('ul', menu);
					if( lists.length > 3 )
					{
						var list = lists[0];
						if( $(list).children('li').length >= 20 )
							list = lists[1];
						if( $(list).children('li').length >= 20 && cat == 'College' )
							list = lists[2];
						if( $(list).children('li').length < 20 )
						{
							var li = document.createElement( 'li' );
							var a = document.createElement( "a" );
							a.href = '/' + team_path_name + '/' + this.search;
							$(a).html( this.name );
							$(li).append( a );
							$(list).append( li );
						}
					}
				}
			});
			//players
			$.each(my_to_load[0], function(index, value) {
				if( this.show & 2 )
				{
					var cat = this.category.replace( " Memorabilia", "" );
					var menu = $('#' + cat + '_drop');
					var lists = $('ul', menu);
					var listind = 2;
					if( cat == 'College' )
						listind = 3;
					else if( lists.length < 4 )
						listind = 0;
					var list = lists[listind++];
					if( $(list).children('li').length >= 20 && lists.length > listind )
						list = lists[listind++];
					if( $(list).children('li').length >= 20 && lists.length > listind )
						list = lists[listind++];
					if( $(list).children('li').length < 20 )
					{
						var li = document.createElement( 'li' );
						var a = document.createElement( "a" );
						a.href = '/' + player_path_name + '/' + this.search;
						$(a).html( this.name );
						$(li).append( a );
						$(list).append( li );
						
						if( listind == 5 && $(list).children('li').length == 20 )
							$(list).append( "<li><a rel='nofollow' href='/player-memorabilia.html'><i>View All Players</i></a></li>" );
					}
				}
			});
			
			$('#NFL_drop, #MLB_drop, #NBA_drop, #NHL_drop, #College_drop').each( function( index, element ) {
				var lists = $('ul', this);
				var done = false;
				$.each( lists, function( listindex, list ) {
					if( listindex > ( index == 4 ? 2 : 1 ) && !done && $(list).children('li').length < 20 )
					{
						$(list).append( "<li><a rel='nofollow' href='/player-memorabilia.html'><i>View All Players</i></a></li>" );
						done = true;
					}
				});
			});
		}
	}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function objects_per_page(x)
{
	document.getElementById('items_page_form_x').value = x;
	document.getElementById('items_page_form').submit();
}

function addslashes( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // *     example 1: addslashes("kevin's birthday");
    // *     returns 1: "kevin\'s birthday"
 
    return str.replace('/\'/g', "&#39;")
}
catpt_xmlhttp=null;
