// global JS object
omgGlobalJS = function() {


		//global vars
		//var;

	return{
		
		/************************
			Init function
			-- attach event handlers
			-- fire drop down menu if present
			-- country select disable for forms
		************************/
		init: function(){
			// drop down menus if LI has UL menu will fire
			if ($("#navigation-items") != null) {
				$("#navigation-items").children("li").hover(
					function(){
						$(this).children("ul").show();
						if($(this).children("ul").children("li").children("ul")){
							$(this).children("ul").children("li").hover(
								function(){
									$(this).children("ul").show();
								},
								function(){
									$(this).children("ul").hide();
								}
							);
						}
					},
					function(){
						$(this).children("ul").hide();
					});
			}
			// end drop down
			
			//network nav handler
			if ($("#omg-network-nav-btn") != null) {
				$("#omg-network-nav-btn").click(this.networkNav);
			}
			
			//generic contact form country handler Aus + Nz
			if ($("#cf-country") != null) {
				$("#cf-country").change(function () {
					  $("#cf-country option:selected").each(function () {
						  var stateTxt = $(this).val();
							if(stateTxt == "Australia"){
								$("#cf-state").removeAttr("disabled");
							} else{
								$("#cf-state").attr("disabled", true); 
							}
						  });
					}).trigger('change');
			}
			//generic contact form handler
			if ($(".cf-submit") != null) {
				$(".cf-submit").click(this.contactForm);
			}
		},
		// end init
		/************************
			contact form validation
			-- fire on submit click
			-- highlight required field labels
		
		************************/		
		contactForm: function(e){
			//stop form submission
			e.preventDefault();
			//check required fields
			var reqFields = $(".cf-required").length;
			var reqFieldsCount = 0;
			$(".cf-error > ul").html("");
			$(".cf-required").each(function(i){
				var fail = false;
				if($(this).attr("type") == "checkbox" && $(this).attr("checked") == false) {
					fail = true;
				} else if($(this).val() == ""){
					fail = true;
				}
				if(fail) {
					$(this).parent().children().children("strong").css("color","#cc0000");
					var fieldText = $(this).parent().children().children("strong").text();
					if(fieldText == "") {
						fieldText = $(this).attr("name");
						fieldText = fieldText.replace("data[", "");
						fieldText = fieldText.replace("]", "");
					}
					$(".cf-error > ul").append("<li>"+ fieldText.split(":", 1) +"</li>");
				} else {
					$(this).parent().children().children("strong").css("color","#000");
					reqFieldsCount++;
				}
			})
			//final check then give a response
			if(reqFieldsCount == reqFields){
				// submit the form
				$("#contact-form form").submit();
				$(".cf-error").hide();
			} else {
				$(".cf-error").fadeIn();
				window.location.href="#contact-form-top";
			}
		},
		/************************
			network nav open close
			-- fire on link click
			-- slide open panel
			-- switch open/close arrow class
		
		************************/
		networkNav : function(e){
			e.preventDefault();
			if($(this).hasClass("btn-open")){
				$(this).removeClass("btn-open");
				$(this).addClass("btn-closed");
			}else{
				$(this).removeClass("btn-closed");
				$(this).addClass("btn-open");				
			}
			$("#omg-network-nav ul").slideToggle("normal");
		}
	 }
}();
// load on document ready
$(function() {
	omgGlobalJS.init();
})
