
var HeadwatersSite = new Class ({
	Implements: [Options, Events],
	
	options: {
		vbswfpath: "http://www.videobridge.ca/IiJQNCAK_flash/loader.swf",
		vbdata: {
			swfoptions: "172/dzfpj/default-",
			swfclientid: "IiJQNCAK",
			swffoldername: "172/dzfpj/default-",
			swfbaseurl: "http://www.videobridge.ca",
			suiteid: "",
			autostart: ""
			
		},
		vbcontainer: "flash-container"
	},
	
	initialize: function(options){
		this.setOptions(options);
		
		this._dom();
		
		// Go.
		if (this.related_content) this._build_related_content();
		if (this.gallery) this._build_gallery();
		if (this.section_floorplans) this._build_floorplans();
		if (this.flash_container) this._embed_videobridge();
		
		// Content Box Stuff
		if (this.owner_login) this._build_owner_login();
		if (this.request_information) this._build_request_information();
		
		if (this.owner_login_footer) this._build_owner_login_footer();
		if (this.request_information_footer) this._build_request_information_footer();
		
		
		if (this.newswave_signup) this._build_newswave_signup();
		if (this.newswave_signup_footer) this._build_newswave_signup_footer();
		
		if (this.appointment_login) this._build_appointment_login();
		
	},
	
	_dom: function() {
		
		var self = this;
		
		this.contentbox = new ContentBox({ container: document.body, cssPath: "/stylesheets/ContentBox.js.css", width: 450, height:250});
		this.contentbox._resizeTo(450, 250, false);
		
		this.flash_container = $("flash-container");
		this.aside = $("aside");
		
		if (this.aside) {
			this.aside.is_open = (this.flash_container) ? true : false;
			this.aside.panel = new SlidePanel({ target: "aside", open: self.aside.is_open });
		}
		
		this.related_content = $("related-content");
		this.details = $("details");
		this.section_floorplans = $("section-floorplans");
		this.gallery = $("gallery");
		this.galleries = $("galleries");
		this.image_container = $("image-container");
		
		this.newswave_signup = $("newswave-signup");
		//this.newswave_signup_form = $("newswave-signup-form");
		
		this.newswave_signup_footer = $("newswave-signup-footer");
		//this.newswave_signup_form_footer = $("newswave-signup-form-footer");
		
		
		this.request_information = $("header").getElement('.request-information');
		this.owner_login =  $("header").getElement('.owners-login');
		
		this.appointment_login = $('register-button');
		
		this.request_information_footer = $("footer-utilities-navigation").getElement('.request-information');
		this.owner_login_footer = $("footer-utilities-navigation").getElement('.owners-login');
		
		
		
	},
	
	_embed_videobridge: function() {
		
		var self = this;
		
		//console.log(self.options.vbdata.suiteid);
		if(self.options.vbdata.swffoldername != "172/dzfpj/default-" && self.options.vbdata.autostart == "0") {
			//console.log('test');
			self.options.vbdata.autostart = 2;
		}
		
		
		self.vbflashobject = new Swiff(self.options.vbswfpath, {
			id: "vbflashobject",
			container: self.options.vbcontainer,
			width: 1000,
			height: 563,
			
			params: {
				wMode: "transparent",
				allowScriptAccess: "always",
				swLiveConnect: "true",
				salign: "TL",
				allowFullScreen: "true"
			},
			
			vars: {
				swfoptions: self.options.vbdata.swfoptions,
				folderName: self.options.vbdata.swffoldername,
				clientid: self.options.vbdata.swfclientid,
				baseURL: self.options.vbdata.swfbaseurl,
				defaultXMLSuite: self.options.vbdata.suiteid,
				autostartSuite: self.options.vbdata.autostart
			},
			
			callBacks: {
                onLoad: function(suiteid) {
	
                	//alert("Flash is loaded! = " + suiteid);
					//aside_panel.close();
					//Swiff.remote(vbflashobject, 'startFlash');
					
            	},

				closePanel: function() {
					//alert('close');
					self.aside.panel.close();
				}
				
            }

		});
		
	},
	
	_build_owner_login: function() {
		
		var self = this;
		
		self.owner_login_button = self.owner_login.getElement("a");
		
		self.owner_login_button.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			self.contentbox._loadContent(self.owner_login_button.get("href"), self.owner_login_button.get("title"));
			
		});
		
	},
	
	_build_owner_login_footer: function() {
		
		var self = this;
		
		self.owner_login_button_footer = self.owner_login_footer.getElement("a");
		
		self.owner_login_button_footer.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			self.contentbox._loadContent(self.owner_login_button_footer.get("href"), self.owner_login_button_footer.get("title"));
			
		});
		
	},
	
	
	
	
	
	
	_build_request_information: function() {
		
		var self = this;
		
		var multi_page_form = function() {
			
			var form_panels_container = $("form-panels-container");
			
			if (form_panels_container) {
				
				form_panels_container.morph = new Fx.Morph(form_panels_container, {transition: Fx.Transitions.Expo.easeOut, duration: 400 });
				form_panels_container.setStyle("width", form_panels_container.getSize().x).addClass("panels");
				
				var form_panels = form_panels_container.getElements(".form-panel");
				var form_buttons = form_panels_container.getParent().getElement(".buttons");
				var submit_button = form_buttons.getElement("#submit-button");
			
				if (form_panels.length > 0) {
					
					var next_button = new Element("a", {
						href: "#",
						id: "next-panel-button",
						text: "Or help us by giving us more info...",
						events: {
							"click": function(evt) {
								
								$('city-province-state').value = "";
								var next_index = form_panels.current_index + 1;
								if (next_index == form_panels.length - 1) {
									next_button.hide();
								}
								
								form_panels_container.morph.start({"height": form_panels[next_index].getSize().y});
								
								form_panels[form_panels.current_index].fade("out");
								form_panels[next_index].fade("in");
								form_panels.current_index = next_index;
								
								
							}
						}
					}).inject(form_buttons);
					
					
					form_panels.each(function(panel, index) {
						
						if (index == 0) {
							
							form_panels.current_index = 0;
							form_panels_container.morph.set({ "height" : panel.getSize().y });
							// next_button.show();
							// submit_button.hide();
							
						} else {
							
							panel.setStyles({"position": "absolute"}).fade("hide");
							
						}
						
					});
				
				}
				
			}
		};
		
		self.request_information_button = self.request_information.getElement("a");
		self.request_information_button.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			self.contentbox._loadContent(self.request_information_button.get("href"), self.request_information_button.get("title"), multi_page_form);
			
		});
		
	},
	
	/*_build_appointment_login: function() {
		
		var self = this;
		
		self.appointment_login_button = self.appointment_login.getElement("a");
		
		self.appointment_login_button.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			
			//self.contentbox._loadContent(self.appointment_login_button.get("href"), self.appointment_login_button.get("title"));
			self.contentbox._loadContent('/contact/appointment', self.appointment_login_button.get("title"));
		});
		
	},*/
	
	_build_appointment_login: function() {
		
		var self = this;
		
		self.appointment_login_button = self.appointment_login.getElement("a");
		
		self.appointment_login_button.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			
			//self.contentbox._loadContent(self.appointment_login_button.get("href"), self.appointment_login_button.get("title"));
			self.contentbox._loadContent('/contact/appointment', self.appointment_login_button.get("title"));
		});
		
	},
	
	/*_build_newswave_signup: function() {
		
		var self =this;
		
		self.newswave_signup_button = $("newswave-signup").getElement("a");
		//self.newswave_signup_button.form = $("newswave-signup-form");
		//self.newswave_signup_button.form.dispose();
		
		self.newswave_signup_button.addEvent("click", function(evt) {
			
			evt.stop();
			//self.contentbox._show();
			self.contentbox._loadContent('/contact/newswave_signup_form', self.appointment_login_button.get("title"));
			
		});
		
	},*/
	
	_build_newswave_signup: function() {
		
		var self =this;
		
		self.newswave_signup_button = $("newswave-signup").getElement("a");
		//self.newswave_signup_button.form = $("newswave-signup-form");
		//self.newswave_signup_button.form.dispose();
		
		
		self.newswave_signup_button.addEvent("click", function(evt) {
			
			evt.stop();
			//self.contentbox._show();
			self.contentbox._loadContent('/contact/newswave_signup_form', self.newswave_signup_button.get("title"));
			
			
		});
		
	},
	
	
	_build_newswave_signup_footer: function() {
		
		var self =this;
		
		self.newswave_signup_button_footer = $("newswave-signup-footer").getElement("a");
		//self.newswave_signup_button_footer.form = $("newswave-signup-form-footer");
		//self.newswave_signup_button_footer.form.dispose();
		
		self.newswave_signup_button_footer.addEvent("click", function(evt) {
			
			evt.stop();
			//self.contentbox._show();
			self.contentbox._loadContent('/contact/newswave_signup_form', self.newswave_signup_button_footer.get("title"));
			
		});
		
	},
	
	
	
	_build_request_information_footer: function() {
		
		var self = this;
		
		var multi_page_form = function() {
			
			
			var form_panels_container = $("form-panels-container");
			
			if (form_panels_container) {
				
				form_panels_container.morph = new Fx.Morph(form_panels_container, {transition: Fx.Transitions.Expo.easeOut, duration: 400 });
				form_panels_container.setStyle("width", form_panels_container.getSize().x).addClass("panels");
				
				var form_panels = form_panels_container.getElements(".form-panel");
				var form_buttons = form_panels_container.getParent().getElement(".buttons");
				var submit_button = form_buttons.getElement("#submit-button");
			
				if (form_panels.length > 0) {
					
					var next_button = new Element("a", {
						href: "#",
						id: "next-panel-button",
						text: "Or help us by giving us more info...",
						events: {
							"click": function(evt) {
								
								$('city-province-state').value = "";
								var next_index = form_panels.current_index + 1;
								if (next_index == form_panels.length - 1) {
									next_button.hide();
								}
								
								form_panels_container.morph.start({"height": form_panels[next_index].getSize().y});
								
								form_panels[form_panels.current_index].fade("out");
								form_panels[next_index].fade("in");
								form_panels.current_index = next_index;
								
							}
						}
					}).inject(form_buttons);
					
					form_panels.each(function(panel, index) {
						
						if (index == 0) {
							
							form_panels.current_index = 0;
							form_panels_container.morph.set({ "height" : panel.getSize().y });
							// next_button.show();
							// submit_button.hide();
							
						} else {
							
							panel.setStyles({"position": "absolute"}).fade("hide");
							
						}
						
					});
				
				}
				
			}
		};
		
		self.request_information_button_footer = self.request_information_footer.getElement("a");
		self.request_information_button_footer.addEvent("click", function(evt) {
	
			evt.stop();
			//contentbox._show();
			self.contentbox._loadContent(self.request_information_button_footer.get("href"), self.request_information_button_footer.get("title"), multi_page_form);
			
		});
		
	},
	
	_build_related_content: function() {
		
		var self = this;
		
		self.related_content.addClass("tabs");
		
		self.related_content.tab_bar = new Element("div", { id: "related-content-tab-bar" }).inject(self.related_content,"top");
		self.related_content.ul = new Element("ul", { "class": "tabbed-navigation" }).inject(self.related_content.tab_bar);
		self.related_content.ul.items = new Array();
		
		var h3s = self.related_content.getElements("h3");
		var tallestContainerHeight = 0;
		
		h3s.each(function(item, index) {
			
			// new list item and anchor
			var li = new Element("li").inject(self.related_content.ul);
			li.a = new Element("a", { href: "#", text: item.get("text") }).inject(li);
			li.a.targetContainer = item.getParent(".related-section");
			
			self.related_content.ul.items.push(li);
			
			if (index == 0) {
				li.addClass("active");
				self.related_content.ul.activeItem = index;
			} else {
				li.a.targetContainer.setStyle("display", "none");
			}
			
			// cleanup
			item.destroy();
			
			// set the height based on the highest container.
			if (li.a.targetContainer.getSize().y > tallestContainerHeight) tallestContainerHeight = li.a.targetContainer.getSize().y;
			
			li.a.addEvent("click", function(evt) {
				
				evt.stop();
				
				self.related_content.ul.items[self.related_content.ul.activeItem].removeClass("active");
				self.related_content.ul.items[self.related_content.ul.activeItem].a.targetContainer.setStyle("display", "none");
				
				li.a.targetContainer.setStyle("display", "block");
				li.addClass("active");
				self.related_content.ul.activeItem = index;
				
			});
			
			
		});
		
		var article = $("article");
		if (article) article.setStyle("margin-bottom", tallestContainerHeight + 30);
		
		self.related_content.ul.items.each(function(item, index){
			
			item.a.targetContainer.setStyle("height", tallestContainerHeight);
			
		});
		
	},
	
	_build_details: function() {
		
		var self = this;
		self.details.addClass("shelf");
		
		self.details.tabbed_navigation = new Element("div", { id: "details-tabbed-navigation" }).inject("detail-information", "top");
		self.details.tabbed_navigation.ul = new Element("ul", { "class": "tabbed-navigation" }).inject(self.details.tabbed_navigation);
		self.details.tabbed_navigation.ul.items = new Array();
		
		var h3s = details.getElements("h3");
		var tallestContainerHeight = 0;
		
		h3s.each(function(item, index) {
			
			var li = new Element("li").inject(self.details.tabbed_navigation.ul);
			li.a = new Element("a", { href: "#", text: item.get("text") }).inject(li);
			li.a.targetContainer = item.getParent(".section");
			
			self.details.tabbed_navigation.ul.items.push(li);
			
			if (index == 0) {
				li.addClass("active");
				self.details.tabbed_navigation.ul.activeItem = index;
			} else {
				li.a.targetContainer.setStyle("display", "none");
			}
			
			// cleanup
			item.destroy();
			
			// set the height based on the highest container.
			if (li.a.targetContainer.getSize().y > tallestContainerHeight) tallestContainerHeight = li.a.targetContainer.getSize().y;
			
			li.a.addEvent("click", function(evt) {
				
				evt.stop();
				
				self.details.tabbed_navigation.ul.items[self.details.tabbed_navigation.ul.activeItem].removeClass("active");
				self.details.tabbed_navigation.ul.items[self.details.tabbed_navigation.ul.activeItem].a.targetContainer.setStyle("display", "none");
				
				li.a.targetContainer.setStyle("display", "block");
				li.addClass("active");
				self.details.tabbed_navigation.ul.activeItem = index;
				
			});
			var primary;
			if (primary = $("primary")) primary.setStyle("margin-bottom", tallestContainerHeight + 80);
			self.details.tabbed_navigation.ul.items.each(function(item, index){

				item.a.targetContainer.setStyle("height", tallestContainerHeight);

			});
			
		});
			
	},
	
	_build_gallery: function() {
		
		var self = this;
		
		self.image_container.setStyles({
			"min-height": 500,
			"height": self.image_container.getElement("img").getSize().y,
			"width": "100%",
			"position": "relative"
		});
		
		self.image_container.image_holder1 = new Element("div", { id: "image-holder-1", "class" : "image-holder" }).wraps(self.image_container.getElement("img"));
		self.image_container.image_holder2 = new Element("div", { id: "image-holder-2", "class" : "image-holder" }).inject(self.image_container).fade("hide");
		self.image_container.image_loader = new Element("div", {id: "image-loader"}).inject(self.image_container).hide();
		
		self.gallery.active_holder = self.image_container.image_holder1;
		self.gallery.active_thumbnail = 0;
		
		self.gallery.thumbnail_items = self.galleries.getElement(".mini-gallery").getElements("li");
		
		
		
		// function that changes the images.
		self.gallery.goto_image = function(index) {
			
			if (index != self.gallery.active_thumbnail) {
				
				
				var item = self.gallery.thumbnail_items[index];
				self.gallery.thumbnail_items[self.gallery.active_thumbnail].removeClass("active");
				
				var isloaded = false;
				
				(function() { if (isloaded == false) { self.image_container.image_loader.show(); } }).delay(100);
				
				// add this image to the unactive image holder.
				var image_path = item.getElement("a").get("href");
				var nextimage = Asset.image( image_path,  {
					"alt" : item.getElement("a").get("alt"),
					onLoad: function() {
						
						isloaded = true;
						
						self.image_container.image_loader.hide();
						
						var inactive_holder = (self.gallery.active_holder == self.image_container.image_holder1) ? self.image_container.image_holder2 : self.image_container.image_holder1;
						inactive_holder.empty();
						nextimage.inject(inactive_holder);

						self.gallery.active_holder.fade("out");
						inactive_holder.fade("in");

						// reset the items
						self.gallery.active_holder = inactive_holder;
						self.gallery.active_thumbnail = index;

						item.addClass("active");
						
						
						centerpos = self.gallery.active_holder.getElement("img").getSize().x;
						centerpos = 500 - (centerpos/2);
						
						self.gallery.active_holder.getElement("img").setStyle("margin-left",centerpos+"px");
						
					}
				});
				
				
				
			} else {
				
				// if (console) console.log("Same image. No action.");
				
			}
			
		};
		
		// left and right buttons.
		self.gallery.next_button = new Element("a", { href: "#next", id: "gallery-next-button", text: "next", title: "Next Image" }).inject(self.image_container, "top");
		self.gallery.previous_button = new Element("a", { href: "#previous", id: "gallery-previous-button", text: "previous", title: "Previous Image" }).inject(self.image_container, "top");
		
		// add the actions to the next and previous buttons.
		self.gallery.next_button.addEvent("click", function(evt) {
			
			evt.stop();
			var index = self.gallery.active_thumbnail;
			var next_index = (index + 1 == self.gallery.thumbnail_items.length) ? 0 : index + 1;
			
			self.gallery.goto_image(next_index);
			
		});
		
		self.gallery.previous_button.addEvent("click", function(evt) {
			
			evt.stop();
			var index = self.gallery.active_thumbnail;
			var previous_index = (index == 0) ? self.gallery.thumbnail_items.length - 1 : index - 1;
			
			self.gallery.goto_image(previous_index);
			
		});
		
		// hide and show buttons only on mouse over
		// self.image_container.addEvents({
		// 	"mouseenter": function(evt) {
		// 		
		// 		self.gallery.next_button.fade("in");
		// 		self.gallery.previous_button.fade("in");
		// 		
		// 	},
		// 	
		// 	"mouseleave": function(evt) {
		// 		
		// 		self.gallery.next_button.fade("out");
		// 		self.gallery.previous_button.fade("out");
		// 		
		// 	}
		// });
		
		// add the image changing function to all of the thumbnails.
		if (self.gallery.thumbnail_items.length > 0) {
			
			var first_item = self.gallery.thumbnail_items[0].addClass("active");
			var init_image = Asset.image(first_item.getElement("a").get("href"), { "alt" : first_item.getElement("img").get("alt") }).replaces(self.gallery.active_holder.getElement("img"));
			
			// replace the first image
			
			self.gallery.thumbnail_items.each(function(item, index) {
				
				item.getElement("a").addEvent("click", function(evt) {
					
					evt.stop();
					self.gallery.goto_image(index);
					
				});
				
			});
			
		}
		
	},
	
	_build_floorplans: function() {
		
		var self = this;
		
		if($('aside')) {
		self.aside.panel.panel.tabControl.addClass("pngfix");
		}
		if (self.image_container && self.aside && $("building-info")) {
			
			var open = "-175px";
			var closed = "0px";
			
			
			//self.aside.addClass("pngfix");
			
			self.image_container.morph = new Fx.Morph(self.image_container, {transition: Fx.Transitions.Expo.easeOut, duration: 400});
			/*self.aside.panel.panel.tabControl.addEvent("click", function(evt) {
				
				if (self.aside.hasClass("open")) {
					//alert('has open');
					self.image_container.morph.start({"left":open});
					
				} else {
					//alert('doesnt have open');
					self.image_container.morph.start({"left":closed});
					
				}
				
			});*/
			
		}
		
	},
	
	
	
	/*_build_newswave_signup: function() {
		
		var self =this;
		
		self.newswave_signup_button = $("newswave-signup").getElement("a");
		self.newswave_signup_button.form = $("newswave-signup-form");
		self.newswave_signup_button.form.dispose();
		
		self.newswave_signup_button.addEvent("click", function(evt) {
			
			evt.stop();
			self.contentbox._show();
			self.contentbox._loadContent(self.newswave_signup_button.form);
			
		});
		
	},*/
	
	
	
	// FLASH REMOTE STUFF
	__flash_some_function_name: function() {
		
		var self = this;
		// if (self.vbflashobject) {
		// 	
		// 	Swiff.remote(self.vbflashobject, "myFlashFunction");
		// 	
		// }
		
	}

});
