/*
Project: 		Navarro Site Content Element Controls
Author: 		Ni�0 Morales
Date Created: 	September 11, 2010
*/
$(document).ready(function() {
	var toplineHover = false;
	var contentResize = function(){
		if($(window).width() < 1000){
			$("#wrapper, #top, #header, .bottomLine, .bottomLineHome, .bottom, .topLine, #footer").addClass("customWidth");
		}else{
			$("#wrapper, #top, #header, .bottomLine, .bottomLineHome, .bottom, .topLine, #footer").removeClass("customWidth");
		}	
	}
	contentResize();
	$(window).resize(
		function(){
			contentResize();
		}
	);
	$("#slideShow").dynamicRotator({width:1000, height:325, interval:5000});
	$('#menu li ul li').hover(
		function() { 
			toplineHover = false;
			var pos = $(this).offset();
			var parentW = $(this).innerWidth();
			var menuW = $('ul:first', this).innerWidth();
			cWidth = $(window).width();
			var totalW =  menuW + pos.left + parentW;
			var mLeft = parentW; 
			
			if(totalW > cWidth){
				mLeft = (menuW * -1) + 8;
			}
			$('ul:first', this).css("margin-left", mLeft);	
			$('ul:first', this).css("margin-top","-10px");
			$("ul:first",this).stop(true, true).fadeIn(500);
		},
		function() { 
			$("ul:first",this).stop(true, true).fadeOut(500);
		}
	);
	$(".galleryContainer .galleryInner .next").click(
		function(){
			var _slider = $(".galleryContainer .galleryInner #slider");
			var s_width = _slider.outerWidth();
			_slider.animate({"marginLeft": "-=673px"}, 400);
			_slider.animate({"marginLeft": "+=100px"}, 500);
		}
	);
	$(".galleryContainer .galleryInner .prev").click(
		function(){
			var _slider = $(".galleryContainer .galleryInner #slider");
			var s_width = _slider.outerWidth();
			_slider.animate({"marginLeft": "+=673px"}, 400);
			_slider.animate({"marginLeft": "-=100px"}, 500)
		}
	);
	$("#post ul.accordion li").has("ul").click(
		function(){
			if($("ul:first", this).is(":visible")){
				$("ul:first", this).slideUp("fast")
				$(this).removeClass("parent");
			}else{
				$("ul:first", this).slideDown("fast")
				$(this).addClass("parent");
			}
			if($(this).parent().hasClass("faq")){
				$(this).siblings().each(
					function(index,item){
						if($("ul:first", this).is(":visible"))	{
							$("ul:first", this).slideUp("fast")
							$(this).removeClass("parent");	
						}
					}
				)
			}
		}
	)
	$(".topLine").mouseover(
		function(){
			toplineHover = true;
		}
	)
	$("#menu").children("li").each(
		function(index,item){
			$(item).hover(
				function(){
					toplineHover = true;
					var pos = $(this).offset();
					$("ul:first",this).css({"left":pos.left + 10})
					$("ul:first",this).stop(true, true).fadeIn(500);
					$(this).siblings().each(
						function(index,item){
							if($("ul:first",$(item)).is(":visible")){
								$("ul:first",$(item)).fadeOut(500);
							}
						}
					);
				},
				function(){
					if(toplineHover == false){
						$("ul:first",this).fadeOut(500);
					}
					toplineHover = false;
				}
			)
		}
	)
	$(".clickToCall").mouseover(
		function(){
			$("#menu").children().each(
				function(index,item){
					if($("ul:first",$(item)).is(":visible")){
						$("ul:first",$(item)).fadeOut(500);
					}
				}
			);	
		}
	);
	
	$(".inputlbl").click(
		function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");	
			}
		}
	);
	$(".inputlbl").blur(
		function(){
			if($(this).val() == ""){
				$(this).val($(this).attr("title"));	
			}
		}					
	)
	
	$("#frm_form_3_container .frm_checkbox:first").html('<input type="hidden" name="item_meta[19][]" id="hidReceivedNewsletter" value="Yes" /><span id="receivedNewsletter">Receive our Newsletter</span>')
	
	$("span#receivedNewsletter").click(
		function(){
			if($("#hidReceivedNewsletter").val() == "Yes"){
				$(this).css("background-position", "-79px -126px");	
				$("#hidReceivedNewsletter").val("No");
			}else{
				$(this).css("background-position", "-67px -138px");	
				$("#hidReceivedNewsletter").val("Yes");
			}
		}
	);
	$("form").submit(
		function(){
			var canSubmit = true;
			$("input, textarea", this).each(
				function(index,item){
					if($(this).hasClass("req")){
						if(($(this).val() == "") || ($(this).val() == $(this).attr("title"))){
							if($(this).attr("id") != "field_phone-number"){
								alert("Please fill up [" +  $(this).attr("title") + "] field");	
								$(this).val("");
								$(this).focus();
								canSubmit = false;
								return false;
							}
						}	
					}
					if($(item).attr("title") == "Email Address" || $(item).attr("title") == "Email"){
						if(!echeck($(item).val())){
							$(item).val("");
							$(item).focus();
							canSubmit = false;
							return false;	
						}
					}
				}
			);
			return canSubmit;
		}
	);
	var echeck = function(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid E-mail ID")
			return false
		}
		if (str.indexOf(" ")!=-1){
			alert("Invalid E-mail ID")
			return false
		}
		return true					
	}
	$("#hidReceivedNewsletter").val("Yes");
	$("div.home ul#menu li#menu-item-136").addClass("current_page_parent");
});

