YAHOO.namespace("YAHOO.ce");
YAHOO.ce.runtime = function(){
	var public = {
		init: function(){
			that = this;
			footer = lib.d.get("footer");
			container = lib.d.get("container");
			menu = lib.d.get("menu");
			menuItems = lib.d.getChildren(menu);
			blurb = lib.d.get("blurb");
			freeQuote = lib.d.get("freeQuote");
			// form object
			if (lib.d.get("freeQuoteForm")){
				YAHOO.ce.freeQuoteForm.init();
			}
			// q&a object
			if (lib.d.get("questions")){
				YAHOO.ce.questions.init();
			}
			// services object
			if (lib.d.get("sizesOverlay")){
				YAHOO.ce.services.init();
			}
			// portfolio object
			if (lib.d.get("portfolio")){
				YAHOO.ce.portfolio.init();
			}
				
			adjusttFooter();
			adjustZ();
			setEvents();
		}
	};
	// private
	var that = null;
	var footer = null;
	var container = null;
	var menu = null;
	var menuItems = null;
	var blurb = null;
	var freeQuote = null;

	function setEvents(){
		lib.e.on(window, "resize", adjusttFooter);
		lib.e.on(menuItems, "mouseover", menuOver);
		lib.e.on(menuItems, "mouseout", menuOut);
		if (YAHOO.photoViewer.controller.getViewer("splashes")) { YAHOO.photoViewer.controller.getViewer("splashes").on("openviewer", adjustZ); }
	}; // end setEvents
	function adjusttFooter(){
		var windowHeight = lib.d.getClientHeight();
		if (windowHeight > 0) {
			var contentHeight = (lib.d.getRegion(container).bottom - lib.d.getRegion(container).top);
			var footerHeight  = (lib.d.getRegion(footer).bottom - lib.d.getRegion(footer).top);
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				lib.d.setStyle(footer, "top", (windowHeight - (contentHeight + footerHeight)) - 20 + 'px');
			}
			else {
				lib.d.setStyle(footer, "top", "0px");
			}
		}
	}; // end adjustFooter
	function adjustZ(){
		lib.d.setStyle(blurb, "position", "absolute");
		lib.d.setStyle(blurb, "z-index", YAHOO.photoViewer.zIndex + 1);
		lib.d.setStyle(freeQuote, "position", "absolute");
		lib.d.setStyle(freeQuote, "z-index", YAHOO.photoViewer.zIndex + 1);
	}; // end adjustZ
	function menuOver(){
		if (this.className != "active") {
			var fade = new lib.a(this, {
				opacity: {
					to: 0
				}
			}, 0.4, YAHOO.util.Easing.easeBoth);
			fade.animate();
		}
	}; // end menuOver
	function menuOut(){
		if (this.className != "active") {
			var fade = new lib.a(this, {
				opacity: {
					to: 1
				}
			}, 0.8, YAHOO.util.Easing.easeBoth);
			fade.animate();
		}
	}; // end menuOver
	// end private
	return public;
}();
lib.e.onDOMReady(YAHOO.ce.runtime.init, YAHOO.ce.runtime, YAHOO.ce.runtime);

YAHOO.ce.freeQuoteForm = function(){
	var public = {
		init: function(){
			freeQuoteForm = lib.d.get("freeQuoteForm");
			required = lib.d.getElementsByClassName("required", "input", freeQuoteForm);
			submitBtn = lib.d.get("submitBtn");

			setEvents();
		}
	};
	// private
	var freeQuoteForm = null;
	var required = null;
	var submitBtn = null;
	var errors = {};

	function setEvents(){
		lib.e.on(submitBtn, "click", validate);
	};// end setEvents
	function validate(e){
		var valid = true;
		for (var a = 0; a < required.length; a++){
			if (!required[a].value.length){
				valid = false;
				errorMsg(required[a]);
			}
			else{
				if (errors[required[a].id]) errors[required[a].id].hide();
			}
		}
		if (valid){
			freeQuoteForm.submit();
		}
	}; // end validate
	function errorMsg(el){
		if (!errors[el.id]){
			errors[el.id] = new YAHOO.widget.Overlay("error-" + el.id, {
				context:[el,"tl","tr"],
				visible:true,
				width:"200px"
			});
			errors[el.id].setBody("This is a required field.");
			errors[el.id].render(document.body);
			lib.d.setStyle(errors[el.id].element, "color", "#ffffff");
			lib.d.setStyle(errors[el.id].element, "font-weight", "bold");
			lib.d.setStyle(errors[el.id].element, "padding", "4px");
			
		}
		else{
			errors[el.id].show();
		}
		var fade = new YAHOO.util.ColorAnim(errors[el.id].element, {
			backgroundColor: {
				to: "#ff6600",
				from: "#ffffff"
			}
		}, 0.4, YAHOO.util.Easing.easeBoth);
		fade.animate();
	}; // end errorMsg

	return public;
}();

YAHOO.ce.questions = function(){
	var public = {
		init: function(){
			questionsUl = lib.d.get("questions");
			questions = lib.d.getChildren(questionsUl);
			answers = lib.d.getElementsByClassName("answer", "p");
			
			config();
			setEvents();
		}
	};
	// private
	var questionsUl = null;
	var questions = null;
	var answers = null;

	function setEvents(){
		for (var a = 0; a < questions.length; a++){
			questions[a].index = a;
			lib.e.on(questions[a], "click", scrollToAnswer);
		}
	};// end setEvents
	function config(){
		var template = document.createElement("div");
		template.setAttribute("align", "right");
		var link = document.createElement("a");
		link.setAttribute("href", "#top");
		link.innerHTML = "Back to the questions";
		template.appendChild(link);
		var clone;
		for (var a = 0; a < answers.length; a++){
			clone = template.cloneNode(true);
			answers[a].appendChild(clone);
		}
		template = null;
	}; // end config
	function scrollToAnswer(e){
		//window.scrollTo(0, lib.d.getY(answers[this.index]) - 25);
		var doc = (YAHOO.env.ua.webkit) ? document.body : document.getElementsByTagName("html")[0];
		var scroller = new YAHOO.util.Scroll(doc, {
			scroll: {
				to: [0, lib.d.getY(answers[this.index]) - 25]
			}
		}, 0.4, YAHOO.util.Easing.easeBoth);
		scroller.animate();
		lib.e.preventDefault(e);
	}; // end scrollToAnswer
	
	return public;
}();

YAHOO.ce.services = function(){
	var public = {
		init: function(){
			garageSizes = lib.d.get("garageSizes");
			garageIncludes = lib.d.get("garageIncludes");

			config();
			setEvents();
		}
	};
	// private
	var garageSizes = null;
	var garageIncludes = null;
	var tips = {};
	
	function setEvents(){
		lib.e.on(garageSizes, "click", showSizes);
		lib.e.on(garageIncludes, "click", showIncludes);
	};// end setEvents
	function config(){
		tips["sizesOverlay"] = new YAHOO.widget.Dialog("sizesOverlay", { 
			width : "400px", 
			visible : false,  
			fixedcenter: true,
			constraintoviewport : true, 
			buttons : [{ 
				text:"Close", 
				handler:hideSizes,
				isDefault: true
			}] 
		}); 
		tips["sizesOverlay"].render();
		tips["packageOverlay"] = new YAHOO.widget.Dialog("packageOverlay", { 
			width : "400px", 
			visible : false,  
			fixedcenter: true,
			constraintoviewport : true, 
			buttons : [{ 
				text:"Close", 
				handler:hidePackages,
				isDefault: true
			}] 
		}); 
		tips["packageOverlay"].render();
	}; // end config
	function showSizes(){
		tips["sizesOverlay"].show();
	};
	function hideSizes(){
		tips["sizesOverlay"].hide();
	};
	function showIncludes(){
		tips["packageOverlay"].show();
	};
	function hidePackages(){
		tips["packageOverlay"].hide();
	};
	return public;
}();

YAHOO.ce.portfolio = function(){
	var public = {
		init: function(){
			that = this;
			portfolio = lib.d.get("portfolio");
			v = YAHOO.photoViewer.controller.getViewer('portfolio0');
			v.loadFlickr({
				flickrApi:{
					apikey: "5ae7887a41f1354366a18ab8a03b32ef",
					method: "flickr.photosets.getList",
					params: {user_id:"24720610@N05"},
					jsoncallback: "YAHOO.ce.portfolio.loadSets"
				}
			});
			
			setEvents();
		}, // end init
		loadSets: function(r){
			var h2 = null;
			var container = null;
			var clear = null;
			var desc = null;
			var anchor = null;
			for (var a = 0; a < r.photosets.photoset.length; a++){
				clear = document.createElement("div");
				lib.d.addClass(clear, "clearBoth");
				portfolio.appendChild(clear);
				h2 = document.createElement("h3");
				h2.innerHTML = r.photosets.photoset[a].title._content;
				portfolio.appendChild(h2);
				anchor = document.createElement("a");
				anchor.setAttribute("name", r.photosets.photoset[a].title._content);
				portfolio.appendChild(anchor);
				desc = document.createElement("p");
				lib.d.addClass(desc, "portDesc");
				desc.innerHTML = r.photosets.photoset[a].description._content;
				portfolio.appendChild(desc);
				container = document.createElement("div");
				container.id = "portfolio" + a;
				portfolio.appendChild(container);
				that.getPhotos(r.photosets.photoset[a], a);
			}
			response = r;
		}, // end loadSets
		getPhotos: function(obj, index){
			if (obj.id){ // photosets.getList call back
				YAHOO.photoViewer.config.viewers["portfolio" + index] = {};
				YAHOO.photoViewer.config.viewers["portfolio" + index].properties = {
					id: "portfolio" + index,
					grow: 0.2,
					fade: 0.2,
					modal: true,
					dragable: false,
					fixedcenter: true,
					position: "absolute",
					xy:[0,0],
					easing: YAHOO.util.Easing.easeBothStrong,
					buttonText:{
						next: "Next",
						prev: "Prev",
						close: "close [x]"
					},
					flickrApi:{
						apikey: "5ae7887a41f1354366a18ab8a03b32ef",
						thumbSize: "square",
						method: "flickr.photosets.getPhotos",
						params: {photoset_id:""}
					}
				};
				// re-init controller
				YAHOO.photoViewer.controller.init("portfolio" + index);
				YAHOO.photoViewer.controller.getViewer("portfolio" + index).on("flickrload", that.updater, index);
				YAHOO.photoViewer.controller.getViewer("portfolio" + index).loadFlickr({
					flickrApi:{
						apikey: "5ae7887a41f1354366a18ab8a03b32ef",
						thumbSize: "square",
						method: "flickr.photosets.getPhotos",
						params: {photoset_id:obj.id},
						jsoncallback: "YAHOO.photoViewer.controller.viewers.portfolio" + index + ".registerFlickrApi"
					}
				});
			}
		}, // end getPhotos
		updater: function(r, index){
			var v = YAHOO.photoViewer.controller.getViewer("portfolio" + index);
			var thumbs = lib.d.getElementsByClassName("photoViewer", "a", lib.d.get("portfolio" + index));
			
			for (var a = 0; a < thumbs.length; a++){
				imgInfo = thumbs[a].fullsource.split("/").pop().replace(".jpg","").split("_");
				id = imgInfo[0];
				secret = imgInfo[1];
				thumbMeta[id + "_" + secret] = {
					dom: thumbs[a]
				};
				v.loadFlickr({
					flickrApi:{
						apikey: "5ae7887a41f1354366a18ab8a03b32ef",
						method: "flickr.photos.getInfo",
						params: {photo_id:id, secret:secret},
						jsoncallback: "YAHOO.ce.portfolio.setDesc"
					}
				});
			}
		}, // end updater
		setDesc: function(rsp){
			var key = rsp.photo.id + "_" + rsp.photo.secret;
			var desc = rsp.photo.description._content;
			thumbMeta[key].dom.firstChild.setAttribute("alt", desc);
		} // ens setDesc
	};
	// private
	var that = null;
	var v = null;
	var response = null;
	var portfolio = null;
	var thumbMeta = {};
	
	function setEvents(){
		v.on("flickrload", that.getPhotos);
	};// end setEvents
	
	return public;
}();