jQuery(function() {	
	//thumbnails flip
	$('.flip').quickFlip();
	
	$.each($('.handler'), function(){
		if ($.browser.msie) {
			$(this).addClass('handler-ie-hack');
		}
	
		$(this).mouseenter(function(){
			rotate($(this));
		});
		
		$(this).mouseleave(function(){
			rotate($(this));
		});
	});

	function rotate(item) {
		item.parents('.page-thumb').addClass('no-shadow');
		var target = item.parents('.page-thumb-rotate').find('.flip');
		target.quickFlipper({
			closeSpeed : 200,
			openSpeed : 200
		});
	}
	
	var enter = true;
	$('#thumb-first').addClass('thumb-hover');
	$('#sherlock-top').addClass('sherlock-change');
	
	//thumbnails hover
	$('.page-thumb').hover(function() {
		if (enter) {
			$('#thumb-first').removeClass('thumb-hover');
			$('#sherlock-top').removeClass('sherlock-change');
		}
		
		$(this).toggleClass('thumb-hover');
	});
	$('#thumb-first').hover(function() {
		if (enter) {
			enter = false;
		}
		
		$('#sherlock-top').toggleClass('sherlock-change');
	});
	
	//timer thumbnails flip
	var thumbs = $('.handler');
	var rotated = 5;
	
	var time = new Date().getTime();
	$(document.body).bind("mousemove keydown DOMMouseScroll mousewheel mousedown touchstart touchmove", function(e) {
		if (rotated != '') {
			rotate($(thumbs[rotated]));
			rotated = '';
		}
		
		time = new Date().getTime();
	});

	var active = true;
	function refresh() {
		if (active && new Date().getTime() - time >= 30000) {
			if (rotated != 5) {
				rotate($(thumbs[rotated]));
			}
			
			var index = Math.floor(Math.random() * thumbs.length);
			rotate($(thumbs[index]));
			rotated = index;
			
			setTimeout(refresh, 5000);
		} else {
			setTimeout(refresh, 5000);
		}
	}

	$(window).blur(function() {
		time = new Date().getTime();
		active = false;
	});
	
	$(window).focus(function() {
		time = new Date().getTime();
		active = true;
	});
	
	setTimeout(refresh, 5000);
	
	//search bar
	jQuery("#form-submit").click(function(){
		if (jQuery("#s").val().length > 0 && jQuery("#s").val() != "Search for Businesses or Services") {
			content = jQuery("#s").val().replace(/ /g, "+"); 
			if (jQuery("#s2").val().length > 0 && jQuery("#s2").val() != "City, State or Zip") {
				content += "-in-" + jQuery("#s2").val().replace(",", "-").replace(/ /g, "+");
			}
		} else if (jQuery("#s2").val().length > 0 && jQuery("#s2").val() != "City, State or Zip") {
			content = "in-" + jQuery("#s2").val().replace(",", "-").replace(/ /g, "+");
		} else {
			return false;
		}
		
		window.location = "http://www.powersites.net/" + content + ".html";
		return false;
	});
});
