(function($) {
	var dcm = {};
	
	/*GRP* Util Functions *GRP*/
	
	dcm.loadScripts = function(scripts, callback) {
		var loadScript = function() {
			var s = scripts.shift();
			if (s) $.getScript(s.substr(0, 7) == "http://" ? s : "assets/templates/equityestates/behaviour/" + s, loadScript);
			else callback();
		};
		loadScript();
	};
	
	/*END* Util Functions *END*/
	
	/*GRP* Rotating Banners *GRP*/
	
	dcm.mainBannerRotator = function() {
		var banners = $(".mainBanner li"), current = 0, swap;
		
		swap = function() {
			var next = current == banners.length - 1 ? 0 : current + 1;
			banners.eq(current).fadeOut(1380);
			banners.eq(next).fadeIn(1380);
			current = next;
		};
		
		banners.each(function(i, b) { $(b).fadeOut(1); });
		banners.eq(0).fadeIn(1);
		
		setInterval(swap, 5800);
	};
	
	/*END* Rotating Banners *END*/
	
	/*GRP* Residence Page Tabs *GRP*/
	
	dcm.residenceTabs = function() {
		var tabs = $(".tabs li");
		$(".tabs").tabs(".tabContent .tabContent", {
			effect: "default",
			onClick: function(t, i) {
				tabs.each(function(t) { $(this)[t == i ? "addClass" : "removeClass"]("active"); });
			}
		})
	};
	
	/*END* Residence Page Tabs *END*/
	
	/*GRP* Residence Page Photos and Floor Plans *GRP*/
	
	dcm.photosFloorPlans = function() {
		var photos = $(".photoGallery a"), floorPlans = $(".floorPlans a");
		
		var options = {
			overlayOpacity: .92,
			containerResizeSpeed: 230,
			imageLoading: 'assets/templates/equityestates/style/lightbox/images/lightbox-ico-loading.gif',
			imageBtnPrev: 'assets/templates/equityestates/style/lightbox/images/lightbox-btn-prev.gif',
			imageBtnNext: 'assets/templates/equityestates/style/lightbox/images/lightbox-btn-next.gif',
			imageBtnClose: 'assets/templates/equityestates/style/lightbox/images/lightbox-btn-close.gif',
			imageBlank: 'assets/templates/equityestates/style/lightbox/images/lightbox-blank.gif'
		};
		
		$(".photoGallery a").lightBox(options);
		if (floorPlans.length) {
			$(".floorPlans a").lightBox(options);
		}
		else {
			$(".floorPlans").remove();
		}
	};
	
	/*END* Residence/Development Page Photos and Floor Plans *END*/


	/*GRP* Destinations Map *GRP*/
	
	dcm.destinationsMap = function() {
		var 
			markers = $(".overlays > ul > li > a"), 
			listItems = $(".residencesLists li a"), 
			map = {
				zoom: 2,
				center: new google.maps.LatLng(20.93495, -8.28125),
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				disableDefaultUI: true
			};

		map = new google.maps.Map($(".destinationsMap")[0], map);

		// setInterval(function() {
		// 	var center = map.getCenter();
		// 	console.log("%n, %n", center.lat(), center.lng());
		// }, 4600);
		
		markers.each(function(marker) {
			var tooltip, dest, lis, lat, lng, mapMarker, infoWindow;

			marker = $(this);
			tooltip = marker.next();

			dest = marker.attr("href").split("#").pop();
			lat = marker.attr("data-lat");
			lng = marker.attr("data-lng");
			
			mapMarker = new google.maps.Marker({
				position: new google.maps.LatLng(lat, lng), 
				map: map, 
				title: "Destination " + dest,
				animation: google.maps.Animation.DROP,
				icon: $("img", marker).attr("src")
			});
			infoWindow = new google.maps.InfoWindow({
				content: tooltip.html()
			});

			google.maps.event.addListener(mapMarker, "click", function() {
				console.log("WHAT THE FUCK");
				infoWindow.open(map, mapMarker);
			});

			marker.attr("title", tooltip.html());
			marker.data("mapMarker", mapMarker);
			marker.data("infoWindow", infoWindow);
			
			lis = listItems.filter("a[rel=dcm-dest-" + dest + "]");
			marker.data("listItems", lis);
			lis.each(function(i, li) { $(li).data("marker", marker) });
		});
		
		$(".overlays > ul > li a[title]").tooltip({
			effect: "slide",
			opacity: .92,
			position: "top center",
			offset: [-4, 0],
			delay: 92,
			onBeforeShow: function() {
				this.getTrigger().data("listItems").each(function(i, li) { $(li).parent().addClass("hover"); });
			},
			onBeforeHide: function() {
				this.getTrigger().data("listItems").each(function(i, li) { $(li).parent().removeClass("hover"); });
			}
		});
		
		listItems.hover(
			function() { $(this).data("marker").data("tooltip").show(); },
			function() { $(this).data("marker").data("tooltip").hide(); }
		);
		
		$("a[href^=#]").live("click", function(e) { e.preventDefault(); });
	};
	
	/*END* Destinations Map *END*/

	
	/*GRP* INITIALIZE *GRP*/
	
	$(function() {
		if ($(".photoGallery").length) {
			dcm.photosFloorPlans();
		}
		if ($(".tabs").length) {
			dcm.loadScripts(["jquery.tools.min.js"], dcm.residenceTabs);
		}
		if ($(".destinationsMap").length) {
			dcm.loadScripts(["jquery.tools.min.js"], dcm.destinationsMap);
		}
		if ($(".mainBanner").length) {
			dcm.mainBannerRotator();
		}
	});
	
	/*END* INITIALIZE *END*/
	
})(jQuery);
