YAHOO.namespace('PIZZACOM');
YAHOO.PIZZACOM.homePage = (function() {
    return {
        init : function() {
			
			//jquery using jcarousel plugin
			$('#carousel').jcarousel({
				auto:5,
				wrap: 'last',
				scroll: 1
			});
			
			var zipcode = '#data-zipcode';
			var city = '#data-city';
			var state = '#data-state';
			var address1 = '#data-address1';
			var searchoption = 'zipsearch';
			
			// hide show city state option
			$(".location-option").click(function() {
				$(".zip-option").hide();
				$(".citystate-option").show();
				searchoption = 'citystatesearch';
			});
			
			// show labels inside fields, use title attribute
			function addLabel(){
				if ($.trim($(this).val()) == '')
					$(this).addClass('sample-label').val($(this).attr('title'));
			}
			function removeLabel(){
				if ($(this).val() == $(this).attr('title'))
					$(this).val('').removeClass('sample-label');
			}
			
			function setLabels(){
				//loop each text field
				$('input[type=text][title!=""]').each(function() {
					if ($.trim($(this).val()) == '' || $.trim($(this).val()) == $(this).attr('title')){
						$(this).val($(this).attr('title'));
						$(this).addClass('sample-label');
					}
				}).focus(removeLabel).blur(addLabel);
			}
			
			// run once on load and on submit
			setLabels();
			
			//submit button
			$("#search-submit-link").click(function() {
				$("#search-form").submit();
			});
			
			// clear address on zip change
			loadedzip = $(zipcode).val();
			$(zipcode).keyup(function() {
				if (($(this).val() != loadedzip) && ($(this).val() != '')){
					$(address1).val('');
					setLabels();
				};
			});
			
			// prepare and check data
			$("#search-form").submit(function(){
				//remove inside labels
				$(this).find('input[type=text][title!=""]').each(function() {
					if ($(this).val() == $(this).attr('title')) $(this).val('');
				});
				// form validation
				if(searchoption == 'zipsearch'){
					var inputzip = $.trim($(zipcode).val());
					if(inputzip == '' || !/^([0-9]{5})$/.test(inputzip) ){
						alert('Enter a valid zip code');
						setLabels();
						return false;
					}
					//clear city state
					$(city).val('');
					$(state).val('');
				}else{
					if($.trim($(city).val()) == '' || $.trim($(state).val()) == ''){ 
						alert('Enter a valid City and State');
						setLabels();
						return false;
					}
					//clear zip code value
					$(zipcode).val('');
				}
				// if user enters address
				var inputaddress1 = $(address1).val();
				if($.trim(inputaddress1) == ''){
					//removes empty spaces
					$(address1).val('');
				}
				if($.trim(inputaddress1) != ''){
					//at least one digit and one character in any order
					if(!/.*((\d.*[A-Za-z])|([A-Za-z].*\d)).*$/.test(inputaddress1) ){
						alert('Enter a valid Address');
						setLabels();
						return false;
					}
				}
				
			});
				
			// NEWSLETTER SIGNUP
			var email = '#data-email';
			$("#newsletter-submit").click(function() {
					$("#newsletter-form").submit();
			});
			// prepare and check data
			$("#newsletter-form").submit(function(){
				// form validation
				if($(email).val() == '' || !/^[\w\.=-]+@[\w\.-]+\.[\w]{2,5}$/.test($(email).val()) ){
					alert('Enter a valid Email');
					setLabels();
					return false;
				}
			});
    	}
    }
})();
YAHOO.util.Event.onDOMReady(YAHOO.PIZZACOM.homePage.init);

