$(function(){

// SILENCE CLICKING 
	$("a.no-click").click(function(e) {
		e.preventDefault();
	});

// #top-nav .equipment SPECIFICS 
	$("#top-nav .equipment ul li").click(function(e) {
		$(this).children("p").slideDown("fast", function() {
			$(this).parent().siblings("li").children("p").slideUp("fast");
		});
		e.preventDefault();
	});

// KEEP ALL ELEMENTS IN THE RESPECTIVE CORNERS
	$(window).resize(function() {
	// jq objects
		$site = $("#site");
		$body = $("#site #body");
		$video_player = $("#video-player");
	// clear whats been done below
		$body.attr("style", "");
		$video_player.attr("style", "");
	// variables
		window_height = $(this).height();
		body_height = $body.height();
		children_height = 0
		$site.children().each(function() {
			children_height += $(this).outerHeight();
		});
	// resize elements
		height_diff = window_height - children_height;
      height_diff = Math.floor(height_diff);
		if( height_diff > 0 ) {
			min_height = body_height + height_diff;
			$body.css({ minHeight: min_height });
         $video_player.css({ top: height_diff/2 });
		}
	});
// on pageload
	$(window).trigger("resize");

// TOOLTIPS 
	$("#top-nav li ul li.thumbs a").each(function() {
		$child = $(this).find("img");
		title = $(this).attr("rel");
		img_src = $child.attr("src");
		$child.tipTip({
			maxWidth:			"216px",
			edgeOffset:			20,
			defaultPosition: "bottom",
			delay: 				0,
			fadeIn:				100,
			fadeOut:				0,
			content: 			"<div class='image'><img src='"+ img_src +"' /></div><div class='title'>"+ title +"</div>"
		});
	})

// SUBMENUS
// Placement + Functionality
	$("#top-nav .item").hover(function() {
		$parent = $(this);
		parent_width = $parent.innerWidth();
		parent_height = $parent.innerHeight();
		$child = $(this).find("ul");
		child_width = $child.innerWidth();
		child_height = $child.innerHeight();
		child_x = $parent.position().left;
		child_y = $parent.position().top;
	
		child_y += parent_height;
		child_x -= child_width - parent_width;
	 
		$child.css({ left: child_x, top: child_y });	
		
	}, function() {
		$child.attr("style", "");
	});
// li.thumbs img DETERMINE .last
	thumb_a_width = $("#top-nav li ul li.thumbs a:eq(0)").innerWidth(); // we just get the first (since it'll have the proper width + margin
	thumb_img_width = $("#top-nav li ul li.thumbs a:eq(0) img").outerWidth();
	right_margin_offset = thumb_a_width - thumb_img_width;
	$("#top-nav li ul li.thumbs").each(function() {
		$container = $(this);
		container_width = $container.width() + right_margin_offset;
		num_per_row = Math.floor( container_width / thumb_a_width );
		$container.find("a").each(function(i) {
			if( (i % num_per_row) + 1 == num_per_row ) {
				$(this).addClass("last");
			}
		});
	});
  
// QUICK CONTACT
// Placement + Functionality
	$("#contact .email a, #contact .phone a").click(function(e) {
		$("#footer .overlay").attr("style", "");
		which_div = $(this).attr("href");
		$frame = $(which_div);
		frame_height = $frame.innerHeight();
		frame_width = $frame.innerWidth();
		border_height = $frame.outerHeight() - $frame.innerHeight();
		$button = $(this);
		frame_x = $button.offset().left;
		frame_y = $button.offset().top;
		button_height = $button.innerHeight();
		frame_y -= frame_height - button_height + (border_height);
		
		$frame.offset({ top: frame_y, left: frame_x });
		e.preventDefault();
		
		$frame.find(".close").click(function(e) {
			$("#footer .overlay").attr("style", "");
			e.preventDefault();
		});
	});
// Sendmail Ajax
	$("#quick-contact form :input").keypress(function() {
		$("#quick-contact .submit").val("Send");
		$("#quick-contact .submit").attr("style", "");
	});
	
	$("#quick-contact form").submit(function(e) {
		
		error = false;
		
		$(this).find(":input").each(function() {
			if( $(this).val() == "" ) {
				error = true;
				return false;
			}
		});
		
		if( !error ) {
		
			$.ajax({
				url:	"/sendmail.php",
				type:	"GET",
				data: {
					to:		$(this).find("input[name=mailto]").val(),
					name:		$(this).find("input[name=name]").val(),
					email:	$(this).find("input[name=email]").val(),
					message:$(this).find("textarea[name=message]").val()
				},
				cache: false,
				success: function(html) {
					alert(html)
				}
			});
		
			
		
		} else {
			
			$(this).find(".submit").val("Required Field");
			$(this).find(".submit").css({ color: "#900" });
			
		}
		
		e.preventDefault();
		return false;
		
	});

  
});

