var aau = {
	init: function(){
		$("a.videoLink,a.videolink").bind("click",aau.showVideo);
	},
	
	showVideo: function(evt){
		evt.preventDefault();
		var vidUrl = $(evt.target).attr("video");
		var width = $(evt.target).attr("width");
		var height = $(evt.target).attr("height");
		var s1 = new SWFObject("http://aau-public.alpineaccessu.com/player/player.swf",'player','320','260','9');
		s1.addParam('allowfullscreen','true');
		s1.addParam('allowscriptaccess','always');
		s1.addParam('flashvars','file='+vidUrl);
		s1.write('videoPlaceholder');
	},
	
	contact: {
		init: function(){
			$("#contact_form").validate({
				submitHandler: function(one,two,three){
					aau.contact.submitForm();
				}
			});
			$("#email2").bind("keydown",aau.contact.compareEmail);
			$("#email2").bind("keyup",aau.contact.compareEmail);
		},
		
		submitForm: function(evt){
/* TODO
	compare 2 emails
	hide form on success
checkbox required?

*/
			
//			evt.preventDefault();
			var fieldValues = "";
			var fields = {};
			var form = $("textarea, input, select");
			aau.contact.messageBody = $("#text").attr("value");
			form.each(function(i,item){
				if(item.name.indexOf("email") == -1 && item.name != "text" && item.name != "submit" && item.name != "subject" && item.name != "interested-in" && item.name != "agree"){
					fieldValues += item.name +": " + item.value + "\n";
					$("#text").attr("value",fieldValues + "\n"  + aau.contact.messageBody);
				}
				if(item.name == "agree"){
					fieldValues += item.name +": " + item.checked + "\n";
					$("#text").attr("value",fieldValues + "\n"  + aau.contact.messageBody);
				}
				if(item.name == "interested-in"){
					$("#subject").attr("value",$("#subject").attr("value") + " - " + item.value);
				}
				fields[item.name] = item.value;
				if (form.size() - 1 == i) {
					fields.text = $("#text").attr("value");
	//				$("#text").attr("value",txt + "\n"  + $("#text").attr("value"));
					$.ajax({
					  url: "/dynamic/contact/contact-us/",
					  global: false,
					  type: "POST",
					  data: fields,
					  dataType: "html",
					  success: function(msg){
						 $("#response").html("<h1>"+msg+"</h1>");
						 $("#contactForm").hide();
					  }
				   });
					return false;
				}
			});
		},
		
		compareEmail: function(evt) {
			var label = $(".email2");
			if($("#email").attr("value") == $("#email2").attr("value")){
				label.addClass("match");
				label.removeClass("diff");
			} else {
				label.removeClass("match");
				label.addClass("diff");
			}
		}
	},
	
	homepageInit: function(){
		aau.homeSpots.init();
		aau.fatFooter.init();
	},
	homeSpots: {
		init: function(){
			aau.homeSpots.links = $("#homeBody li a");
			aau.homeSpots.count = aau.homeSpots.links.size();
			aau.homeSpots.links.bind("click", aau.homeSpots.change);
			aau.homeSpots.change(null,1);
			aau.homeSpots.autoChangeInterval = setInterval('aau.homeSpots.change(null,"next")',10000);
			
			$(".homeSpot .videoLink").parents().filter(".homeSpot").each(function(i,d){tb_init($("a",d).attr("href",$("a",d).attr("href")+"?width=725&height=415"))});
		},
		change: function(evt,spotNum){
			aau.homeSpots.prev = aau.homeSpots.curr;
			if (spotNum == "next" || spotNum == "prev") {
				aau.homeSpots.curr += spotNum == "next" ? 1 : -1;
			} else if(spotNum >= 0){
				aau.homeSpots.curr = spotNum;
			} else {
				evt.preventDefault();
				clearInterval(aau.homeSpots.autoChangeInterval);
				aau.homeSpots.curr = evt.target.innerHTML;
				if(aau.homeSpots.curr == aau.homeSpots.prev){
					return;
				}
			}
			
			if(aau.homeSpots.curr > aau.homeSpots.count){
				aau.homeSpots.curr -= aau.homeSpots.count;
			} else if (aau.homeSpots.curr < 1){
				aau.homeSpots.curr += aau.homeSpots.count;
			}
			if(aau.homeSpots.prev >= 0){
				$("#spot" + aau.homeSpots.prev).fadeOut("slow");
			}
			$("#spot" + aau.homeSpots.curr).fadeIn("slow");
			$("#homeBody li a.current").removeClass("current");
			$("#spotLink" + aau.homeSpots.curr).addClass("current");
		}
	},
	
	fatFooter: {
		init: function(){
			var nav = $(".fatFooter>a");
			nav.bind("click",aau.fatFooter.move);
			
			aau.fatFooter.items = $(".fatFooter li");
			while($(".fatFooter li").size() < 5){
				aau.fatFooter.items.clone().appendTo(".fatFooter ul");
			}
			aau.fatFooter.items = $(".fatFooter li");
			aau.fatFooter.itemWidth = $(aau.fatFooter.items.get(0)).width()+10;
			aau.fatFooter.totalWidth = aau.fatFooter.items.size() * aau.fatFooter.itemWidth;
			aau.fatFooter.maxL = aau.fatFooter.totalWidth - aau.fatFooter.itemWidth * 2;
			
			aau.fatFooter.itemsAnimating = [];
		
			aau.fatFooter.items.each(function(i,li){
				aau.fatFooter.itemsAnimating[i] = 0;
				var newL = i*(aau.fatFooter.itemWidth);
				if (newL > aau.fatFooter.maxL){
					newL -=aau.fatFooter.totalWidth;
				}
				$(li).css({"position": "absolute", "left":newL+"px" });
				var imgWidth = $("img",$(li)).width();
				$("div",$(li)).css({left: imgWidth+"px",bottom: 0, "position":"absolute"});
			});	
			
		},
		
		move: function(evt){
			evt.preventDefault();
			if(jQuery.inArray(1,aau.fatFooter.itemsAnimating) >= 0){
				return;
			}
			var dir = $(evt.currentTarget).attr("class");
			aau.fatFooter.items.each(function(i,li){
				li = $(li);
				var diff = dir == "next" ? -aau.fatFooter.itemWidth : aau.fatFooter.itemWidth
				var currentL = parseInt(li.css("left"));
				var newL = currentL+diff;
				if(newL < -(aau.fatFooter.itemWidth + 20)){
					li.css({"left":(newL+aau.fatFooter.totalWidth)+"px"});
				} else if (newL > aau.fatFooter.maxL){
					li.css({"left":(newL-aau.fatFooter.totalWidth)+"px"});
				} else {
					var itm = i
					aau.fatFooter.itemsAnimating[itm]=1
					li.animate({left: newL+"px"},"slow","swing",function(){aau.fatFooter.itemsAnimating[itm]=0;});
				}
			});
		}
	}
}
$(document).ready(aau.init);