jQuery.noConflict();

(function($) {
	function preload(arrayOfImages) {
	    $(arrayOfImages).each(function(){
	        $('<img/>')[0].src = this;
	        // Alternatively you could use:
	        // (new Image()).src = this;
	    });
	}
	$(document).ready(function(){
		$('iframe').each(function(){
			var url = $(this).attr("src")
			$(this).attr("src",url+"?wmode=transparent")
		});
		
		$('.infoBox').corner('6px');
		$('input[type="text"], textarea').corner('3px');
		
		DD_belatedPNG.fix('.menu, #blogIndicator');
		
		var couponColor = '';
		
		$('#dimmer').css('opacity',0.7);
		$('a[target="lightbox"]').click(function(e){
			e.preventDefault();
			if ($(this).hasClass('coupon')){
				$('#lightboxContainer').addClass('coupon');
				
				if ($(this).attr('rel').indexOf('Percent')>0){
					$('#lightboxSwish').attr('src', 'http://www.nufloors.ca/resources/images/design/couponLightboxSwishBlue.png');
					couponColor = '#1f52a5';
				}else{
					couponColor = '#ef4035';
				}
				
				$('#lightbox').fadeIn().find('#lightboxContent').html('<h3 style="color:'+ couponColor +'; font-size:24px; font-weight:normal; margin-bottom:15px; font-family:Helvetica, sans-serif;">Enter your information to receive<br/>your FloorSaver coupon by email.</h3><form id="nufEventForm"><input type="text" name="firstName" value="First name" title="First name" style="margin-right: 10px; width:258px;" /> <input type="text" name="lastName" value="Last name" title="Last name" style="width:258px;" /><br/><input type="text" name="email" value="Email address" title="Email address" style="width:540px;" /><input type="text" name="confirmEmail" value="Confirm email address" title="Confirm email address" style="width:540px;" /><br/> <input type="submit" value="" style="float:right;" /> <input type="checkbox" value="no" id="nufOptOut" name="optOut" checked="" /><label for="nufOptOut">I wish to receive future emails about news & promotions<br/>from Nufloors.</label><input type="hidden" name="location" value="" id="nufEventLocation" /><input type="hidden" name="amount" value="" id="nufEventAmount" /><input type="hidden" name="filepath" value="" id="nufEventFile" /><div class="clear"></div></form>');
				
				var currentLocation = location.pathname;
				var firstSlash = currentLocation.indexOf('/', 1);
				currentLocation = currentLocation.substring(1, firstSlash);
				
				$('#nufEventLocation').attr('value', currentLocation);
				
				$('#nufEventAmount').attr('value', $(this).attr('rel'));
				$('#nufEventFile').attr('value', $(this).attr('href'));
				
			}else{
				$('#lightbox').fadeIn().find('#lightboxContent').load($(this).attr('href'));
			}
		});
		$('a#closeLightbox, #dimmer').click(function(e){
			e.preventDefault();
			$('#lightbox').fadeOut(function(){
				$(this).find('#lightboxContainer').removeClass();
			});
		});
		
		var currentVal = '';
		$('form#nufEventForm input[type="text"]').live('focus',function(){
			if ($.trim($(this).attr('value')) == 'First name' || $.trim($(this).attr('value')) == 'Last name' || $.trim($(this).attr('value')) == 'Email address' || $.trim($(this).attr('value')) == 'Confirm email address') {
				$(this).attr('value', '').css({
						'backgroundColor': '#ddd',
						'color': '#000'
					});
			}
			currentVal = $(this).attr('title');
		});
		
		$('form#nufEventForm input[type="text"]').live('blur',function(){
			if ($.trim($(this).attr('value')) == '') {
				$(this).attr('value', currentVal).css({
						'backgroundColor': '#C6C7C9',
						'color': '#666'
					});
			}
		});
		
		$('form#nufEventForm input[name="confirmEmail"]').live('keyup', function(){
			if ($('form#nufEventForm input[name="email"]').attr('value') == $('form#nufEventForm input[name="confirmEmail"]').attr('value') && $('form#nufEventForm input[name="email"]').attr('value').indexOf('@', 1) > -1){
				$('form#nufEventForm input[name="confirmEmail"], form#nufEventForm input[name="email"]').css({
					'backgroundColor': '#92d89a',
					'color': '#fff'
				});
			}
		});
		
		$('form#nufEventForm').live('submit', function(e){
			e.preventDefault();
			if ($('form#nufEventForm input[name="firstName"]').attr('value') != 'First name' && $('form#nufEventForm input[name="lastName"]').attr('value') != 'Last name' && $('form#nufEventForm input[name="email"]').attr('value') != 'Email address' && $('form#nufEventForm input[name="confirmEmail"]').attr('value') != 'Confirm email address' && $('form#nufEventForm input[name="email"]').attr('value') == $('form#nufEventForm input[name="confirmEmail"]').attr('value') && $('form#nufEventForm input[name="email"]').attr('value').indexOf('@', 1) > -1) {
				$.ajax({
					type: 'POST',
					url: "http://www.nufloors.ca/resources/coupon/nufEventLightbox.php",
					data: $('form#nufEventForm').serialize(),
					success: function(){
						$('#lightboxContent').html('<h3 style="color:'+ couponColor +'; font-size:42px;">Thank you!</h3><h3 style="color:'+ couponColor +'; font-size:24px; font-weight:normal; margin-bottom:15px; font-family:Helvetica, sans-serif;">Your FloorSaver coupon<br/>has been emailed to you.</h3>');
					}
				});
			} else {
				if ($('form#nufEventForm input[name="firstName"]').attr('value') == 'First name') {
					$('form#nufEventForm input[name="firstName"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="lastName"]').attr('value') == 'Last name') {
					$('form#nufEventForm input[name="lastName"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="email"]').attr('value') == 'Email address') {
					$('form#nufEventForm input[name="email"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="confirmEmail"]').attr('value') == 'Confirm email address') {
					$('form#nufEventForm input[name="confirmEmail"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="email"]').attr('value') != $('form#nufEventForm input[name="confirmEmail"]').attr('value')){
					$('form#nufEventForm input[name="confirmEmail"], form#nufEventForm input[name="email"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="email"]').attr('value').indexOf('@', 1) == -1){
					$('form#nufEventForm input[name="email"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
				if ($('form#nufEventForm input[name="confirmEmail"]').attr('value').indexOf('@', 1) == -1){
					$('form#nufEventForm input[name="confirmEmail"]').css({
						'backgroundColor': '#f58574',
						'color': '#fff'
					});
				}
			}
		});
		
		/*var curVal = '';
		$('input[type="text"], textarea').focus(function(){
			curVal = $(this).val();
			if (!$(this).attr('rel')){
				$(this).val('');
				$(this).attr('rel',curVal);
			}else if ($(this).val() == $(this).attr('rel')){
				$(this).val('');
			}
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).val($(this).attr('rel'));
			}
		});*/
		
		$('#flooringChoices .dropdown').css('width', ($('#flooringChoices').width() + 2) + 'px');
		
		if ($('#spinboxQueue').length) {
			
			$('#spinboxQueue img').each(function(){
				/*var thisImg = $(this).attr('src');
				thisImg = thisImg.replace('/thumbs','');
				console.log(thisImg);
				preload(thisImg);*/
			});
			$('.storeHoursOverlay').css('opacity', 0);
			$('.storeHoursLink img').mouseenter(function(){
				$(this).parent().parent().find('.storeHoursOverlay').stop().animate({
					opacity: 1,
					right: '46px'
				}, 200);
			}).mouseleave(function(){
				$('.storeHoursOverlay').stop().animate({
					opacity: 0,
					right: '0px'
				}, 200);
			});
			$('#spinboxQueue img:gt(0)').css('opacity', 0.3);
			$('#spinboxQueue img:eq(0)').addClass('selected');
			$('#spinboxQueue img').mouseenter(function(){
				if (!$(this).hasClass('selected')) {
					$(this).css('opacity', 0.7);
				}
			}).mouseleave(function(){
				if (!$(this).hasClass('selected')) {
					$(this).css('opacity', 0.3);
				}
			}).click(function(){
				spin($(this).attr('src'), $(this).attr('rel'), $(this).attr('target'));
				$(this).addClass('selected').css('opacity', 1);
				if (spinPaused) {
					$('#spinboxPause a#pausePlay img').attr('src','http://refresh.nufloors.ca/resources/images/design/spinboxPause.png');
				}
			});
			
			var numImages = $('#spinboxQueue img').length, spinTimeout;
			var spin = function(nextImg, nextLink, nextTarget){
				clearTimeout(spinTimeout);
				if (!nextImg || typeof(nextImg) == 'number') {
					if ($('#spinboxSlider img.selected').index() == (numImages - 1)) {
						var nextObject = $('#spinboxSlider img').eq(0);
					}
					else {
						var nextObject = $('#spinboxSlider img.selected').next();
					}
					var newImg = nextObject.attr('src');
					var newLink = nextObject.attr('rel');
					var newTarget = nextObject.attr('target');
				}
				else {
					var newImg = nextImg;
					var newLink = nextLink;
					var newTarget = nextTarget;
					var nextObject = $('#spinboxSlider img[src="' + newImg + '"]');
				}
				
				var oldImg = $('#spinboxImage').attr('src');
				newImg = newImg.replace('/thumbs', '');
				
				if (!newTarget) {
					newTarget = '_blank';
				}
				if (!newLink) {
					newLink = '#';
				}
				
				$('#spinboxFader').attr('src', oldImg).fadeIn(function(){
					$('#spinboxImage').attr('src', newImg).parent().attr({
						href: newLink,
						target: newTarget
					});
				}).fadeOut(function(){
					$('#spinboxQueue img').removeClass('selected').css('opacity', 0.3);
					nextObject.addClass('selected').css('opacity', 1);
				});
				spinTimeout = setTimeout(spin, 4000);
			};
			
			var spinPaused = 1;
			if (!$('#spinboxSlider img').eq(0).hasClass('static')) {
				spinTimeout = setTimeout(spin, 4000);
				spinPaused = 0;
				$('#spinboxPause a#pausePlay img').attr('src','http://refresh.nufloors.ca/resources/images/design/spinboxPause.png');
			}
			
			$('#spinboxPause a#pausePlay').css('opacity', '0.5');
			$('#spinboxImageArea').mouseenter(function(){
				$('#spinboxPause').stop().fadeTo('fast',0.7);
			}).mouseleave(function(){
				$('#spinboxPause').stop().fadeTo('fast',0);
			});
			$('#spinboxPause a#pausePlay').click(function(e){
				if (!spinPaused) {
					e.preventDefault();
					clearTimeout(spinTimeout);
					spinPaused = 1;
					$(this).find('img').attr('src','http://refresh.nufloors.ca/resources/images/design/spinboxPlay.png');
				}else{
					e.preventDefault();
					spinTimeout = setTimeout(spin, 2000);
					spinPaused = 0;
					$(this).find('img').attr('src','http://refresh.nufloors.ca/resources/images/design/spinboxPause.png');
				}
			}).mouseenter(function(){
				$(this).css('opacity','1');
			}).mouseleave(function(){
				$(this).css('opacity','0.5');
			});
			
			var spinboxQueuePage = 1;
			$('#spinboxLeft').css('opacity', 0.2);
			$('#spinboxLeft').click(function(){
				if (numImages <= 12) {
					if (spinboxQueuePage > 1) {
						$('#spinboxSlider').animate({
							left: '0px'
						});
						spinboxQueuePage--;
						$('#spinboxLeft').removeClass('button').css('opacity', 0.3);
						$('#spinboxRight').addClass('button').removeAttr('style');
					}
				}else{
					if (spinboxQueuePage == 2) {
						$('#spinboxSlider').animate({
							left: '0px'
						});
						spinboxQueuePage--;
						$('#spinboxLeft').removeClass('button').css('opacity', 0.3);
						$('#spinboxRight').addClass('button').removeAttr('style');
					}else if (spinboxQueuePage == 3) {
						$('#spinboxSlider').animate({
							left: '-666px'
						});
						spinboxQueuePage--;
						$('#spinboxRight').addClass('button').removeAttr('style');
					}
				}
			});
			if (numImages <= 6) {
				$('#spinboxQueue').css('margin-left', '0px');
				$('#spinboxLeft').hide();
				$('#spinboxRight').hide();
			}
			
			$('#spinboxRight').click(function(){
				if (numImages <= 12) {
					if (spinboxQueuePage == 1) {
						$('#spinboxSlider').animate({
							left: '-666px'
						});
						spinboxQueuePage++;
						$('#spinboxRight').removeClass('button').css('opacity', 0.3);
						$('#spinboxLeft').addClass('button').removeAttr('style');
					}
				}else{
					if (spinboxQueuePage == 1) {
						$('#spinboxSlider').animate({
							left: '-666px'
						});
						spinboxQueuePage++;
						$('#spinboxLeft').addClass('button').removeAttr('style');
					}else if (spinboxQueuePage == 2) {
						$('#spinboxSlider').animate({
							left: '-1332px'
						});
						spinboxQueuePage++;
						$('#spinboxRight').removeClass('button').css('opacity', 0.3);
						$('#spinboxLeft').addClass('button').removeAttr('style');
					} 
				}
			});
			
		}
		
		$('ul.nav li.dropdownTrigger').mouseenter(function(){
			$(this).find('.dropdown').show();
		}).mouseleave(function(){
			$(this).find('.dropdown').hide();
			$('table.subMenu').hide();
			if ($(this).find('.subMenu').length) {
				$(this).find('.menu').css('width','100%');
			}
		}); 
		
		$('ul.nav .dropdown .menu td.dropdownTrigger').mouseenter(function(){
			$(this).closest('.menu').css('width','300px');
			$('table.subMenu').hide();
			$(this).find('table.subMenu').show();
		}).mouseleave(function(){
			//$(this).closest('.menu').css('width','100%');
		});
		
		$('.button').live('mouseover', function(){
			$(this).stop().css('opacity',0.7);
		});
		$('.button').live('mouseleave', function(){
			$(this).stop().css('opacity', 1);
		});
		
		$('#blogIndicator').css('opacity', 0);
		$('#blogIcon').mouseenter(function(){
			$('#blogIndicator').stop().animate({
				opacity: 1,
				right: '-64px'
			}, 200);
		}).mouseleave(function(){
			$('#blogIndicator').stop().animate({
				opacity: 0,
				right: '-15px'
			}, 200);
		});
		
		$('.newsContainer').append('<div class="clear"></div>');
		
		$('select.sectionSelect').change(function(){
			var selectedValue = $(this).find('option:selected').val(), callback;
			if($(this).hasClass('stainguide')) {
				callback = function() {
					location.hash = '#'+selectedValue;
				}
			}
			$('body, html').animate({scrollTop: $('a[name="' + selectedValue + '"]').offset().top - 10}, callback);
		});
		
		$('a.backToTop').click(function(e){
			e.preventDefault();
			$('body, html').animate({scrollTop: 0}, function() {
				location.hash = '#';
			});
		});
		
		$('.entry').append('<div class="clear">&nbsp;</div>');
		
		$('a.print').click(function(e){
			e.preventDefault();
			var title = $(this).closest('.entry').find('h3 a').text();
			var content = $(this).closest('.entry').html();
			//content = content.replace('<p class="entryOptions"><a href="#" class="print">print</a> <a href="#" class="backToTop">back to top</a></p>', '');
			content = jQuery.trim(content);
			content = content.substring(0, content.length-113);
			var printWindow = window.open('','','width=657,height=400,left=200,top=100');
			var body = printWindow.document.getElementsByTagName("body")[0];
			body.innerHTML = '';
			printWindow.document.write('<html><head><title>' + title + '</title><style>body { font-family:Helvetica, sans-serif; margin:0px;}h3 { font-size:24px; color:#002a92; font-family:\'Friz Quadrata W01 Bold\', Georgia, serif; margin:5px 0px;}p {font-size:14px;color: #59595b;margin:5px 0px;}</style></head><body onload="window.print();"><div style="height:370px; overflow:scroll; padding:15px;"><p style="font-size:11px;"><em><a href="javascript:window.print();">Click here to print this page</a></em></p>' + content + '</div></body></html>'); 
		});
		
		/*
		This script is written by Eric (Webcrawl@usa.net)
		For full source code, installation instructions,
		100's more DHTML scripts, and Terms Of
		Use, visit dynamicdrive.com
		*/
		
		function printit(){
		alert(123);
		if (window.print) {
			window.print() ;  
		} else {
			var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
			WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
		}
		}
		
		//flooring calculator
		$floorCalculatorForm = $('#measurements');
		if($floorCalculatorForm.length) {
			$floorCalculatorForm.find('input').bind('keyup focus click', function(e) {
				var $this = $(this), value = $this.val(), unit = $this.hasClass('feet') ? 'ft' : ($this.hasClass('inch') ? 'in' : ($this.hasClass('sqft') ? 'sq ft' : '$'));
				value = $.trim(value.replace(unit, '').replace(',', '.'));
				var curCaret = $this.caret().begin;
				if(value.charAt(0) == '.') {
					value = '0'+value;
					curCaret+=1;
				}
				if(value.charAt(value.length-1) != '.') {
					value = parseFloat(value, 10);
				}
				if(isNaN(value)) {
					value = '';
				}
				value+=' '+unit;
				
				$this.val(value);
				var maxCaretPos = value.length-unit.length-1;
				//console.log('caret', curCaret, $this.caret().begin, maxCaretPos);
				if(curCaret > maxCaretPos) {
					curCaret = maxCaretPos;
				}
				$this.caret(curCaret)
				
				var floorWidth = {'feet':parseFloat($('#inputWidthFt').val()),'inches':parseFloat($('#inputWidthIn').val())};
				var floorLength = {'feet':parseFloat($('#inputLengthFt').val()),'inches':parseFloat($('#inputLengthIn').val())};
				if(isNaN(floorWidth['feet'])) floorWidth['feet'] = 0;
				if(isNaN(floorLength['feet'])) floorLength['feet'] = 0;
				if(!isNaN(floorWidth['inches'])) {
					floorWidth['feet'] += floorWidth['inches']/12;
				}
				if(!isNaN(floorLength['inches'])) {
					floorLength['feet'] += floorLength['inches']/12;
				}
				var areaResult = Math.round(floorWidth['feet'] * floorLength['feet']*100)/100;
				if (areaResult > 0) {
					$('#areaResult').text(areaResult + ' sq ft');
				}
				else {
					$('#areaResult').text(' sq ft');
				}
				
				var $inputArea = $('#inputArea');
				var area = parseFloat($inputArea.val());
				if(isNaN(area)) area = 0;
				if(!area && areaResult > 0 || $inputArea.hasClass('auto')) {
					$('#inputArea').addClass('auto').val(areaResult + ' sq ft');
					area = areaResult;
				}
				
				var cost = parseFloat($('#inputCost').val());
				if(isNaN(cost)) cost = 0;
				
				var costResult = Math.round(area * cost * 100)/100;
				console.log(costResult, area, cost);
				if (costResult > 0) {
					$('#costResult').text(costResult+' $');
				}
				else {
					$('#costResult').text(' $');
				}
				
			});
			$('#inputArea').bind('focus click keydown', function() {
				$(this).removeClass('auto');
			});
		}
		
		$(window).load(function() {
			$('.mainContent').find('a.playVideo[href*="' + location.hash.replace('#','') + '"]').click();
		});
		$('ul.nav .dropdown:contains("Featured") .menu a').click(function(){
			var nextLink = $(this).attr('href').substring($(this).attr('href').lastIndexOf('#')+1);
			$('.mainContent').find('a.playVideo[href*="' + nextLink + '"]').click();
		});
		
		$('a.readMore').click(function(e){
			e.preventDefault();
			$(this).hide().closest('.readMoreContainer').find('.readMoreCollapsed').slideDown();
		});
		
	});
})(jQuery);
