/* stop IE 5/6 flicker */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

/* execute on document load (jquery) */
$(document).ready(function(){
	
	// Add rollover to our home page button
	$("#paint_button").each(function(i) {
		// switch our png with gif on home page only for IE5/6
		if ($.IE6Below()) {
			try { this.src = this.src.substring(0,this.src.lastIndexOf('.')) + ".gif"; } catch(err) {};
		};
		
		// load rollover state
		var theImgOver = new Image();
		var suffix = this.src.substring(this.src.lastIndexOf('.'));
		theImgOver.src = this.src.substring(0,this.src.lastIndexOf('.')) + "_over" + suffix;
		
		$(this).mouseover(function() {
			this.src = this.src.substring(0,this.src.lastIndexOf('.')) + "_over" + this.src.substring(this.src.lastIndexOf('.'));
		});
		$(this).mouseout(function() {
			this.src = this.src.substring(0,this.src.lastIndexOf('.')-5) + this.src.substring(this.src.lastIndexOf('.'));
		});
	});
	
	// Change logo to gif for ie5/6
	$("#logo img").each(function(i) {
		if ($.IE6Below()) {
			try { this.src = this.src.substring(0,this.src.lastIndexOf('.')) + ".gif"; } catch(err) {};
		};
	});
	
	// If the height of <body> is less than the viewport then increase content div so footer is at bottom
	$.placeFooter = function() {
		if ($("body").height() < $.viewportHeight() && !($("body#home").length)) {
			var currentContentHeight = $("#content").height(); // current height of #content
			var differenceHeight = $.viewportHeight() - $("body").height(); // difference between body and viewport
			$("#content").css("min-height", currentContentHeight + differenceHeight + "px"); // difference added to existing height
			// ie 5/6 version
			if ($.IE6Below()) {
				$("#content").height(currentContentHeight + differenceHeight + "px"); // difference added to existing height
			};
		};
	};
	
	// Clear data and replace footer
	$.rePlaceFooter = function() {
		if (!$("body#home").length)	{ // if it's not the home page
			// reset current values
			$("#content").css("height","auto"); //reset values
			$("#content").css("min-height","190px"); //reset values
			// ie 5/6 version
			if ($.IE6Below()) {
				var currentContentHeight = $("#content").height(); // current height of #content
				var differenceHeight = $.viewportHeight() - $("body").height(); // difference between body and viewport
				$("#content").height(currentContentHeight + differenceHeight + "px");
			};
			// Re-Place our footer
			$.placeFooter();
			try {clearTimeout(rePlaceFooterAction);} catch(e){};
		};
	}
	
	// Place our footer
	$.placeFooter();
	
	// On window resize, replace footer
	window.onresize = function () {
		try {clearTimeout(rePlaceFooterAction);} catch(e){};
		rePlaceFooterAction = setTimeout('$.rePlaceFooter()',100);
	};
	
	// Add print version of logo
	logoPrintVersion = $("#logo img").clone(); // use clone instead of new img() for safari
	$(logoPrintVersion).attr({ src: "/images/layout/logo_print.gif", alt: "Designated Tree", id: "logo_print" });
	$(logoPrintVersion).appendTo("body");
	
});


// Return true if browser is Internet Explorer and version 6 or earlier
$.IE6Below = function() {
	if ($.browser.msie) {
		try {
			if (ScriptEngineMajorVersion() <= 5 && ScriptEngineMinorVersion() < 7) { // ie 6- only
				return true;
			}
		} catch(err) { return false; };
		return false;
	} else {
		return false;
	};
}

// Return the available content height space in browser window
$.viewportHeight = function() {
	var h = 0;
	if (typeof(window.innerHeight) == "number") {
		h = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			h = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				h = document.body.clientHeight;
			};
		};
	};
	return h;
};



// tabbed content
$.fn.tabs = function(options) {
	// basic stuff
	var ON_CLASS = 'show';
	var OFF_CLASS = 'hidden';
	var MAX_NUMBER_OF_IMAGES = 3; // this should reflect the amount of images we have for each building
	// options
	var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on : 1;
	this.each(function() {
		$(this).find('>div').not(':eq(' + on + ')').addClass(OFF_CLASS);
		$(this).find('.building_images').addClass(OFF_CLASS);
		$(this).find('.building1').removeClass(OFF_CLASS);
		$(this).find('.building1').addClass(ON_CLASS);
		$(this).find('.switch_navigation').removeClass(OFF_CLASS);
		$(this).find('#explore_buildings').removeClass(OFF_CLASS);
		$(this).find('.switch_navigation>a:eq(' + parseInt(on-1) + ')').addClass(ON_CLASS);
		var container = this;
		$(this).find('.switch_navigation>a').click(function() {
			if ($(this).is('#arial-view')) {
				$(".direction").css("display","none");
			}
			if (!$(this).is('.' + ON_CLASS)) {
				var re = /([_\-\w]+$)/i;
				var target = $('#' + re.exec(this.href)[1]);
				var target2 = $('.' + re.exec(this.href)[1]);
				if (target.size() > 0) {
					$(container).find('>div:visible').addClass(OFF_CLASS);
					$(container).find('#explore_buildings').removeClass(OFF_CLASS);
					$(container).find('.building_images').removeClass(ON_CLASS);
					$(container).find('.building_images').addClass(OFF_CLASS);
					$(container).find('.switch_navigation').addClass(ON_CLASS);
					target.removeClass(OFF_CLASS);
					target2.removeClass(OFF_CLASS);
					target2.addClass(ON_CLASS);
					$(container).find('.switch_navigation>a').removeClass(ON_CLASS);
					$(this).addClass(ON_CLASS);
					// hide buttons
					$(".buttonset1").css("display","none");
					$(".buttonset1 .rendering, .buttonset1 .floorplan").removeClass("active");
					// floorbplans
					$(".building_floorplans").each(function(i) {
						if ($(this).hasClass("show")) {
							$(this).removeClass("show");
							$(this).addClass("hidden");
						};
					});
				} else {
					//alert('No data for this.');
				};
			};
			return false;
		});		
		$(this).find('.building_images_navigation_box').each(function(i) { // go through each of our building_images_navigation_box divs
			var container2 = this;

			$(this).find("a").click(function() { // add this click to each of the href's inside this building_images_navigation_box div
				if (!$(this).is('.' + ON_CLASS)) {
					var re = /([_\-\w]+$)/i;
					var re2 = /^\w+/i;
					var re3 = /\w+$/i;
					var targetdiv = $('.' + re2.exec(re.exec(this.href)[0])[0]); // when hash is #building1-3 it matches "building1"
					var targetimg = $(targetdiv).find("img")[re3.exec(this.href)[0]-1]; // #buidling1-3 matches "3" (3-1 for array index)
					$(targetdiv).find("img").removeClass(ON_CLASS);
					$(targetdiv).find("img").addClass(OFF_CLASS);
					$(targetimg).removeClass(OFF_CLASS);
					$(targetimg).addClass(ON_CLASS); // turn this image on based on second part of hash
					$(container2).find('a').removeClass(ON_CLASS);
					$(this).addClass(ON_CLASS);
					$(targetdiv).find('.availability-available, .availability-sold, .availabitliy-limited').removeClass(OFF_CLASS);
				};
				return false;
			});
			
			$(this).find(".enlarge_image").click(function() { // change "enlarge" link to call thickbox on the current viewable image
				// remove click border
				this.blur();
				// get current viewable image
				var viewableimglink = $($(this).parent().parent().find("img.show")[0]).parent()[0];
				// get caption: either title or name attribute
				var caption = viewableimglink.title || viewableimglink.name || "";
				// get rel attribute for image groups
				var group = viewableimglink.rel || false;
				// display the box for the elements href
				TB_show(caption, viewableimglink.href, group);
			});

		});
		
		// find which are sold/available add our sold/available images overlays
		$(this).find('.switch_navigation>a').each(function(i) {
			var re = /([_\-\w]+$)/i;
			var target = $('#explore_buildings ' + '.' + re.exec(this.href)[1]);
			if ($(this).is('.sold')) {
				if (target.size() > 0) {
					if (target.is('.building_images')) {
						target.append("<div class='availability-sold'></div>");
					}					
				}
			} else if ($(this).is('.available')) {
				if (target.size() > 0) {
					if (target.is('.building_images')) {
						target.append("<div class='availability-available'></div>");
					}
				}
			} else if ($(this).is('.limited')) {
				if (target.size() > 0) {
					if (target.is('.building_images')) {
						target.append("<div class='availability-limited'></div>");
					}
				}
			}
		});
		
	});
	// if there is a hash in the form of #building1-1 then load that image enlarged
	this.each(function() {
		if (document.location.hash && !(document.location.hash == "" || document.location.hash == undefined) && document.location.hash.length > 1) {
			var re = /([_\-\w]+$)/i;
			var re2 = /^\w+/i;
			var re3 = /\w+$/i;
			var orghash = re.exec(document.location.hash)[0];
			var hash1 = re2.exec(orghash)[0];
			var hash2 = parseInt(re3.exec(orghash)[0]-1);
			var container = this;
			var target = $('#'+hash1);
			var target2 = $('.'+hash1);
			if (target.length > 0 && target2.length > 0 && hash2 < MAX_NUMBER_OF_IMAGES) {
				$(container).find('>div:visible').addClass(OFF_CLASS);
				$(container).find('#explore_buildings').removeClass(OFF_CLASS);
				$(container).find('.building_images').removeClass(ON_CLASS);
				$(container).find('.building_images').addClass(OFF_CLASS);
				$(container).find('.switch_navigation').addClass(ON_CLASS);
				target.removeClass(OFF_CLASS);
				target2.removeClass(OFF_CLASS);
				target2.addClass(ON_CLASS);
				target2.find("img").removeClass(ON_CLASS);
				target2.find("img").addClass(OFF_CLASS);			
				$(target2.find("img")[hash2]).removeClass(OFF_CLASS);
				$(target2.find("img")[hash2]).addClass(ON_CLASS);
				target2.find(".building_images_navigation_box a").removeClass(ON_CLASS);
				$(target2.find(".building_images_navigation_box a")[hash2]).addClass(ON_CLASS);
				$(container).find('.switch_navigation>a').removeClass(ON_CLASS);
				$('.switch_navigation>a').each(function(i){
					var re = /([_\-\w]+$)/i;
					if (re.exec(this.href)[0] == hash1) {
						$(this).addClass(ON_CLASS);
					}
				});
				// launch enlarged view...
				$.launchEnlarge = function() {
					// get current viewable image
					var viewableimglink = $(target2.find("img")[hash2]).parent()[0];
					// get caption: either title or name attribute
					var caption = viewableimglink.title || viewableimglink.name || "";
					// get rel attribute for image groups
					var group = viewableimglink.rel || false;
					// display the box for the elements href
					TB_show(caption, viewableimglink.href, group);
				};
				enlargetimeout = setTimeout('$.launchEnlarge()',500);
			};
		};
	});
};

/* execute on document load (jquery) */
// do site map stuff
$(document).ready(function(){
	$(".view-site-plan").click(function(){
		$("#site-plan-wrapper").css("display","block");
		$("#explore_buildings").css("margin-bottom","190px");
		//$("#default_text").css("display","block");
		$(".direction").css("display","block");
		if ($.IE6Below()) {
			iebodyheight = $("body").height();
			$("#darkness").css("display","none");
			$("body").append('<div id="ie-dark"></div>');
			$("#site-plan-wrapper .site-map-base").appendTo("body");
			$("#site-plan-wrapper").append('<div id="site-map-base-placeholder"></div>');
			$("#ie-dark").css({display:'block',height:iebodyheight});
			$(".site-map-base").css("display","block");
		}
		var ON_CLASS = 'show';
		var OFF_CLASS = 'hidden';
		//$("#tabswitch_box_wrapper>div:visible").not("#explore_buildings,#default_text").addClass(OFF_CLASS);
	});
	/*$("#site-plan-wrapper").click(function(){
		$(this).css("display","none");
		$("#explore_buildings").css("margin-bottom","11px");
		$("#default_text").css("display","none");
	});*/
	$('.map-link').hover(function() {
		$("#"+$(this).attr("rel")).css("display","block");
	},function() {
		$("#"+$(this).attr("rel")).css("display","none");
	});
	$("#close-map").click(function() {
		$(".view-site-plan").trigger('click');
		$("#site-plan-wrapper").css("display","none");
		$("#explore_buildings").css("margin-bottom","11px");
		$("#darkness-ie").remove();
		if ($.IE6Below()) {
			$("#ie-dark").css("display","none");
			$(".site-map-base").css("display","none");
		}
		//$("#arial-view").removeClass("show");
		$(".direction").css("display","none");
		return false;
	});
	$('.map-link').click(function() {
			var re = /([_\-\w]+$)/i;
			var target = $('#' + re.exec(this.href)[1]);
			var target2 = $('.' + re.exec(this.href)[1]);
			var container = $('div#tabswitch_box_wrapper');
			var ON_CLASS = 'show';
			var OFF_CLASS = 'hidden';
			if (target.size() > 0) {
				$(container).find('>div:visible').addClass(OFF_CLASS);
				$(container).find('#explore_buildings').removeClass(OFF_CLASS);
				$(container).find('.building_images').removeClass(ON_CLASS);
				$(container).find('.building_images').addClass(OFF_CLASS);
				$(container).find('.switch_navigation').addClass(ON_CLASS);
				target.removeClass(OFF_CLASS);
				target2.removeClass(OFF_CLASS);
				target2.addClass(ON_CLASS);
				$(container).find('.switch_navigation>a').removeClass(ON_CLASS);
				// show buttons
				$(".buttonset1").css("display","block");
				$(".buttonset1 .rendering").addClass("active");
				$(".buttonset1 .floorplan").removeClass("active");
				// floorbplans
				$(".building_floorplans").each(function(i) {
					if ($(this).hasClass("show")) {
						$(this).removeClass("show");
						$(this).addClass("hidden");
					};
				});
			} else {
				//alert('No data for this.');
			};
			orginallink = re.exec(this.href)[1];
			$('.switch_navigation>a').each(function(i){
				var re = /([_\-\w]+$)/i;
				if (re.exec(this.href)[0] == orginallink) {
					$(this).addClass(ON_CLASS);
					//$(".view-site-plan").addClass(ON_CLASS); // give our siteplan link a class of show
				}
			});
			
			$("#site-plan-wrapper").css("display","none");
			if ($.IE6Below()) {
				$("#ie-dark").css("display","none");
				$(".site-map-base").css("display","none");
			}
			$("#explore_buildings").css("margin-bottom","11px");			
			$("#default_text").css("display","none");
		return false;
	});
});

// allow ability to see floorplans
$(document).ready(function(){
	$(this).find('a.floorplan').click(function() {
		$(this).addClass("active");
		$('a.rendering').removeClass("active");
		$(".building_images").each(function(i) {
			if ($(this).hasClass("show")) {
				$(this).removeClass("show");
				$(this).addClass("hidden");
				$(this).next().addClass("show");
				$(this).next().removeClass("hidden");
			};
		});
		return false;
	});
	$(this).find('a.rendering').click(function() {
		$(this).addClass("active");
		$('a.floorplan').removeClass("active");
		$(".building_floorplans").each(function(i) {
			if ($(this).hasClass("show")) {
				$(this).removeClass("show");
				$(this).addClass("hidden");
				$(this).prev().addClass("show");
				$(this).prev().removeClass("hidden");
			};
		});
		return false;
	});
});