$(document).ready(function(){
var auto_change_occurred = false;
$("select.cat_nav_sort_links").change(function(){

   	val = $(this).val().split(":");
	if (val[1] && auto_change_occurred == true) {
		window.location.href = val[1];
	}
	else {
		$("#category_results").load('/content_php/content_four_column_sort.php?s=' + val[0], '', function(){
			$("#category_results").show();
		});
		
		hash = '#' + val[0];
		
		//If sorting by popular, set the page hash to be blank 
		// and clear navigation hashes.
		if (hash.indexOf('pop') != -1) {
			//window.location.hash = '';
			// Clear Page Navigation Hashes
			$('ul.cat_nav_pages li a').each(function(i){
				h = this.href.lastIndexOf('#');
				if (h == -1) {
					this.href = this.href;
				}
				else {
					this.href = this.href.substr(0, h);
				}
				
			})
		}
		else {
			//window.location.hash = hash;
			// Update Page Navigation Links With Current Hash
			$('ul.cat_nav_pages li a').each(function(i){
				h = this.href.lastIndexOf('#');
				if (h == -1) {
					this.href = this.href + hash;
				}
				else {
					this.href = this.href.substr(0, h) + hash;
				}
				
			})
			//Set yellow background on selected element
			//$("select.cat_nav_sort_links option:selected").css("background-color", "#FAFAD4");
			//$("select.cat_nav_sort_links").css("background-color", "#FAFAD4");
		};
			}
    });

if (window.location.hash.indexOf('pl') != -1) {
    $('#sort_pl').attr("selected", true);
    $("select.cat_nav_sort_links").val($('#sort_pl').val());
    $("select.cat_nav_sort_links").trigger("change");
	$('div.cat_nav div.left').show();
}

if (window.location.hash.indexOf('ph') != -1) {
    $('#sort_ph').attr("selected", true);
    $("select.cat_nav_sort_links").val($('#sort_ph').val());
    $("select.cat_nav_sort_links").trigger("change");
	$('div.cat_nav div.left').show();
}
auto_change_occurred = true;
});
