/* initializing variables */
var left = 0;
var stopLeft = true;
var stopRight = true;
var pause = false;
var feature = "";

$(document).ready(function() {
	$.easing.def = 'easeOutCubic';
	
	//check window width
	if($(window).width()>1280) $('body').addClass("wide");
	
	//newsletter subscription information tooltip
	$("#subscribe a.info[title]").tooltip({ position: 'center right', offset: [9, 7], tipClass: 'subscribe_tip' });
	$(".subscribe_tip").live("hover", function() { $(this).hide(); });
	
	//submit newsletter subscription form
	$(".subscribe").click(function() {
		$("#subscribe form").submit();
		return false;
	});
	$("#subscribe form").submit(function() {
		if(validEmail($("#subscribe input[name=email]").val())) return true;
		else {
			$("#subscribe input[name=email]").css("color","#ff0030");
			return false;
		}
	});
	
	//homepage feature scroller
	if($("#landing_feature").length>0) {
		//preload all background images
		if($('body').hasClass("wide")) $(["/img/1920/feature0.jpg","/img/1920/feature1.jpg","/img/1920/feature2.jpg","/img/1920/feature3.jpg","/img/1920/reports_bg.jpg","/img/1920/leadership_bg.jpg","/news/wp-content/themes/goodman/images/1920/news_bg.jpg"]).preload();
		else $(["/img/1280/feature0.jpg","/img/1280/feature1.jpg","/img/1280/feature2.jpg","/img/1280/feature3.jpg","/img/1280/reports_bg.jpg","/img/1280/leadership_bg.jpg","/news/wp-content/themes/goodman/images/1280/news_bg.jpg"]).preload();
		
		//homepage feature scroller actions
		$(".scrollable").scrollable({circular: true, onSeek: function() { switchBackground(); }}).navigator().autoscroll(18000);
		feature = $(".navi a").last();
		switchBackground();
	}
	
	//homepage featured listings
	if($("#landing_featured").length>0) {
		$("#landing_featured a.listing").click(function() {
			if(lq) return true;
			else {
				var listing = $(this).attr("href");
				var listing_id = listing.substr(listing.indexOf("/",1)+1);
				location.href = "/listings#"+listing_id;
				return false;
			}
		});
	}
	
	//market trend scroller
	if($("#trend_scroller").length>0) {
		$("#trend_scroller").scrollable({circular: true}).autoscroll(6000);
	}
	
	//report archives - show more reports
	if($("#report_landing").length>0) {
		$(".report .more").click(function() {
			$(this).parent().find(".visible").last().next().addClass("visible").slideDown();
			if($(this).parent().find("ul").last().hasClass("visible")) $(this).parent().find(".more").css("visibility","hidden");
			$("html, body").animate({scrollTop:$(document).height()}, "slow");
			return false;
		});
	}
	
	//leadership timeline shifting
	$(".shift_right").hover(function() { stopLeft = false; shiftLeft(); }, function() { stopLeft = true; });
	$(".shift_left").hover(function() { stopRight = false; shiftRight(); }, function() { stopRight = true; });
	
	if($("#contact_form").length>0) {
		//contact form submission
		$("#contact_form .submit a").click(function() { $("#contact_form form").submit(); return false; });
		$("#contact_form form").submit(function() {
			$("label .required.flagged").removeClass("flagged");
			var errors = '';
			if($("input[name=first_name]").val() == "") {
				errors += 'You must enter your first name.<br />';
				$("input[name=first_name]").prev().find(".required").addClass("flagged");
			}
			if($("input[name=last_name]").val() == "") {
				errors += 'You must enter your last name.<br />';
				$("input[name=last_name]").prev().find(".required").addClass("flagged");
			}
			if($("input[name=email_address]").val() == "") {
				errors += 'You must enter your email.<br />';
				$("input[name=email_address]").prev().find(".required").addClass("flagged");
			} else if(!validEmail($("input[name=email_address]").val())) {
				errors += 'You must enter a valid email address.<br />';
				$("input[name=email_address]").prev().find(".required").addClass("flagged");
			}
 
			if(errors=='') {
				var query = 'first_name='+$("input[name=first_name]").val()+'&last_name='+$("input[name=last_name]").val()+'&company='+$("input[name=company]").val()+'&website='+$("input[name=website]").val()+'&email_address='+$("input[name=email_address]").val()+'&phone_number='+$("input[name=phone_number]").val()+'&mailing_address='+$("input[name=mailing_address]").val()+'&city='+$("input[name=city]").val()+'&province='+$("input[name=province]").val()+'&postal_code='+$("input[name=postal_code]").val()+'&country='+$("input[name=country]").val()+'&comments='+$("textarea[name=comments]").val()+'&hear_how='+$("input[name=hear_how]").val()+'&subscribe='+$("input[name=subscribe]").val()+'&buy='+$("input[name=buy]").val()+'&sell='+$("input[name=sell]").val()+'&asset_type='+$("input[name=asset_type]").val()+'&location='+$("input[name=location]").val()+'&size='+$("input[name=size]").val();
				$.post('/inc/process.php',query,function(r) {
					if (r.result == 'success') {
						$("#contact_overlay .text").html(r.message);
						
						//reset form fields
						$("#contact_form .drop").slideUp();
						$(":input").not(":button, :submit, :reset, :hidden").val('').removeAttr('checked').removeAttr('selected');
						$(".checkbox:first").addClass("checked");
						$(".checkbox").not(":first").removeClass("checked");
						$("input[name=subscribe]").val('Yes');
						$("input[name=buy]").val('');
						$("input[name=sell]").val('');
						$("#select").html('');
						$("input[name=asset_type]").val('');
					} else $("#message").html('<span style="color:#ff0030;">An error has occured. Please try again later.</span>');
				$("#contact_overlay").overlay().load();
					//var position = $("#message").position();
					//$("html, body").animate({scrollTop:(position.top-24)});
				},'json');
			} else {
				$("#contact_overlay .text").html('<span style="color:#ff0030;">'+errors+'</span>');
				$("#contact_overlay").overlay().load();
				//var position = $("#message").position();
				//$("html, body").animate({scrollTop:(position.top-24)});
			}
			return false;
		});
		
		//checkboxes
		$("span.checkbox").click(function() {
			if($(this).hasClass("checked")) {
				$(this).removeClass("checked");
				$(this).next().val("");
			} else {
				$(this).addClass("checked");
				$(this).next().val("Yes");
			}
			
			//contact form extension
			if($("#contact_form input[name=buy]").val()=="Yes") $("#contact_form .drop").slideDown(function() { $("html, body").animate({scrollTop:$(document).height()}, "slow"); });
			else if($("#contact_form input[name=sell]").val()=="Yes") $("#contact_form .drop").slideDown(function() { $("html, body").animate({scrollTop:$(document).height()}, "slow"); });
			else $("#contact_form .drop").slideUp();
			
			return true;
		});
	
		//dropdown functionality
		$("#asset_wrapper").hover(function() {
			if(!pause) $("#asset_wrapper ul#options").slideDown();
		}, function() {
			pause = true;
			$("#asset_wrapper ul#options").slideUp(function() { pause = false; });
		});
		$("#asset_wrapper #options li a").click(function() {
			pause = true;
			$("#contact_form input[name=asset_type]").val($(this).html());
			$("#asset_wrapper #select").html($(this).html());
			$("#asset_wrapper ul#options").slideUp(function() { pause = false; });
			return false;
		});
	
		//contact form overlay
		$("#contact_overlay").overlay({ expose:{ color:'#000000', loadSpeed:150, opacity:0.85 }, top: 200, left: "center", fixed: true });
	}
	
	//map overlay
	if($("#map .expand").length>0) {
		$("#map_overlay").overlay({ expose:{ color:'#000000', loadSpeed:150, opacity:0.85 }, top: "center", left: "center", fixed: false });
		$("#map .map").hover(function() { $("#map .expand").addClass("hover"); },function() { $("#map .expand").removeClass("hover"); });
		$("#map .map").click(function() { $("#map .expand").click(); return false; });
		$("#map .expand").click(function() {
			$("#map_overlay").overlay().load();
			return false;
		});
	}
	
	if($(".leadership_left").length>0) {
		//adjust width for leadership_left div
		var cur_width = 672;
		var add_width = ($(window).width()-986)/2;
		if(add_width>0) $(".leadership_left").width(cur_width+add_width);
		$(".leadership_left .border_top").width(cur_width+add_width-26);
		//$(".leadership_left ul#subnav li").css("padding-left",add_width);
		
		//adjust top margin for divs below #leadership_content
		var margin = 40-14;
		$(".leadership_callout").each(function() {
			margin += $(this).height();
		});
		margin = $("#leadership_content").height()-margin;
		if($("#team_summary").length>0) $("#team_summary").css("margin-top",margin+12+"px");
		else if(margin>0) $("#footer").css("margin-top",margin+12+"px");
	}
	
	//adjust width for far_left div
	if($(".far_left").length>0) {
		var padding = $(".far_left .inner").css("paddingRight");
		var cur_width = $(".far_left .inner").width()+parseInt(padding.substring(0,padding.length-2));
		var add_width = ($(window).width()-986)/2;
		if(add_width>0) $(".far_left").width(cur_width+add_width);
		
		if($("#reports_intro").length>0 || $("#contact_intro").length>0 || $("#fourohfour_intro").length>0) {
			//adjust top margin for divs below #leadership_content
			var margin = $(".far_left").height()+31;
			if($("#report_landing").length>0) $("#report_landing").css("margin-top",margin-$(".reports_callout").height()+"px");
			else if($("#contact_form").length>0) $("#contact_form").css("margin-top",margin+97+"px");
			else if($("#fourohfour_intro").length>0) $("#footer").css("margin-top",margin+97+"px");
			else $("#footer").css("margin-top",margin+"px");
		}
	}
	
	if($("body").height()<$(window).height()-84) $("body").height($(window).height()-84);
});

$(window).resize(function() {
	//check window width
	if($(window).width()>1280) $('body').addClass("wide");
	else if($('body').hasClass("wide")) $('body').removeClass("wide");
	
	//switch feature background image
	if($('body').hasClass("feature0") || $('body').hasClass("feature1") || $('body').hasClass("feature2") || $('body').hasClass("feature3")) {
		var background = $(".navi a.active").attr("href");
		if($('body').hasClass("wide")) $('body').css("background-image","url(/img/1920/"+background+".jpg)");
		else $('body').css("background-image","url(/img/1280/"+background+".jpg)");
	}
	
	if($(".leadership_left").length>0) {
		//adjust width for leadership_left div
		var cur_width = 672;
		var add_width = ($(window).width()-986)/2;
		if(add_width>0) $(".leadership_left").width(cur_width+add_width);
		$(".leadership_left .border_top").width(cur_width+add_width-26);
		//$(".leadership_left ul#subnav li").css("padding-left",add_width);
		
		//reset timeline position
		left = 0;
		$("#timeline .items").css("left",left);
	}
	
	//adjust width for far_left div
	if($(".far_left").length>0) {
		var padding = $(".far_left .inner").css("paddingRight");
		var cur_width = $(".far_left .inner").width()+parseInt(padding.substring(0,padding.length-2));
		var add_width = ($(window).width()-986)/2;
		if(add_width>0) $(".far_left").width(cur_width+add_width);
	}
});

// switch landing background
function switchBackground() {
	var cur_feature = $(".navi a.active");
	if(cur_feature.attr("href")==feature.attr("href")) setTimeout('switchBackground()',100);
	else {
		var background = $(".navi a.active").attr("href");
		if(background.indexOf("/content")>-1) {
			$('body').removeClass("feature0").removeClass("feature1").removeClass("feature2").removeClass("feature3");
			$('body').css("background-image","url("+background+")");
		} else {
			$('body').removeClass("feature0").removeClass("feature1").removeClass("feature2").removeClass("feature3");
			$('body').addClass(background);
			if($('body').hasClass("wide")) $('body').css("background-image","url(/img/1920/"+background+".jpg)");
			else $('body').css("background-image","url(/img/1280/"+background+".jpg)");
		}
		feature = $(".navi a.active");
	}
}

//validate email address
function validEmail(email) {
	if(email.length>0) {
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (emailPattern.test(email) == false) return false;
		else return true;
	} else return false;
}

//shift leadership timeline to the left
function shiftLeft() {
	if(!stopLeft && ($("#timeline .items").width()+left)>=$(window).width()) {
		$("#timeline .items").css("left",left);
		left -= 5;
		setTimeout('shiftLeft()',25);
	} else stopLeft = true;
}

//shift leadership timeline to the right
function shiftRight() {
	if(!stopRight && left<=0) {
		$("#timeline .items").css("left",left);
		left += 5;
		setTimeout('shiftRight()',25);
	} else stopRight = true;
}

//image preloading
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
