$(document).ready(function() {

	// Removes the font-face stylesheet once the google appended one is in to prevent duplication of styles in inspector
	$('link[title=font-face]').remove();


	

	$('.services-box ul li:last-child').addClass('last');

	// Dropdown workaround for IE 7, doesn't work without this
	$('.ie7 .drop').hover(function() {
		$(this).addClass('hover');
	});
	
	$('.fading-articles').cycle({
		timeout: 6000
	});

	$('.wwww').cycle();
	
	
	/*
	 * Contact Iframe
	 */
	var $iframe = $('#iframe');
	//$iframe.hide();
	
	$('ul.offices a').click(function(e) {
		
		//Iframe control
		$iframe.show();
		$iframe.prop("src", $(this).attr("href"));
		
		//UI Feedback
		$("ul.offices li").removeClass("active");
		$(this).parent().parent().parent().addClass("active");
		
		e.preventDefault();
	});
	
	
	
	
	
	/*
	 * Load slider based on body class
	 */
	if($("body").hasClass("home") || $("body").hasClass("residential") || $("body").hasClass("student-living") || $("body").hasClass("commercial") || $("body").hasClass("lender-services")){
		slider()
	}
});





// ================
// = Image Slider =
// ================
function slider(){
	
	var imageCache = [], animating = false, currentItem = 0, autoPlay = false;

	/*
	 * Config  times
	 */
	var transition 			= 300;
	var overlayTransition 	= 400;
	var autoPlayDelay		= 5000;
	
	
	/*
	 * Nodes
	 */
	var $slideWrapper 		= $("#slider");
	var $slideItems			= $slideWrapper.find(".slide");
	var $slideNavItems		= $slideWrapper.find(".slider-nav li");
	
	
	
	// ==============
	// = Initial UI =
	// ==============
	$slideItems.eq(currentItem).addClass("current");
	
	
	// ======================
	// = Autoplay attribute =
	// ======================
	if($slideWrapper.data("autoplay") == true){
		autoPlay = true;
	};
	autoPlayNext();
	
	
	
	// ==================
	// = Preload images =
	// ==================
	$slideItems.find("img").each(function(i, item){
		var img = document.createElement("img");
		img.src = item.src;
		imageCache.push(img);
	})
	

	
	
	// ==========
	// = Events =
	// ==========
	
	 // Cycle through the nav items, attaching hover event handling
	$slideNavItems.each(function(i, item){
		
		var position = i; // Current nav item - also represents current image
		

		
		$slideNavItems.eq(position).mousemove(function() {
			
			
			//Bail out if currently animating
			if(animating || position == currentItem) return;
			
			slideTo(position);
			
			/*
			 * Reset the nav wrapper and add the heavy and current classes
			 */
			var liClass = $slideNavItems.eq(position).attr('class');
			$slideNavItems.eq(position).parent().attr("class", "slider-nav medium");
			$slideNavItems.eq(position).parent().addClass(liClass);
			$slideNavItems.find('a').removeClass('heavy');
			$slideNavItems.eq(position).find("a").addClass('heavy');
		})
	})
	
	
	
	
	function slideTo(newItem) {
		
		//Bail out if currently animating
		if(animating || newItem == currentItem) return;
		
		animating = true;
		
		// Prep z-indexes & visibility
		$slideItems.eq(newItem).addClass("pre_slide");
		
		// Fade out the top item	
		$slideItems.eq(currentItem).fadeOut(transition, function(){
			
			//Show the overlay
			$slideItems.eq(newItem).find(".overlay").fadeIn(overlayTransition, function(){
				
				//Reset the old item
				$slideItems.eq(currentItem).removeClass("current").show().find(".overlay").hide();
				$slideItems.eq(newItem).removeClass("pre_slide").addClass("current");
				
				currentItem = newItem;
				animating = false;
				
				autoPlayNext()
			})
			
		});
		
	}
	
	
	function autoPlayNext () {
		
		if(autoPlay && $slideItems.length > 1){
			setTimeout(function(){
				var next = currentItem+1;
				if(next == $slideItems.length){
					next = 0
				}
		
				slideTo(next);
			}, autoPlayDelay)
			
		}
	}
}
