var gridload;
$(document).ready(function()
{            
	$('a.viewList').click(function(e)
	{                  
		//preventDefault(e);
		//if (CurSeries != this.id)
		//{
			$("a#"+CurSeries).css("cursor","pointer");
			show_list(this.id);
			//while (CurSeries!=this.id){    }
			//$(this).css("cursor","not-allowed");
		//}   
		$('#Listings #ListingsExpanded #grid_title').html($(this).attr("rel"));
		return false;
	});
			  
	$('#top_search').change(function(e)
	{                                       
		$("#top_search option:selected").each(function () 
		{
			if ($(this).attr('rel') == 'no_items')
			{
				alert('Coming soon; Please check back later.');
				return false;
			}
			else
			{
				var book_value = "";
				var book_title = "";

				$("#top_search option:selected").each(function () {
					book_value += $(this).val();
					book_title += $(this).text();
				});
				
				if (book_value != '')
				{
					$("a#"+CurSeries).css("cursor","pointer");
					
					show_list(book_value);

					$('#Listings #ListingsExpanded #grid_title').html(book_title);
				}
				return false;
			}
		});
	});
			
	$('#grid_img').click(function(e)
	{
		if (this.rel && this.rel.length >0)
			window.open(this.rel);
		return preventDefault(e);
	});
			
	$('#closeGrid').click(function()
	{
		if ($('#SeriesExpanded').css('display') == 'block')
			$('#SeriesExpanded').slideUp(1000);
		
		$("a#"+CurSeries).css("cursor","pointer");
		CurSeries = "x";
		
		$('#Listings #ListingsExpanded').slideUp(1000,function()
		{
			hide_all_content_panels();
		});
	});		
 });
 
	
	function show_list(series_id)
	{
		//if (CurSeries != series_id)
		//{                       
			CurSeries = series_id;
			$("#SeriesBottom #Content #loading").show();
			if ($('#SeriesExpanded').css('display') == 'block')
				$('#SeriesExpanded').slideUp(1000);
				
			$('div#grid_spot').slideUp('fast');
			$('#Listings #ListingsExpanded').slideUp(1000,function(){
				show_content_panel("div#grid_spot","#right_grid");
				$('#grid_img').each(function()
				{
					this.src="./_ui/media/no_picture.jpg";
					this.alt="No Image";
					this.title="No Image";
				});
				$("div#grid_spot").html('<table id="grid" width="762" height="400"><tr><td>&nbsp;</td></tr></table>');
				AbortAjax();
				gridload = $.post("setup.php","series_id="+series_id, function(data)
				{
					$('#Listings #ListingsExpanded #grid_title').html(data.title);
					$('#detail_title').text(data.title);
					$("table#grid").flexigrid(data.grid).flexReload();
					$("#SeriesBottom #Content #loading").hide();
					if (data.img) {
						$('#grid_img').fadeOut(function(){
							$this = $(this);
							this.src = "./resize2.php?img="+data.img.src+"&w=95&h=122";
							this.alt = data.img.alt;
							this.title = data.title;
							if (data.img.link) {
								$this.css("cursor","pointer");
								this.rel = data.img.link;
							}
							else {
								this.rel = '';
							}
							$(this).fadeIn('slow');
						});
					}
				
					if (data.user)
						$('.addtoFave').show();
					else
						$('.addtoFave').hide();
						
					html = '';
					UserFaves = new Array();
					$('#right_grid .rightNav').html(html).hide();
					if (data.favourites)
					{
						$('#right_grid .rightNav').html('<div class="yellow_bg">Favorites</div>'); 
						$.each(data.favourites,function()
						{
							html = html + this.li;
							UserFaves.push(this.id);
						});
						$('#right_grid .rightNav').append(html).fadeIn();
					}
					
					gridload = undefined;
				},'json');
				$
				$('#Listings #ListingsExpanded').slideDown(1000);
				$("div#grid_spot").slideDown('fast');	
			});
		//}
		return false;
	}

	var rowload;
	function row_clicked($id,$sID,$fromFave){
		changeItemTitle('Loading...');
		CurItem = $id;
		$x = UserFaves.locate($id);
		
		if (LoggedIn && (!$fromFave && !$x)) 
			$('.addtoFave').show();
		else
			$('.addtoFave').hide();
		
		$('#faveAdded').hide();	
		var url='item_details.php?id='+$id;
		if (typeof $sID != 'undefined')
			url = url + "&amp;sid="+$sID;
		else
			url = url + "&amp;sid="+CurSeries;
		
		$("#SeriesBottom #Content #loading").show();
		$('#row'+$id).removeClass('trSelected');
		$("#SeriesBottom #Content #loading").hide();
		if (rowload) 
			$('#DetailDisplayDialog').jqmHide();
		
		$('#DetailDisplayDialog').attr('rel',url).addClass('jqmWindow').jqm().jqmShow();
		if (typeof $sID != 'undefined')
			return false;
		else
			return true;
	}
	
	function update_popup_title()
	{
		if ($('#DetailsContent').exists())
			if (frames['DetailsContent'].document)
				$('#detail_title').html(frames['DetailsContent'].document.title);
	}
	
	function removeFave(series_id,item_id) {
		$ToSend = new Object();
		$ToSend['action'] = "delfave";
		$ToSend['item_id'] = item_id;
		$ToSend['series_id'] = series_id;
		$.post("user.php",$ToSend,function(data){
			if (data.removed)
			{
				$('div#fav_link_'+series_id+'_'+item_id).fadeOut(function(){
					$('div#fav_link_'+series_id+'_'+item_id).remove();
				});
			}
		},'json');
		return false;
	}
function removeSeriesFaves(series_id) {
	$ToSend = new Object();
	$ToSend['action'] = "delfave";
	$ToSend['series_id'] = series_id;
	$.post("user.php",$ToSend,function(data){
		if (data.removed)
		{
			$('div#fav_series_'+series_id).fadeOut(function(){
				$('div#fav_link_'+series_id).remove();
			});
		}
	},'json');
	return false;
}
