$(document).ready(function() {


		
		/*
		--------------------------------------------------------------
		Sort Button Functions
		--------------------------------------------------------------
		*/
		
		$(".sort_options").hide();
		$(".open_options").show();
		
		$(".get_preset").hide();
		var set_contact = $(".get_preset").attr("preset");
		var show_options = $(".get_preset").attr("show_options");
		
		if (show_options == "false") {
			$(".combo_box").hide();
		}
		
		if (set_contact != "") {
			
			var set_child_num = Number(set_contact);
			var set_child = $(".sort_options").children("#selections").children("p:nth-child("+set_child_num+")");
			var new_text = set_child.text();
			var new_email = set_child.attr("email");
			$(".sort_btn").children("p").text(new_text);

			$("input.contact").attr("value",new_email);
		}
		
		$(".sort_btn").click(function() {
		
			if ($(this).attr("selected") == "true") {
				
				$(this).next().slideUp("fast");
				$(this).css("background-position-y","-24px");
				$(this).attr("selected", "false");
			} else {
			
				resetSorts();
					
				$(".sort_options").slideUp("fast");
				$(".sort_options").attr("selected", "false");
				
				$(this).next().slideDown("fast");
				$(this).css("background-position-y","0px");
			
				$(this).attr("selected", "true");
			}
		});
		
		function resetSorts() {
			
			$(".sort_btn").each( function() { 
	
				$(this).next().slideUp("fast");
				$(this).css("background-position-y","-24px");
				$(this).attr("selected", "false");
			});
		}
		
		$(".sort_options").children("#selections").children("p").click(function() {
			
			var new_text = $(this).text();
			var new_email = $(this).attr("email");
			$(".sort_btn").children("p").text(new_text);
			resetSorts();
			
			$("input.contact").attr("value",new_email);
		});
	    
	    
		/*
		$(".sort_options").each(function() {
			
			var num_children = $(this).children("#selections").children("a").length;
			
			if ($(this).hasClass("open_options")) {
				if (num_children < 13) {
				
					$(this).css("height", Number((num_children*24)-3));
					$(this).children("#selections").children("a").css("width",145);
					$(this).children("#drag_container").hide();
				}
			} else {
				if (num_children < 6) {
				
					$(this).css("height", Number((num_children*24)-3));
					$(this).children("#selections").children("a").css("width",145);
					$(this).children("#drag_container").hide();
				}
			}
		});
		*/

	    
	    var scrollContent = '';

    	function getScrollContent(o) {
    		sc = $(o).parent().prev();
    		
    		sch = sc.height();
    		par = $(o).parent().parent();
    		parh = par.height();
    		miny = parh - sch;
    		maxy = 0;
    		sbh = parh - $(o).height();
		}

		
		$(".sort_options").children("#drag_container").children("#dragger").draggable({
			
			axis: 'y',
			containment: 'parent',
			
			start: function(event, ui) {
				getScrollContent( this );
			},
			
		   drag: function(event, ui) {
			   
			   var percent = $(this).position().top/sbh;
			   var newPos = (miny * percent);
			   sc.css("top",newPos);
		   }
		});
		
		/*
		--------------------------------------------------------------
		Sort Button Functions
		--------------------------------------------------------------
		*/
		
	});
