// JavaScript Document

$(document).ready(function() {
	
	// fancybox functions
	$(".zoom1").click(function () {
		$.fancybox({
			'overlayOpacity'	:	.75,
			'overlayColor'		:	'#000'
		});
		return false;
	});
	
	$(".iframe").click(function () {
		var vars = getUrlVars($(this).attr('href'));
		var url = $(this).attr('href');
		var dWidth = Number(vars['w']);
		var dHeight = Number(vars['h']);
		
		//Math.floor((window.innerWidth - 10)/2);
		//Math.floor((window.innerHeight - 10)/2);
		
		//var dWidth = Math.floor((window.innerWidth));
		//var dHeight = Math.floor((window.innerHeight);

		window.top.$.fancybox({
			'href'				: url,
			'width'				: dWidth,
			'height'			: dHeight,
			'type'				: 'iframe',
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 0
		});
		return false;
	});
		
	$(".hires").click(function () {
		var url = $(this).attr('href');
		var dWidth = 500;//Math.floor(window.innerWidth);
		var dHeight = 500;//Math.floor(window.innerHeight);

		window.top.$.fancybox({
			'href'				: url,
			'width'				: dWidth,
			'height'			: dHeight,
			'type'				: 'image',
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 0
		});
		return false;
	});
	
	/*$(".iframe_modal").click(function () {
		var vars = getUrlVars($(this).attr('href'));
		var url = $(this).attr('href');
		var dWidth = Number(vars['w']);
		var dHeight = Number(vars['h']);
		window.parent.$.fancybox({
			'href'				: url,
			'width'				: dWidth,
			'height'			: dHeight,
			'type'				: 'iframe',
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 0
		});
		return false;
	});*/
	
	$(".inline").fancybox({
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 0
	});
	
	$("a[rel=gallery]").fancybox({
		'width'				: 330,
		'height'			: 330,
		'overlayOpacity'	: .75,
		'overlayColor'		: '#000',
		'padding'			: 10,
		'titlePosition' 	: 'inside',
		/*'titleShow'			: false,*/
		'type'				: 'image'
	});
	
	/*$(".gallery").click(function () {
		window.top.$.fancybox({
			'width'				: 330,
			'height'			: 330,
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 10,
			'titlePosition' 	: 'outside'
			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
			}
		});
		return false;
	});*/
	
	// ask mohawk button event
	$('#ask').click(function () {
		$('.ask_mohawk').toggleClass('off');
		$('.ask_mohawk_form').slideToggle("slow", function () {
								
		});
	});
	
	// ask my store button event
	$('.ask_my_store_btn').click(function () {
		
				if($('.ask_my_store').css("left") == "-380px") {
					$('.ask_my_store').animate({ left: '+=380' }, 500);
				}
		
				if($('.ask_my_store').css("left") == "0px") {
					$('.ask_my_store').animate({ left: '-=380' }, 500);
				}
			
	});
	
	$('#ask').mouseover(function () { $(this).css("cursor","pointer");$(this).css("background-image","url(\'/img/page_parts/tab_ask_mohawk_over.gif\'"); }).mouseout(function () { $(this).css("background-image","url(\'/img/page_parts/tab_ask_mohawk.gif\'"); });
	
	// color detail event
	$('.view_detail').click(function() {
		if ($(this).parent().parent().next().attr('class') != "product_color") {
			$(this).parent().parent().next().slideToggle();
			return false;
		}
	})
	
	$('.acc_unit').click(function() {
		if ($(this).children('div').attr('class') == "acc") {
			$(this).children('div').slideToggle();
			return false;
		}
	})
	
	// set image width
	if ($('#main_image').length != 0) {
		$('.product_image').css('width', $('#main_image').attr('width'));
		$('.product_image').css('margin', '40px auto 0 auto');
	}
	
	// scroll event for product menu
	if ($('#prod_nav').length != 0) {
		var pos = $('#prod_nav').offset();	
		
		$(window).scroll(function () {
			if ($(this).scrollTop() > pos.top - 10) {
				$('#prod_nav').css('position','fixed');
				$('#prod_nav').css('top','0');
			} else if ($(this).scrollTop() < pos.top) {
				$('#prod_nav').css('position','relative');
				$('#prod_nav').css('top','0');
			}
		});
	}
	
	// accordian toggle
	$('.search_acc h1').toggle(function () {
		$(this).parent().css('height', '8px');
		$(this).css("background", "url('/img/page_parts/arrow_red.gif') 3px no-repeat");
	}, function () {
		$(this).parent().css('height', '');	
		$(this).css('background', "url('/img/page_parts/arrow_red_down.gif') no-repeat");
	});
	
	// add smart tip to carpet accordian
	if ($('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_pnlFeatures').length != 0) { 
		/*$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_pnlFeatures').append('<div id="coll1"><h1>Smartstrand Sorona</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p></div><div id="coll2"><h1>Smartstrand</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p></div><div id="coll3"><h1>Weardated</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p></div><div id="coll4"><h1>Everstrand</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p></div>');
		$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Psorona,#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Psmartstrand,#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Pweardated,#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Peverstrand').addClass("clue-tip");
		$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Psorona').attr("rel","#coll1");
		$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Psmartstrand').attr("rel","#coll2");
		$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Pweardated').attr("rel","#coll3");
		$('#ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_Peverstrand').attr("rel","#coll4");*/
	}
		
	$('.ssl1').mouseleave(function (event) { event.stopPropagation(); });
	
	//$('#ssl1,#ssl2,#ssl3,#ssl4,#ssl5,#ssl6,#ssl7,#ssl8,#ssl9,#ssl0,#ssl11,#ssl12').mouseleave(function (event) { event.stopPropagation(); });
	
	// initialize background toggle
	if ($('#Li1').length != 0) {
		$('#Li1').click(function () { setBackgroundColor("grey"); setCookie("backgroundColor","grey",365); });
		$('#Li2').click(function () { setBackgroundColor("black"); setCookie("backgroundColor","black",365); });
		$('#Li3').click(function () { setBackgroundColor("white"); setCookie("backgroundColor","white",365); });
	}
	
	// set background color
	checkBackgroundColor();
	
	// add event for warranty registration login
	$('#warranty_sign_in').click(function () {
		$('#f_1').css("display","none");
		$('#f_2').css("display","block");
	});
	
	$('.rate_box').click(function () {
		$(this).animate( {height: 270}, 650, function() {  } );
		$('#rate_box_btn').fadeOut();
		$('#ratethis').fadeIn();
	});
	
	// modify ASP.NET form
	$('#aspnetForm').css("height","100%");
	
	// add event for my gallery cart
	$('.cart').click(function () { document.location.href= "/my-mohawk/my-gallery.aspx"; });
	$('.cart').mouseover(function () { $(this).css("cursor","pointer");$(this).css("background-image","url(\'/img/page_parts/tab_cart_over.gif\'"); }).mouseout(function () { $(this).css("background-image","url(\'/img/page_parts/tab_cart.gif\'"); });		
	
	// add event for store locator
	$('.store_locator').click(function () { 
		document.location.href="/mohawk-retailer/store_locator.aspx"; 
	});
	
	// add event for store locator input field
	$('.store_locator_search_2').append("<div class=\"right\" id=\"zipExecute\" style=\"width:20px;height:30px;\"></div>");
	
	$('#txtZipHead').focus(function () {
		$('.store_locator').unbind('click');
		$('#txtZipHead').keypress(function(event) {
			if (event.keyCode == 13) {
				ShowLocator();
				event.returnValue=false;
			}
		});
		$('#zipExecute').click(function () { 
			ShowLocator();
		});
		/*if ($(this).attr("value") == "zip") {
			$(this).attr("value", "");
		}*/
	});
	
	// add event for search bar
	$('#SearchButton').click(function () {
		return ShowSearch();
		event.returnValue=false;
	});
	
	$('#siteSearchTextbox').focus(function () {
		$('#siteSearchTextbox').keypress(function(event) {
			if (event.keyCode == 13) {
				//ShowSearch();
				//event.returnValue=false;
			}
		});
	});
	
	// initialize image gallery
	if ($('.img_thumbs').length != 0) { assignTabs(".img_thumbs","#img_gallery",img_thumbs,img_gallery); }
	
	// initialize homepage tabs
	if ($('.tabtab').length != 0) { assignTabs(".tabtab","#tabs",nav,div); }

	// initialize product tabs
	if ($('.tabtab2').length != 0) { assignTabs(".tabtab2","#tabs",nav,div); }
	
	// initialize sub page navigation
	var xars = getUrlVars(document.location.href);
	var xg = Number(xars['pg']);
	
	if (xg == null) {
		xg = 0;	
	}
	
	if ($('.sub_nav').length != 0) {
		assignTabs(".sub_nav","#pages",nav,div,xg); 
	}
	if ($('.sub_nav_2').length != 0) { assignTabs(".sub_nav_2","#pages",nav,div,xg); }
	
	if ($('.sub_nav').length != 0 && $('.sub_nav_2').length != 0) { assignTabs(".sub_nav_2","#pages",nav,div,xg); }
	
	// initialize landing page tabs
	if ($('.feature_unit_nav').length != 0) { assignTabs(".feature_unit_nav","#pages",nav,div,xg); }
	if ($('.side_nav').length != 0) { assignTabs(".side_nav","#pages",nav,div,xg); }
	
	// initialize cluetips
	if ($('.clue-tip').length != 0) { clueTipInit(); }
	
	// initialize address toggle on store locator page
	$('#address_toggle').click(function () { $('#address1').hide(); $('#address2').show(); });
	
	// initialize rating stars
	if ($('.stars-wrapper1').length != 0) {	$(".stars-wrapper1").stars(); }
	
	// set background image for home page
	if ($('.home_main').length != 0) { setBackground(); }
	
	// initialize home page sliders
	if ($('.slider').length != 0) { setTimeout(setSlider, 1000);}
	
	// blank input text
	$('.input_text').focus(function () { $(this).attr('value', ''); });
	$('input').focus(function () { 
		var str = $(this).attr("value");
		if (str == "zip" || str == "required" || str == " please enter your zip code" || str == "please enter your zip code" || str == "(mm/dd/yy)" || str == "email" || str == "password" || str == " email" || str == " password") { 
			$(this).attr("value", ""); 
		}
	});
	
	// store select handler
	$('.store_select').click(function () {
		$.fancybox.close();
		$('#dl1').hide();
		$('#dl3').fadeIn();
	});
	
	$('#manual').click(function () {
		$('#dl1').hide();
		$('#dl2').fadeIn();						 
	});
	
	// delay product navigation
	setTimeout(delayProductNav, 500);
	
	// center load status box
	centerLoadStatus();
	$(window).resize(function () {
		centerLoadStatus();				   
	});
});
	
	var img_thumbs = new Array();
	var img_gallery = new Array();
	var nav = new Array();
	var div = new Array();
	
	var bg_selectors = new Array("#Li1","#Li2","#Li3");
	var bg_colors = new Array("grey","black","white");
	
	// function to parse url string variables
	function getUrlVars(str) {
		if (str) {
			var i;
			var vars = new Array();
			var hashes = str.slice(str.indexOf('?') + 1).split('&');
			
			for (var i = 0; i < hashes.length; i++) {
				hash = hashes[i].split('=');
				vars.push(hash[0]);
				vars[hash[0]] = hash[1];
			}
			
			return vars;
		} else {
			//loadStatus("No values");
			//alert("no values");	
		}
	}	
	
	// function to create tabs
	function assignTabs (tab, con, nav, div, init) {
		$(tab + ' ul').children().each( function (index) {			
			// add to nav array
			nav[index] = $(this);
			
			// assign click
			$(this).click(function () {
				pg = index;
				tabhilite(tab, pg, nav, div);
			});
			
		});
		
		$(con).children().each( function (index) {
			div[index] = $(this);									  
		});
		
		if (init) {
			tabhilite(tab, init, nav, div);
		} else {
			// initialize
			tabhilite(tab, 0, nav, div);
		}
	}
	
	// function to select and hilite tabs
	function tabhilite (tab, pg, nav, div) {
		var i;
		$(tab).css('display','block');
		for (i = 0; i < nav.length; i++) {
			if (i == pg) {
				if (div) {
					$(nav[pg]).addClass("on");
					$(div[pg]).fadeIn();
					//$(div[pg]).animate({"left": "+=50px"}, "slow");
				} else {
					alert("NO!");	
				}
				//$(nav[pg]).css('color', '#fff');
				//$(nav[pg]).css('font-weight', 'bold');
				//$(nav[pg]).css('text-decoration', 'none');
			} else {
				if (div) {
					$(nav[i]).removeClass("on");
					$(div[i]).hide();
				} else {
					alert("NO!");	
				}
				//$(nav[i]).css('color', '#000');
				//$(nav[i]).css('font-weight', 'normal');
				//$(nav[i]).css('text-decoration', 'underline');
			}
		}
	}
	
	// select option jump menu
	// usage onchange="MM_jumpMenu('self',this,0)"
	function MM_jumpMenu(targ,selObj,restore) { //v3.0
		eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
		if (restore) selObj.selectedIndex=0;
	}
	
	// clue tip init and parameters
	function clueTipInit () {
		$('.clue-tip').cluetip({
			width: 200,
			cluezIndex: 97,
			positionBy: 'fixed',
			topOffset: 0,
			leftOffset: 10,
			local: true,
			hideLocal: true,
			cluetipClass: 'mhk',
			attribute: 'rel',
			arrows: true, 
			showTitle: false,
			dropShadow: false,
			clickThrough: true,
			// settings for when hoverIntent plugin is used
			hoverIntent: {
				sensitivity: 3,
				interval: 50,
				timeout: 0
			}
		});
	}
	
	function loadingAnimation (state) {
		$('.load_animation').css('display',state);
		/*if (state == "display") {
			$('.load_animation').fadeIn();
		}
		if (state == "none") {
			$('.load_animation').fadeOut();
		}*/
	}
	
	function loadStatus (msg) {
		$('.load_status').fadeIn();
		$('.load_status h3').contents().remove();
		$('.load_status h3').append(msg);
		setTimeout (hideStatus, 3300);
	}
	
	function popOver (id) {
		$(id).fancybox({
			'overlayOpacity'	: .75,
			'overlayColor'		: '#000',
			'padding'			: 0
		}).trigger('click');
		$(id).unbind('click');
	}
	
	function hideStatus () {
		$('.load_status').fadeOut();
	}
	
	function setCookie(c_name,value,exdays) 	{
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	function getCookie(c_name) {
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++) {
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name) {
				return unescape(y);
			}
		}
	}
	
	function checkBackgroundColor() {
		var cook1=getCookie("backgroundColor");
		if (cook1 != null && cook1 != "") {
			setBackgroundColor(cook1);
		} else {
			setCookie("backgroundColor","grey",365);
		}
	}
	
	function setBackgroundColor(cl) {
		var i;
		
		for (i = 0; i < bg_colors.length; i++) {
			// remove color classes
			$('.column_right_645').removeClass(bg_colors[i]);
			
			// hilite selector
			$(bg_selectors[i] + " a").removeClass("selected");
			if ($.inArray(cl, bg_colors) == i) {
				$(bg_selectors[i] + " a").addClass("selected");
			}
		}
		// add new color class
		$('.column_right_645').addClass(cl);
	}
	
	function delayProductNav () {
		// button events for product nav
		$('.pr_carpet,.pr_hardwood,.pr_ceramic,.pr_laminate').hover(
			function () {
				if ($(this).attr("class") == "pr_carpet") {
					$(this).css("background-image","url('/img/page_parts/prd_1_on.gif')");
				}
				if ($(this).attr("class") == "pr_hardwood") {
					$(this).css("background-image","url('/img/page_parts/prd_2_on.gif')");
				}
				if ($(this).attr("class") == "pr_ceramic") {
					$(this).css("background-image","url('/img/page_parts/prd_3_on.gif')");
				}
				if ($(this).attr("class") == "pr_laminate") {
					$(this).css("background-image","url('/img/page_parts/prd_4_on.gif')");
				}
				$(this).find('a').css('color','#e12f21');
				$(this).find('.prd').show();
				$('.product_nav').css("background-image","url('/img/page_parts/prd_bg_on.gif')");
			},
			function () {
				$(this).css("background-image","none");
				$(this).find('a').css('color','#fff');
				$(this).find('.prd').hide();
				$('.product_nav').css("background-image","url('/img/page_parts/prd_bg.gif')");
			}
		);
	}
	
	function centerLoadStatus () {
		var left = new String((window.innerWidth - 250)/2) + "px";
		$('.load_status').css('left',left);
	}
	
	function setAskMyStore (state) {
		if (state == "show") {
			$('.ask_my_store').css("display","block");
		}
		if (state == "hide") {
			$('.ask_my_store').css("display","none");
		}
	}
	
	var slides = new Array();
	var slide_buttons = new Array();
	var destinations = new Array();
	var sld = 0;
	var busy = false;
	var timer = null;
	
	function setSlider () {
		// get the number of slides
		var slide_num = $('.slider_content').children().size();
		
		// slide buttons
		var buttons = "";
		
		// set width on content holder
		$('.slider_content').css('width', new String(345 * slide_num) + "px")
		$('.slider_buttons').css('width', new String(15 * slide_num) + "px");
		
		// create a button for each slider and make the entire slider box clickable
		$('.slider_content').children().each( function (index) {			
			// add to slides array
			slides[index] = $(this);
			
			$(this).click( function () {
				var thisURL = $(this).contents().find("a").attr('href');
				window.location.href = thisURL;
			});
			
			button = "<li></li>";
			$('.slider_buttons ul').append(button);			
		});
		
		// create event handler for each slider button
		$('.slider_buttons ul').children().each( function (index) {
			slide_buttons[index] = $(this);
			$(this).click( function () {
				goToSlide(index, "press");						 
			});
		});
		
		// initialize buttons
		goToSlide(sld);
		
		// start timer
		setTimer();
	}
	
	function goToSlide (pg, press) {
		if (pg == null || pg == undefined) {
			pg = sld;	
		}
		
		// hilite button
		for (i = 0; i < slide_buttons.length; i++) {
			if (i == pg) {
				if (div) {
					$(slide_buttons[pg]).addClass("on");
				} else {
					alert("NO!");	
				}
			} else {
				if (div) {
					$(slide_buttons[i]).removeClass("on");
				} else {
					alert("NO!");	
				}
			}
		}
		
		// move slider
		var xPos = -(pg * 345)
		$('.slider_content').stop().animate({ left: xPos }, 800);
		
		// set slide number
		if (press) {
			sld = pg;
		} else {
			sld += 1;
			if (sld > slide_buttons.length - 1) {
				sld = 0;
			}
		}
	}
	
	function setTimer () {
		timer = setInterval("goToSlide()",5000);
		$('#sliders').hover(function () {
			clearInterval(timer);
			$('#sliders').mouseleave(function () {
				timer = setInterval("goToSlide()",5000);	
				$('#sliders').unbind("mouseleave");
			});
		});
	}
	
	function slideGo (goto) {
		//setTimeout(goToSlide(goto),5000);
	}
	
	var background_img = new Array("img/page_parts/home_main_1.jpg");
	
	function setBackground () {
		var min = 0;
		var max = background_img.length - 1;
		var ran_num = Math.floor(Math.random() * (max - min + 1) + min);
		$('.home_main').css("background-image","url(\'" + background_img[ran_num] + "\')");
	}
