var GB_ANIMATION = true;
$(document).ready(function() {
	//Open External Links in a New Window/tab
	$('a[rel*=external]').click( function() {
		this.target = "_blank";
	});
	/*! jQuery Cycle Plugin (withOUT Transition Definitions) */
    $('#rotator').cycle({
		cleartypeNoBg: true, // enable cleartype corrections
        prev:    '.rotatorLeft',
        next:    '.rotatorRight'
	});
	$('#Testimonials').cycle({
		cleartypeNoBg: true, // enable cleartype corrections
        prev:    '.rotatorLeft',
        next:    '.rotatorRight'
	});
	$('.rotatorLeft').show();
	$('.rotatorRight').show();
	$('.rotatorTop').removeClass('noScript');
	//jQuery FAQ
	$('#faq').makeFAQ();
	//ListNav
	$('#glossary').listnav({ 
    	includeNums:false,
		showCounts:false,
		initLetter: 'a',
	    noMatchText: '<strong>Sorry.</strong> No entries matched your selected letter, please click another letter.'
	});
	
	// Contact Form Functions			   	
	$("div.loading").hide();
	$("div#thanks").hide();
	$("div#spammer").hide();
	$("form.retry").hide();
	$("div#noMoreChances").hide();

	jQuery.validator.addMethod("phone", function(phone_number, element) {
		phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1(.|-)?)?(\([2-9]\d{2}\)|[2-9]\d{2})(.|-)?[2-9]\d{2}(.|-)?\d{4}$/);
	}, "Invalid Phone Number");	
	jQuery.validator.messages.required = "Required";
	$("form.validate").validate({
		messages: {
			email: {
				email: "Invalid Email Address"
			}
		}
	});
	
	/* Feedback Detect Window Size
	if ($.browser.msie) { }
	else {
		if ($(window).width()>=1000) {
			$("div#feedback").show();
		}
		$(window).resize(function() {
			if ($(window).width()<=1000) {
				$("div#feedback").hide();
			}
			else {
				$("div#feedback").show();
			}
		});
	}*/
	
	// Lazy Loader
	$("div.middle img").lazyload({         
		 placeholder : "/images/gal-bg.png",
		 threshold : 400
	});
	
	// Greybox
	if ($.browser.msie) {
        $("a.greybox").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,579,950);
          return false;
		});
        $("a.gallery").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,435,546,'gallery');
          return false;
        });
        $("a.system").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,579,895,'system');
          return false;
        });
	}
	else {
        $("a.greybox").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,529,900);
          return false;
		});
        $("a.gallery").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,385,496,'gallery');
          return false;
        });
        $("a.system").click(function(){
          var t = this.title || $(this).text() || this.href;
          GB_show(t,this.href,529,845,'system');
          return false;
        });
	}
});

function submitForm() {
	var formType = $('.type').val();
	var postdata = $('#' + formType).serialize();
	var formValid = $('#' + formType).valid();
	//alert(postdata);
	
	if(formValid == true) {
		$(".submit").hide();
		$("div.loading").show();
		$.get("includes/php/mailer.php", {postdata: ""+postdata+""}, function(data){
			if(data.response == 'true') {			
				$("div.loading").hide();
				$("form#" + formType).hide();
				$("div#spammer").show();
				$(".submit").show();
				$("form.retry").show();
			} else {
				pageTracker._trackPageview('/' + formType + 'Submitted');
				$("form." + formType).hide();
				$("div#thanks").show();
				$("h1").hide();
				if(formType = "contact") {
					$("div.mapRight").hide();
					$("p.intro").hide();
				}
			}
		}, "json");
	}
}

function submitRetryForm() {
	var formType = $('.type').val();
	var answer = $('#answer').val();
	var postdata = $('#' + formType).serialize();

	$(".submit").hide();
	$("div.loading").show();
	$.get("includes/php/mailer.php", {postdata: ""+postdata+"", answer: ""+answer+"", retry: "true"}, function(data){
		if(data.response == 'failure') {
			$("div.loading").hide();
			$("form.retry").hide();
			$("div#noMoreChances").show();	
			$("div#spammer").hide();
		} else {
			pageTracker._trackPageview('/' + formType + 'Submitted');
			$("form.retry").hide();
			$("div#spammer").hide();
			$("div#thanks").show();
			$("h1").hide();
			if(formType = "contact") {
				$("div.mapRight").hide();
				$("p.intro").hide();
			}
		}
	}, "json");
}

/* for ie6 hover nav functionality */
sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);