/* ------------------------------------------------------------

					  AdamsGolf.com
				~  adamsgolf.mootools.js  ~
			
						  Dependencies: 
							- /js
								|_ mootools-trunk-1547.js (v 1.2b 1547)
 
   ------------------------------------------------------------ */ 

var AdamsGolf = {
	
	homePage: 'http://www.adamsgolf.com',
	currentFontSize: null,
	currentLineHeight: null,
	fontDefaultSize: 16,
	fontSizeMax: 20,
	fontSizeMin: 14,
	fontCookie: 'AdamsGolfFont',
	fontCookieExpires: 365,
	gallery404: '/images/gallery/gallery-404.jpg',
	
	initializeDOM : function() {
		// Initialize DOM elements on domready, not for functions that require images

												AdamsGolf.winInit();				// Initialize window scroll FX
												AdamsGolf.loadSounds();				// if page uses FX load the flash file
												//AdamsGolf.textReplacement();		// sIFR replacement
		if ( el = $('search_text') ) 			AdamsGolf.enableSearch(el);			// style search box
												AdamsGolf.headerAddGradient();		// add gradient to headers
		if ( el = $('menu') ) 					AdamsGolf.sfHover(el);				// add nav hover class for IE6
		if ( el = $('entry-content') )			AdamsGolf.setFontSize(el);			// set font size for copy
		if ( el = $('article-tools') )			AdamsGolf.enableTools(el);			// enable article tools
		if ( el = $('skip-content') )			AdamsGolf.skipToContent(el);		// skip to content button
		if ( el = $('product-banner') )			AdamsGolf.productBanner(el);		// initialize product banners
		if ( $$('div.performance-chart') )		AdamsGolf.initChart(el);			// initialize performance charts
		if ( $$('div.clubset-clubs') || 
			 $$('div.clubset-tech') )			AdamsGolf.initClubset(el);			// initialize clubset specs
		if ( el = $('showChart') )				AdamsGolf.showChartBtn(el);			// add event to show performance chart animation
												AdamsGolf.productGallery(el);		// initialize product gallery
		if ( el = $('product-page') )			AdamsGolf.productCarousel();		// initialize product page carousel
												AdamsGolf.hybridMorph();			// add hybrind-fairway morph movie
		if ( el = $('home-slideshow') )			AdamsGolf.homeCarousel();			// initialize home page carousel
		if ( el = $('home-subnav') )			AdamsGolf.homeSubnav();				// initialize home subnav
		if ( el = $('dixx-screenshots') )		AdamsGolf.initDixxPutter(el);		// initialize carousel for DiXX putter
		if ( el = $('custom-options') )			AdamsGolf.customOptions(el);		// initialize custom options table and button
	},
	
	initializeSite : function() {
		// Initialize the rest of the site not applicable to initializeDOM
		var roar = new Roar({ position: 'lowerLeft' });
		//roar.alert('Development Copy Only','You are viewing a development copy of AdamsGolf.com.  Copyright 2008 Combined Arts Media, Inc.; All Rights Reserved.');
	},
	
	winInit : function() {
		AdamsGolf.winFX = new Fx.Scroll(window, {
    		duration: 400,
    		wait: false,
    		transition: Fx.Transitions.Quad.easeInOut
		});
	},
	
	hybridMorph : function() {
		if ( $('hybrid-wood-morph') ) {
			var so = new SWFObject("/swf/Hybrid-Fairway-Morph.swf?v=1.3", "hybridwoodmorph", "550", "265", "8");
			so.addParam("quality", "best");
			so.addParam("wmode", "transparent");
			so.write("hybrid-wood-morph");
		}
	},
	
	playMorph : function() {
		// setup Flash specific functions for ExternalInterface
		function callExternalInterface() {
			thisMovie("ExternalInterface").toggleReq();
		};
		
		function startMorph() {
			thisMovie("hybridwoodmorph").startmorph();	
		};
		
		function thisMovie(movieName) {
			if ( window.ie ) 
				return window[movieName];
			else 
				return document[movieName];
		};
		
		// play sound
		startMorph();
	},
	
	loadSounds : function() {
		if ( $('loadsounds') ) {
			var so = new SWFObject("/swf/sounds.swf?v=1.1", "mysounds", "1", "1", "8");
			so.addVariable("myVolume","35");
			so.addParam("scale","noscale");
			so.write("loadsounds");
		}
	},
	
	playSoundFX : function() {
		// setup Flash specific functions for ExternalInterface
		function callExternalInterface() {
			thisMovie("ExternalInterface").toggleReq();
		};
		
		function startSound() {
			thisMovie("mysounds").startsound();	
		};
		
		function thisMovie(movieName) {
			if ( window.ie ) 
				return window[movieName];
			else 
				return document[movieName];
		};
		
		// play sound
		startSound();
	},
	
	customOptions : function(el) {
		
		// set hide fx for table element
		var myTable = new Fx.Slide(el);
		myTable.hide();
		
		// setup button toggle
		$('view-custom-options').addEvent('click', function(e) {
			// stop event from firing
			e.stop();
			
			// toggle table
			myTable.slideIn();
			
			// style carousel height to current item height
			//var currentHeight 	= ( window.ie6 ) ? $('carousel-mask').clientHeight : $('carousel-mask').getStyle('height');
			//var theHeight 		= ( window.ie6 ) ? el.clientHeight : el.getStyle('height');
			var currentHeight 	= $('carousel-mask').getStyle('height');
			var theHeight 		= el.getStyle('height');
			
			//$('carousel-mask').fx.start(currentHeight+15,theHeight+15);
			
			if ( !window.ie6 ) {
				currentHeight = currentHeight.replace(/px$/,'');
				theHeight = theHeight.replace(/px$/,'');
			}
			
			//console.log(currentHeight+':'+theHeight+':'+(parseInt(currentHeight)+30));
			$('carousel-mask').fx.start(parseInt(currentHeight)+30,parseInt(currentHeight)+parseInt(theHeight));
			//alert(parseInt(currentHeight)+':'+theHeight+':'+(parseInt(currentHeight)+30));
			// hide element
			this.setStyle('display','none');
		});
	},
	
	initDixxPutter : function(el) {
		var screenshots = $$('#dixx-screenshots li');
		
		// change display for carousel items
		screenshots.each(function(el) {
			if ( window.ie6 ) 
				el.setStyle('display','inline'); // CSS fix for IE6 floats
			else	
				el.setStyle('display','block');
		});
		
		AdamsGolf.dixx = new noobSlide({
			mode: 'vertical',
			box: el,
			size: 127,
			items: screenshots,
			onWalk: function(currentItem) { console.log(currentItem.getFirst().alt); }
		});
	},
	
	homeSubnav : function() {
		// set up vars for boxes and shadows
		var navboxes 	= $$('#home-subnav li a');
		var navshadows 	= $$('#home-subnav li span');
		
		navboxes.each( function(box) {
			box.fx = new Fx.Styles(box,{duration:150, transition: Fx.Transitions.linear});
			var myShadow = box.getParent().getLast();
			box.addEvent('mouseenter', function(e) {
				this.fx.start({
					'margin-top': [0, -5],
					'margin-bottom': [0, 5]
				});
				myShadow.fx.start(1, 0.3);
			});
			box.addEvent('mouseleave', function(e) {
				this.fx.start({
					'margin-top': [-5, 0],
					'margin-bottom': [5, 0]
				});
				myShadow.fx.start(0.3, 1);
			});
		});
		navshadows.each( function(shadow) {
			shadow.fx = new Fx.Style(shadow,'opacity',{duration:200, transition: Fx.Transitions.linear});
		});
	},
	
	homeCarousel : function() {
		// set vars for the carousel
		var carousel 		= $('home-slideshow');
		var slideshowItems 	= $$('#home-slideshow li');
		var numItems		= slideshowItems.length;
		
		// set this ul as a carousel
		carousel.addClass('carousel');
		
		// change display for carousel items
		slideshowItems.each(function(el) {
			if ( window.ie6 ) 
				el.setStyle('display','inline'); // CSS fix for IE6 floats
			else	
				el.setStyle('display','block');
		});
		
		// setup handles
		var myHandles = new Element('ul', {
			'id': 'home-slideshow-handles',
			'class': 'clearfix'
		});
		for ( var i = 0; i < numItems; i++ ) {
			var newHandle = new Element('li', {
				html: i+1
			});
			newHandle.injectInside(myHandles);
		}
		
		// add handles to carousel container mask (disabled Aug 9, 2008 per Cindy Herington)
		myHandles.injectInside('carousel-mask');
		myHandles.getFirst().addClass('active');
		
		// setup noobSlide
		var cs = new noobSlide({
			box: carousel,
			startItem: 0,
			items: slideshowItems,
			size: 898,
			handles: $$('#home-slideshow-handles li'),
			interval: 15000,
			autoPlay: true,
			onWalk: function(currentItem,currentHandle){
				// style for handles
				this.handles.extend(carouselhandles).removeClass('active');
				$$(currentHandle,carouselhandles[this.currentIndex]).addClass('active');
			}
		});
		var carouselhandles = $$('#home-slideshow-handles li');
	},
	
	productCarousel : function() {
		// strip current sub-menu anchor href
		var subMenu = $$('ul.add-menu li a');
		if ( $('product-page').hasClass('product-overview') ) {
			subMenu.each( function(link) {
				link.href = '#';
			});
		} else {
			return;
		}
		
		// set this div as a carousel
		var carousel = $('page-content');
		carousel.addClass('carousel');
		$('carousel-mask').fx  = new Fx.Style('carousel-mask','height',{duration:400, transition: Fx.Transitions.linear});
		
		// change display for carousel items to counteract request for separate content pages
		$$('div.content-pane').each(function(el) {
			if ( window.ie6 ) 
				el.setStyle('display','inline'); // CSS fix for IE6 floats
			else	
				el.setStyle('display','block');
		});
		
		// setup noobSlide
		var cs = new noobSlide({
			box: carousel,
			startItem: 0,
			items: $$('div.content-pane'),
			size: 898,
			handles: $$('ul.add-menu li'),
			onWalk: function(currentItem,currentHandle){
				// play sound
				AdamsGolf.playSoundFX();
				
				// style for handles
				this.handles.extend(carouselhandles).removeClass('active');
				$$(currentHandle,carouselhandles[this.currentIndex]).addClass('active');
				
				// style carousel height to current item height
				var currentHeight 	= $('carousel-mask').getStyle('height');
				var theHeight 		= ( window.ie6 ) ? currentItem.clientHeight : currentItem.getStyle('height');
				$('carousel-mask').fx.start(currentHeight+15,theHeight+15);
				
				// init chart or Dixx carousel or hFW morph movie
				if ( this.currentIndex == 1 ) {
					if ( AdamsGolf.dixx ) {
						AdamsGolf.dixx.walk(0);
						AdamsGolf.dixx.play(5000,'next',1000);
					}
					if ( $('hybrid-wood-morph') ) AdamsGolf.playMorph();
				} else {
					if ( AdamsGolf.dixx ) AdamsGolf.dixx.stop();
				}
			}
		});
		
		// carousel handles
		var carouselhandles = $$('ul.add-menu li');
		
		// set first handle to active
		carouselhandles[0].addClass('active');
		
		// fix height for first item
		var myItems		= $$('div.content-pane');
		var maskHeight 	= $('carousel-mask').getStyle('height');
		var firstHeight = ( window.ie6 ) ? myItems[0].clientHeight : myItems[0].getStyle('height');
		$('carousel-mask').fx.start(maskHeight+15,firstHeight+15);
	},
	
	textReplacement : function() {
		var UniversExtBold = {src: '/swf/sIFR/UniversExtBold.swf'};
		sIFR.activate(UniversExtBold);
		sIFR.replace(UniversExtBold, {selector: 'h2.product-title', wmode: 'transparent', css: ['.sIFR-root { margin-bottom: 0; color: #6d6d6d; z-index: 1;}']});
		sIFR.replace(UniversExtBold, {selector: 'h3.sidebar-title', wmode: 'transparent', css: ['.sIFR-root { color: #cc0000; text-transform: uppercase; z-index: 1;}']});
		sIFR.replace(UniversExtBold, {selector: 'ul#clubset-tech li', wmode: 'transparent', css: ['.sIFR-root { color: #3f3f3f; font-size: 12px; z-index: 1;}']});
		//sIFR.debug.ratios({ src: '/swf/sIFR/UniversExtBold.swf', selector: 'h2.product-title' });
	},
	
	headerAddGradient : function() {	
		// dismiss ie6
		if ( window.ie6 ) return false;
		
		// dismiss womens section
		if ( $('content').hasClass('womens') ) return false;
		
		// initialize vars
		var myHeaders 		= $$('h2.product-title');
		
		// for each header add span to DOM to trigger gradient with opacity FX
		myHeaders.each( function(header) {
			// make each header relative positioning
			header.setStyle('position','relative');
			var gradientSpan		= new Element('span', {'class':'text-gradient'});
			gradientSpan.myEffect 	= new Fx.Style(gradientSpan,'opacity',{duration: 800, transition: Fx.Transitions.linear}).set(0);
			gradientSpan.injectTop(header);
			gradientSpan.myEffect.start(0,1);
		});
	},
	
	productBanner : function(el) {
		// if ie6 kill function
		if ( !window.ie ) {
		
			// initialize banners
			var myBanners = $$('div#product-banner img');
			
			// if only one banner make sure its visible otherwise start FX
			if ( myBanners.length > 1 ) {
				// initiate chain class	
				var myChain = new Chain();
				
				// loop through each banner and set FX and add to chain
				var z = 1;
				var numBanners = myBanners.length;
				myBanners.each( function(banner) {
					banner.setStyle('zIndex',z);
					banner.setStyle('display','block');
					if ( z > 1 ) {
						banner.myEffect = new Fx.Style(banner,'opacity',{duration: 1000, transition: Fx.Transitions.linear}).set(0);
						myChain.chain( function() { 
							banner.myEffect.start(0,1); 
						});
					}
					z++;
				});
				
				// run chain function
				var runChain = function() { 
					myChain.callChain();
					if ( myChain.$chain.length == 0 ) {
						runChain = $clear(timer);
					}
				}
				
				// start chain
				var myTime	= ( t = $('banner-timer') ) ? t.value : 1200;
				var timer 	= runChain.periodical(myTime);
			
			} else {
				myBanners[0].setStyle('display','block');
			}
		}
	},
	
	productGallery : function(el) {
		// Init gallery
		if ( $$('div.product-gallery') ) {
		
			// Init vars
			var gallerySm		= $$('ul.gallery-thumbs li');
			var galleryFrames	= $$('div.gallery-lg img');
			var galleryThumbs	= $$('ul.gallery-thumbs li a');
			var thumbLinks		= new Array();
			
			galleryFrames.each( function(e) {
				e.addEvent('error', function() {
					this.setProperty('src',AdamsGolf.gallery404);
				});
			});
			
			// set click events for each thumbnail
			galleryThumbs.each( function(e) {
				var myLgFrame = e.getParent().getParent().getParent().getFirst().getFirst();
				var myImage = e.href.replace(AdamsGolf.homePage,"");
				thumbLinks.include(myImage);
				e.addEvent('mouseenter', function(ev) {
					gallerySm.each( function(t) {
						t.removeClass('active');
					});
					myLgFrame.setProperty('src',myImage);
					e.getParent().addClass('active');
					ev.stop();
				});
				e.addEvent('click', function(ev) {
					ev.stop();
				});
			});
					
			// set first thumb to active
			/*
			var firstImage = galleryFrame.getProperty('src');
			var t = 0;
			thumbLinks.each( function(link) {
				if ( link == firstImage ) gallerySm[t].addClass('active');
				t++;
			});
			*/
		}
	},
	
	showChartBtn : function(el) {
		el.addEvent('click', function(e) {
			AdamsGolf.showChart();
			e.stop();
		});
	},
	
	initChart : function(el) {
		// Init vars
		var chartBars  = $$('span.chart-line');
		var chartBkgds = $$('ul.performance-chart li');

		// Set chart bars to be hidden
		chartBars.each( function(e) {
			e.myWidth  = 62 * (e.getStyle('width').toInt() / 100);
			e.myEffect = e.effect('width', {duration: 1000, transition: Fx.Transitions.linear}).set(0);
		});
		
		// Set backgrounds opacity to 0
		chartBkgds.each( function(e) {
			e.myEffect = new Fx.Style(e,'opacity',{duration: 600, transition: Fx.Transitions.linear}).set(0);
		});
		
		AdamsGolf.showChart();
	},
	
	showChart : function() {
		// Init vars
		var chartBars  = $$('span.chart-line');
		var chartBkgds = $$('ul.performance-chart li');
		
		chartBkgds.each( function(e) { e.myEffect.set(0) });
		chartBars.each( function(e) { e.myEffect.set(0) });
		
		// initiate chain class	
		var myChain = new Chain();
	
		// for each background set fx chain
		chartBkgds.each( function(e) { myChain.chain( function() { e.myEffect.start(0,1); }); } );
		
		// add show bars to chain
		myChain.chain( function() {
			chartBars.each( function(e) {
				e.myEffect.start(0,e.myWidth);
			});
		});
		
		// run chain function
		var runChain = function() { 
			myChain.callChain(); 
			if ( myChain.$chain.length == 0 ) runChain = $clear(timer);
		}
		
		// start chain
		var timer = runChain.periodical(300);
	},
	
	initClubset : function(el) {
		// Init vars
		var clubsetClubs  	= $$('ul.clubset-clubs li');
		var clubsetTech 	= $$('ul.clubset-tech li');
		
		if ( clubsetClubs.length == 0 && clubsetTech.length == 0 ) return false;
		
		// Set opacity to 0
		clubsetClubs.each( function(e) {
			e.myEffect = new Fx.Style(e,'opacity',{duration: 400, transition: Fx.Transitions.linear}).set(0);
		});
		clubsetTech.each( function(e) {
			e.myEffect = new Fx.Style(e,'opacity',{duration: 400, transition: Fx.Transitions.linear}).set(0);
		});
		
		AdamsGolf.showClubset();
		
	},
	
	showClubset : function() {
		// Init vars
		var clubsetClubs  	= $$('ul.clubset-clubs li');
		var clubsetTech 	= $$('ul.clubset-tech li');
		
		clubsetClubs.each( function(e) { e.myEffect.set(0) });
		clubsetTech.each( function(e) { e.myEffect.set(0) });
		
		// initiate chain class	
		var myChain = new Chain();
	
		// for each set fx chain
		clubsetClubs.each( function(e) { myChain.chain( function() { e.myEffect.start(0,1); }); } );
		clubsetTech.each( function(e) { myChain.chain( function() { e.myEffect.start(0,1); }); } );
		
		// run chain function
		var runChain = function() { 
			myChain.callChain(); 
			if ( myChain.$chain.length == 0 ) runChain = $clear(timer);
		}
		
		// start chain
		var timer = runChain.periodical(250);
		
	},
	
	skipToContent : function(el) {
		if ( !window.ie6 && !window.ie7 ) {
			el.addEvent('click', function(e) { 
				if ( content = $('content') ) {
					AdamsGolf.winFX.toElement(content);
					e.stop();
				}
			});
		}
	},
	
	setFontSize : function(el) {
		// check for font size cookie
		if ( fontsize = AdamsGolf.checkCookie(AdamsGolf.fontCookie) ) 
		{
			AdamsGolf.currentFontSize 	= parseInt(fontsize);
			AdamsGolf.currentLineHeight 	= AdamsGolf.currentFontSize + 5;
			el.setStyle('fontSize', AdamsGolf.currentFontSize + 'px');
			el.setStyle('lineHeight', AdamsGolf.currentLineHeight + 'px');
			//el.style.fontSize 		= AdamsGolf.currentFontSize + 'px';
			//el.style.lineHeight 	= AdamsGolf.currentLineHeight + 'px';
			//alert(currentFontSize+' : '+currentLineHeight);
		} 
		else 
		{
			AdamsGolf.currentFontSize 	= AdamsGolf.fontDefaultSize;
			AdamsGolf.currentLineHeight 	= AdamsGolf.currentFontSize + 5;
			AdamsGolf.createCookie(AdamsGolf.fontCookie,AdamsGolf.currentFontSize,AdamsGolf.fontCookieExpires);
		}
	},
	
	enableTools : function(el) {
		var content 	= $('entry-content');		// content to resize
		var btn_up		= $('font-increase');		// increase font button
		var btn_down	= $('font-decrease');		// decrease font button
		var btn_reset	= $('font-reset');			// reset font button
		
		btn_up.addEvent('click', function(e) { 
			if ( AdamsGolf.currentFontSize <= AdamsGolf.fontSizeMax ) AdamsGolf.currentFontSize += 2;
			AdamsGolf.adjustFont(content);
			e.stop();
		});
		btn_down.addEvent('click', function(e) { 
			if ( AdamsGolf.currentFontSize >= AdamsGolf.fontSizeMin ) AdamsGolf.currentFontSize -= 2;
			AdamsGolf.adjustFont(content);
			e.stop();
		});
		btn_reset.addEvent('click', function(e) { 
			AdamsGolf.currentFontSize = AdamsGolf.fontDefaultSize;
			AdamsGolf.adjustFont(content);
			e.stop();
		});
	},
	
	adjustFont : function(el) {
		AdamsGolf.currentLineHeight 	= AdamsGolf.currentFontSize + 5;
		el.setStyle('fontSize', AdamsGolf.currentFontSize + 'px');
		el.setStyle('lineHeight', AdamsGolf.currentLineHeight + 'px');
		//el.style.fontSize 		= AdamsGolf.currentFontSize + 'px';
		//el.style.lineHeight 	= AdamsGolf.currentLineHeight + 'px';
		AdamsGolf.createCookie(AdamsGolf.fontCookie,AdamsGolf.currentFontSize,AdamsGolf.fontCookieExpires);
	},
	
	enableSearch : function(el) {
		var searchDefaultVal 	= "Search Adams Golf..."; 	// Default Value
		var searchAutoSave 		= "com.AdamsGolf.search"; 					// Autosave URL
		var searchNumResults 	= 10; 									// Number of Results
		if ( window.webkit ) {
				// Change input type to "search" - Safari/WebKit ONLY
			el.setAttribute('type', 'search');
            el.setAttribute('placeholder', searchDefaultVal);
            el.setAttribute('autosave', searchAutoSave);
            el.setAttribute('results', searchNumResults);
		} else {
				// Toggle input value of normal text input
            el.onfocus = function() { if (el.value == searchDefaultVal) el.value = ''; };
            el.onblur  = function() { if (el.value == '') el.value = searchDefaultVal; };
            if (el.value == '') el.value = searchDefaultVal;
		}
	},
	
	sfHover : function(el) {
		if ( window.ie ) {
			var sfEls = el.getElementsByTagName("LI");
			for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() {
					this.className+=" sfhover";
				}
				sfEls[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
				}
			}
		}
	},
	
	checkCookie : function(name) {
		return Cookie.get(name);
	},
	
	createCookie : function(name,value,days) {
		Cookie.set(name, value, {duration: days, path: "/"});
	}
	
};

	// Initialize Site
window.addEvent('domready', AdamsGolf.initializeDOM);
window.addEvent('load', AdamsGolf.initializeSite);