// pos of image gallery
var panTime = 4000;
var captionDelay = 2000;
var captionFadeTime = 1000;
var fadeOutTime = 1000;
var fadeInTime = 1000;
var waitTime = 2000;
var animating = false;

// function to make color bullets
function makeColorBullets(i,e,bulletClass,textClass) {
	
	$(e).addClass(bulletClass);
	$(e).find("li").wrapInner("<span class=" + textClass + " />");
	
}

// highlight
function toggleHighlight(i,e) {
	
	var myURL = $(e).find("img").attr("originalSRC");

	if (!$(e).hasClass("selected")) {
		var newURL = myURL.substr(0,myURL.lastIndexOf(".")) + "-on" + myURL.substr(myURL.lastIndexOf("."),myURL.length);
		$(e).addClass("selected");
	} else { 
		var newURL = myURL;
		$(e).removeClass("selected");
	}
	
	$(e).find("img").attr("src",newURL);
	
}


// function for hovers
function makeHover(i,e) {
	
	var myURL = $(e).find("img").attr("src");
	$(e).find("img").attr("originalSRC",myURL);
	
	if (myURL.indexOf("-on.") == -1) {
		
		var myOnURL = myURL.substr(0,myURL.lastIndexOf(".")) + "-on" + myURL.substr(myURL.lastIndexOf("."),myURL.length);
	
		$(e).hover(function() {
			if (!$(e).hasClass("selected")) $(e).find("img").attr("src",myOnURL);
		},function(){
			if (!$(e).hasClass("selected")) $(e).find("img").attr("src",myURL);
		});
		
	}
	
}

$.fn.scrollify = function() {

	// set up
	var myRate = 10;
	var myPadding = parseInt($("#logos table tr td:first").css("paddingLeft")) + parseInt($("#logos table tr td").css("paddingRight"));
	var myPosition = $("#logos table tr td").position();
	if (myPosition != null) {
		var myLeft = myPosition.left - ($("#logos table tr td").width() + myPadding);
		var myDuration = ($("#logos table tr td").width() + myPadding) * myRate;

		// hide first element
		$("#logos table").animate({marginLeft:myLeft}, myDuration, "linear", function() {
		
			// move first element to end of row
			$("#logos table tr td:first").appendTo("#logos table tr");
			$("#logos table").css("marginLeft",0);
			$("#logos").scrollify();
		
		});
	}


}

$.fn.fadify = function() {
	
	$('#logos table:first').fadeIn().delay(4000).fadeOut(function() { 
		$('#logos table:first').appendTo('#logos');
		$('#logos').fadify();
	});

}

$.fn.panning = function(pos) {
	
	var myFadeInTime = 0;
	var img;
	var cap;
	var myInt = 0;
	var animAtt = "";
	
	if (pos == -1) myFadeInTime = 300;
	
	$("#hero").fadeIn(fadeInTime,function(){

		// handle old image and caption
		if (pos > -1) {
			
			var oldImg = $("#hero").find("img:eq(" + pos + ")");
			var oldCap = $("#hero").find("div.caption:eq(" + pos + ")");
			
			$(oldImg).fadeOut(fadeOutTime,function() {
				$(oldImg).attr("style","");
			});
			
			$(oldCap).fadeOut(fadeOutTime,function() {
				$(oldCap).attr("style","");
			});
			
		}			
		
		// increment pos
		pos++;
		
		// check exceeding of count
		if (pos == $("#hero").find("img").length) pos = 0;
		
		// handle new image and caption
		img = $("#hero").find("img:eq(" + pos + ")");
		cap = $("#hero").find("div.caption:eq(" + pos + ")");
		
		if ($(img).hasClass("up") || $(img).hasClass("down")) {
			if ($(img).hasClass("up")) animAtt = "bottom";
			else animAtt = "top";
			myInt = -1 * ($(img).height() - 328);
		} else {
			if ($(img).hasClass("left")) animAtt = "right";
			else animAtt = "left";
			myInt = -1 * ($(img).width() - 957);
		}
		
		myAnimObj = new Object();
		myAnimObj[animAtt] = myInt;

		// reset opacity and display
		$(img).animate({opacity:0},0);
		$(img).css("display","block");
		
		$(img).animate({opacity:1},{queue:false, duration:fadeInTime}).animate(myAnimObj,panTime,"easeInOutQuad",function(){
			setTimeout("$('#hero').panning(" + pos + ")",waitTime);
		});
		$(cap).delay(captionDelay).fadeIn(captionFadeTime);
				
	});
	
}

$.fn.checkForms = function() {
	
	if ($("#portfolio-body").hasClass("rental")) {
		$(".rental-form").fadeIn();
	} else {
		$(".condo-form").fadeIn();
	}
	
}


$(document).ready(function() {
	
	$("[rel=lightbox]").fancybox({
		titlePosition	: 'inside',
		centerOnScroll	: true,
		overlayOpacity	: 0.6
	});
	
	$(".popup-page").fancybox({
		centerOnScroll	: false,
		overlayOpacity	: 0.6
	});
	
	$("ul#nav a").hover(function(){
		$(this).css("background-color",$(this).css("border-bottom-color"));
	},function(){
		if (!$(this).hasClass("selected")) $(this).css("background-color","transparent");
	});
	
	$("a.portfolio-plan").fancybox({
		titlePosition	: 'inside',
		centerOnScroll	: true,
		overlayOpacity	: 0.6
	});
	
	$("a.portfolio-image").fancybox({
		titlePosition	: 'inside',
		centerOnScroll	: true,
		overlayOpacity	: 0.6
	});
	
	$('a.portfolio-next').bind("click",function(e){
		
		e.preventDefault();
		
		// get gallery
		var myGallery = $(this).siblings(".portfolio-gallery-hold").find(".portfolio-gallery")
	
		// get pos and total
		if ($(myGallery).attr("pos") == undefined) $(myGallery).attr("pos","0");
		var pos = $(myGallery).attr("pos");
		var total = $(myGallery).children("a").length;
		var distance = $(myGallery).children("a:eq(0)").width();
		var newLeft = 0;
	
		// are we at the end?
		if (pos < total - 1) {
		
			// increment pos
			pos++;
			newLeft = -(distance) * pos;
	
		} else {
		
			// reset position
			pos = 0;
		
		}
		
		// animate
		$(myGallery).animate({left:newLeft});
	
		// set new pos
		$(myGallery).attr("pos",pos);
		
	});

	$('a.portfolio-prev').bind("click",function(e){
		
		e.preventDefault();

		// get gallery
		var myGallery = $(this).siblings(".portfolio-gallery-hold").find(".portfolio-gallery")
	
		// get pos and total
		if ($(myGallery).attr("pos") == undefined) $(myGallery).attr("pos","0");
		var pos = $(myGallery).attr("pos");
		var total = $(myGallery).children("a").length;
		var distance = $(myGallery).children("a:eq(0)").width();
		var newLeft = -(distance*(total-1));
	
		// if we can still move
		if (pos > 0) {
		
			// decrement pos
			pos--;
			newLeft = -(distance) * pos;
		
		} else {
		
			// reset pos to end
			pos = total - 1;
		
		}

		// animate
		$(myGallery).animate({left:newLeft});

		// set new pos
		$(myGallery).attr("pos",pos);		
		
	});
	
	
	$("#feature-props a").hover(function(e) {
		e.preventDefault();
		$(this).children("div").fadeIn(300);
	},function(e) {
		e.preventDefault();
		$(this).children("div").fadeOut(300);
	});
	
	$("#hero").panning(-1);

	$("#contact-type").bind("change",function() {
		$("input[name=submission]").val("0");
		$("form").submit();
	});
	
	$("form").bind("submit",function() {
		
		var errorText = "";
		
		if ($("[name=submission]").val() == 1) {
		
			if ($("[name=contact-name]").val() == "") errorText += "You must enter a name.\n";
			if ($("[name=contact-phone]").val() == "") errorText += "You must enter a phone number.\n";
			if ($("[name=contact-email]").val() == "") errorText += "You must enter an email address.\n";
			
		}
		
		if (errorText == "") {

			return true;

		} else {

			alert(errorText);
			return false;
		
		}
			
		
	});
	
	$("#portfolio-body").checkForms();
	
	$(".floorplans").find("a").each(function(i,e){
	
		var myCaption = $(e).attr("title");
		$(e).find("img").after('<div class="white-caption">' + myCaption + '</div>');
		
	});
	
});
