window.addEvent('domready', function() {
	//Initializers
	if (Browser.Engine.trident) {
		(function() { initTabs($$("#navigator li")); }).delay(3000)
	}
	else {
		initTabs($$("#navigator li"));
	}

	initAbout();

	$('logo').addEvent('click', function(e) {
		new Event(e).stop();
		$('home_link').fireEvent('click');
	});
	
	//Shop Email Signups
	$('signup_form').addEvent('submit', function(e) {
		new Event(e).stop();
		new Request({
			url : "/signups?email=" + $("signup_email").getProperty('value'),
			method : "post",
			onComplete : function() {
				$('signup_bin').empty().set('html', this.response.text);
			}
		}).send();
	});
});

function initAbout() {
	var aboutScroll = new Fx.Scroll('about_scroller', {
		wait: false,
		duration: 1250,
		transition: Fx.Transitions.easeInOut
	});
	
	//Make sure first scroller shows on page load
	$('about_section').setStyle('display', 'block');
	$('about_icon').setStyle('visibility', 'visible');
	new Fx.Scroll('about_scroller', {wait: false, duration: 0 }).start(0,0);
	$('about').setStyle('height', 35);
	
	//Set up scrolling tabs
	var aboutSubTabs = $$("#about .sub_tab");
	aboutSubTabs.each(function(subTab, i) {
		subTab.addEvent('click', function(e, i) {
			new Event(e).stop();
			var target = $(this.getProperty('rel'));
			aboutSubTabs.each(function(st) { st.removeClass('sub_current'); });
			if (target.getProperty('id') != 'welcome') {
				$('about_icon').setStyle('display', 'none');
			}
			else {
				(function() { $('about_icon').setStyle('display', 'block'); }).delay(900);
			}
			aboutScroll.toElement(target);
		});
	});
}

function initTabs(tabs) {
	if (Browser.Engine.trident) {
		var sectionFader = new Fx.Tween($('section_wrapper'), { duration : 0 });		
	}
	else {
		var sectionFader = new Fx.Tween($('section_wrapper'));	
	}
	
	var sections = $$(".section");
	var sectionBins = $$(".section_bin");
	var subMenus = $$("ul.sub_navigator");
	tabs.each(function(tab, i) {
		tab.addEvent('click', function() {
			var targetSection = $(tab.getProperty("rel") + "_section");
			var targetSubMenu = $(tab.getProperty("rel"));
			
			// Assign background image
			if (targetSection.getChildren("div[class=section_bin]")[0]) {
			  var targerSectionBin = targetSection.getChildren("div[class=section_bin]")[0];
			  if (targerSectionBin.getProperty('rel') != null) {
			    targerSectionBin.setStyle('background-image', "url(" + targerSectionBin.getProperty('rel') + ")");
			  }
			}
			
			sectionFader.start('opacity', 0).chain(function() {
				//Hide All Sections
				sections.each(function(section) { section.setStyle('display', 'none'); });
				sectionBins.each(function(bin) { bin.setStyle('display', 'none'); });
				$$("div[class=overlay]").each(function(overlay) { overlay.setStyle('visibility', 'hidden') });
				//Set Current Tab
				tabs.each(function(othertabs) { othertabs.removeClass('current') });
        targetSubMenu.getChildren("li").each(function(otherSubTab) { otherSubTab.removeClass('sub_current'); });
				tab.addClass('current');
				//Hide All Sub Menus
				subMenus.each(function(s) { s.tween('height', 0); });
				this.callChain();
			}).chain(function() {
				//Now Show Sub Menu
				targetSubMenu.tween('height', 90);
				//Set Current Section, Fade In and Initialize. . .
				switch (tab.getProperty("rel")) {
					case "about" :
						initAbout();
						break;
					case "brands" :
						$('brands_overlay').setStyle('visibility', 'visible');
						$('current_section').setProperty('rel', $$(targetSection.getChildren("div[class=section_bin]"))[0].getProperty('id'));
						$($('current_section').getProperty('rel')).setStyle('display', 'block').fade('in');
						initSubTabs(targetSubMenu.getChildren("li"), targetSection.getChildren("div.section_bin"));
						break;
					case "retail" :
						$('current_section').setProperty('rel', $$(targetSection.getChildren("div[class=section_bin]"))[0].getProperty('id'));
						$($('current_section').getProperty('rel')).setStyle('display', 'block').fade('in');
						(function() { 
							$('retail_overview').getFirst("div[class=thumb_bar]").tween('height', 50);
						}).delay('2000');
						break;
					default :
						$('current_section').setProperty('rel', $$(targetSection.getChildren("div[class=section_bin]"))[0].getProperty('id'));
						$($('current_section').getProperty('rel')).setStyle('display', 'block').fade('in');
						initSubTabs(targetSubMenu.getChildren("li"), targetSection.getChildren("div.section_bin"));
				}
				targetSection.setStyle('display', 'block');
				if (Browser.Engine.trident) {
					targetSection.setStyles({ 
						'height' 	: 420,
						'overflow' 	: 'visible'
					});
				}
				this.callChain();
			}).chain(function() {
				//Bring Main Section Back
				sectionFader.start('opacity', 1);
			});
		});
	});
}

function initSubTabs(subTabs, subSections) {
	subTabs.each(function(st) {
		st.addEvent('click', function() {
		  // Assign background image
			$$("div.thumb_bar").each(function(tb) { tb.setStyle('height', 0); });
			$$("div.overlay").each(function(o) { o.setStyle('visibility', 'hidden') });
			var exitBin = $($('current_section').getProperty('rel'));
			var targetBin = $(st.getProperty("rel"));
			
      if (targetBin.getProperty('rel') != null) {
        targetBin.setStyle('background-image', "url(" + targetBin.getProperty('rel') + ")");
      }
			
			var thumbBar = $(targetBin.getFirst("div[class=thumb_bar]"));
			var targetOverlays = targetBin.getChildren("div[class=overlay]");
			
			
			if (thumbBar) {
				var targetThumbs = thumbBar.getChildren("img[class=thumb]");
			}
			
			subTabs.each(function(otherSubTab) { otherSubTab.removeClass('sub_current'); });
			
			st.addClass('sub_current');
			new Fx.Tween(exitBin).start('opacity', 0).chain(function() {
				exitBin.setStyle('display', 'none');
				$(targetBin).setStyles({
					'opacity' : 0,
					'display' : 'block'
				});
				$('current_section').setProperty("rel", targetBin.getProperty("id"));
				this.callChain();
			}).chain(function() {
				//NOT FADING RIGHT
				targetBin.fade('in');
				this.callChain();
			}).chain(function() {
				if (targetOverlays[0]) {
					targetOverlays[0].setStyle('visibility', 'visible');
					if (targetBin.getProperty('id') == 'juniors') {
						(function() { $('juniors_bubble').fade('in'); }).delay('2000');
						(function() { $('juniors_bubble').fade('out'); $('juniors_bubble2').fade('in'); }).delay('7000');
						(function() { $('juniors_bubble2').fade('out'); $('juniors_bubble3').fade('in'); }).delay('12000');
						(function() { $('juniors_bubble3').fade('out'); $('juniors_bubble').fade('in'); }).delay('17000');
						(function() { $('juniors_bubble').fade('out'); $('juniors_bubble2').fade('in'); }).delay('22000');
						(function() { $('juniors_bubble2').fade('out'); $('juniors_bubble3').fade('in'); }).delay('27000');
					}
				}
				if (thumbBar) {
					thumbBar.tween('height', 90);
					targetThumbs.each(function(thumb, i) {
						thumb.addEvent('mouseenter', function() {
							if (targetOverlays.length == 0) return;
							targetOverlays.each(function(otherOverlay) { otherOverlay.setStyle('visibility', 'hidden') });
							targetOverlays[i].setStyle('visibility', 'visible');	
						});
					});	
				}
			});
		});
	});
}
