var FloorPlanMapper = Class.extend({
	plans: null,
	currentPlan: {name: null, element: null},
	currentDiagram: {name: null, element: null},
	genericPlan: null,
	penthousePlan: null,
	lobbyPlan: null,
	floorPlanImages: null,
	pdfLink: '/pdf/riverside_tour/floorplans/',

	init: function() {
		this.genericPlan = $('#generic-floorplan');
		this.viewsPlan = $('#views-floorplan');
		this.penthousePlan = $('#penthouse-floorplan');
		this.plans = $('div.diagram');
		this.floorPlanImages = $('#floorplan-images');
		this.highlight();
		this.bindMapAreas();
		this.bindViewPdf();
	},

	buildingContext: function(map) {
		this.floorPlanImages.hide();
		this.plans.hide();
		$('.floorplan').hide();

		var mapper = this;
		this.currentDiagram.element = map;
		this.currentDiagram.name = map.className;

		setTimeout(function(){
			switch (mapper.currentDiagram.name)
			{
				case 'penthouse':
					plan = mapper.penthousePlan;
					break;

				case 'views':
					plan = mapper.viewsPlan;
					break;

				default:
					plan = mapper.genericPlan;
			}

			plan.show();
			plan.effect("bounce", { times: 2, distance: 15 }, 300);

			// cannot highlight at the top b/c IE7 breaks down
			$(plan).children('img').maphilight({fade: true, stroke:false,fillColor:'ff0000',fillOpacity:0.2});
		}, 500);
	},

	genericContext: function(map) {
		this.currentPlan.name = map.getAttribute('plan');
		this.currentPlan.element = map;
		this.plans.hide();
		this.floorPlanImages.show();
		$('#plan_' + this.currentPlan.name).fadeIn(3000);
	},

	bindMapAreas: function() {
		var mapper = this;

		$('map[name=building] > area').click(function(e){
			e.preventDefault();
			mapper.buildingContext(this);
		});

		$('map[name=generic-floorplan-map] > area').click(function(e){
			e.preventDefault();
			mapper.genericContext(this);
		});
	},

	bindViewPdf: function() {
		var that = this;
		$('#view-pdf').click(function(){
			that.viewPdf(that.currentPlan.name);
			return false;
		});
	},

	viewPdf: function(name) {
		var link;

		if (typeof name != 'undefined')
			link = name;
		else
			link = this.currentPlan.name;

		this.openWindow(this.pdfLink + link + '.pdf');
	},

	openWindow: function(link) {
		window.open(link);
	},

	highlight: function() {
		$('#building').maphilight({fade: true, stroke:false,fillColor:'ff0000',fillOpacity:0.2});
	}
});
