/**
 * Contains all this site's custom JavaScript EXCEPT form validations
 * for pages like sign in, sign up, edit profile, etc. which are found in
 * [asset directory]/js/jquery-custom-form-x.x.js
 * 
 * This file, DOES include the validation for the search bar as that 
 * is on almost all pages, except the sign in and sign up flows
 */
$(document).ready(function(){
	/**
	 * For SEO reasons, we have text in the header links, 
	 * but this code removes that text so it will not be seen
	 */
	$('#navGuide, #navGrow').html("&nbsp;");
	
	/* protecting the search validation from focusFlow pages which do not have the search bar */
	if (typeof($('#searchKeyword').val()) != 'undefined') {
		/** set the prefill here for the search bar */
		var preFill = ['Search for...', 'City, State'];
		
		/**
		 * set the prefill on load, when someone clicks in the input 
		 * box, remove the prefill
		 */
		/* cookie is saving prevKeyword, and Java Backend is using it as the prefill */
		var prevKeyword=$('#searchKeyword').val();
		$('#searchKeyword').val(prevKeyword==''?preFill[0]:prevKeyword)
			.click(function () {
					if ($.trim($(this).val()) == preFill[0] || $.trim($(this).val()) == prevKeyword) {
						$(this).val('').attr('class', 'legit');
					}
				})
			.blur(function () {
			    	if(!$.trim($(this).val())) {
			    		$(this).val(preFill[0]).attr('class', '');
			    	}
			    });
		
		/**
		 * remove the prefill when someone clicks in the input box
		 */
		/* cookie is saving prevLocation, and Java Backend is using it as the prefill */
		var prevLocation=$('#searchCitySt').val();
		$('#searchCitySt').val(prevLocation==''?preFill[1]:prevLocation)
			.click(function () { 
					if ($.trim($(this).val()) == preFill[1] || $.trim($(this).val()) == prevLocation) {
						$(this).val('').attr('class', 'legit');
					}
			    })
		    .blur(function () {
				if(!$.trim($(this).val())) {
					$(this).val(preFill[1]).attr('class', '');
				}
	    });
		
		/**
		 * if the user submits without changing either the keyword or city
		 */
		$('#search')
			.submit(function(ev) {
				var doSubmit = true,
					val = [$.trim($('#searchKeyword').val()),
					       $.trim($('#searchCitySt').val())],
					i = 0;
				/* keywords */
				if (val[i] == preFill[i]) {
					$('#searchKeyword').val('');
				} else {
					$('#searchKeyword').attr('class', 'legit');
				}
				i += 1;
				/* city, ST */
				if (val[i] == preFill[i]) {
					doSubmit = false;
				} else {
					$('#searchCitySt').attr('class', 'legit');
				}
				if (!doSubmit) {
					ev.preventDefault();
				}
			});
	}
		
	/**
	 * wrap portletsPad with the main portlets box
	 */
	$('#portlets')
		// wrap with balanced pair, or set of tags
		.wrap('<div class="portletsBox"></div>');
	/**
	 * add corners to outer main portlets box
	 */
	$('.portletsBox')
		// add to the front inside the box
		.prepend(
			'<div class="portletsTopLeft"></div>' +
		    '<div class="portletsTopRight"></div>'
	    )
	    // append to the back inside the box
		.append(
			'<div class="portletsBottomLeft"></div>' +
		    '<div class="portletsBottomRight"></div>'
		);
	/**
	 * wrap individual portlet with a portlet box
	 */
	$('.portlet')
		// wrap with balanced pair, or set of tags
		.wrap('<div class="portletBox"></div>');
	/**
	 * add corners to the individual portlet boxes
	 */
	$('.portletBox')
		// add to the front inside the box
		.prepend(
			'<div class="topLeft"></div>' +
		    '<div class="topRight"></div>'
	    )
	    // append to the back inside the box
		.append(
			'<div class="bottomLeft"></div>' +
		    '<div class="bottomRight"></div>'
		);

	/**
	 * Star ratings
	 * Turn stars on or off on all previous stars when mouse hovers over or out of a rating star.  
	 * This must work even when there are multiple sets of ratings on one page.
	 * Also used to initialize stars on a page.
	 */ 
	function hoverRating(element, on) {
		element = $(element);
		var fillStarsUpTo = element;
		var rating, index;
		var labels = element.nextAll(".ratingLabels");
		labels.children().removeClass("ratingLabelVisible");
		if (on) {
			index = parseInt(element.parent().children(".star").index(element));
		} else {
			rating = parseInt(element.nextAll(".reviewScore").val()) || 0;
			index = rating - 1;
			fillStarsUpTo = element.parent().children(".star").eq(index);
		}
		element.parent().children(".star").removeClass("filledStar");
		if (on || rating > 0) {
			fillStarsUpTo.prevAll(".star").andSelf().addClass("filledStar");
			labels.children().eq(index).addClass("ratingLabelVisible");
		}
	}

	/**
	 * Notice when the mouse moves over a rating star.
	 */
	$(".rating").hover(function(){ 
		hoverRating(this,1); 
	},function(){ 
		hoverRating(this,0); 
	});
	
	/**
	 * Store the rating on click. There must be a sibling element with class "reviewScore" after the stars.
	 */ 
	$(".rating").click(function(){
		var element = $(this);
		var index = parseInt(element.parent().children(".rating").index(element));
		element.nextAll(".reviewScore").val(index+1);
	});
	
	/**
	 * Initialize stars if the page is preloaded with ratings.  Each rating must be in 
	 * an element with class "reviewScore" and which is a sibling of stars with class "rating".
	 */ 
	$(".reviewScore").each(function(){
		var element = $(this);
		var rating = parseInt(element.val()) || 0;
		if (rating > 0) {
			fillStarsUpTo = element.parent().children(".star").eq(rating-1);
			hoverRating(fillStarsUpTo, 0);
		}
	});
	
	/**
	 * add body and content to the overlay boxes
	 */
	$('.overlay .content')
		// wrap with balanced pair, or set of tags
		.wrap('<div class="body"></div>');
	
	/**
	 * add corners to the overlay boxes
	 */
	$('.overlay')
		// add to the front inside the box
		.prepend(
			'<div class="oTopLeft"></div>' +
			'<div class="oTopRight"></div>' +
			'<div class="oTopCenter"></div>'
	    )
	    // append to the back inside the box
		.append(
			'<div class="oBottomCenter"></div>' +
			'<div class="oBottomLeft"></div>' +
			'<div class="oBottomRight"></div>'
		);
	/**
	 * add to the front of the overlay box body
	 */
	$('.overlay .body')
		// add to the front inside the body
		.prepend(
			'<div class="close">' +
				'<a href="close">\n</a>' +
			'</div>'
		);

	/**
	 * set click events to open specific overlays add business 
	 * and edit profile links
	 */
	$('#profile #iconLinkH1, #profile #editLinkH1').click(function(ev){
		ev.preventDefault();
		$('.overlay').hide();
		$('#profile #editOverlayH1').show();
	});
	$('#profile #iconLinkH2, #profile #editLinkH2').click(function(ev){
		ev.preventDefault();
		$('.overlay').hide();
		$('#profile #editOverlayH2').show();
	});
	$('#businessList #addBusinessLink').click(function(ev){
		ev.preventDefault();
		$('#businessList #addBusinessOverlay').show();
	});
	$('#businessList #addAnotherLink').click(function(ev){
		ev.preventDefault();
		$('#businessList #addAnotherOverlay').show();
	});
	$('#signIn #whatIsLink').click(function(ev){
		ev.preventDefault();
		$('#signIn .overlay').toggle('slow');
	});

	/**
	 * set a click event to close the overlay when clicking the close x
	 */
	$('.overlay .close a').click(function(ev){
		ev.preventDefault();
		$('.overlay').hide();
	});
	
	
});// end .ready

function openIAMForgotPage(){
	window.open('https://about.intuit.com/commerce/account/secure/forgot_password.jsp');
}
