/* Default Javascript File */

$(document).ready(function() {
  // do stuff when DOM is ready
  
	$('#carousel, #contentHome').cycle({ 
		timeout:  7500, 
		speed:  1000 
	});
  
    /* ---------------- START Contact Form Validation ---------------- */

	$("#contactForm").validate({
		errorElement: "span",
		errorPlacement: function(error, element) {
			if (element.attr("name") == "ctl00$phone") {
				$("#ctl00_phone").css("margin-left", "0px");
				error.css("margin-left", "0px");
				error.appendTo(element.parent());
			} else {
				//error.insertAfter(element);
				error.insertBefore(element.prev());
			}
		},
		rules: {
			//none
			first_name: "required",
			last_name: "required",
			email: { required: true, email: true },
			message: "required"
		},
		messages: {
			first_name: 'field required',
			last_name: 'field required',
			email: 'field required',
			message: 'field required'
		},
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				//alert("errors total = "+errors);
				//$('table.questions td label.error').parent().parent().addClass('errorRow');
			} else {
				// $("div.error").hide();
			}
		},
		onkeyup: false
	});
	
	/* ---------------- END Contact Form Validation ---------------- */

});

function addLoadEvent(func) {

  var oldonload = window.onload;

  if (typeof window.onload != 'function') {

    window.onload = func;

  } else {

    window.onload = function() {

      oldonload();

      func();

    }

  }

}

function focusLabels() {

  if (!document.getElementsByTagName) return false;

  var labels = document.getElementsByTagName("label");

  for (var i=0; i<labels.length; i++) {

    if (!labels[i].getAttribute("for")) continue;

    labels[i].onclick = function() {

      var id = this.getAttribute("for");

      if (!document.getElementById(id)) return false;

      var element = document.getElementById(id);

      element.focus();

    }

  }

}

function resetFields(whichform) {

  for (var i=0; i<whichform.elements.length; i++) {

    var element = whichform.elements[i];

    if (!element.defaultValue) continue;

    element.onfocus = function() {

    if (this.value == this.defaultValue) {

      this.value = "";

     }

    }

    element.onblur = function() {

      if (this.value == "") {

        this.value = this.defaultValue;

      }

    }

  }

}

function isFilled(field) {

  if (field.value.length < 1 || field.value == field.defaultValue) {

    return false;

  } else {

    return true;

  }

}

function prepareForms() {

  for (var i=0; i<document.forms.length; i++) {

    var thisform = document.forms[i];

    resetFields(thisform);
}

}

addLoadEvent(focusLabels);
addLoadEvent(prepareForms);


/* ---------------- START Sub Nav Menu Functionality ---------------- */


$('#subnav ul ul').hide().find('ul');
$('#subnav ul ul ul').show();
$('#subnav ul li.active ul').show();
$('#subnav > ul > li:has(li)').not('#subnav ul ul li').addClass('acc-arrow').find('a').not('#subnav ul ul li a').wrapInner('<span></span>');


/* ===== SubNav functions ============ */

$('#subnav > ul > li.acc-arrow').bind( "hover", { speed:200, delay:200 }, function(){});
$('#subnav > ul > li.acc-arrow').hover( hoverNavMain, outNavMain );

// set mouseout timer to remove subnav if outside for more than 1 second
var hideTimer = setTimeout(function() {  }, 1);//initialise so not undefined
var open;

function hoverNavMain() {
	clearTimeout(hideTimer );
	open = true;
	$(this).find('a').next().slideDown('normal');
	$(this).addClass("current");
	
}	

function outNavMain() {
	var target = this;
	$(this).addClass("current");
	clearTimeout(hideTimer);
	hideTimer = setTimeout(function() { 
		open = false;
		$('#subnav li.current').not('li.active, #subnav ul li li').find('a').next().slideUp('normal', function () {
			if(open == false){
				$('#subnav li.current').not('li.active, #subnav ul li li').removeClass("current");
			}else {
				$(target).addClass("current");
			}
			
		  });
	//$('#subnav li.current').not('li.active, #subnav ul li li').removeClass("current");
	 }, 1000);//end hideTimer
}


/* ---------------- END Sub Nav Menu Functionality ---------------- */
