var done = false;
var current_offset = 0;
var last_offset = 0;
var current_sort_by = 'date';
var current_sort_direction = 'desc';
var current_filter_string = 'none';
var last_filter_string = '';
var last_query;
var last_hash = '';
var current_row = '';
var is_loading = false;

$(document).ready(function() {
	if (document.all) {
       $(".nav li").fixHover();
	 }
	
	check_hash(true);
	load_content();
	hash_listener();
	
	$(window).scroll(function(){
		var wh = $(document).height() - $(window).height();
		if  ($(window).scrollTop() >= (wh-400)){
			scroll_handler();
		}
	})
	
});

/* functions */

function set_hash() {
	if (current_sort_direction == 'asc') 
		var hash_direction = 'A';
	else 
		var hash_direction = 'D';
 	
	if (current_sort_by == 'date') 
		var hash_sort_by = 'D';
	
	if (current_sort_by == 'title') 
		var hash_sort_by = 'T';
	
	if (current_sort_by == 'artist') 
		var hash_sort_by = 'N';
	
	if (current_sort_by == 'price') 
		var hash_sort_by = 'P';

	window.location.hash = '#'+hash_sort_by+hash_direction+'/'+current_filter_string+'/'+current_row;
	
	$('#sort_options').val(current_sort_by+'_'+current_sort_direction);
	
	current_hash = last_hash = window.location.hash;
}

function check_hash(ready) {
	var current_hash =  window.location.hash.substr(1);
	if (current_hash.length>2) {
		var sort_temp = current_hash.substr(0,1);
		
		if (sort_temp == 'D') 
			current_sort_by = 'date';
		
		if (sort_temp == 'T')
			current_sort_by = 'title';

		if (sort_temp == 'N') 
			current_sort_by = 'artist';
		
		if (sort_temp == 'P')
			current_sort_by = 'price';
		
		if (current_hash.substr(1,1)=='A')
			current_sort_direction = 'asc';
		else
			current_sort_direction = 'desc';
			
		var temp_split = current_hash.substr(3).split('/');
			
		current_filter_string = temp_split[0].replace(/ /g, '_');
		if (current_filter_string != 'none')	
			$('#filter_string').val(current_filter_string);
		else
			$('#filter_string').val('');
			
		if (temp_split.length>1) {
			current_row = temp_split[1];
		} else {
			current_row = '';
		}	
		
		last_hash = current_hash;
		load_content();
		
		if (ready) {
			set_hash();
		}
	}
}

function check_filter() {
	var filter_string = $('#filter_string').val();
	if (filter_string.length>2 && filter_string != current_filter_string) {
		current_filter_string = filter_string.replace(/ /g, '_');
		done = false;
		current_offset = 0;
		last_offset = 0;
		current_row = '';
		load_content();
		set_hash();
		$('#filter_reset').show();
	}
	if (filter_string.length==0) {
		current_filter_string = 'none';
		done = false;
		current_offset = 0;
		last_offset = 0;
		current_row = '';
		load_content();
		set_hash();
		$('#filter_reset').hide();
	}
}

function reset_filter() {
	$('#filter_string').val('');
	check_filter();
}

function load_content() {
	var my_id = new Date().getTime();

	is_loading = true;
	$('#wrapper').html('<div class="loading" id="loading'+my_id+'"><span style="font-weight: bold; font-size: 11px">Loading&hellip;</span></div>');
	
	if (current_filter_string=='none') {
		var temp_filter_string = artist_slug;
	} else {
		var temp_filter_string = current_filter_string + '_'+artist_slug;
	}
	
	$.getJSON("/ajax/query/"+slug+"_"+artist_slug+"/"+temp_filter_string+"/"+current_sort_by+'_'+current_sort_direction+"/"+"0"+"/"+"4"+'/'+my_id,
		function(data){
			if(data.count != 4)
				done = true;				
			current_offset = data.count;
			is_loading = false;
			
			if (data.count==0) {
				data.html = 'No items match your criteria.';
			}
			
			$('#wrapper #loading'+my_id).html(data.html);		
		}
	);
}

function scroll_handler() {
	if (current_offset != last_offset && !done && !is_loading) {
		is_loading = true;
		
		var my_id = new Date().getTime();
		
		$('#wrapper').append('<div class="loading" id="loading'+my_id+'"><span style="font-weight: bold; font-size: 11px">Loading&hellip;</span></div>');
		
/* 		current_offset += 4; // set for # expected */
		if (current_filter_string=='none') {
			var temp_filter_string = artist_slug;
		} else {
			var temp_filter_string = current_filter_string + '_'+artist_slug;
		}
		
		$.getJSON("/ajax/query/"+slug+"_"+artist_slug+"/"+temp_filter_string+"/"+current_sort_by+'_'+current_sort_direction+"/"+current_offset+"/"+"4"+'/'+my_id,
			function(data){
				is_loading = false;
				if(data.count != 4)
					done = true;
				current_offset += (data.count); // correct for # received
				$('#wrapper #loading'+my_id).html(data.html);
			}
		);
	}
}

function set_sort(sort_by, sort_direction) {
	if (sort_by != current_sort_by || sort_direction != current_sort_direction) {
		current_row = '';
		current_sort_by = sort_by;
		current_sort_direction = sort_direction;
		done = false;
		current_offset = 0;
		last_offset = 0;
		load_content();
		set_hash();
	}
}

var scrolled_to_anchor = true;

function hash_listener() {
	var current_hash =  window.location.hash;
	if (last_hash != current_hash) {
		check_hash();
		last_hash = current_hash;
	}
	
	var found_anchor = false;
	var my_id = new Date().getTime();

	$('#wrapper a').each(function() {
		if (this.name == last_hash.substring(1)) {
			found_anchor = true;
			this.id = 'found_anchor_'+my_id;
		}
	})	


	if (!found_anchor && current_row != '')  {
		scrolled_to_anchor = false;
		if (!done) {
/* 			current_offset += 4; // set for # expected */
			
			if (current_filter_string=='none') {
				var temp_filter_string = artist_slug;
			} else {
				var temp_filter_string = current_filter_string + '_'+artist_slug;
			}
			
			$.getJSON("/ajax/query/"+slug+"_"+artist_slug+"/"+temp_filter_string+"/"+current_sort_by+'_'+current_sort_direction+"/"+current_offset+"/"+"4"+'/'+my_id,
				function(data){
					if(data.count != 4)
						done = true;
					current_offset += (data.count); // correct for # received
					$('#wrapper').append(data.html);
				}
			);
		}
	} else {
		if (found_anchor && !scrolled_to_anchor) {
			$.scrollTo('#found_anchor_'+my_id, 800, {offset:-124});
			scrolled_to_anchor = true;
		}
	}
	
	var filter_string = $('#filter_string').val();
	
	if (filter_string.length>2) {
		$('#filter_reset').show();
	}

	if (filter_string.length==0) {
		$('#filter_reset').hide();
	}
	
	setTimeout('hash_listener()', 500);
}

function click_item(row) {
	current_row = row;
	set_hash();
	return true;
}

function check_select() {
	var v = $('#sort_options').val().split('_');
	set_sort(v[0], v[1]);
}
