/**
 * Functions and classes for Dine4az.com
 */

var Forms = {
	/**
	 * Returns the remaining number of characters for a maximum-length form field
	 */
	
	defaultSearchLabel: 'Enter Zip Code',
    validateCitySearch:function(formObject) {
        var valid = false;
        var count = 0;
        var inputs = formObject.getElementsByTagName('select');
        for(var i = 0; i < inputs.length; i++) {
            var selectId = inputs[i].id;
            var errorDiv = selectId + '_error';
            if(inputs[i].value == '') {
                if(selectId == 'search_city') {
                    var error_msg = 'Please select a City';
                } else if (selectId == 'filter_cat') {
                    var error_msg = 'Please Select Cuisine Type';
                }
                document.getElementById(errorDiv).innerHTML = error_msg;
                count = count + 1;
            } else {
                document.getElementById(errorDiv).innerHTML='';
            }
        }
        if(count == 0) {
            valid = true; 
        }
        return valid;
    },
    regionSelected:function(input, citySearch) {
        pattern = /region/gi;
        if(input.value.match(pattern)) {
            citySearch.value = 'false';
        } else {
            citySearch.value = 'true';
        }
    },
	characterCount:function(input, maxChars, idToUpdate) {
		var currentCount = input.value.length;
		// maxlength for textareas
		if(input.nodeName.toLowerCase() == 'textarea' && currentCount > maxChars) {
			var val = input.value;
			input.value = val.slice(0, maxChars);
			currentCount = input.value.length;
		}
		var remaining = maxChars - currentCount;
		document.getElementById(idToUpdate).value = remaining;
	},
	/**
	 * Disable coupon value field if type='other'
	 */
    disableCouponValue:function(couponType,couponValueInput) {
        if(couponType == 'other') {
            couponValueInput.disabled = true;
        } else {
            couponValueInput.disabled = false;
            couponValueInput.focus();
        }
    },
    defaultSearchMessage:function(searchInput) {
        if(searchInput.value == '') {
            searchInput.value = Forms.defaultSearchLabel;
        } else if (searchInput.value == Forms.defaultSearchLabel) {
            searchInput.value = '';
        }
    }
} // end Forms{}

var Flash = {
    successCssSelector: '#flashMessage.success',
    fadeoutSuccess:function() {   
        // fade out success messages if any
		if($(Flash.successCssSelector).length > 0) {
    		setTimeout(function(){
                $(Flash.successCssSelector).fadeOut(2000);
            }, 2000);
        }
        
    }
}

$(document).ready(function() {

	$("#log_in_link").toggle(
		function () {
			$("li#log_in form").css("display", "block");
			$("li#log_in").toggleClass("open");
			$("div#blind").css({
				"display" : "block",
				"z-index" : "18"
			});
		},
		function () {
			$("li#log_in form").css("display", "none");
			$("li#log_in").toggleClass("open");
			$("div#blind").css({
				"display" : "none",
				"z-index" : "18"
			});
		}
	);

	$("ul#new-restaurants li:even").addClass("alt");
	$("ul#user_state>li:last>a").addClass("last");
	$("ul#sponsors li:even").addClass("alt");
	$("div#main form p:even").addClass("alt");
	$("ol#reviews li:last").addClass("last");
	$("div#aside ul li.vcard:first").addClass("first");
	$("div#aside ul li.vcard:last").addClass("last");

	$("#refer_a_friend_link").toggle(
		function () {
			$("div#refer_a_friend").css("display", "block");
			$("div#blind").css({
				"display" : "block",
				"z-index" : "29"
			});
		},
		function () {
			$("div#refer_a_friend").css("display", "none");
			$("div#blind").css({
				"display" : "none",
				"z-index" : "18"
			});
		}
	);
	
/*
	$("#aside li.vcard div.restaurant-details-container").slideToggle();

	$("#aside li.vcard").click(
		function () {
			$(this).children("div.restaurant-details-container").slideToggle();
		}
	);
*/
    
    // fade out success messages if any
	Flash.fadeoutSuccess();
	// if(document.getElementById('search') && !document.getElementById('admin')) {
	//         Forms.defaultSearchMessage(document.getElementById('search'));
	//     }
	//     if(document.getElementById('search-index')) {
	//         Forms.defaultSearchMessage(document.getElementById('search-index'));
	//     }
	
});