var SS = {
	
	init: function () {
		log( '---->SS.init()' );
		SS.setHeaderNav();
		SS.headerImageReplace();
		SS.setBackToTop();
		SS.setHomeVariantNav();
		SS.checkHomeIntroCookie();
		SS.setTabCallout();
		SS.setCollectionNav();
		SS.setRecoPosition();
		SS.faqAccordion();
	}, //-- SS.init()

	/*
	 * Header Navigation
	 */
	setHeaderNav: function () {
		if ( $( '.categoryNavigation .navItem' ).size() === 0 ) return;
		log( '---->SS.setHeaderNav()' );
		// hover states
		$( '.navItem' ).find( 'a' ).not( '.isPage' ).hover(
			function () { $( this ).addClass( 'ON' ); },
			function () { $( this ).removeClass( 'ON' ); }
		);
		// <br> search - margin/height fix
		$( '.navTab.Text' ).each( function () {
			var regCheck = /<br[^>]*>/i;
			if ( $( this ).html().match( regCheck ) !== null ) {
				log( 'Found Match: ' + $( this ).html() );
				$( this ).css({
					'paddingTop': '10px',
					'height': '18px'
				});
			}
		});
	}, //-- SS.setHeaderNav()

	/*
	 *	Home
	 */	
	setHomeVariantNav: function () {
		if ( $( 'nav.Variant.Home' ).size() === 0 ) return;
		log( '---->SS.setHomeVariantNav()' );
		SS.verticalAlign( 'nav.Variant figure img', 'marginTop' );
		var homeHeroWidth = $( 'nav.Variant figure img' ).width(),
			bWidth = Utils.getBrowserDims( 'width' ),
			homeOffset = parseInt( Utils.itemOffset( $( 'nav.Variant figure' ), 'left' ) );
		if ( bWidth >= homeHeroWidth ) {
			altLeft = homeOffset;
		} else {
			altLeft = ( homeHeroWidth - bWidth ) / 2;
		}
		$( 'nav.Variant figure' ).css( 'left', '-' + altLeft + 'px' );
		$( 'nav.Variant figure' ).animate({
			left: '-' + altLeft + 'px'
		}, 500, 'linear' );
	}, //-- SS.setHomeVariantNav()
	
	destroyVideo: function () {
		log( '---->SS.destroyVideo()' );
		$( '#flashContainer' ).fadeOut();
		$( 'aside.Social, section.Category, aside.calloutTabs' ).fadeIn();
		SS.setHomeIntroCookie();
	}, //-- SS.destroyVideo()
	
	checkHomeIntroCookie: function () {
		if ( $( '#flashContainer' ).size() === 0 ) return;
		log( '---->SS.checkHomeIntroCookie()' );
		/* Cookie Data */
		var ss_introCookie_name = 'ss_intro';
		var ss_introCookie_date = new Date();
			ss_introCookie_date.setDate( ss_introCookie_date.getDate() + 1 );
		var ss_introCookie_year = ss_introCookie_date.getFullYear();
		var ss_introCookie_month = ss_introCookie_date.getMonth();
		var ss_introCookie_day = ss_introCookie_date.getDate();
		var ss_introCookie_expires = new Date( ss_introCookie_year, ss_introCookie_month, ss_introCookie_day, 00, 01, 00 );
		var ss_introCookie_path = '/';
		var results = document.cookie.match( '(^|;) ?' + ss_introCookie_name + '=([^;]*)(;|$)' );
		// if no cookie set
		if ( !results ) {
			$( 'aside.Social, section.Category, aside.calloutTabs' ).hide();
			loadIntroVideo();
		} else {
			$( '#flashContainer' ).remove();
		}
	}, //-- SS.checkHomeIntroCookie()
	
	setHomeIntroCookie: function () {
		log( '---->SS.setHomeIntroCookie()' );
		var ss_introCookie_name = 'ss_intro';
		var ss_introCookie_date = new Date();
			ss_introCookie_date.setDate( ss_introCookie_date.getDate() + 1 );
		var ss_introCookie_year = ss_introCookie_date.getFullYear();
		var ss_introCookie_month = ss_introCookie_date.getMonth();
		var ss_introCookie_day = ss_introCookie_date.getDate();
		var ss_introCookie_expires = new Date( ss_introCookie_year, ss_introCookie_month, ss_introCookie_day, 00, 01, 00 );
		var ss_introCookie_path = '/';
		document.cookie = ss_introCookie_name + '=introSet;path=' + ss_introCookie_path + ' ;expires=' + ss_introCookie_expires;
	}, //-- SS.setHomeIntroCookie()

	setTabCallout: function () {
		if ( $( 'aside.calloutTabs' ).size() === 0 ) return;
		log( '---->SS.setTabCallout()' );
		/*	set overall width of aside.calloutTabs for centering
			set z-index so items stack from left to right */
		var setWidth = 0;
		$( 'aside.calloutTabs' ).find( 'a' ).each( function ( i ) {
			setWidth = setWidth + $( this ).outerWidth();
			$( this ).css( 'zIndex',310 - i );
		});
		$( 'aside.calloutTabs' ).css( 'width',setWidth + 'px' );
	}, //-- SS.setTabCallout();

	/*
	 *	Collection Navigation
	 */
	setCollectionNav: function () {
		if ( $( '.collectionNavigation' ).size() === 0 ) return;
		log( '---->SS.setCollectionNav()' );
		if ( $( 'aside.calloutTabs').find( 'a' ).size() === 0 ) {
			$( 'footer' ).css( 'top','29px' );
		}
		// set first and last items
		var navItemFirst = $( '.collectionNavigation' ).find( 'a:first h1' ), navItemLast = $( '.collectionNavigation' ).find( 'a:last h1' ), i, count = 1, cnHeight;
		// add dividers
		$( '.collectionNavigation' ).find( 'a' ).each( function ( i ) {
			if ( count <= i ) {
				log( 'adding divider class' );
				$( this ).before( $( '<div class="divider"></div>' ) );
				count = count + 1;
			}
		});
		SS.collectionNavMatchHeight();
		SS.collectionNavHover();
	}, //-- SS.setCollectionNav()

	collectionNavMatchHeight: function () {
		log( '---->SS.collectionNavMatchHeight()' );
		var minPaddingTop = $( '.collectionNavigation' ).find( 'a h1' ).css( 'paddingTop' ),
			minPaddingBottom = $( '.collectionNavigation' ).find( 'a h1 ' ).css( 'paddingBottom' ),
			minPadTotal = minPaddingTop + minPaddingBottom;
		// set matching height
		cnHeight = Utils.get_min_max($( '.collectionNavigation' ).find( 'a' ),'vertical' )[1] - minPadTotal;
		$( '.collectionNavigation' ).find( 'h1' ).each( function () {
			$( this ).css( 'height', cnHeight + 'px' );
		});
		$( '.collectionNavigation' ).find( '.divider' ).each( function () {
			$( this ).css( 'height', ( cnHeight - minPadTotal ) + 'px' );
		});
	}, //-- SS.collectionNavMatchHeight()

	collectionNavHover: function () {
		log( '---->SS.collectionNavHover()' );
		// hover states
		$( '.collectionNavigation' ).find( 'a' ).not( '.isPage' ).hover (
			function () { $( this ).addClass( 'OVER' ); },
			function () { $( this ).removeClass( 'OVER' ); }
		);
	}, //-- SS.collectionNavHover()

	/*
	 *	Product Detail
	 */
	setRecoPosition: function () {
		if ( $( 'section.Reco' ).size() === 0 ) return;
		log( '---->SS.setRecoPosition() ');
		SS.verticalAlign( $( 'section.Reco figure img' ), 'marginTop' );
	}, //-- SS.setRecoPosition()
	
	/*
	 *	FAQs
	 */
	faqAccordion: function () {
		if( $( '#accordion' ).size() === 0 ) return;
		log( '---->SS.accordionSetup()' );
		$("#accordion").accordion({ header: "h3", autoHeight: false });
		//hover states on the static widgets
		$('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);
	}, //-- SS.accordionSetup()
	
	/* COMMON */
	headerImageReplace: function ( isDetail ) {
		log( '---->SS.headerImageReplace()' );
		var detailOverride = '';
		if ( isDetail === true ) { detailOverride = '.Detail '; }
		if ( $( detailOverride + 'hgroup img' ).size() !== 0 ) {
			$( detailOverride + 'hgroup h1' ).addClass( 'ir' );
			$( detailOverride + 'hgroup h2' ).addClass( 'ir' );
		}
		if ( $( '#subPageHeader img' ).size() !== 0 ) {
			$( '#subPageHeader #subTitle, #subPageHeader #title' ).addClass( 'ir' );
		}
	}, //-- SS.headerImageReplace
	
	setBackToTop: function () {
		if ( $( '.BackToTop' ).size() === 0 ) return;
		log( '---->SS.setBackToTop()' );
		$( '.BackToTop' ).click( function ( event ) {
			event.preventDefault();
			window.scrollTo( 0,0 );
		});
	}, //-- SS.setBackToTop
	
	setLoadingGraphic: function ( display ) {
		log( '---->SS.setLoadingGraphic()' );
		var slgContainer = $( '#DynamicDataHolder' );
		if ( $( slgContainer ).find( '.overlay' ).size() === 0 ) {
			slgContainer.append( '<div class="overlay"></div><div class="graphic"></div>' );
			$( slgContainer ).find( '.overlay' ).css( 'opacity','0.2' );
		}
		switch ( display.toLowerCase() ) {
			case 'on':
				$( slgContainer ).show();
				break;
			case 'fadein':
				$( slgContainer ).fadeIn();
				break;
			case 'off':
				$( slgContainer ).fadeOut();
				break;
			default: log( '-->ERROR: SS.setLoadingGraphic() - switch passed bad value: ' + display + '\nShould be ON or OFF' );
		}
	}, //-- SS.setLoadingGraphic

	/* HELPERS */
	verticalAlign: function ( group, marginType ) {
		log( '---->SS.verticalAlign()' );
		var matchHeight = Utils.get_min_max( group, 'vertical' )[1], marginAdd = 0;
		$( group ).each(function ( i ) {
			marginAdd = matchHeight - $( this ).innerHeight();
			$( this ).css( marginType,marginAdd + 'px' );
			marginAdd = 0;
		});
	}, //-- SS.verticalAlign()
	
	defineAjax: function () {
		log( '---->SS.defineAjax()' );
		$.ajaxSetup({
			dataType: 'html',
			type: 'GET',
			crossDomain: true,
			timeout: 3000,
			error: function( xhr ) {
				log( '\n\nERROR: ---->IIC.defineAjax()\n' + xhr.status + ': ' + xhr.statusText + '\n\n' );
			}
		});
	}, //-- SS.defineAjax
	
	doAjax: function ( pushURL, setTimeout, setAsync, callback, pushParams ) {
		log( '---->SS.doAjax()' );
		log( 'Ajax Loading: ' + pushURL );
		$.ajax({
			timeout: setTimeout,
			async: setAsync,
			cache: true,
			url: pushURL,
			success: function( data ) {
				log( 'SUCCESS' );
				if ( callback ) {
					if ( pushParams ) {
						callback.call( this, data, pushParams );
					} else if ( !pushParams ) {
						callback.call( this, data );
					}
				}
			}
		});
	} //-- SS.doAjax

};
	
$(document).ready( function () {
	SS.init();
});
