// IDMA javascript custom functions

// Font replacement for tabs

Cufon.replace('.mainpage_links li a');

// Slideshow on home page

$(document).ready(function() {
	$("#carousel").cycle({
		delay: 2000,
		speed: 500
	});
});

// make tabs have highlight when currently selected

$(document).ready(function() {

	var pathName = parseUri(window.location);
   
   $('a.menulink').each(function() {
      var linkName = parseUri($(this).attr('href'));
      // alert (linkName.host + " : " + pathName.host);
      
      if (linkName.path == pathName.path && (pathName.host == "" || linkName.host == pathName.host)) {
         $(this).addClass("current");
      };
   });

});


// newsletter form validation

$(document).ready(function() {

	$("#footer_newsletter form").validate({
	
	messages: {
		last_name: "Oops! Please enter your name.",
		webtolead_email1: {
			required: "Oops! Please enter your e-mail address.",
			email: "Your email address must be in the format of name@domain.com."
			}
		},
	errorPlacement: function(error, element) {
    	element.parent().prepend(error);
   	},
   	submitHandler: function(form) {
    	$(form).append('<input type="hidden" name="campaign_id" value="a131c0d1-bbf1-c468-023d-4bf9a74c87b2" /><input id="info_assigned_user_id" name="assigned_user_id" value="86615ef3-5538-7f96-56f1-4bb3f9454f53" type="hidden" />');
    	form.submit();
   }
	
	});

});






// application form
	
$(document).ready(function() {
if ($("#contactformwrapper").length) {

	// if the form exists on this page, then... 	
	$("#info_primary_address_country").after("<select id='info_primary_address_country' name='primary_address_country' onchange='updateState(this.id)'></select>").remove();
	
	$("#info_primary_address_state").after("<select id='info_primary_address_state' name='primary_address_state' class='required'></select>").remove();

	$("#apply_primary_address_country").after("<select id='apply_primary_address_country' name='primary_address_country' onchange='updateState(this.id)'></select>").remove();
	
	$("#apply_primary_address_state").after("<select id='apply_primary_address_state' name='primary_address_state' class='required'></select>").remove();
	
	$.getScript("/mod/theme_i4dma/views/default/js/country_state.js", function() {
	initCountry();
	}
	);

	// initialize buttons at top of form

	$("#selection input").click(	
		function() {
			var val = $(this).attr("id");
			
			if (val == "contactinformation") val = "information";
			if (val == "contactapplication") val = "application";
			$("#information, #application").hide();
			$("#" + val).slideDown();
			}		
		);
		
		
	// make textareas clear when clicked inside
	
	$("#contactformwrapper textarea").click(function(){
		if (!$(this).hasClass("clicked")) {
			$(this).addClass("clicked").val("");
		};
	});
	
	// set up form validation
	
	$("#information form").validate({
	
	messages: {
		first_name: "Oops! Please enter your first name.",
		last_name: "Oops! Please enter your last name.",
		primary_address_city: "Oops! Please enter your city or town.",
		primary_address_country: "Oops! Please enter your country.",
		primary_address_state: "Oops! Please enter your state or province.",
		webtolead_email1: {
			required: "Oops! Please enter your e-mail address.",
			email: "Your email address must be in the format of name@domain.com."
			},
		area_of_interest_c:"Please enter an area of interest.",
		phone_home:"Oops! Please enter your phone number."
			
	},
	errorPlacement: function(error, element) {
     element.parent().prepend(error);
   	},
   	submitHandler: function(form) {
    	$(form).append('<input type="hidden" name="campaign_id" value="d7bdbb31-3f69-afbd-1c68-4bf7fd1d2d5c" />');
    	form.submit();
   }	
	
	});
	
	
	$("#application form").validate({
	
	messages: {
		first_name: "Oops! Please enter your first name.",
		last_name: "Oops! Please enter your last name.",
		primary_address_city: "Oops! Please enter your city or town.",
		primary_address_country: "Oops! Please enter your country.",
		webtolead_email1: {
			required: "Oops! Please enter your e-mail address.",
			email: "Your email address must be in the format of name@domain.com."
			},
		area_of_interest_c:"Please enter an area of interest.",
		phone_home:"Oops! Please enter your phone number."
			
	},
	errorPlacement: function(error, element) {
     element.parent().prepend(error);
   	},
   	submitHandler: function(form) {
    	$(form).append('<input type="hidden" name="campaign_id" value="d7bdbb31-3f69-afbd-1c68-4bf7fd1d2d5c" />');
    	form.submit();
   }	
	
	});
	
	
	
	// end test to see if contact form exists
	}

});


// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


