/* global.js */

var locations = false;
var locs = [];
var scroll = false;

var map;
var coder;
var bounds;
var map_loaded = false;

var photos = [];
var photo = false;
var photo_mask = false;
var photo_current = 0;
var photo_tween;


/* initial actions */
window.addEvent('domready', function() {
	
	// photos
	if (photos && photos.length > 1) {
		photosStart();
	}
	
	// placeholder textfields
	$$('input.placehold').each(function(input, i) {
		if (!input.get('value')) {
			input.set('value', input.get('alt'));
		}
	});
	$$('input.placehold').addEvent('focus', function(e) {
		if (this.get('value') == this.get('alt')) {
			this.set('value', '');
		}
	});
	$$('input.placehold').addEvent('blur', function(e) {
		if (!this.get('value')) {
			this.set('value', this.get('alt'));
		}
	});
	
	// email links
	$$('a.email').addEvent('click', function(e) {
		e.stop();
		noSpam(this);
	});
	
	// map links
	$$('a.map-view').addEvent('click', function(e) {
		e.stop();
		openTab(false, 'map');
		//return GEvent.trigger(locs[this.get('rel')], 'click');
		locs[this.get('rel')].expand();
	});
	
	// stop links
	$$('a.stop').addEvent('click', function(e) {
		e.stop();
	});
	
	// smooth scrolling
	$$('a.smooth').addEvent('click', function(e) {
		e.stop();
		smoothScroll(this);
	});
	$$('a.up').removeEvents('click');
	$$('a.up').addEvent('click', function(e) {
		e.stop();
		scroll = new Fx.Scroll(window).toTop();
	});
	
	// tool tips
	new Tips('.tipped', {
		text: ''
	});
	
	// tabs
	if (tabs = $$('ul.tabs li a')) {
		tabs.addEvent('click', function(e) {
			e.stop();
			openTab(this);
		});
	}
	
});

window.addEvent('load', function() {
	
	// load the map
	loadMap();
	
});

if ($type('GUnload') == 'function') window.addEvent('unload', GUnload);


/* tabs */
function openTab(el, tab) {
	if (!el && tab) {
		el = $$('ul.tabs li a[rel='+tab+']');
	}
	if (el && $$('div.tabbed[rel='+tab+']')) { // tab and tabbed pane exist
		$$('ul.tabs li a.current').removeClass('current');
		$$('div.tabbed').each(function(item, i, array) {
			if (item.get('rel') == this.get('rel')) {
				item.addClass('current');
			} else {
				item.removeClass('current');
			}
		}, el);
		if (el.get('rel') == 'map') {
			loadMap();
		}
		el.addClass('current');
	}
}


/* photos */
function photosStart() {
	if (photo_divs = $$('.photo')) photo = photo_divs[0];
	if (mask_divs = $$('.photo-mask')) photo_mask = mask_divs[0];
	if (photo && photo_mask) {
		photo_tween = new Fx.Tween(photo_mask, {
			property: 'opacity',
			duration: 3000,
			onComplete: function() {
				photo_mask.setStyle('background-image', 'url('+photos[photo_current]+')');
				photo_tween.set(1);
				photosNext.delay(3000);
			}
		});
		photosNext.delay(7000);
	}
}
function photosNext() {
	photo_current = photo_current + 1 == photos.length ? 0 : photo_current + 1;
	var image = new Asset.image(photos[photo_current], {
		onload: function() {
			photo.setStyle('background-image', 'url('+photos[photo_current]+')');
			photo_tween.start(0);
		}
	});
}

/* smooth scroll */
function smoothScroll(a) { // pass `a` element
	var el = $$('a[name='+a.get('href').split('#').join('')+']')[0];
	scroll = new Fx.Scroll(window).toElement(el);
}

/* no spam */
function noSpam(el) {
	addy = el.get('href') != '#' ? el.get('href').split('mailto:').join('') : el.get('html');
	window.location = 'mailto:'+addy.split('[at]').join('@').split(' ').join('');
}

/* map */
function loadMap() {
	if (($('locations-map') || $('neighborhood-map')) && map_loaded == false && GBrowserIsCompatible()) {
		if ($('locations-map')) {
			var id = 'locations-map';
		} else if ($('neighborhood-map')) {
			if (els = $$('div.tabbed[rel=map]')) {
				if (!els[0].isDisplayed()) return false;
			} else {
				return false;
			}
			var id = 'neighborhood-map';
		}
		map = new GMap2(document.getElementById(id));
		map.setMapType(G_NORMAL_MAP); // G_PHYSICAL_MAP
		//map.addControl(new GSmallZoomControl3D());
		map.addControl(new GLargeMapControl3D());
		//map.setCenter(new GLatLng(0,0), 0);
		
		if (locations) { // neighborhoods or homes
			coder = new GClientGeocoder();
			bounds = new GLatLngBounds();
			locations.each(function(item, i, array) {
				new Loc(item);
			});
		}
		
		map_loaded = true;
	}
}
function Loc(obj) {
	this.data = obj;
	this.locate();
}
if ($type('GOverlay') == 'function') Loc.prototype = new GOverlay();
Loc.prototype.locate = function() {
	if (this.data.lat && this.data.lng) {
		this.point = new GLatLng(this.data.lat, this.data.lng);
		this.place();
	} else if (this.data.address) {
		coder.getLatLng(this.data.address, function(point) {
			this.point = point ? point : false;
			this.place();
		}.bind(this));
	}
}
Loc.prototype.place = function() {
	if (this.point) {
		//var marker = new GMarker(point);
		//map.addOverlay(marker);
		map.addOverlay(this);
		bounds.extend(this.point);
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		locs[this.data.id] = this;
	} else {
		(function(){this.locate()}).delay(50, this);
	}
}
Loc.prototype.expand = function() {
	$$('.map-overlay.expanded').each(function(item, i, array) {
		if (loc = item.retrieve('loc', false)) loc.contract();
	});
	this.overlay.removeEvents();
	this.overlay.set('html', this.data.html);
	this.overlay.addClass('expanded');
	this.close = this.overlay.getElement('a.map-overlay-close');
	if (this.close != null) {
		this.close.removeEvents('click');
		this.close.addEvent('click', function(e) {
			e.stop();
			this.contract();
		}.bind(this));
	}
	this.redraw(true);
	
	map.setZoom(18);
	map.panTo(this.point);
	//map.setCenter(this.point, 17);
}
Loc.prototype.contract = function() {
	this.overlay.removeEvents();
	this.overlay.addEvent('click', function(e) {
		e.stop();
		this.expand();
	}.bind(this));
	this.overlay.removeClass('expanded');
	this.overlay.set('html', this.data.title);
	this.redraw(true);
}
Loc.prototype.initialize = function(map) {
	overlay = new Element('div', {
		'class' : 'map-overlay '+this.data.type,
		'html' : this.data.title
	});
	this.overlay = overlay;
	this.overlay.store('loc', this);
	if (this.data.type == 'home') this.contract();
	/*this.elements = $$('.map-view[rel='+this.data.id+']');
	this.elements.addEvent('click', function(e) {
		e.stop();
		openTab(false, 'map');
		this.expand();
	});*/
	map.getPane(G_MAP_MAP_PANE).appendChild(overlay);
	this.map_ = map;
	this.redraw();
	new Asset.image(this.data.thumbnail);
}
Loc.prototype.remove = function() {
	this.overlay.destroy();
}
Loc.prototype.copy = function() {
	return new Loc(this.data);
}
Loc.prototype.redraw = function(force) {
	if (!force) return;
	var pixel = map.fromLatLngToDivPixel(this.point);
	vert = pixel.y;
	this.overlay.setStyle('top', vert + 'px');
	horiz = pixel.x - this.overlay.getSize().x / 2;
	//horiz = pixel.x;
	//horiz = pixel.x - this.overlay.getSize().x;
	this.overlay.setStyle('left', horiz + 'px');
}











