(function($) {
	$(function() {
		$(document).ready(function() {
			if($("#accordion")) { $("#accordion").accordion({ autoHeight: false, navigation: true, collapsible: true, active: false }); }

			//Trophies
			if($('#trophy_right')) {
				var rows = $('.trophy_row').length - 1;
				var position = 0;
				$('#trophy_box_wrap').css("width", (rows + 1) * 462);

				$('#trophy_right').click(function() {
					if(position < rows) {
						position++;
						$('#trophy_box_wrap').animate({"left": "-" + position * 462 + "px"}, 1000);
					}
				});

				$('#trophy_left').click(function() {
					if(position <= rows && position != 0) {
						position--;
						$('#trophy_box_wrap').animate({"left": "-" + position * 462 + "px"}, 1000);
					}
				});
			}


			if($('.answer_click').length > 0) {
				var first = $('.answer_click').get(0);
				var firstid = $(first).attr('id').substr(1);
				$(first).addClass('active_q');
				$('#a' + firstid).show();

				$('.answer_click').click(function(){
					var id = $(this).attr('id').substr(1);
					$('.answer_click').removeClass('active_q');
					$(this).addClass('active_q');
					$('.answer_hide').hide();
					$('#a' + id).show();
				});
			}


			//State Selector
			if($('#state_select')) {
				var select  = $('#state_select').find('select').get(0);
				$(select).change(function() {
					var state = $(this).val();
					var statestr = serverurl + '/retailers/?state=' + state;
					window.location = statestr;
				});
			}

			//Firearm Selector
			if($('#firearm_type')) {
				$($('#firearm_type')).change(function() {
					var type = $(this).val();
					if(type) { window.location = serverurl + '/load-data/?type=' + type;
					} else { window.location = serverurl + '/load-data'; }
				});
			}


			//Cartridge Selector
			if($('#cartridge_type')) {
				$($('#cartridge_type')).change(function() {
					var type = $.getUrlVar('type');
					var view = $(this).val();
					if(type) { window.location = serverurl + '/load-data/?type=' + type + '&view=' + view;
					} else if (view) { window.location = serverurl + '/load-data/?view=' + view;
					} else { window.location = serverurl + '/load-data'; }
				});
			}

		});
	});


	$.extend({
		getUrlVars: function(){
			var vars = [], hash;
			var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
			for(var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; }
			return vars;
		},
		getUrlVar: function(name){ return $.getUrlVars()[name]; }
	});
})(jQuery);

