/********************************

	global.js
	Global JavaScript Document
	- I contain custom JavaScript written for qualitycabinets.com
	- I should be included throughout the site
	
	Notes:
		+ I rely on JQuery v1.4.2

********************************/

// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} }; }

var quality = {	
	 setButtonStyles: function() {
	 	var $myWidth, $myHeight;
	 	var $myHorzPadding = 0;
	 	var $myVertPadding = 0;
	 
	 	var $myHtml, $curButtonLink, $curButton;
	 	var i = 0;
	 
	 	$('button.button-replacer, input.button-replacer').each(function() {
	 		$myHtml = $.trim($(this).html()).length == 0 ? $(this).val() : $(this).html();
	 		$(this).css({ 'position': 'absolute', 'width': '1px', 'height': '1px', 'top': '-2px', 'left': '0', 'border': '0 none', 'background': 'none' });
	 		$(this).wrap('<div class="button-replaced-container" style="position: relative; display: inline;"></div>');
	 		if ($(this).hasClass('arrow')) {
	 			$(this).after('<a href="#" class="button button-replaced button-replacer arrow" id="button-replacement-' + i + '" ValidationGroup="mailingList">' + $myHtml + '</a>');
	 		} else {
	 			$(this).after('<a href="#" class="button button-replaced button-replacer" id="button-replacement-' + i + '">' + $myHtml + '</a>');
	 		}
	 		$(this).attr('rel', 'button-replaced-ref-' + i);
	 		i++;
	 	});
	 
	 	$('button.button-replacer-primary, input.button-replacer-primary').each(function() {
	 		$myHtml = $.trim($(this).html()).length == 0 ? $(this).val() : $(this).html();
	 		$(this).css({ 'position': 'absolute', 'width': '5px', 'height': '5px', 'top': '0', 'left': '0', 'border': '0 none' });
	 		$(this).wrap('<div class="button-replaced-container" style="position: relative; display: inline;"></div>');
	 		if ($(this).hasClass('arrow')) {
	 			$(this).after('<a href="#" class="button-replaced button-replacer-primary arrow" id="button-replacement-' + i + '">' + $myHtml + '</a>');
	 		} else {
	 			$(this).after('<a href="#" class="button-replaced button-replacer-primary" id="button-replacement-' + i + '">' + $myHtml + '</a>');
	 		}
	 		$(this).attr('rel', 'button-replaced-ref-' + i);
	 		i++;
	 	});
	 
	 	$('a.button-replaced').each(function() {
	 		$curButtonLink = $(this);
	 		$curButtonLink.click(function() {
	 			var replacementId = $(this).attr('id').split('-');
	 			$('input[rel=button-replaced-ref-' + replacementId[replacementId.length - 1] + ']').click();
	 			return false;
	 		});
	 	});
	 
	 	$('a.button-replacer').each(function() {
	 		if ($(this).hasClass('arrow')) {
	 			$(this).append('<span class="button-arrow"></span>');
	 			$(this).css('padding-right', '14px');
	 		};
	 	});
	 
	 	$('a.button-replacer-primary').each(function() {
	 		if ($(this).hasClass('arrow')) {
	 			$(this).append('<span class="button-arrow"></span>');
	 			$(this).css('padding-right', '14px');
	 		};
	 	});
	 
	 	$('h1').each(function() {
	 		$(this).click(function() {
	 			return false;
	 		});
	 	});
	 },
	
	// Add class to body to remove non-JS fallback styles
	removeFallbacks: function() {
		$('body').addClass('js');
	},
	
	//Wrap Registration marks in nav with <sup>
	fixRBall: function() {
		var re = /(®)/;
		$(".side-subnav-container ul.open li a:not(:input):not(label):not(select)").contents().filter(function(){
		    return this.nodeType !== 1;
	 	}).each(function(){
	 	      var output = $(this).text().replace(re,"<sup>$1</sup>");
	 	      if (output != $(this).text() && $(this).parents('sup').length === 0) {
	 	            $(this).wrap("<p></p>").parent('p').html(output).contents().unwrap();
	 	      }
	 	});
	 },
	 
	setupColorBox: function() {
		$(".gallery-detail").colorbox({ transition: 'elastic', speed: 500, rel:'gal' });
		$("body.small-spaces .gallery-detail, body.creative-expression .gallery-detail, body.universal-design .gallery-detail ").colorbox({ transition: 'elastic', speed: 500, rel:'gal', width: '710px' });
	 	$(".zoom-detail").colorbox({ transition: 'elastic', speed: 500 });
	 	$(".image-detail").colorbox({ transition: 'elastic', title: true, speed: 500 });
	 },
	
	addButtonArrows: function() {
		$('a.button.arrow').append('<span/>');
		
		$('.link-list li:not(".follow") a').append('<span/>');
	},
	
	initDealerMiniSearch: function() {
	 	var defaultDealerSearch = "Where e.g. Monroe MI or Zip";
	 	$('.mini-find-dealer input[type=text]').val(defaultDealerSearch).addClass('light');
	 	$('.mini-find-dealer input[type=text]').focus(function() {
	 		if ($(this).val() == defaultDealerSearch) {
	 			$(this).val("").removeClass('light');
	 		}
	 	});
	 	$(".mini-find-dealer input[type=text]").blur(function() {
	 		if ($(this).val() == "") {
	 			$(this).addClass('light').val(defaultDealerSearch);
	 		}
	 	});
	 },
	
	//Set active state on current nav item
	initNavHighlight: function() {
		$curURL = document.URL;
		var myFolder = $curURL.split('/', 6);
		//Check if class needs to be added to main nav
		if (myFolder[3] != "") { //Primary Level Nav
			$('ul.main-nav-list > li > a').each(function(){
				var isFound = $(this).attr('href').toString().search( myFolder[3] );
				if( isFound > 0 ) {
					$(this).addClass('active');
					//Search Sub-nav
					if( myFolder[4] && myFolder[4] != "" ){ //Seconardy Level Nav
						$(this).parent().find('> ul > li > a').each(function(){
							var isSubFound = $(this).attr('href').toString().search( myFolder[4] );
							if (isSubFound > 0) {
								$(this).addClass('active');
								if( myFolder[5] && myFolder[5] != ""){ //Third Level Nav
									$(this).parent().find('> ul > li > a').each(function(){
										console.log( myFolder[5] + " = " + $(this).attr('href').toString());
										var isSubSubFound = $(this).attr('href').toString().search( myFolder[5] );
										if (isSubSubFound > 0) $(this).addClass('active');
									});
								}
							}
						});
					}
				}
			});
		}
	},
	
	// setSustainablityCurrentNavHighlight: function() {
	// 	if ($('ul.green-side-nav')) {
	// 		$curURL = document.URL;
	// 		var myFolder = $curURL.split('/', 5);
	// 		var bgOffset;
	// 		//Check is class needs to be added to main nav
	// 		if (myFolder[4] && myFolder[4] != "") {
	// 			var curURL = myFolder[4].split('.', 2);
	// 			$('ul.green-side-nav a#' + curURL[0] + '-link').parent().addClass('selected');
	// 		}
	// 	}
	// },
	// 
	
	helpIE: function() {
	 	//Add markup for bottom of boxes
		$('html.ie div.banner-box:not(.box-snug), html.ie div.box-med:not(.box-snug), html.ie div.box-lg:not(.box-snug)').append('<div class="js-banner-box-bottom"/>');
	 	$('html.ie div.box-snug:last-child').append('<div class="js-banner-box-bottom"/>');
	 	$('html.ie table.spec-table tr:nth-child(odd) th, html.ie table.spec-table tr:nth-child(odd) td').addClass('zebra');
	 	$('div.banner-box div.product:nth-child(4n)').css('padding-right', '0').after('<br style="clear:both" />');
	 
	 	//Add Carrot to bread crumb as IE doesn't support css append
		$('.ie7 .breadcrumbs li.link').append('   >   ');
	},
	
	 setPressroomSubNav: function() {
	 	var currentURL = location;
	 	var reNewsReleases = /\/pressroom\/news-releases\//i;
	 	var reFeaturedStories = /\/pressroom\/featured-stories\//i;
	 	var $link;
	 
	 	if (reNewsReleases.test(currentURL)) {
	 		$link = $('ul.subnav-main').find('a[href="/pressroom/news-releases/"]');
	 	} else if (reFeaturedStories.test(currentURL)) {
	 		$link = $('ul.subnav-main').find('a[href="/pressroom/featured-stories/"]');
	 	}
	 
	 	if ($link) {
	 		//Set classes
	 		$link.parent().addClass('active');
	 		$link.addClass('active');
	 	}
	 },
	
	setParams: function() {
		$mainNavLi = $('.main-nav-list > li');
		$mainNavLinks = $('.main-nav-list > li > a');
	},

	initCufon: function() {
		
		//Set up Franklin Gothic Medium replacements
	 	Cufon.set('fontFamily', 'Franklin Gothic Medium');
	 	Cufon.replace('#header label span, div.menu-container a.top, a.button');
	 	
	 	Cufon.replace('div.place-trademark', {
	 		textShadow: '-3px -3px rgba(255, 255, 255, 0.3), 3px -3px rgba(255, 255, 255, 0.3), -3px 3px rgba(255, 255, 255, 0.3), 3px 3px rgba(255, 255, 255, 0.3), -2px -2px rgba(255, 255, 255, 0.3), 2px -2px rgba(255, 255, 255, 0.3), -2px 2px rgba(255, 255, 255, 0.3), 2px 2px rgba(255, 255, 255, 0.3)'
	 	});
	 	Cufon.replace('div.fade-dot h3', { color: '-linear-gradient(#6E7433, #484C1C)' });
	 	Cufon.replace('div.banner-box:not(.lg) h3', {
	 		textShadow: '-2px -2px rgba(255, 255, 255, 0.1), 2px -2px rgba(255, 255, 255, 0.1), -2px 2px rgba(255, 255, 255, 0.1), 2px 2px rgba(255, 255, 255, 0.1)'
	 	});

	 	Cufon.replace('.grid_upev .upcoming-events p');
	 	Cufon.replace('h1.sub span.sm');
	 	Cufon.replace('h3.article-title a');
	 	Cufon.replace('.button-replacer');
	 	Cufon.replace('div.banner-box.lg h3');
	 	Cufon.replace('h4');
	 	Cufon.replace('.popupGrey');
	 	Cufon.replace('.su_container h1');
	 
	 	//Set up Franklin Gothic Demi replacements
	 	Cufon.set('fontFamily', 'Franklin Gothic Demi');
	 
	 	Cufon.replace('.side-features h3', {
	 		textShadow: '2px 2px rgba(77, 82, 32, 0.4)'
	 	});
	
		// Set up Franklin Gothic Book replacements
		$replaceList = $('.nav ul.dropdown ul.second > li > a:not(".active"), .footer .copyright, .footer .qc-dot-com a, .footer .product-lines a, .footer .email-signup a, .footer .browse-other h1');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook',
			hover: { color: '#9b550e' }
		});
		
		Cufon.replace('.nav ul.dropdown ul.second > li > a.active', {
			fontFamily: 'FranklinGothicBook'
		});
		
		$replaceList = $('.nav a.minor');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook',
			hover: { color: '#9b550e',
			 		background: 'white'}
		});
		
		$replaceList = $('body.landing-page .section h2 span.normal');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook',
			textShadow: '1px 1px rgba(0, 0, 0, 0.2), 2px 2px rgba(0, 0, 0, 0.1)'
		});
		$replaceList = $('body.landing-page .section .copy p');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook'
		});
		
		$replaceList = $('body.sub-landing-page .links-social .links a span.cta');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook'
		});
				
		$replaceList = $('body.sub-landing-page .scroller h2 span.normal');
		Cufon.replace($replaceList, {
			fontFamily: 'FranklinGothicBook',
			textShadow: '1px 1px rgba(0, 0, 0, 0.2), 2px 2px rgba(0, 0, 0, 0.1)'
		});
		
		// Set up Franklin Gothic Demi replacements

		$replaceList = $('.nav ul.dropdown > li > a:not(".active"), .link-list a, .link-list p, .footer .qc-dot-com h1, .footer .product-lines h1, .footer .email-signup h1, .header .email-signup h1, .header .dealer-search-pop h1, .footer .follow-us h1, .homepage .follow p').not('body.homepage .link-list a, body.landing-page .link-list a, body.sub-landing-page .link-list a, ul.social-link-list a, body.sub-landing-page .link-list a');

		Cufon.replace($replaceList, {
			fontFamily: 'Franklin Gothic Demi',
			hover: { color: '#679EA5' }
		});
		
		Cufon.replace('.nav ul.dropdown > li > a.active', {
			fontFamily: 'Franklin Gothic Demi'
		});
		

		$replaceList = $('body.sub-landing-page .link-list a, body.landing-page .link-list a, body.homepage .link-list a' );
		Cufon.replace($replaceList, {
			fontFamily: 'Franklin Gothic Demi',
			textShadow: '1px 1px rgba(0, 0, 0, .4)',
			hover: { color: '#eeeeee' }
		});
		
		$replaceList = $('body.sub-landing-page .links-social .links a span.title');
		Cufon.replace($replaceList, {
			fontFamily: 'Franklin Gothic Demi',
			hover: { color: '#9b550e' }
		});
		
		$replaceList = $('body.landing-page .section h2 span.emphasis, body.sub-landing-page .scroller h2 span.emphasis');
		Cufon.replace($replaceList, {
			fontFamily: 'Franklin Gothic Demi',
			textShadow: '1px 1px rgba(0, 0, 0, 0.2), 2px 2px rgba(0, 0, 0, 0.1)'
		});
		
		// Set up Franklin Gothic Medium replacements		
		Cufon.replace('.content h1', {
			color: '-linear-gradient(#779B9F, #526A6D)',
			fontFamily: 'Franklin Gothic Medium',
			textShadow: '-3px -3px rgba(255, 255, 255, 0.3), 3px -3px rgba(255, 255, 255, 0.3), -3px 3px rgba(255, 255, 255, 0.3), 3px 3px rgba(255, 255, 255, 0.3), -2px -2px rgba(255, 255, 255, 0.3), 2px -2px rgba(255, 255, 255, 0.3), -2px 2px rgba(255, 255, 255, 0.3), 2px 2px rgba(255, 255, 255, 0.3)'
		});	
		Cufon.replace('h1 sup', {
	 		color: '-linear-gradient(#779B9F, #526A6D)',
	 		textShadow: '-3px -3px rgba(255, 255, 255, 0.3), 3px -3px rgba(255, 255, 255, 0.3), -3px 3px rgba(255, 255, 255, 0.3), 3px 3px rgba(255, 255, 255, 0.3), -2px -2px rgba(255, 255, 255, 0.3), 2px -2px rgba(255, 255, 255, 0.3), -2px 2px rgba(255, 255, 255, 0.3), 2px 2px rgba(255, 255, 255, 0.3)'
	 	});
		Cufon.replace('h1.featured-news-release', {
	 		color: '-linear-gradient(#779B9F, #526A6D)',
	 		textShadow: '-3px -3px rgba(255, 255, 255, 0.3), 3px -3px rgba(255, 255, 255, 0.3), -3px 3px rgba(255, 255, 255, 0.3), 3px 3px rgba(255, 255, 255, 0.3), -2px -2px rgba(255, 255, 255, 0.3), 2px -2px rgba(255, 255, 255, 0.3), -2px 2px rgba(255, 255, 255, 0.3), 2px 2px rgba(255, 255, 255, 0.3)'
	 	});
	},
	
	//Setup Site Dropdown Menus
	setupSiteSubnav: function() {
		var thisOpen, thisClose;
		$mainNavLi.hoverIntent(function() {
			var $this = $(this);
			clearTimeout(thisClose);
			$('ul.dropdown').not($('ul.dropdown', $this)).slideUp('fast');
			$('ul.dropdown', $this).slideDown('fast');
		},
		function() {
			var $this = $(this);
			clearTimeout(thisOpen);
			thisClose = setTimeout(function() {
				$('ul.dropdown', $this).stop().slideUp('fast');
			}, 250);
		});
		//Add Bottom Border to dropdown menus
		$('ul.dropdown > li:last-child').addClass('dropdown-bottom');
	},
	
	//Clear input field on user focus
	initInputFocus: function() {
		inputFocus( $('.header .dealer-search-pop input.text'), "Zip Code" );
		inputFocus( $('.footer .email-signup input.email-address'), "Email Address");
		inputFocus( $('.header .email-signup input.text'), "Email Address" );
		
		function inputFocus( $target, $value){
			$target.focus(function(){
				if ($target.val() === '' || $target.val() === $value) {
					$target.val('');
				}
			});
			$target.blur(function(){
				if ($target.val() === '' || $target.val() === $value) {
					$target.val( $value );
				}
			});
		}
	},
	
	//Attach link to inline div then display div on click
	modalLinkSetup: function(){
		formModel( $('#newsletter-signup'), $('.sub-nav a.newsletter-signup'), $('.sub-nav') );
		formModel( $('#find-dealer-pop'), $('.sub-nav a.find-dealer'), $('.sub-nav') );
				
		function formModel( $thisForm, $invokeLink, $formContainer ){
			$thisForm.css({
				'opacity': 0,
				'left': 120
			});
			
			$invokeLink.click(function(e){
				$formContainer.find('.open').css({
					'opacity': 0,
					'left': 120
				}).removeClass('open');
				$thisForm.addClass('open');
				$thisForm.show();
				//Check if already at position
				var $distance = $thisForm.css('left') > 200 ? '+=100' : '200';
				$thisForm.animate({
					opacity: 1,
					left: $distance
				}, 1000, function(){
				// Animation complete.
				});
				
				$thisForm.find('.close').one('click', function(){
					$thisForm.removeClass('open');
					$thisForm.hide();
					$thisForm.css({
						'opacity': 0,
						'left': 120
					});
				});
				
				e.preventDefault();
			});
		}
	},
	
	sidebarSetup: function() {
		$('.ie7 .sidebar-links li:last').css('background', 'none');
	}
};
