var HomeSlider = {
  
  	'slideshowState' : 'active',
	'lastInd' : 0,
	'restoreSlideShowInd' : 0,
	
	//BIND PAGE EVENTS
	'bindEvents' : function(){
		$(".slideshow-thumb").livequery('mouseover', this.freezeImage);
		$(".slideshow-thumb").livequery('mouseout',	 this.antifreeze);
        $(".slideshow-thumb").livequery('click',     this.stopImage);
	},
	
	'freezeImage': function() {
		ind = $(this).attr('id').match(/\d+$/);
		HomeSlider.changeImage(ind);
    },
	
	'antifreeze': function() {
		HomeSlider.changeImage(HomeSlider.lastInd);
	},
    
	'stopImage': function() {
		//alert($(this).attr('id') + ' - click');
		ind = $(this).attr('id').match (/\d+$/);
		HomeSlider.lastInd = ind;
		HomeSlider.changeImage(ind);
	},
	
	'changeImage': function(ind) {
		var active = $('.slideshow.active');
		active.removeClass('active');
		var toactive = $('#slide_'+ind+'.slideshow');
		$('.slideshow-thumb').removeClass('active');
		$('#slide_thumb_' + ind).addClass('active');
		toactive.addClass('active');
		HomeSlider.slideshowState = 'stopped';
		$.cookies.set('homeslide', ind);
	},
	
	'slideSwitch': function () {
	
		if ($('#floatbox-box') && $('#floatbox-box').is(':visible')) return false;
		if (HomeSlider.slideshowState != 'active' && HomeSlider.restoreSlideShowInd < 2) {
			HomeSlider.restoreSlideShowInd++;
			return;
		}
		
		HomeSlider.slideshowState = 'active';
		HomeSlider.restoreSlideShowInd = 0;
	    var active = $('.slideshow.active');
	    if ( active.length == 0 ) active = $('.slideshow:last');
	    var next =  active.next().length ? active.next() : $('.slideshow:first');
	    active.addClass('last-active');
	    $('.slideshow-thumb').removeClass('active');
	    $('#slide_thumb_' + next.attr('id').match(/\d+$/)).addClass('active');
	    next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            active.removeClass('active last-active');
	        });
	    $.cookies.set('homeslide', next.attr('id').match(/\d+$/));
	}

};

$(document).ready(function(){
	window.setInterval( "HomeSlider.slideSwitch()", 6000 );
	HomeSlider.bindEvents();

	if (null != $.cookies.get('homeslide')) {
		HomeSlider.changeImage($.cookies.get('homeslide'));
	} else {
		HomeSlider.changeImage(Math.floor(Math.random() * 3));
	}
	HomeSlider.slideshowState = 'active';
});
