/*Utilities*/
Array.prototype.getNext = function(entry) {//Gets array value after 'entry', used in ajaxLoad
	for (var i = 0; (this[i] != entry) && (i < this.length); i++){
		return (i >= (this.length - 1))?false:this[i + 1];	
	}	
};
var section = function() {//Sets 'section' to current section (residential, commercial, or real estate)
	var matches = location.pathname.match(/\/(residential|commercial|realestate)\//);
	return matches?matches[0].replace(/\//g,''):'';
}();

/*Ajax (for forms or links)*/
function ajaxLoad() {//Pulls classname after ajax on links or forms and inserts ajax call to that id
	var target = $(this).attr('class').split(' ').getNext('ajax');
	if (target) {
		var isForm = ($(this)[0].nodeName == 'FORM');
		var url = $(this).attr(isForm?'action':'href') || location.pathname;
		$('#' + target).load(url + ((url.search(/\?/) == -1)?'?':'&') + 'ajax=1' + (isForm?('&' + $(this).serialize()):''));
	}
	return !target;
}
function ajaxInit() {
	$('a.ajax').live('click',ajaxLoad);
}

/*External Links*/
function externalInit() {
	$('.external').live('click',function() {
		window.open($(this).attr('href'));
		return false;
	});
}

/*Gallery*/
function galleryStripSlide(dir) {//Moves the gallery strip
	var pages = Math.ceil($('#galleryStripSlider').children().length / 4);
	var thisPage = Number($('#galleryStripSlider').css('left').replace('px','')) / -524;
	var newPage = thisPage + dir;
	if ((newPage < pages) && (newPage >= 0)) {
		$('#galleryStripSlider').css('left',String((thisPage + dir) * -524) + 'px');
	} else {
		newPage = thisPage;
	}
	if (newPage > 0) {
		$('#galleryStripLeft').show();
	} else {
		$('#galleryStripLeft').hide();
	}
	if (newPage < (pages - 1)) {
		$('#galleryStripRight').show();
	} else {
		$('#galleryStripRight').hide();
	}
}
function galleryInit() {
	$('#galleryStripLeft').live('click',function() {//Make slide left
		galleryStripSlide(-1);
	});
	$('#galleryStripRight').live('click',function() {//Make slide right
		galleryStripSlide(1);
	});
	$('.galleryThumbLink').live('click',function() {//Opens full size
		var src = $('img',this).attr('src').replace('thumb','full');
		var alt = $('img',this).attr('alt');
		$('#galleryFull img').attr('src',src);
		$('#galleryFull img').attr('alt',alt);
		$('#galleryFullTitle').html(alt);
		return false;
	});
}

/*Navigation*/
function navInit() {
	$('#topNav li').hover(function() {
		$(this).addClass($(this).attr('id') + 'Hover');
	},function() {
		$(this).removeClass($(this).attr('id') + 'Hover');
	});
	$('#subNav li a:not(.current)').hover(function() {
		$(this).addClass('hover');
	},function() {
		$(this).removeClass('hover');
	});
}

/*Quotes*/
function quoteInit() {
	setInterval(function() {
		var currentItem;
		var firstItem = false;
		var nextItem = false;
		$('#quoteBlock div').each(function() {
			if (firstItem === false) {
				firstItem = $(this);
			}
			if (nextItem === true) {
				nextItem = $(this);
			} else if ($(this).hasClass('current')) {
				currentItem = $(this);
				nextItem = true;
			}
		});
		if (nextItem === true) {
			nextItem = firstItem;
		}
		currentItem.removeClass('current');
		currentItem.fadeOut(1000,function() {
			nextItem.fadeIn(1000,function() {
				nextItem.removeClass('hidden');
				nextItem.addClass('current');
			});
		});
	},5000);
}

/*Image Rotator*/
function rotatorInit(){
	$(".index_rotator").load(imageLoaded).hide();
	var curr = $(".index_rotator:first");	
	
	curr.load(function(){
		$(this).fadeIn();	
	});

	
	var indexBoundary = $(".index_rotator").length;
	var currIndex = 0;
	setInterval(function(){
		currIndex++;
		if(currIndex >= indexBoundary){
			currIndex = 0;	
		}
		
		var curr = $(".index_rotator:visible");
		var next = $(".index_rotator:eq("+currIndex+")");
		
		
		curr.fadeOut("normal", function(){
			next.fadeIn("normal");	
		});
		
			
	}, 7000);
}

function imageLoaded(){
	var height1 = $(this).height();
	var height2 = $("#rotator_container").height();
	var padding = (height2-height1)/2;
	$(this).css("margin-top", padding);
	
}

$(function() {
	ajaxInit();
	externalInit();
	galleryInit();
	navInit();
	quoteInit();
	rotatorInit();
});