var Counties = {
  
  	'slideshowState' : 'active',
	'lastInd' : 0,
	'restoreSlideShowInd' : 0,
	
	//BIND PAGE EVENTS
	'bindEvents' : function(){
		$("#counties-list").livequery('change',      this.changeCounty);
		$(".slideshow-thumb").livequery('mouseover', this.freezeImage);
		$(".slideshow-thumb").livequery('mouseout',	 this.antifreeze);
        $(".slideshow-thumb").livequery('click',     this.stopImage);
	},
	
	'changeCounty': function() {
		if ($(this).attr('value'))
			window.location = '/counties/desc/' + $(this).attr('value');
	},

	'navigate': function() {
		window.location = $(this).attr('href');
	},

	'freezeImage': function() {
		ind = $(this).attr('image_ind');
		Counties.changeImage(ind);
    },
	
	'antifreeze': function() {
		Counties.changeImage(Counties.lastInd);
	},
    
	'stopImage': function() {
		ind = $(this).attr('image_ind');
		Counties.lastInd = ind;
		Counties.changeImage(ind);
	},
	
	'changeImage': function(ind) {
		var $active = $('#slideshow IMG.active');
		$active.removeClass('active');
		var $toactive = $('#slideshow IMG[image_ind='+ind+']');
		$toactive.addClass('active');
		Counties.slideshowState = 'stopped';
	},
	
	'slideSwitch': function () {
		if (Counties.slideshowState != 'active' && Counties.restoreSlideShowInd < 4) {
			Counties.restoreSlideShowInd++;
			return;
		}
		
		if( $('#slideshow IMG').length < 2 )
			return;
		
		Counties.slideshowState = 'active';
		Counties.restoreSlideShowInd = 0;
	    var $active = $('#slideshow IMG.active');
	    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
	    var $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');
	    $active.addClass('last-active');
	    $next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            $active.removeClass('active last-active');
	        });
	}

};

$(document).ready(function(){
	window.setInterval( "Counties.slideSwitch()", 6000 );
	Counties.bindEvents();
});