$(document).ready(function() {initMenu();});

function initMenu() {
	$("a[id^=nav_title_]:last").parent().addClass("last");
	$("li[id^=subnav_]").addClass("last");
	$("#subnav_5 li:last").addClass("last");
	$("#subnav_6 li:last").addClass("last");
	$("#nav2").hover(function() {
		$("a[id^=nav_title]").hover(function() {
			$("li[id^=subnav_]").css("display","none");
			$id_number = $(this).attr("id");
			$id_number = $id_number.substr(10);
			$(("#subnav_") + $id_number).css("display","inline");
		})
	}, function() {
			$("li[id^=subnav_]").css("display","none");
			$("#subnav_" + $open_menu).css("display","inline");
	});
	if(($open_menu).substr(2) != "") {
		$open_menu = $open_menu.substr(0,1);
	}
	$("#subnav_" + $open_menu).css("display","inline");
}

/* Email functions start */

//Grab data from email fields
function ef_email($type) {
	$email_id = "";
	$email_reply = "";
	//Grab all input id into $email_arr
	$inputs = $('#email_form input, #email_form textarea, #email_form select');
	var values = {};
	$email_arr = new Array;
	$inputs.each(function() {
		if ($(this).attr('id') && $(this).attr('id') != 'ef_submit') {
			$email_arr.push($(this).attr('id'));
		}
		if (($(this).attr('type') == 'checkbox') && ($email_arr[$email_arr.length-1] != $(this).parent().attr('id'))) {
			$email_arr.push($(this).parent().attr('id'));
		}
    });

	//Validate if all required fields filled
	$required = true;
	for (i = 0; i < $email_arr.length; i++) {
		if (($('#'+$email_arr[i]).attr('id').substr(0,6) == 'ef_r_e')) {
			$email_id = $('#'+$email_arr[i]).attr('id');
			$email_reply = $('#'+$email_arr[i]).val();
		}
		if ($('#'+$email_arr[i]).attr('id').substr(0,4) == 'ef_r') {
			if ($('#'+$email_arr[i]).attr('type') != '') {
				if (!$('#'+$email_arr[i]).val()) {
					$required = false;
					$('#'+$email_arr[i]).css("border","1px solid #f00");
				} else {
					$('#'+$email_arr[i]).css("border","1px solid #bbb");
				}
			} else {
				if ($("#"+$email_arr[i]+" :checked").size() == 0) {
					$required = false;
				}
			}
		}
	}
	if (!$required) {
		alert("Please enter the required fields!");
		return false;
	}

	//Concat all data to a string with items separated by :::
	$send_str = "";
	$sep = "|||";
	for (i = 0; i < $email_arr.length; i++) {
		if ($('#'+$email_arr[i]).attr('type') == '') {
			$check_type = "";
			$check_type_checked = $("#"+$email_arr[i]+" :checked");
			if ($check_type_checked.size() > 0) {
				$send_str += $('#'+$email_arr[i]).attr('name')+"::";
				$check_type = "";
				$check_type_checked.each(function() { 
					$check_type += $(this).val()+"::";
				});
				$check_type = $check_type.substr(0,$check_type.length-2);
			}
			$send_str += $check_type+$sep;
		} else {
			if (i == $email_arr.length - 1) {
				$sep = "";
			}
			$send_str += $('#'+$email_arr[i]).attr('name')+"::" + $('#'+$email_arr[i]).val()+$sep;
		}
	}

	$send_str = rawurlencode($send_str);
	
	if (!$('#'+$email_id).val()) {
		ef_send($type,$send_str);
	} else {
		$email = $('#'+$email_id).val();
		if (ef_emailValidate($email)) {
			ef_send($type,$send_str);
		} else {
			alert("Email address was not correct!");
			return false;
		}
	}
}

//Execute sending
function ef_send ($type,$send_str) {
	$("#ef_submit").val("Sending...");
	$.post("/email/",{type:$type,reply:$email_reply,send_str:$send_str},function(data){$("#email_form").text(data);}, "html");
}

//Email format validation
function ef_emailValidate(email) {
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(email);
}
/* Email functions end */

function rawurlencode(str) {
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A');
}

function linkOut(name){
  switch(name){
	case "products":
	newwindow = window.open("/products");
	break;
	case "contact":
	newwindow = window.open("/contact");
	break;
  }
}