
window.addEvent('domready', function(){
  initEvents();
	intro();
	/* position(); */
});

var previous;
var current = "intro";



function initEvents() {
	
	/*
	var mySlide = new Slider($('area'), $('knob'), {
		steps: 480,
		mode: 'vertical',
		onChange: function(step){
			//$('upd').setHTML(step);
			//console.log(step);
		}
	}).set(0);
	*/
	
	$('nav-logo').addEvent('click', function(){
		this.blur();
	  loadContent("intro");
	});
  
  $$('#nav-main li a').each(function(element) {
  	var fx = new Fx.Styles(element, {duration:150, wait:false});

  	element.addEvent('click', function(){
  	  var parent = this.getParent().getParent().getProperty('id');
  	  var section = parent.substr(4,parent.length);

			this.blur();
			
			//console.log("parent: " + parent + ", section: " + section);	
			
			//console.log("parent's parent's children: " + $(parent).getParent().getChildren());
			
			$(parent).getParent().getChildren().each(function(element) {
				
				var i = element.getProperty('id');
				var j = i.substr(4,i.length);
			
				if (j == section) {
					element.getFirst().getFirst().addClass('sel');
				} else {
					element.getFirst().getFirst().removeClass('sel');
				}
				
			});
			
			
			//console.log("section: " + section + ", current: " + current);
			
			if (section != current) {
				loadContent(section); 
				//this.style.borderTop = "4px solid #6a6a6a";
				//this.style.backgroundPosition = "0 -4px";

				/*
				var thus = this;

				this.parentNode.parentNode.parentNode.getChildren().each(function(e){
				  console.log(thus);
				  if (thus != e) {
				    console.log("not same");
						thus.style.border = "none";
				  }
				});
				*/
				
			} 
			  	  
  	});
		
  });


	$$('#nav-sub-about-us li a').each(function(element) {
  	var fx = new Fx.Styles(element, {duration:200, wait:false});
  	
  	element.addEvent('click', function(){
  	  var parent = this.getParent().getParent().getProperty('id');
  	  var section = parent.substr(4,parent.length);
			this.blur();
  	  loadContent(section);
  	});
		
  });

	
	$$('#nav-sub-showroom li a').each(function(element) {
  	var fx = new Fx.Styles(element, {duration:200, wait:false});
  	
  	element.addEvent('click', function(){
  	  var parent = this.getParent().getParent().getProperty('id');
  	  var section = parent.substr(4,parent.length);
			this.blur();
  	  loadContent(section);
  	});
		
  });
  
}

function position() {
	
//	$('backing').style.height =  getHeight() - 60 + "px";
	
}


function intro() {
	
	///loadContent(current);
	//showShowroomNav();
	
	/*
	$('introimg').setOpacity(0);
	
	new Fx.Styles('introimg', {
		duration: 500
	}).start({
		'opacity': 1
	});
	*/
	
	
}





function loadContent(section, project) {

	if (!project) {
		project = 1;
	}

	var fx = new Fx.Styles('main', {
		duration: 300,
		onFailure: function() {
			$('main').innerHTML = "Error loading Data.  Please check your internet connection.";
		},
		onComplete: function() {
			
			position();

			if (section == "restorations" || 
					section == "new-homes" ||
					section == "renovations" ||
					section == "basements" ||
					section == "bathrooms" ||
					section == "kitchens" ||
					section == "exteriors" ||
					section == "cabinetry") {
				var url = "showroom.php";
			} else {
				var url = "xml/" + section + ".xml";
			}
			

			var myXhr = new XHR({method: 'get', onSuccess: function(txt, xml){
				
				$('main').innerHTML = txt;

				// FADE BACK IN :	
				new Fx.Styles('main', {
					duration: 300
				}).start({
					'opacity': 1
				});

			}});
			myXhr.send(url, "category=" + section + "&project=" + project);
		}
	}).start({
		'opacity': 0
	});
	
	current = section;

}


	


function setupShowroomEvents() {
	$$('#thumbnails li a').each(function(element) {
		
		var f = new Fx.Styles(element, {duration:100, wait: false});
		f.start({'opacity': .5});
		
  	var fx = new Fx.Styles(element, {duration:200, wait: false});

  	element.addEvent('mouseenter', function(){
  		fx.start({ 'opacity': 1 });
  	});

  	element.addEvent('mouseleave', function(){
  		fx.start({ 'opacity': .5 });
  	});
  	
		//var sourcey = element.getFirst().getProperty('src');
		////var alty = element.getFirst().getProperty('alt');
		////alert(element.getFirst());

/*
  	element.addEvent('click', function(){
			//fx.start({ 'opacity': 0 });
		
			// Fade out righty-showroom
			new Fx.Styles('showmain', {
				duration: 250,
				onComplete: function() {
					$('showmain').innerHTML = "";
					


					///var pathy = sourcey.substr(0, 28);
					///var mainpath = pathy + "main" + pathy.substr(28, 6) + ".jpg";	
					///alert("pathy: " + pathy);

					
					var mainpath = "./images/showroom/basement/" + alty + "main.jpg";
					alert("mainpath: " + mainpath);
					
					//var mainpath = "./images/showroom/basement/1main.jpg";
					var img = new Element('img', {'src': mainpath});
					img.inject('showmain');
					new Fx.Styles('showmain', { duration: 250	}).start({'opacity': 1});
				}
			}).start({'opacity': 0});
  	});
*/
		
  });


	//projects-nav
	
	$$('#projects-nav li a').each(function(element){
		element.addEvent('click', function(){
			
			var sub = this.getParent().getParent().getProperty('class');	
			//console.log("xml/" + sub + this.innerHTML + ".xml");			
			
			/*
			new Fx.Styles('showmain', {
				duration: 250,
				onComplete: function() {
					$('showmain').innerHTML = "";
					var mainpath = "./images/showroom/basement/2main.jpg";
					var img = new Element('img', {'src': mainpath});
					img.inject('showmain');
					new Fx.Styles('showmain', { duration: 250	}).start({'opacity': 1});
				}
			}).start({'opacity': 0});
			*/
			
		});
	});

}


/*

function loadImage(which) {
	
	//var alty = this.getProperty('alt');
	//var alty = img.getProperty('alt');
	
	// Fade out righty-showroom
	new Fx.Styles('showmain', {
		duration: 250,
		onComplete: function() {
			$('showmain').innerHTML = "";

			///var pathy = sourcey.substr(0, 28);
			///var mainpath = pathy + "main" + pathy.substr(28, 6) + ".jpg";	
			///alert("pathy: " + pathy);

			
			var mainpath = "./images/showroom/basements/" + which + "main.jpg";
			//alert("mainpath: " + mainpath);
			
			//var mainpath = "./images/showroom/basement/1main.jpg";
			var img = new Element('img', {'src': mainpath});
			img.inject('showmain');
			new Fx.Styles('showmain', { duration: 250	}).start({'opacity': 1});
		}
	}).start({'opacity': 0});
}


function closeIntro() {
	// console.log('closeIntro');
	
	new Fx.Styles('intro', {
		duration: 500,
		onComplete: openMain
	}).start({'opacity': 0});
	
	//this.blur();
}

*/



/*
function loadAsset() {
	var target = 'shot';
	var source = '';
	
	$(target).innerHTML = "loading...";
	new Fx.Styles(target, { duration: 250, onComplete: function() {				
			new Asset.image('./images/globe_west_2048.jpg', {
				id: 'myImage', 
				title: 'myImage', 
				onload: function() {
					new Fx.Styles(target, { duration: 1000	}).start({'opacity': 1});
				}
			}).inject(target);
		}				
	}).start({'opacity': .5});
}
*/


function openShot(n, section) {
	
	//console.log("openShot(): " + n);
	
	var target = 'shot';
	var path = "./images/showroom/" + current + "/" + section + "/" + n + ".jpg";
	//var path = './images/globe_west_2048.jpg';

	new Fx.Styles(target, {
		duration: 200,
		onComplete: function() {
			$(target).innerHTML = "";
			//$('loading').innerHTML = "loading...";

			new Asset.image(path, {
				id: 'myImage', 
				title: 'myImage',
				//width: '100px',
				//height: '100px',
				onload: function() {
					
					//div#righty.righty-showroom-within div.focus
					//$$('div.focus')[0].style.backgroundPosition = "0 -800px"; //.setStyle({'background': 'green'});			
					//$('loading').style.backgroundPosition = "-200px";
					//$('loading').innerHTML = "";
					
					new Fx.Styles(target, { duration: 400	}).start({'opacity': 1});
				}
			}).inject(target);
		
		}
	}).start({'opacity': 0});
	
	$('righty').getFirst().getLast().getChildren().each(function(e){ 
		var text = e.getFirst().getFirst().getText();
		if (text == n) {
			e.getFirst().getFirst().addClass('shoot');
		} else {
			e.getFirst().getFirst().removeClass('shoot');
		}
	});
	
}

function fadeBackIn() {
		new Fx.Styles('shot', { duration: 1000 }).start({'opacity': 1});
}




function nextSet(option) {
	//console.log("nextSet()");
	
	// get NextSet by Id and replace text
	
	$('nextsetter').setText("prev 10");
	$('nextsetter').setProperty('href', '#');
	$('nextsetter').addEvent('click', prevSet);
	
	$('righty').getFirst().getLast().getChildren().each(function(e){ 
		
		var el = e.getFirst().getFirst();
		
		if (el.getText() != "") {
			
			set = parseInt(el.getText()) + 10;

			el.setText(set);

			el.setProperty('href', "#");
			el.addEvent('click', function(e) {
				openShot(this.getText(), 1);
			});
		}
		
	});
	
	if (option == "end") {
		openShot(20, 1);
	} else {
		openShot(11, 1);
	}
	
}


function prevSet(option) {
	//console.log("prevSet");
	
	$('nextsetter').setText("next 10");
	$('nextsetter').setProperty('href', '#');
	$('nextsetter').addEvent('click', nextSet);
	
	$('righty').getFirst().getLast().getChildren().each(function(e){ 
		
		var el = e.getFirst().getFirst();
		
		if (el.getText() != "") {
			
			set = parseInt(el.getText()) - 10;

			el.setText(set);

			el.setProperty('href', "#");
			el.addEvent('click', function(e) {
				openShot(this.getText(), 1);
			});
		}
		
	});
	
	if (option = "start") {
		openShot(1, 1);
	} else {
		openShot(10, 1);
	}

}



function openShotPrev(section) {
	var index = $('shot').getFirst().getProperty('src').substr($('shot').getFirst().getProperty('src').length - 6,2);
	var n;
	
	if (index.substr(0,1) == "/") {
		n = index.substr(1,1);
	} else {
		n = index.substr(0,2);
	}
	
	var aImages = $$('div.header')[0].getFirst().getNext().getChildren();

	if (n == 1) {
		openShot(aImages.length - 2, section);						
	} else {
		openShot(parseInt(n)-1, section);
	}
	
		
}

function openShotNext(section) {
	var index = $('shot').getFirst().getProperty('src').substr($('shot').getFirst().getProperty('src').length - 6,2);
	var n;
	
	if (index.substr(0,1) == "/") {
		n = index.substr(1,1);
	} else {
		n = index.substr(0,2);
	}
		
	var aImages = $$('div.header')[0].getFirst().getNext().getChildren();
	
	if (n == aImages.length - 2) {
		openShot(1, section);						
	} else {
		openShot(parseInt(n)+1, section);
	}

}




/*


name //input
phone //input
email //input

kind //select
property //select
building-age //select
start-time //select
planning //radio

documents-chk-written-outline
documents-chk-scrap-book
documents-chk-scope-work
documents-chk-design-drawings
documents-chk-permit-drawings
documents-chk-building-permit
documents-chk-combination
documents-chk-none

professional-chk-architect
professional-chk-architectural-technologist
professional-chk-designer
professional-chk-draftsman
professional-chk-builder
professional-chk-general-contractor
professional-chk-design-and-build-firm

flooring-chk-hardwood
flooring-chk-carpet
flooring-chk-tile
flooring-chk-other

bathrooms-how-many
bathroom-fixtures-for-each

finishes-chk-ceramic
finishes-chk-porcelain
finishes-chk-stone-with-marble
finishes-chk-vanity
finishes-chk-storage

cabinetry //select
countertops //select
doors //select

doors-painted-stained //radio
baseboard-painted-stained //radio

cabinetry-locations-and-types //textarea

baseboard-painted-stained //radio

heating-chk-forced-air
heating-chk-radiant
heating-chk-solar

cladding-chk-brick
cladding-chk-stone
cladding-chk-cement-board
cladding-chk-stucco
cladding-chk-aluminum-siding
cladding-chk-vinyl-siding
cladding-chk-wood-siding
cladding-chk-other

windows-chk-aluminum-clad-with-wood-interior
windows-chk-prefinished-wood
windows-chk-vinyl

eavestroughs //select
roof //select
budget //select

financing-arranged //radio

found-us-how //select


exceptions:

textarea#electrical-details
textarea#network-details
textarea#home-theatre-details
textarea#technology-upgrades
input#referral-person
textarea#comments



*/


function validate() {
	//console.log("validating form...");
	
	checkInputFields();
	checkCheckBoxes();
	checkRadioButtons();
	checkSelectBoxes();
	
	if (checkInputFields() &&
			checkCheckBoxes() &&
			checkRadioButtons() &&
			checkSelectBoxes()) {
		return true; // validation success
	} else {
		return false; // validation fail
	}
	
	/*
	if (!$$('li.missed')) {
		return true;
	} else {
		return false;
	}
	*/

}


function checkInputFields() {
	var bast = true;
	var required = $('q').getElements('input[type=text]');
	required.each(function(e) {
		if (!e.value && 
				 e.name != "referral-person") { // exception (not required)
			e.getParent().addClass('missed');
			//console.log(e.name + " has no value");
		} else {
			if (e.getParent().hasClass('missed')) { // remove class missed next time around
				e.getParent().removeClass('missed');
				bast = false;
			}
		}
	});
	return bast;
}


function checkCheckBoxes() { // possible problem.
	var bast = true;
	var required = $('q').getElements('li').getElements('input[type=checkbox]');
	required.each(function(e) {
  	if (e.length > 0) {
           var atleastone = false;
           e.each(function(f) {
               if (f.getValue()) {
                   atleastone = true; //it's alright!
               } else {
						bast = false;
					}
  });

			if (atleastone) {
				bast = true; // added may 28 2008
				if (e.getParent().getParent().hasClass('missed')) {
					e.getParent().getParent().removeClass('missed'); //remove outline if existant!
				}	
			} else {
				e.getParent().getParent().addClass('missed'); //outline it!
			}
    }
	});
	return bast;
}


function checkRadioButtons () {
	var bast = true;
	var required = $('q').getElements('li').getElements('input[type=radio]');
	required.each(function(e) {
	    if (e.length > 0) {
	        var onechecked = false;
	        e.each(function(f) {
	            if (f.checked) {
	                onechecked = true;
	            }	else {
								bast = false;
							}
	        });
	        if (onechecked) {
	            if (e.getParent().getParent().hasClass('missed')) {
	                e.getParent().getParent().removeClass('missed');
	            }
	        } else {
	            e.getParent().getParent().addClass('missed');
	        }
	    }
	});
	//return bast;
	return true;
}


function checkSelectBoxes() { // possible problem.
	var bast = true;
	var required = $('q').getElements('li').getElements('select');
	required.each(function(e) {
		if (e.length > 0) {
			var oneselected = false;
			e.each(function(f) {
				if (f.getValue() != "") {
					oneselected = true;
				} else {
					bast = false;
				}
			});
			if (oneselected) {
				bast = true; // added may 28 2008
				if (e.getParent().hasClass('missed')) {
					e.getParent().removeClass('missed');
				}
			} else {
				e.getParent().addClass('missed');
			}
		}
	});
	return bast;
}

/*
function checkTextAreas () {
	var bast = true;
		var required = $('questionnaire').getElements('li').getElements('textarea');
		required.each(function(e) {
			if (e.length > 0) {
	                    var onefilledin = false;
	                    e.each(function(f) {
	                        console.log(f);
	                        if (f.getValue()) {
	                            onefilledin = true;
	                        } else {
	                            bast = false;
	                        }
	                    });
	                    if (onefilledin) {
	                        console.log(e);
	                    }
	                }
		});
		return bast;
}
*/

function go () {
	
	//temp();
	
	if (validate()) {
		//console.log("validation successful");
		//$('q').send();
		
		var url = "q.php";
		
		var req = new Ajax(url, {
			method: 'post',
			data: $('q'),
			onRequest: function() {
				//console.log("Request init");
				$$('input[type=button]')[0].disabled = true;
			},
			onSuccess: function(txt, xml) {
			  //console.log("Success");
			  //alert("Thank You for your submission.  \nYou will be sent a confirmation email informing you that we have received your request.");
				// clear form:
				$('q').reset();
				$$('input[type=button]')[0].disabled = false;
				loadContent('thanks');
			},
			onFailure: function() {
				alert("Error sending form.  Please check all of the fields and try again.");
				$$('input[type=button]')[0].disabled = false;
			}
		}).request();
		
	} else {
		alert("Please fill in the form fields outlined in red.");
		//console.log("validation failed");
	}

}


function klear () {
	$('q').reset();
}


function temp () {
	
	$$('input[type=text]').each(function(e) {
		e.value = "test";
	});
	
	$$('input[type=checkbox]').each(function(e) {
     e.checked = true;
	});

	$$('input[type=radio]').each(function(e) {
			e.checked = true;
	});
	
	$$('select').each(function(e) {
	    e.value = "New House";
	    e.value = "Raw Land";
	    e.value = "1 to 24 years";
	    e.value = "1 to 3 months";
	    e.value = "Wood Stained";
	    e.value = "Laminate";
	    e.value = "Hollow Core Colonial";
	    e.value = "Small";
	    e.value = "Aluminum";
	    e.value = "Shingle";
	    e.value = "$1,000.00 to $10,000.00";
	    e.value = "Referral";
	});
	
}
