// JavaScript Document

dojo.provide('blossomhomes');

blossomhomes = {
	
	defaultSection: null,
	visibleSection: null,
	subsectionTimer: null,
	
	init: function() {
		
		// register blossomhomes
		dojo.registerModulePath("blossomhomes", "../../blossomhomes");
		
		var d = dojo.query("#navSub ul.selected");
		if(d.length > 0) {
			this.defaultSection = d[0].id;
		}
		this.visibleSection = this.defaultSection;
		
		var _this = this;
		var fixLink = function(a) {
			dojo.connect(a, "onmouseover", dojo.hitch(_this, "animate", a, true));
			dojo.connect(a, "onmouseout", dojo.hitch(_this, "animate", a, false));
			dojo.connect(a, "onfocus", dojo.hitch(_this, "focus", a, true));
			dojo.connect(a, "onblur", dojo.hitch(_this, "focus", a, false));
			a._anim = null;
		};
		
		var hovers = dojo.query("#navMain li a");
		dojo.forEach(hovers, fixLink);
		var logo = dojo.query("#mainTitle a")[0];
		fixLink(logo);
		
		var subsections = dojo.query("#navSub ul");
		dojo.forEach(subsections, function(ul) {
			dojo.connect(ul, "onmouseover", null, dojo.hitch(this, "showSubsections", ul.id));
			dojo.connect(ul, "onmouseout", this, "hideSubsections");
			ul._anim = null;
		}, this);
		
		this.initContactForm();
		this.initFeatures();
		this.initGalleries();
	},
	// animation for header
	animate: function(a, dir) {
		if(a._anim !== null) {
			a._anim.stop();
		}
		var args = {node: a, duration: dir ? 250 : 750};
		a._anim = dir ? dojo.fadeIn(args) : dojo.fadeOut(args);
		a._anim.play();
		
		if(a.parentNode.nodeName.toLowerCase() == "li") {
			// now show the subsections
			if(dir) {
				var subsid = "sub" + a.parentNode.id;
				this.showSubsections(subsid);
			} else {
				this.hideSubsections();
			}
		}
	},
	
	// sets up a swapout of the current subsections to a different set
	showSubsections: function(id) {
		if(this.subsectionTimer !== null) {
			clearTimeout(this.subsectionTimer);
		}
		this.subsectionTimer = setTimeout(dojo.hitch(this, "_showSubsections", id), 150);
	},
	
	// does the actual swap of visible section to given section
	_showSubsections: function(id) {
		if(this.visibleSection == id) {
			// do nothing if already showing
			return;
		}
		// loop through other subsections and make sure they are hidden
		dojo.forEach(dojo.query("#navSub ul"), function(ul) {
			if(ul.id != this.visibleSection) {
				if(ul._anim !== null) {
					ul._anim.stop();
				}
				dojo.style(ul, "display", "none");
			}
		}, this);
		if(this.visibleSection !== null) {
			var toHide = dojo.byId(this.visibleSection);
			if(toHide._anim !== null) {
				toHide._anim.stop();
			}
			toHide._anim = dojo.fadeOut({
				node: toHide,
				duration: 200,
				onEnd: function() {
					dojo.removeClass(toHide.id.substr(3), "selected");
					blossomhomes.visibleSection = id;
					dojo.style(toHide, "display", "none");
					toHide._anim = null;
					if(id !== null) {
						var toShow = dojo.byId(id);
						blossomhomes._fadeInSubsection(toShow);
					}
				}
			}).play();
		} else if(id !== null) {
			var toShow = dojo.byId(id);
			this.visibleSection = id;
			this._fadeInSubsection(toShow);
		}
	},
	// finishes the fading in of a subsection
	_fadeInSubsection: function(toShow) {
		dojo.addClass(toShow.id.substr(3), "selected");
		dojo.style(toShow, "opacity", 0);
		dojo.style(toShow, "display", "block");
		toShow._anim = dojo.fadeIn({
			node: toShow,
			duration: 150,
			onEnd: function() {
				toShow._anim = null;
			}
		}).play();
	},
	
	// sets a timer to restore the default subsections after two seconds
	hideSubsections: function() {
		if(this.subsectionTimer !== null) {
			clearTimeout(this.subsectionTimer);
		}
		this.subsectionTimer = setTimeout("blossomhomes._hideSubsections();", 1000);
	},
	
	// restores the default selected subsections
	_hideSubsections: function() {
		clearTimeout(this.subsectionTimer);
		this.subsectionTimer = null;
		// return to default subsections
		this._showSubsections(this.defaultSection);
	},
	
	// Renders the anchor for focusing
	focus: function(a, dir) {
		dojo.style(a, "opacity", (dir ? 1 : 0));
	},
	
	// initialize the contact form buttons (simple hover effects)
	initContactForm: function() {
		var buttons = dojo.query("#contactForm input.button");
		dojo.forEach(buttons, function(b) {
			// makes IE6 show button hover
			dojo.connect(b, "onmouseover", null, dojo.hitch(dojo, "toggleClass", b, "buttonHover", true));
			dojo.connect(b, "onmouseout", null, dojo.hitch(dojo, "toggleClass", b, "buttonHover", false));
		});
	},
	
	// initialize the features page - won't run on other pages
	initFeatures: function() {
		if(dojo.hasClass(dojo.body(), "features")) {
			
			var hash = window.location.hash;
			var selected = false;
			if(hash) {
				var selected = "feature-" + hash.substr(1);
			}
			var pages = dojo.query("#featuresList > div");
			dojo.forEach(pages, function(page, index) {
				
				var li = dojo.byId(page.id+"-link");
				var link = dojo.query("a", li)[0];
				
				var sel = index == 0;
				if(selected) {
					sel = page.id == selected;
				}
				if(sel) {
					this.oldPage = page;
					dojo.addClass(li, "selected");
				} else {
					dojo.style(page, "display", "none");
				}
				dojo.connect(link, "onclick", null, dojo.hitch(this, "showFeaturesPage", page, li));
				
			}, this);
			
			// add clearing block at the end
			var clear = document.createElement("div");
			clear.innerHTML = "&nbsp;";
			dojo.style(clear, "clear", "both");
			dojo.byId("content").appendChild(clear);
		}
	},
	// used to gracefully swap out the selected pages
	showFeaturesPage: function(page, li, event) {
		if(this.oldPage == page) {
			return;
		}
		dojo.query("#featuresMenu li").removeClass("selected");
		dojo.addClass(li, "selected");
		if(dojo.isIE < 7) {
			// ie 6 has problems with the filters.
			dojo.style(this.oldPage, "display", "none");
			dojo.style(page, "display", "block");
			this.oldPage = page;
			return;
		}
		if(this._features_anim) {
			this._features_anim.stop(true);
		}
		var self = this;
		this._features_anim = dojo.fadeOut({
			node: this.oldPage,
			duration: 250,
			onEnd: function() {
				dojo.style(self.oldPage, "display", "none");
				self.oldPage = page;
				dojo.style(page, "opacity", 0);
				dojo.style(page, "display", "block");
				self._features_anim = dojo.fadeIn({ node: page, duration: 250 }).play();
			}
		}).play();
	},
	
	// initialize galleries
	initGalleries: function() {
		var gals = dojo.query("#content div.gallery");
		if(gals.length > 0) {
			// load in the gallery
			dojo.require('blossomhomes.gallery');
			blossomhomes.gallery.init(gals);
		}
	}
};

dojo.addOnLoad(blossomhomes, "init");