$(document).ready(function() {

	/* Use this js doc for all application specific JS */
 /* $(window).resize(function() {
    console.log($(window).width());
  });*/
  
  	$("h1,h2,h3,h4,p,.button,time").textShadow();

	/* TABS --------------------------------- */
	/* Remove if you don't need :) */
	
	var tabs = $('dl.tabs');
		tabsContent = $('ul.tabs-content')
	
	tabs.each(function(i) {
		//Get all tabs
		var tab = $(this).children('dd').children('a');
		tab.click(function(e) {
			
			//Get Location of tab's content
			var contentLocation = $(this).attr("href")
			contentLocation = contentLocation + "Tab";
			
			//Let go if not a hashed one
			if(contentLocation.charAt(0)=="#") {
			
				e.preventDefault();
			
				//Make Tab Active
				tab.removeClass('active');
				$(this).addClass('active');
				
				//Show Tab Content
				$(contentLocation).parent('.tabs-content').children('li').css({"display":"none"});
				$(contentLocation).css({"display":"block"});
				
			} 
		});
	});
	
	
	/* Nav Arrow Slider ------------- */
	/* Slides Arrow under Subnavigation */
	
	var arrow = $('#sub-nav .arrow'),
		hide  = false,
		buttons = $('#sub-nav a'),
		selected = $('#sub-nav .current_page_item');
		
	
	if(selected.length)
	{
		var offset = selected.position(),
			position = offset.left + (selected.outerWidth()/2 - 3);
	
		// Set the initial position
		arrow.css('left', position);
		
		// animate on button hovers
		buttons.hover(
			// On Hover On
			function(){
			
				arrow.css('display', 'block');
				
				var current = $(this);
					offset = current.position();
				var slideTo = offset.left + (current.outerWidth()/2 - 3);
				
				arrow.stop().animate({left: slideTo}, 500);
			}, 
			// On Hover Off
			function(){
				
				arrow.stop().animate({left: position}, 500, function() {
					/*if( ! hide)
					{
						arrow.css('display', 'none');
					}*/
				});
			}
		);
	} // end check For selected button
	else
	{
		arrow.remove();
		//arrow.css('display', 'none');
		//hide = true;
	}
	
	/* COLORBOX -------------- */
	/* Modal Window for gallery */
	if($('.ngg-galleryoverview').length)
	{
		$('a.colorbox').colorbox({rel:'gal'});
	}
	
	/* FORMS ------------- */
	/*  */
	
	var processurl = '/wp-content/themes/dcdc/helpers/process.php';
	
	if($('#contact_form').length)
	{
		var contact_form = $("#contact_form");
		
		contact_form.validate({
	 		errorElement: "div",
	 		errorClass: "alert-box error",
	 		rules: {
	    		name: "required",
	    		email: {
	    			required: true,
	    			email: true
	    		},
	    		cmessage: "required"
	    	},
	    	messages: {
	    		"name": "Please enter your name.",
				"email": "Please enter your email.",
				"cmessage": "Please enter a message."
	    	},
	 		submitHandler: function(form) {
	   			
	   			$.ajax({
					type: "POST",
					cache: false,
					url: processurl,
					data: contact_form.serialize(),
					datatype: "json",
					success: function (data) {
						var obj = jQuery.parseJSON(data);
					
						if(obj.emailed == true)
						{
							contact_form.fadeOut('normal', function(){
								contact_form.parent().append('<div class="alert-box success">Thank you for contacting DCDC. We will be in touch with as soon as possible.</div>').fadeIn('normal');
							});
						}
					}//success function
				});//ajax
				
			} // End Submit Handler
		});
	}
	
	if($('#tour_form').length)
	{
		var tour_form = $("#tour_form");
		
		tour_form.validate({
	 		errorElement: "div",
	 		errorClass: "alert-box error",
	 		rules: {
	    		first_name: "required",
	    		last_name: "required",
	    		email: {
	    			required: true,
	    			email: true
	    		},
	    		confirm_email: {
	    			required: true,
	    			email: true,
	    			equalTo: "#email"
	    		},
	    		phone: {
	    			required: true,
	    			phoneUS: true
	    		},
	    	},
	    	messages: {
	    		"first_name": "Please enter your First Name.",
	    		"last_name": "Please enter your Last Name.",
				"email": "Please enter your Email.",
				"confirm_email": "Your Email does not match",
				"phone": "Please enter a valid phone number."
	    	},
	 		submitHandler: function(form) {
	   			
	   			$.ajax({
					type: "POST",
					cache: false,
					url: processurl,
					data: tour_form.serialize(),
					datatype: "json",
					success: function (data) {
						var obj = jQuery.parseJSON(data);
					
						if(obj.emailed == true)
						{
							tour_form.fadeOut('normal', function(){
								tour_form.parent().append('<div class="alert-box success">Thank you for contacting DCDC. We will be in touch with as soon as possible to accommodate your request.</div>').fadeIn('normal');
							});
						}
					}//success function
				});//ajax
			}
		});
	}
	
	
	/* Google Maps ------------- */
	/* Initializes google maps */
	
	if($('#google').length)
	{
		gmap();
		
		var t;
		
		$('#google').hover(function() {
				// Hover on
				var map = $(this);
			
				// Set a hover intent before animating.
				t = setTimeout(function() {
				
					map.animate({
						height: 400
					}, 500);
					
				}, 500)
			
			}, function() {
				// Hover Off
				var map = $(this);
			
				clearTimeout(t);
			
				map.animate({
					height: 190
				}, 500);
			}
		);
	}
	
	
});


var map;
var markers = [];
var windows = [];

function gmap() {
	
	var addresses = [new google.maps.LatLng(40.954363,-76.600714),new google.maps.LatLng(40.969335,-76.55797)];
	var	center = new google.maps.LatLng(40.938804,-76.564236);
	var	mapOptions = {
			zoom: 12,
			center: center,
			disableDefaultUI: true,
			panControl: false,
			zoomControl: false,
			scaleControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		
	map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
		
	for(i in addresses) {
		addMarker(addresses[i]);
	}	
	
	windows.push('<div id="content" style="overflow: hidden; min-width: 305px;"><div><h1>DCDC</h1><p style="font-size: 12px;" >398 Wall Street<br /> Danville, PA 17821-1744 <br /><a href="http://g.co/maps/552kj" target="_blank">Get Directions</a></div></div>');
	
	windows.push('<div id="content" style="overflow: hidden; min-width: 305px;"></div><div><h1>DCDC</h1><p style="font-size: 12px;" >2719 Bloom Road<br /> Danville, PA 17821-9141 <br /><a href="http://g.co/maps/kf226" target="_blank">Get Directions</a></div></div>');
	
	showOverlays();
			
}

// Add A Marker
function addMarker(location) {
	marker = new google.maps.Marker({
		position: location,
		map: map
	});
	markers.push(marker);
}

// Shows any Markers currently in the array
function showOverlays() {
  if (markers) {
    for (i in markers) {
      markers[i].setMap(map);
      
      attachWindows(markers[i], windows[i]);
    }
  }
}

// Attach Windows and Add Click Event
function attachWindows(marker, message) {
  var infowindow = new google.maps.InfoWindow(
      { 
      	content: message
      });
      
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
  });
}

