window.addEvent('domready',function() {
									
/*	new SmoothScroll({ duration:700 }, window);*/
/*	new Fx.SmoothScroll({
		links: '.smoothAnchors',
		wheelStops: false
	});*/
									
	/* Fall backs for HTML 5 attr such as placeholder & required */
	function supportsPlaceholder() {
		var i = document.createElement('input');
		return 'placeholder' in i;
	}
	
	var textFields = $$('textarea');
	var inputFields = $$('input').combine(textFields);
	
	inputFields.each(function(el) {
		if (!supportsPlaceholder()) {
			  var placeholderText = el.get('placeholder');
			  if (placeholderText) {
				  var textOverlay = new OverText(el, {
					textOverride: placeholderText
				  });
			  }
		}
	});
									
	if ( document.id("homepage_fader") != null ) {
											
		if ( document.id("news_snippet") != null ) 
		{
			
			
			window.addEvent('load',function() {
									
				(function() {
					var overlay2 = document.id("gdOverlay"), news_snippet = document.id("news_snippet");
					
					winWidth = window.getWidth();
					winHeight = window.getHeight();
					
					overlay2.setStyles({
						display: 'block', 
						width: winWidth, 
						height: winHeight,
						top: window.getScrollTop(), 
						left: window.getScrollLeft(),
						'z-index': 1
					});
					
					news_snippet.setStyle('display', 'block')
					
					fx = {
						overlay23: new Fx.Tween(overlay2, {property: "opacity", duration: 360}).set(0),
						news_snippet23: new Fx.Tween(news_snippet, {property: "opacity", duration: 360}).set(0)
					};
		
					(function() { 
							  
					  fx.overlay23.start(0.8);
					  fx.news_snippet23.start(1);
					  
					}).delay(360);
					
					//settings
					var fader = document.id("homepage_fader"), list = fader.getElements('li'), duration = 1500, count = 0;
				
					list.setStyle('opacity', '0');
					list[0].setStyle('opacity', '1');
					new Element("a", {
						title: "Saigon Fabrics",
						href: "/"
					}).wraps(list[list.length-1].getElement("img"));
					
					var run = function() {
			
						if ( count == (list.length-1) ) {
							killRun();
						} else {
							var k =	list[++count].set('tween', {duration: 1500});
							k.tween('opacity', 1);
						}		
					};
						
					var killRun = function() {
						clearInterval(process);
					}
					
					y = function() {
						
						fx.news_snippet23.start(0);						
						fx.overlay23.start(0);
						
						var process = run.periodical(duration);
						
						(function() { news_snippet.dispose(); } ).delay(360);
						
					}
					
					overlay2.addEvent("click", y);
					news_snippet.getElement(".close").addEvent("click", y);
					
					
					
				}).delay(360);
		
		//		overlay = new Fx.Tween(overlay, {property: "opacity", duration: 360}).set(0);
		//		overlay.set(1);
			
			});
		}
		else
		{
			//settings
			var fader = document.id("homepage_fader"), list = fader.getElements('li'), duration = 1500, count = 0;
		
			list.setStyle('opacity', '0');
			list[0].setStyle('opacity', '1');
			new Element("a", {
				title: "Saigon Fabrics",
				href: "/"
			}).wraps(list[list.length-1].getElement("img"));
			
			var run = function() {
	
				if ( count == (list.length-1) ) {
					killRun();
				} else {
					var k =	list[++count].set('tween', {duration: 1500});
					k.tween('opacity', 1);
				}		
			};
			
			var process = run.periodical(duration);
				
			var killRun = function() {
				clearInterval(process);
			}
			
			
		}
	
	}
	
	if ( document.id("quotes") != null ) {
									
		//settings
		var faderquotes = document.id("quotes"), listquotes = faderquotes.getElements('blockquote'), durationquotes = 5000, countquotes = 0;
	
		listquotes.setStyle('opacity', '0');
		listquotes[0].setStyle('opacity', '1');
		
		var runquotes = function() {
			
			
			
			var k2 = listquotes[countquotes].set('tween', {durationquotes: 2000});
			k2.tween('opacity', 0);

			if ( countquotes == (listquotes.length-1) ) countquotes = -1;	
				
			var k =	listquotes[++countquotes].set('tween', {durationquotes: 2000});
			k.tween('opacity', 1);

		};
		
		processquotes = (function() { 
			run = runquotes.periodical(durationquotes); 
		});
		
		processquotes();
										
		killQuotes = (function() {
			$clear(run);
		});
		
		faderquotes.addEvents({"mouseenter": killQuotes, "mouseleave": processquotes});
	
	}
		
	if (  document.id("canvas_fabrics") != null ) {
	
		//settings
		var loading_spinner = $$(".loading_spinner"), loading, loader, duration = 50, count3 = 0, div=12, div2=24;
	
		loading = function() {
			loader = (function() {
				++count3;
				loading_spinner.setStyle('background-position', -(count3*div2) + 'px top');
			}).periodical(duration);
		};
		
		var galleryHandler = document.id("canvas_fabrics"), 
			galleryImageFocus = galleryHandler.getElement('.display-large'),
			galleryMaginifier = galleryHandler.getElement('.magnifier'),
			galleryImageFrame = galleryHandler.getElement('.display-large-frame') || null,
			galleryImages = galleryHandler.getElements('.cf-thumbnails li :first-child');
			
		galleryImages.each(function(thumb, i) {
									
			galleryImages[i].addEvent('click', function() {
				loading();
				loading_spinner.setStyle('display', 'block');										
				galleryImageFocus.setStyle('display', 'none');
				var src = thumb.get('rel');
				if ( thumb.get('data') != null ) {
					 galleryHandler.getElement('h4').set('text', thumb.get('data'));
				}
				galleryMaginifier.set('rel', thumb.getParent().getElement("a").get('id'));
				galleryImageFocus.set('rel', thumb.getParent().getElement("a").get('id'));
				galleryImageFocus.set('src', src);
				GalleryDisplay.scanPage();
				
				(function() {
					$clear(loader);
					loading_spinner.setStyle('display', 'none');
 					var imgFocus = galleryImageFocus.set('tween', {duration: 1000});
					galleryImageFocus.setStyles({'display': 'block', 'opacity': 0});
					imgFocus.tween('opacity', 1);
				}).delay(500);
				
				
			});
			
		});
		
		galleryImages[0].fireEvent("click");

		if ( galleryImageFrame != null ) galleryImageFrame.addEvent("click", function() { galleryImageFrame.getNext().fireEvent('click'); });
		
		galleryMaginifier.addEvent("click", function(a) { document.id(this.get("rel")).fireEvent('click'); });
		galleryImageFocus.addEvent("click", function(a) { document.id(this.get("rel")).fireEvent('click'); });
	}
	
	if ( document.id("logo") != null ) {
		new Element("a", {
			title: "Saigon Fabrics",
			href: "/"
		}).wraps(document.id("logo").getElement("img"));
	}
	
	if ( document.id("page_useful-links") != null ) {
		document.id("content").getElements("li a").set("target", "_blank");
	}
	
	if ( document.id("signup") != null ) 
	{
		window.addEvent('load',function() {
											
			var overlay2 = document.id("gdOverlay"), signupBox = document.id("signupBox"), signupButton = document.id("signup");
						
			winWidth = window.getWidth();
			winHeight = window.getHeight();
			
			signupButton.addEvent("click", function(a) {
													
				a.stop();
			
				overlay2.setStyles({
					display: 'block', 
					width: winWidth, 
					height: winHeight,
					top: window.getScrollTop(), 
					left: window.getScrollLeft(),
					'z-index': 1
				});
				
				signupBox.setStyle('display', 'block')
				
				fx = {
					overlay23: new Fx.Tween(overlay2, {property: "opacity", duration: 360}).set(0),
					signupBox: new Fx.Tween(signupBox, {property: "opacity", duration: 360}).set(0)
				};
				
			
		
				(function() { 
						  
					fx.overlay23.start(0.8);
					fx.signupBox.start(1);
					
					y = function() {
						
						fx.signupBox.start(0);						
						fx.overlay23.start(0);
						
					}
					
					signupBox.getElement(".close").addEvent("click", y);
					
					signupBox.getElement("form").addEvent("submit", function() {
					
						error_counter = false;
						error_message = "";
						
						signupBox.getElement("form").getElements("[rel=required]").each(function(field) {
						
							if ( field.get('data') != null ) {
								
								if ( field.get('data') == "email" && !(/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+\/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(field.get('value')) ) {
									
									error_counter = true;
									error_message += "Please enter a valid email address. For example \"fred@domain.com\".\n";
						
								}
						
							} else {
								
								if ( field.get('value').replace(/^\s*|\s*$/g,'').length < 1 ) {
									error_counter = true;
									error_message += field.get('placeholder') + " field is required.\n";
								}
								
							}
							
						});
							
						if ( error_counter ) {
							alert(error_message);
							return false;
						}
					
					}); 
					
				}).delay(360);
						
			});

		});
		
	}
	
//	document.id(document.body).adopt(
//		$$([
//			overlay = document.id("signup_box").addEvent("click", close)
//		]).setStyle("display", "none")
//	); 
		
});	


