$(document).ready(function(){
	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

	$("ul.topnav li span").click(function() { //When trigger is clicked...

		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){
			console.log("hover "+$(this).parent());
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
		
	$('ul.topnav li').hover(
	  function () {
		$('.menu-first', this).addClass('slide-down');
		$('.subnav', this).slideDown('fast');
	  },
	  function () {
		obj = this;
		$('.subnav', this).slideUp('slow');//, function(){ $('.menu-first', obj).removeClass('slide-down'); });
	  }
	);

	$('a.mailme').defuscate();	
});

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true,
        find: /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi,
        replace: '$1@$2'
    }, settings);
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            $(this).attr('href', $(this).attr('href').replace(settings.find, settings.replace));
            var is_link = true;
        }
        $(this).html($(this).html().replace(settings.find, (settings.link && !is_link ? '<a href="mailto:' + settings.replace + '">' + settings.replace + '</a>' : settings.replace)));
    });
};

function checkLength(Obj, message){
if (Obj.value.length == 0){
alert(message+": Field can not be left blank.")
return false
}
return true;
}

function checkForm(formObj) {
if (!checkLength(formObj.p_name,'Name')){
return false;
}
if (!checkLength(formObj.p_email,'Email')){
return false;
}
if (!checkLength(formObj.p_company,'Company')){
return false;
}
if (!checkLength(formObj.p_phone,'Phone')){
return false;
}
return true;
}

function submitForm() {
    if(checkForm(document.submissionform)){
        document.submissionform.submit();
    }
}

