var ddInterval = 200;
var currentDd;
var wh;
var xmlquery;

jQuery.preloadImages = function() {
	for(var i = 0; i<arguments[0].length; i++) {
		jQuery("<img>").attr("src", arguments[0][i]);
	}
}

if (preloadAr.length>0) $.preloadImages(preloadAr);


$(document).ready(function(){
	//$(".nav-el-over").show();
	$(".nav-el").hover(
		function(e) {
			
			var sec = (this.id=="nav-el-map") ? "map" : (this.id=="nav-el-n") ? "n" : "a";
			$(".nav-el-over",this).show();			
			
			if (sec=="map") return; // end here if its map
			
			if (currentDd!=null && currentDd!=sec) {
				sobCloseDropDown(currentDd);
			}
			
			clearTimeout(this.ddOutTimeout);	
			clearTimeout(this.ddInTimeout);
			this.ddInTimeout = setTimeout(function(){
				sobOpenDropDown(sec);
			},ddInterval);
			
		//	$(".nav-el-extra",this).hide();
			
			currentDd = sec;
		},
		function(e) {
			
			var sec = (this.id=="nav-el-map") ? "map" : (this.id=="nav-el-n") ? "n" : "a";
			if (sec=="map") {
				$(".nav-el-over",this).hide();
				return;
			}
			if (this.ddIsOn) return;
			
			clearTimeout(this.ddInTimeout);
			clearTimeout(this.ddOutTimeout);
			this.ddOutTimeout = setTimeout(function(){
				sobCloseDropDown(sec);
			},ddInterval/2);
			
			currentDd = null;
		}
	);
	
	$("#g-ds-bottom").hover(function(){
		if (currentDd==null) return;
		el = $("#nav-el-"+currentDd)[0];
		clearTimeout(el.ddInTimeout);
		clearTimeout(el.ddOutTimeout);
	},null);
	
	$(".navdd-phase2").hover(
		function() {
			this.orgtext = $(this).html();
			$(this).html("Coming Soon");
		},
		function() {
			$(this).html(this.orgtext);
		}
	)
	
	var headerheight = $("#header").height();
    wh = $(window).height()-headerheight+20;
	if ( $("#content").height() < wh ) $("#content").height(wh);
	
	$(window).resize(function(){
		wh = $(window).height()-headerheight;
		if ( $("#content").height() < wh ) $("#content").height(wh);
	})
	
	$(".faces-item").hover(
		function(){
			$(".faces-item-info",this).fadeOut(200);
		},
		function(){
			$(".faces-item-info",this).fadeIn(600);
		}
	)
	
	$("#navdd-a a").hover(
		function() {
			this.orgsrc = $('img',this)[0].src;
			var newsrc = this.orgsrc.replace("_2","_1");
			$('img',this)[0].src = newsrc;
		},
		function() {
			$('img',this)[0].src = this.orgsrc;
		}
	)
	//maps setup
	$("#nav-el-map").click(function(){
		launchMap();
	});
	
	$("#nsav-search").keydown(function(e){
		if (e.keyCode==13) {
			var searchValue = this.value;
			
			window.location.href="/search/"+ encodeURI(searchValue);
		}
	})
	
	// contact forms
	
	$("#contact-form").submit(function() {
		$("#contact-form-msg").hide();
		
		var valid = isValidEmail( $("input[name=email]").val() );
		if ($("input[name=name]").val() == "") valid = false;
		if ($("input[name=comments]").val() == "") valid = false;
		
		if (!valid) {
			$("#contact-form-msg").css({borderColor:"red"}).text("All fields are required.").slideDown(200);
		}else{
		//	$("#contact-form-msg").css({borderColor:"green"}).text("Thanks for your comment!").slideDown(200);
			var str = $(this).serialize();
			
			$.ajax({
			   type: "POST",
			   url: "/mail.php",
			   data: str,
			   success: function(msg){
			     $("#contact-form-msg").css({borderColor:"green"}).text(msg).slideDown(200);
			   }
			 });
			
		}
		
		return false;
    });

	$(".submitform").submit(function() {
		var valid = true;
		var invalidMsg = "Please fill out the required fields correctly.";
		$("input,select",this).each(function(i,el){
			if ($(el).hasClass("required")) {
				if ($(el).val()=="") {
					valid = false;
					$(el).addClass("redborder");
				}else{
					$(el).removeClass("redborder");
				}
			}
			
			if ($(el).attr("name").indexOf("email")!=-1) {
				var emaileval = isValidEmail( $(el).val() );
				if (!emaileval) {
					valid = false;
					$(el).addClass("redborder");
				}else{
					$(el).removeClass("redborder");
				}
			} 
		})
		
		$.scrollTo(0,300);
		
		if (!valid) {
			$(".form-msg",this).css({borderColor:"red"}).text(invalidMsg).slideDown(200);
		}else{
			var str = $(this).serialize();
			
			
			$.ajax({
			   type: "POST",
			   url: "/submitform.php",
			   data: str,
			   success: function(msg){
					if (msg.match(/success/)!=null) {
					    $(".contact-form-wrapper").text(msg);
						$(".contact-form-wrapper").css({height:wh-200+"px"});
					}else{
						alert(msg);
					}
			   }
			 });
			
		}
		return false;
    });


	// datepicker for events
	if ($("#events-datepicker")[0]!=null) {
		$("#events-datepicker").datepicker();
		insertEvents();
	}
	
	var facesPage = 0;
	if ($(".faces")[0]!=null) {
		$("#faces-arrow-l").click(function(){
			if (facesPage>0) facesPage--;
			else return;
			$(".faces").eq(0).scrollTo($(".faces-page").eq(facesPage),400);
		})
		$("#faces-arrow-r").click(function(){
			if ($(".faces-page").get(facesPage+1)!=null) facesPage++;
			else return;
			$(".faces").eq(0).scrollTo($(".faces-page").eq(facesPage),400);
		})
	}
	
	var galleryPage = 0;
	if ($(".item-gallery")[0]!=null) {
		$itemGallery = $(".item-gallery").eq(0);
		$itemGallery.scrollTo(0,200);
		var count = $("img",$itemGallery).length; 
		
		$("#gallery-arrow-l").click(function(){
			if (galleryPage>0) galleryPage--;
			else galleryPage=count-1;
			$itemGallery.scrollTo($("img",$itemGallery).eq(galleryPage),200);
		})
		$("#gallery-arrow-r").click(function(){
			if ($("img",$itemGallery).get(galleryPage+1)!=null) galleryPage++;
			else galleryPage=0;
			$itemGallery.scrollTo($("img",$itemGallery).eq(galleryPage),200);
		})
	}	

})

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function insertEvents() {
	
}


function navSearch() {
	var searchValue = $("#nav-search")[0].value;
	window.location.href="/search/"+ encodeURI(searchValue);
	return false;
}


var mapsOn = false;
function launchMap() {
	$.scrollTo(85,500);
	
//	$("#sob-gmaps-wrapper").append("<div class='sob-gmaps'></div>");
	var postId = (arguments[0]!=null) ? arguments[0] : null;
	mapsOn = true;
	$("#sob-gmaps-wrapper").show();
	var newxPos = $("#wrapper").offset().left+106;
	$("#sob-gmaps-wrapper").css({left:newxPos+"px"});
	$("#dimmer").css({height:$("#content").height()+"px"})
	$("#dimmer").fadeIn(400);
	$("#dimmer").click(closeMap);
	setTimeout(function(){
		gmapload(postId);
	},500);
}

function closeMap() {
	mapsOn = false;
	//$(".sob-gmaps").eq(0).remove();
	$("#sob-gmaps-wrapper").hide();
	$("#dimmer").fadeOut(200);
}

function sobOpenDropDown(sec) {
	var el = $("#nav-el-"+sec)[0];
	$("#navdd-"+sec).show();
	
	var pos = $("#wrapper").offset();
	if (sec=="a") { pos.left +=106};
	if (sec=="n") { pos.left +=10};
	$("#navdd-"+sec).css({top:(pos.top+204)+"px",left:pos.left+"px"});
	
	$("#dimmer").css({height:$("#content").height()+"px"})
	$("#dimmer").fadeIn(400);
	$("#navdd-"+sec).hover(
		function(){
			el.ddIsOn = true;
		},
		function(){
			el.ddIsOn = false;
			$(el).mouseout();
		}
	)
	
//	$(".nav-el-extra",el).hide();
}

function sobCloseDropDown(sec) {
	var el = $("#nav-el-"+sec)[0];
	if (el.ddIsOn) return;
	
	$("#navdd-"+sec).hide();
	//$("#nav-el-"+sec+" .nav-el-over").hide();
	if (currentDd==null && !mapsOn) $("#dimmer").fadeOut(200);

//	$(".nav-el-extra",el).show();
}