// shadow box init
Shadowbox.init();

var pageName = window.location.pathname.toString();
//pageName = pageName.substring(1, pageName.length);
//var sub = pageName.match(/announcements/);

$.validator.setDefaults({
	highlight: function(input) {
		$(input).addClass("textInputOn");
	},
	unhighlight: function(input) {
		$(input).removeClass("textInputOn");
	}
});

$(function(){
	if (pageName == 'plant-catalog-alphabetical') {
		$('#catalogAlphaList').listnav({
			noMatchText: 'No plants found matching that letter, please choose another letter.',
			showCounts: false
		});
	}
});

//$(window).load(dropDownList);

// a custom method making the default value invalid
$.validator.addMethod("defaultInvalid", function(value, element) {
	return value != element.defaultValue;
}, "");

$(document).ready(function() {	
	// Twitter Feed
	$(".tweet").tweet({
		join_text: "auto",
		username: "treelandaz",
		avatar_size: 78,
		count: 1,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	
	if (pageName.match('/announcements/') != null) {
		$('#pageName').hide();
	}
	
	if (pageName.match('/_webapp_1167603/') != null) {
		document.title = "Treeland Nurseries | Plant Catalog | " + document.title;
	}
	
	// Newsletter Form Validation
	$("#newsletterForm").validate({
		messages: {
			FirstName: "",
			LastName: "",
			EmailAddress: ""
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit();
			alert('Your subscription request has been received. Please check your email for confirmation.');
			$(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	// Search Form Validation
	$("#siteSearch").validate({
		messages: {
			CAT_Search: ""
		},
		errorClass: "textInputSearch",
		errorElement: "div"
	});
	
	// Login Form Validation
	$("#loginForm").validate({
		messages: {
			SZUsername: "",
			SZPassword: "",
			Username: "",
			Password: ""
		},
		errorElement: "div"
	});
	
	// Ask Bogie Form Validation
	$("#askBogieForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: ""
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit();
			alert('Your question has been received!');
			$(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	// Contact Form Validation
	$("#contactForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FullName: "",
			EmailAddress: "",
			HomePhone: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your request has been submitted successfully. One of our team members will be in contact with you shortly.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	//wholesaleRegForm
	$("#wholesaleRegForm").validate({
		rules: {
			ValidateBox: {
				required: true,
				range: [4,4]
			}
		},
		messages: {
			FirstName: "",
			LastName: "",
			Company: "",
			EmailAddress: "",
			WorkPhone: "",
			WorkAddress: "",
			WorkCity: "",
			WorkState: "",
			WorkZip: "",
			Username: "",
			Password: "",
			PasswordConfirm: "",
			ValidateBox: ""
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit();
			alert('Your request has been submitted successfully. One of our team members will be in contact with you shortly.');
			jQuery(form).resetForm();
			return false;
		},
		errorElement: "div"
	});
	
	// Populate Product Types On Change
	$("#CAT_Custom_164494").change(dropDownList);
});

function dropDownList(){
	var category = $('#CAT_Custom_164494').val();
	
	$('#CAT_Custom_164495').html('');
	
	switch(category) {
		case 'Bedding':
			$('#CAT_Custom_164495').html('<option value="Annuals">Annuals</option><option value="Bulbs">Bulbs</option><option value="Herbs">Herbs</option><option value="Vegetables">Vegetables</option>');
		break;
		
		case 'Shrubs':
			$('#CAT_Custom_164495').html('<option value="Grasses">Grasses</option><option value="Ground Cover">Ground Cover</option><option value="Roses">Roses</option><option value="Shrubs">Shrubs</option><option value="Succulants">Succulants</option><option value="Vines">Vines</option>');
		break;
		
		case 'Trees':
			$('#CAT_Custom_164495').html('<option value="Deciduous">Deciduous</option><option value="Desert">Desert</option><option value="Evergreen">Evergreen</option><option value="Fruit">Fruit</option><option value="Palms">Palms</option>');
		break;
		
		default:
			$('#CAT_Custom_164495').html('');
	}
}

// Accordion
$(function(){
	$("#accordion").accordion({ header: "h3", active: false, collapsible: true });
});

// Replace Broken Image
function replaceImage (whichImage) {
	whichImage.src='/images/noPhotoTh.jpg';
}

// Custom jquery code
function formatText(index, panel) {
  return index + "";
}

(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
	
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

$.preLoadImages("/images/buttons/submitOn.png", "/images/buttons/submit2On.png",
					"/images/buttons/submit3On.png", "/images/buttons/submit4On.png",
					"/images/forms/textInputOn.png", "/images/buttons/registerOn.png", 
					"/images/buttons/loginOn.png");
					
