// Background Image PreLoading
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }

	// preload images	
	$.preLoadImages(
		"/app/style/images/bg-communities.jpg", 
		"/app/style/images/bg-contact-us.jpg", 
		"/app/style/images/bg-careers-mockup.jpg", 
		"/app/style/images/bg-employee-login.jpg", 
		"/app/style/images/bg-resident-resources.jpg", 
		"/app/style/images/bg-about-us.jpg", 
		"/app/style/images/bg-home.jpg"
	);
})(jQuery)


jQuery(document).ready(function($) {
	$(".hidden").hide().removeClass("hidden");
	$(".datepicker").datepicker();
	// Mobile Menu
	$("#mobile-menu").change(function() {
		window.location = $(this).val();																	
	});
				
				
	/* Show/Hide links - <a href="#element_id_to_toggle_visibility">Toggle</a> */
	$(".toggle-element-visibility").bind("click", function(event) {
		$element = $($(this).attr("href"));
		
		if($element.is(":visible")) {
			$element.slideUp("fast", function() {
				$element.trigger("hide");
			});
		} else {
			$element.slideDown("fast", function() {
				$element.trigger("show");
				
			});
		}
		$element.removeClass('hidden');
		
		event.preventDefault();
	});
	
	// Meet Our People
	if($("#meet-our-people").exists()) {
		var $overlay = $("#overlay");
		$(".meet-person .caption").css("opacity", "0.9");
		
		$(".meet-person .thumb-person").click(function() {
			var $thumbnail = $(this);
			var $person = $thumbnail.parent();
			var $openPerson = $(".full-person:visible");
			
			$overlay.show();
			$openPerson.fadeOut();
			$person.css("z-index", "60").siblings().css("z-index", "50");
			$(".meet-person").find(".thumb-person, .caption").fadeTo("slow", 0.5);			
			$person.find(".full-person").fadeIn();
			
			// Replace Bio and Job Title
			$name = $person.find(".name").text();
			$job = $person.find(".job").text();
			$longJob = $person.find(".long-job").text();
			$title = ($longJob != "") ? $longJob : $job;
			$("#current-person-title").html("<span style='font-size: 20px'>" + $name + "</span> | " + $title);
			if(!$("#current-person-bio").data("default-text")) $("#current-person-bio").data("default-text", $("#current-person-bio").html());
			$("#current-person-bio").html($person.find(".bio").html());
		});
		
		$overlay.click(function() {
			$(".meet-person").find(".thumb-person").fadeTo("fast", 1);				
			$(".meet-person").find(".caption").fadeTo("slow", 0.9);

			$(".meet-person").find(".full-person").hide();
			$("#current-person-title").html("");
			$("#current-person-bio").html($("#current-person-bio").data("default-text"));
			$overlay.hide();
		});
	}

	if($(".first_focus").exists()) $(".first_focus:first").focus();
	if($(".confirm").exists()) {
		$(".confirm").click(function() {
			var text = "Are you sure?";
			if($(this).data("confirm").length > 0) {
				text = $(this).data("confirm");
			}
			return confirm(text);
		});
	}
	
	// Community Accordian
	$(".region-community-list h3").click(function() {
		$region = $(this).parent();
		$communities = $region.find("ul");
		if(!$region.hasClass("selected")) {
			$(".region-community-list h3").removeClass("down-arrow");
			$(this).addClass("down-arrow");
			$currentlySelected = $("li.selected");
			$currentlySelected.find("ul").slideUp();
			$currentlySelected.removeClass('selected');
			
			$communities.slideDown("fast");
			$region.addClass("selected");
		}
	});
	
	if($("form#contact").exists()) {
		$("form#contact").submit(function() {
			if($(this).find("#contact_antispam").val() != "12") {
				alert("Please fill out the anti-spam challenge question correctly. This helps prevent spammers from using our contact form.");
				return false;
			} else {
				return true;
			}
		});
	}
	
	// Communitry Sort Pics
	if($("#photos").exists()) {
		$("#photos").sortable({
			items : '.photo',
			'stop' : function() {
				$.get("/portal/communities/ajax/update-photo-sort/?" + $(this).sortable("serialize"));
				console.log($(this).sortable("serialize"));
			}
		});
	}
	
	// Community Gallery
	if($("#community-thumbs").exists()) {
		var gallery = $('#community-thumbs').galleriffic({
				delay:                     3000, // in milliseconds
				numThumbs:                 8, // The number of thumbnails to show page
				preloadAhead:              12, // Set to -1 to preload all images
				enableTopPager:            false,
				enableBottomPager:         true,
				maxPagesToShow:            4,  // The maximum number of pages to display in either the top or bottom pager
				imageContainerSel:         '#community-slideshow', // The CSS selector for the element within which the main slideshow image should be rendered
				controlsContainerSel:      '#community-controls', // The CSS selector for the element within which the slideshow controls should be rendered
				captionContainerSel:       '', // The CSS selector for the element within which the captions should be rendered
				loadingContainerSel:       '#community-loading', // The CSS selector for the element within which should be shown when an image is loading
				renderSSControls:          false, // Specifies whether the slideshow's Play and Pause links should be rendered
				renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
				playLinkText:              'Play',
				pauseLinkText:             'Pause',
				prevLinkText:              '&nbsp;',
				nextLinkText:              '&nbsp;',
				nextPageLinkText:          'Next &rsaquo;',
				prevPageLinkText:          '&lsaquo; Prev',
				enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
				enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
				autoStart:                 true, // Specifies whether the slideshow should be playing or paused when the page first loads
				syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
				defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
				onSlideChange:             undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
				onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
				onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
				onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
				onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
				onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
				onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
		});
	}
});



window.log = function(){
  log.history = log.history || [];
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

jQuery.fn.exists = function(){return jQuery(this).length>0;} // Usage: if($(".element").exists()) { /* do something */ }
