
var portfolio = {
	max_h : 0,
	imgs : Array(),
	imgCount: 0,
	currentImg: 0,
	run : 1,
	runOnce : 0,
	marqueeTop : 0,
	init : function ( ) {
		$("#larger").css("height", "auto");
		portfolio.marqueeTop = ($("#larger").offset().top);
		$("#larger li").slice(0,1).css("display","block");
		$("#larger li").slice(1).css("display","none");
		var i = 0;
		$("#larger img").each(function (i) {
				portfolio.imgCount++;
				portfolio.imgs[i] = new Image();
				$(portfolio.imgs[i]).load(function(){portfolio.counter(-1);});
				portfolio.imgs[i].src = this.src;
				portfolio.max_h = ( this.height > portfolio.max_h ) ? this.height : portfolio.max_h;
			}
		);
		if ( $("#larger").height() < portfolio.max_h ) {
			$("#larger").height(portfolio.max_h);
		}
		if ( $("#larger").height() < $("#thumbs").height() ) {
			$("#larger").height($("#thumbs").height())
		}
		$("#thumbs a").each(function(i){
			this.onclick = new Function( "return portfolio.change(" + i + ")" );
		});
	},
	change : function ( i ) {
		portfolio.run = 0;
		$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).fadeOut(1000);
		if ( ( $(window).scrollTop() > portfolio.marqueeTop ) ) {
			if ( ( $("#larger").height() + portfolio.marqueeTop ) >= ( $("#larger li").slice(i,i+1).height() + ($(window).scrollTop() ) ) ) {
				$("#larger li").slice(i,i+1).css("top",$(window).scrollTop()-portfolio.marqueeTop);
			} else {
				$("#larger li").slice(i,i+1).css("top", $("#larger").height()-$("#larger li").slice(i,i+1).height() );
			}
		} else {
			$("#larger li").slice(i,i+1).css("top",0);
		}
		$("#larger li").slice(i,i+1).fadeIn(1000);
		$("#thumbs li").slice(portfolio.currentImg,portfolio.currentImg+1).removeClass("current");
		$("#thumbs li").slice(i,i+1).addClass("current");
		portfolio.currentImg = i;
		return false;
	},
	counter : function ( n ) {
		portfolio.imgCount += n;
		if ( portfolio.imgCount == 0 && portfolio.runOnce == 0 ) {
			portfolio.runOnce = 1;
			setTimeout( "portfolio.slideShow()", 6000 );
			$("#larger a").lightBox();
		}
	},
	slideShow : function ( ) {
		if ( portfolio.run == 1 ) {
			var last = portfolio.currentImg;
			if ( $("#larger li").length-1 <= portfolio.currentImg ) {
				portfolio.currentImg = 0;
			} else {
				portfolio.currentImg++;
			}
			$("#larger li").slice(last,last+1).fadeOut(1000);
			if ( ( $(window).scrollTop() > portfolio.marqueeTop ) ) {
				if ( ( $("#larger").height() + portfolio.marqueeTop ) >= ( $("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).height() + ($(window).scrollTop() ) ) ) {
					$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).css("top",$(window).scrollTop()-portfolio.marqueeTop);
				} else {
					$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).css("top", $("#larger").height()-$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).height() );
				}
			} else {
				$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).css("top",0);
			}
			$("#larger li").slice(portfolio.currentImg,portfolio.currentImg+1).fadeIn(1000);
			$("#thumbs li").slice(last,last+1).removeClass("current");
			$("#thumbs li").slice(portfolio.currentImg,portfolio.currentImg+1).addClass("current");
			setTimeout( "portfolio.slideShow()", 6000 );
		}
	}
}

var contactForms = {
	agentCheck : function ( ) {
		var fields = {	"First_Name":"First Name",
						"Last_Name":"Last Name",
						"Agency":"Agency",
						"City":"City",
						"State":"State",
						"Phone":"Phone",
						"Email":"Email Address"
					};
		var msg = "";
		for ( field in fields ) {
			if ( document.getElementById(field).value == "" ) {
				msg += "Please enter your " + fields[field] + "\n";
			}
		}
		if ( msg ) {
			alert( msg );
			return false;
		}
		return true;
	},
	contactCheck : function ( ) {
		var fields = {	"First_Name":"First Name",
						"Last_Name":"Last Name",
						"Phone":"Phone",
						"Email":"Email Address"
					};
		var msg = "";
		for ( field in fields ) {
			if ( document.getElementById(field).value == "" ) {
				msg += "Please enter your " + fields[field] + "\n";
			}
		}
		if ( msg ) {
			alert( msg );
			return false;
		}
		return true;
	}
}

$(document).ready( function( ) {
	var portfolioSlides = document.getElementById("thumbs");
	if ( portfolioSlides ) {
		portfolio.init();
	}
	$("#client-gallery a").lightBox();
});

