/*
---

script: client/EE.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

var EE = new Class({
	Implements: [Class.Occlude, Options, Log],
	
	element: document,
	property: "EE",
	options: {},
	config: {},
	
	apiUrl: null,
	apiToken: null,
	page: null,
	
	initialize: function(options) {
		var content, toggleSizeButton, path;
		
		if(this.occlude()) {
			return this.occluded;
		}
		
		this.enableLog();
		
		if( ! EE.session) {
			var path = document.location.href.toURI().get("directory").split("/");
			path = path.slice(0, path.indexOf("admin") + 1).join("/");
			
			EE.session = new Hash.Cookie("eec", {path: path, autoSave: true, duration: 3});
			EE.session.set("v", "2.1.5");
		}
		
		this.session = EE.session;
		
		// Options Priority (high to low): child instantiation argument, global config, child/page class definition, EE class definition
		if(this.page && this.config.pageControllers && this.config.pageControllers[this.page]) {
			this.setOptions(this.config.Controllers[this.property.substr("EE.Controllers.".length)], options);
		}
		else {
			this.setOptions(options);
		}
		
		// Main Banner
		if(document._domready && document.id("mainBanner")) {
			new RS.Fader("#mainBanner li", this.options.mainBanner);
		}
		else {
			window.addEvent("domready", function() { 
				new RS.Fader("#mainBanner li", this.options.mainBanner);
			}.bind(this));
		}
	},
	
	siteUrl: function(path) {
		return this.baseUrl + (path || "");
	},
	
	callAPI: function(operation, params, onSuccess, onFailure) {
		if($type(operation) !== "string" || ! this.apiUrl) {
			return false;
		}
		
		params = $type(params) === "object" ? params : {};
		
		return new Request.JSON({
			url: this.apiUrl,
			noCache: true,
			onSuccess: onSuccess,
			onFailure: onFailure,
			data: {
				token: this.apiToken,
				operation: operation,
				params: params
			}
		}).post();
	}
});

EE.Controllers = {};

EE.load = function() {
	var args, controllers, instance = document.retrieve("EE");
	
	if( ! instance || instance.loaded) {
		return null;
	}
	
	controllers = instance.config.pageControllers[instance.page];
	
	if($chk(controllers)) {
		args = arguments;
		
		$splat(controllers).each(function(controller, i) {
			if(EE.Controllers[controller]) {
				new (EE.Controllers[controller])(args[i] || {});
			}
		});
	}
	
	return instance.loaded = true;
};

DatePicker.implement({
	options: { 
		pickerClass: "ee-datePicker",
		dayShort: 2,
		monthShort: 3,
		startDay: 0, // Sunday (0) through Saturday (6) - be aware that this may affect your layout, since the days on the right might have a different margin
		timePicker: false,
		timePickerOnly: false,
		yearPicker: true,
		yearsPerPage: 20,
		format: "n/j/Y",
		allowEmpty: false,
		inputOutputFormat: "Y-m-d", // default to unix timestamp
		animationDuration: 460,
		startView: "year", // allowed values: {time, month, year, decades}
		positionOffset: { x: 0, y: 0 },
		minDate: null, // { date: '[date-string]', format: '[date-string-interpretation-format]' }
		maxDate: null, // same as minDate
		debug: false,
		toggleElements: null
	}
});

new EE();


/*
---

script: client/config.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

EE.implement({config: {
	
	pageControllers: {
		destinations_landing: "DestinationsLanding",
		residence: "Residence",
		inquiries: "Inquiries"
	},
	
	mainBanner: {
		startIndex: 0,
		interval: 5.8,
		fadeDuration: 1.38,
		pauseOnHover: false
	},
	
	Controllers: {
		DestinationsLanding: {
			map: {
				container: "destinationsMap",
				position: {
					lat: 23,
					lng: -57,
					zoom: 2
				},
				markerSize: {
					x: 23,
					y: 23
				},
				markerColors: {
					"current": "#339999",
					"future": "#F4FC54"
				}
			},
			msg: {
				"noMap": "Your browser is not compatible with Google Maps, so you will not be able to see the destinations map."
			}
		},
		
		Residence: {},
		
		Inquiries: {}
	}
	
}});


/*
---

script: client/init.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

var console;
if ( ! console) { console = {log: $empty}; }
else if ( ! console.log) { console.log = $empty; }

window.addEvent("domready", function() {
	EE.load();
});

window.addEvent("load", function() {
	
});


/*
---

script: client/destinations/destinations_landing.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

EE.Controllers.DestinationsLanding = new Class({
	Extends: EE,
	Binds: ["createMapMarker", "addDestination"],
	property: "EE.Controllers.DestinationsLanding",
	options: {
		
	},
	
	mapMarkers: [],
	
	initialize: function(options) {
		this.parent(options);
		
		this.mapContainer = document.id(this.options.map.container) || document.getElement(this.options.map.container);
		
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(this.mapContainer);
			this.map.addControl(new GMapTypeControl());
			this.map.setCenter(new GLatLng(this.options.map.position.lat, this.options.map.position.lng), this.options.map.position.zoom);
			
			$each(this.destinations, this.addDestination);
		}
		else {
			RS.displayMessage(this.options.msg.noMap, "error", this.mapContainer);
		}
	},
	
	addDestination: function(destination) {
		var marker, resdTypes = [], markerColor = "#000000", infoWindowHtml;
		
		if( ! $chk(destination.lat) || ! $chk(destination.lng) || $H(destination.residences).getLength() == 0) {
			return null;
		}
		
		infoWindowHtml = "<h3><a href=\"" + this.siteUrl("destinations/" + destination.slug) + "\">" + destination.title + "</a></h3><h4>Residences:</h4><ul>";
		$each(destination.residences, function(resd) {
			resdTypes.include(resd.status);
			infoWindowHtml += "<li><a href=\"" + this.siteUrl("residences/" + resd.slug) + "\">" + resd.title + "</a></li>";
		}.bind(this));
		infoWindowHtml += "</ul>";
		
		$each(this.options.map.markerColors, function(color, types) {
			types = types.split(".");
			
			if(types.length == resdTypes.length && types.every(function(type) { return resdTypes.contains(type); })) {
				markerColor = color;
			}
		});
		
		marker = this.createMapMarker(destination.lat, destination.lng, this.options.map.markerSize.x, this.options.map.markerSize.y, markerColor);
		
		GEvent.addListener(marker, "click", function() {
			//document.location.href = this.siteUrl("destinations/" + destination.slug);
			marker.openInfoWindowHtml(infoWindowHtml);
		}.bind(this));
		/*
		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(infoWindowHtml);
		});
		GEvent.addListener(marker, "mouseout", function() {
			this.map.closeInfoWindow()
		}.bind(this));
		*/
		
		this.mapMarkers.push(marker);
		this.map.addOverlay(marker);
		
		return marker;
	},
	
	createMapMarker: function(lat, lng, w, h, col) {
		return new GMarker(new GLatLng(lat, lng), {icon: IconFactory.createMarkerIcon({width: w, height: h, primaryColor: col})});
	}
});


/*
---

script: client/destinations/residence.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

EE.Controllers.Residence = new Class({
	Extends: EE,
	Binds: [],
	property: "EE.Controllers.Residence",
	options: {
		
	},
	
	mapMarkers: [],
	
	initialize: function(options) {
		this.parent(options);
		
		new RS.Tabs(document.getElement(".tabContent"));
	}
});


/*
---

script: client/inquiries.js

description: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

requires:
- /SCRIPTNAME

provides: [CLASSNAME]

...
*/

EE.Controllers.Inquiries = new Class({
	Extends: EE,
	Binds: ["showInvestorInfo", "hideInvestorInfo"],
	property: "EE.Controllers.Inquiries",
	options: {
		mask: {
			hideOnClick: true,
			useIframeShim: true,
			"class": "rs-mask"
		}
	},
	
	initialize: function(options) {
		this.parent(options);
		
		this.investorContainer = document.getElement("div.accreditedInvestor").hide().inject(document.body);
		this.investorOpen = document.getElement("a.accreditedInvestor");
		
		this.mask = new Mask(document.body, this.options.mask);
		
		this.investorOpen.addEvent("click", this.showInvestorInfo);
		this.mask.addEvent("hide", this.hideInvestorInfo);
	},
	
	showInvestorInfo: function(e) {
		if(e) {
			e.preventDefault();
		}
		
		this.mask.show();
		this.investorContainer.show();
	},
	
	hideInvestorInfo: function(e) {
		if(e) {
			e.preventDefault();
		}
		
		this.investorContainer.hide();
	}
});

