var Site = {
	SlideShow: {
		init: function() {
			new SlideShow("photoBox");
		}
	},
	
	Contact: {
		init: function() {
			// input active states
			var form 	= document.getElement("#section_contact .contactForm"),
			inputs 		= form.getElements("input[type=text], textarea");
			inputs.each(function(input, i) {
				input.addEvents({
					"focus": function() {
						input.addClass("active");
					},
					"blur": function() {
						input.removeClass("active");
					}					
				});
			});
			
		}
	},	
	
	Estimates: {
		init: function() {
			// input active states
			var form 	= document.getElement("#section_estimate .estimateForm"),
			inputs 		= form.getElements("input[type=text], textarea");
			inputs.each(function(input, i) {
				input.addEvents({
					"focus": function() {
						input.addClass("active");
					},
					"blur": function() {
						input.removeClass("active");
					}					
				});
			});
			
			// time select
			var multiSelect	= form.getElement(".multiSelect"),
			multiInputs 	= multiSelect.getElements("input[type=checkbox]"),
			multiRows		= multiSelect.getElements("tr");
			
			multiInputs.each(function(input, i) {
			
				input.addEvent("click", function() {
					multiRows[i][(this.get("checked")) ? "addClass" : "removeClass"]("selected");
				});
				
				if (input.get("checked")) input.fireEvent("click");
				
			});
			
		}
	},
	
	Faq: {
		curr: null,
		init: function() {
			this.element = document.getElement("#section_faq .faqWrap");						
			this.wrappers = this.element.getElements(".faqItem");
			this.toggles = this.element.getElements("h2");
			this.togd = this.element.getElements(".answer");
			
			this.toggles.each(function(el, i) {
				//this.togd[i].hide();
				el.addEvents({
					"click": function() {						
						this.show(i);
					}.bind(this),
					"mouseenter": function() {						
						el.addClass("over");
					}.bind(this),
					"mouseleave": function() {
						el.removeClass("over");
					}.bind(this)
				});
			}, this);
			
			if (window.location.hash) {
				var start = window.location.hash;
				if (start.indexOf("#") != -1) {
					start = parseInt(start.replace("#", ""));
					if (start <= this.toggles.length) this.show(start - 1);
				}				
				
			}
			
		},
		
		show:function(index) {						
			if ($chk(this.curr) && this.curr != index) {
				//this.togd[this.curr].hide();
				this.wrappers[this.curr].removeClass("active");

			}
			
			var display = (this.togd[index].getStyle("display") == "none");
			//this.togd[index][display ? "show" : "hide"]();	
			this.wrappers[index][display ? "addClass" : "removeClass"]("active");
			
			this.curr = index;
		}
		
	}
};


var SlideShow = new Class({
	Implements: [Events, Options],
	
	options: {
		
		prevSelector: "#photoPrevious",
		nextSelector: "#photoNext",
		
		slideSelector: ".photoItem",
		
		navClassName: "photoNav",
		navTagName: "ul",
		navChildTagName: "li",
		navActiveClass: "active",
		navParentSelector: null,
		
		tween: {
			duration: 500
		},
		
		autoRotate: true,
		rotateDuration: 3000,
		
		startSlide: 0,
		
		preloadImg: "images/x.gif",
		preloadClass: "photoLoading"
	},
	curr: null,
	
	initialize: function(element, opts) {
		this.element = document.id(element);
		
		if (!this.element) return;
		
		this.setOptions(opts);
		
		this.slides = this.element.getElements(this.options.slideSelector);
		this.prepSlides();
		
		this.slideOut = new Fx.Tween(this.slides[this.startSlide], $merge({property: "opacity", link: "cancel"}, this.options.tween));
		this.slideIn = new Fx.Tween(new Element("div"), $merge({property: "opacity", link: "cancel"}, this.options.tween));
		
		this.prev = this.element.getElement(this.options.prevSelector);
		this.next = this.element.getElement(this.options.nextSelector);
		
		this.curr = this.options.startSlide;
		
		this.preload();
		this.attach();		
	},
	
	prepSlides: function() {
		this.nav = new Element(this.options.navTagName, {"class": this.options.navClassName});
		this.navChilds = [];
		
		this.slides.each(function(slide, i) {
			var chk = (i == this.options.startSlide);
			
			slide.setStyles({
				left: 0,
				position: "absolute",
				top: 0,
				zIndex: this.slides.length - i
			});
			
			slide.set("opacity", (chk) ? 1 : 0);
			
			var navItem = new Element(this.options.navChildTagName, {"class": (chk) ? this.options.navActiveClass : ""});
			this.nav.adopt(navItem);
			this.navChilds.push(navItem);
			
		}, this);
		
		this.nav.setStyle("zIndex", this.slides.length + 1);
		this.nav.inject(document.getElement(this.options.navParentSelector) || this.element);
		
	},
	
	preload: function() {		
		var len = this.slides.length, count = 0;
		var onPhotoLoaded = function(index) {
			this.slides[index].removeClass(this.options.preloadClass);
			count = count + 1;
			if (count >= len) this.startRotation();
		}.bind(this);
		
		var photos = [];
		var images_urls = [];
		var images_alts = [];
		this.slides.each(function(slide, i) {
			var photo = slide.getElement(".photo img") || new Element("img");
			var photo_src = photo.get("src");
			var photo_alt = photo.get("alt");			
			
			photo.set("src", this.options.preloadImg);
			slide.addClass(this.options.preloadClass);
			
			photos.push(photo);
			images_urls.push(photo_src);
			images_alts.push(photo_alt);
			
			/*
			var myimg = new Asset.image(photo_src, {				
				onLoad: function(img) {
					photo.set({
						"src": photo_src,
						"alt": photo_alt
					});
					onPhotoLoaded(i);
				}
			});*/
			
		}, this);
		var myimages = new Asset.images(images_urls, {
			onProgress: function(counter, index) {
				photos[index].set({
					'src': 	images_urls[index],
					'alt': images_alts[index]
				});
			},
			onError: function(counter, index) {
				photos[index].set({
					'src': 	images_urls[index],
					'alt': images_alts[index]
				});
			},			
			onComplete: function() {
				this.startRotation();
			}.bind(this)
		});

	},
	
	startRotation: function() {
		if (this.options.autoRotate) {
			this.element.addEvents({
				"mouseenter": function() {
					$clear(this.rotateTimeout);
				}.bind(this),
				
				"mouseleave": function() {
					$clear(this.rotateTimeout)
					this.rotateTimeout = this.nextSlide.periodical(this.options.rotateDuration, this);
				}.bind(this)
			});
			
			this.rotateTimeout = this.nextSlide.periodical(this.options.rotateDuration, this);				
		}	
	},
	
	prevSlide: function() {
		var prev = (this.curr - 1 < 0) ? this.slides.length - 1 : this.curr - 1;
		this.show(prev);
	},
	
	nextSlide: function() {
		var next = (this.curr + 1 > this.slides.length - 1) ? 0 : this.curr + 1;
		this.show(next);
	},
	
	show: function(index) {
		if (index == this.curr) return this;

		this.slideOut.element = this.slides[this.curr];
		this.slideIn.element = this.slides[index];

		this.slideOut.start(0);
		this.slideIn.start(1);
		
		this.navChilds[this.curr].removeClass(this.options.navActiveClass);
		this.navChilds[index].addClass(this.options.navActiveClass);

		this.curr = index;
		
		this.fireEvent("show", [this.slides[index], index])
		
		return this;
	},
	
	toElement: function() {
		return this.element;
	},
	
	attach: function() {
		this.prev.addEvent("click", function() {
			this.prevSlide();
		}.bind(this));
		this.next.addEvent("click", function() {
			this.nextSlide();
		}.bind(this));
		
		return this;
	},
	
	detach: function() {
		this.prev.removeEvents("click");
		this.next.removeEvents("click");
		
		return this;
	}
});
