/**
 * NS Stationsvoorzieningen
 * 
 * @version   1.00.090603
 * @author    LBI Lost Boys
 */
NS(function($){

	function Voorzieningen() {
		this.form = $('#form-voorzieningen');
		this.facilities = $('#form-facility');

		NS.relateLink(/submit-/, this.handleClick.bind(this));
		NS.subscribe('submit', this.handleSubmit.bind(this));

		var map = $("#draggableMap");
		if(map.length){
			this.mapController = new DraggableMapController(map);
		}
	}

	Voorzieningen.prototype = {
		Defaults: {
			validateRequired: true,
			validateServer: false,
			replaceInputs: true,
			simulateClick: false
		},

		owns: function(form) {
			return (this.form[0] == form) || (this.facilities[0] == form);
		},

		prefers: function(setting) {
			return this.Defaults[setting];
		},

		handleClick:function(link, rel) {
			var type = /submit-([^ ]+)/i.exec(rel)[1];

			switch (type) {
				case 'search':
					NS.forms.submit(this.form[0]);
				break;
				case 'facility':
					this.requestFacility();
				break;
			}
			return true;
		},

		handleSubmit:function(e) {
			var form = e.target;
			var facility = this.facilities[0];
			if(form == facility) {
				e.preventDefault();
				this.requestFacility();
			}
		},

		requestFacility:function() {
			NS.XHR.sendForm(this.facilities[0], NS.getProperty('POST_FACILITIES'), this.displayFacility.bind(this));
		},

		displayFacility:function(xml) {
			var content = $(xml).find('voorziening').text();
			var target = $('#facility-response');
			NS.DOM.write(target, content);
		}
	};

	/**
	 * Draggable map
	 */
	function DraggableMapController(map) {
		this.map = map;
		NS.relateLink(/map-/i, this.handleClick.bind(this));
		this.map.bind('mousedown', this.startDrag.bind(this));
		$(document).bind('mouseup', this.endDrag.bind(this));
		$(document).bind('mousemove', this.handleDrag.bind(this));
	}

	DraggableMapController.prototype = {
		handleClick:function(link, rel) {
			var type = /map-([a-z]+)/i.exec(rel)[1];
			switch (type) {
				case 'set':   this.setMap(link, rel); break;
				case 'move':  this.clickMoveMap(link, rel); break;
				case 'print': this.printMap(link, rel); break;
			}
			return true;
		},

		startDrag:function(e) {	
			this.dragMode = true;
			this.tx = e.clientX;
			this.ty = e.clientY;
			e.preventDefault();
		},
		
		endDrag:function(e) { 
			this.dragMode = false; 
		},

		handleDrag:function(e) {	
			if(!this.dragMode) {
				return;
			}

			var x = e.clientX,
				y = e.clientY,
				dx = x - this.tx,
				dy = y - this.ty;

			this.moveMap(dx, dy);
			this.tx = x;
			this.ty = y;
			e.preventDefault();
		},

		moveMap:function(dx, dy) {
			var map = this.map[0];
			var tx = map.offsetLeft + dx;
			var ty = map.offsetTop + dy;
			if(tx > 0) { tx = 0; }
			if(ty > 0) { ty = 0; }
			var mx = map.offsetWidth - map.parentNode.offsetWidth;
			var my = map.offsetHeight - map.parentNode.offsetHeight;
			if(tx < -mx) { tx = -mx; }
			if(ty < -my) { ty = -my; }
			this.map.css({
				left: tx + 'px',
				top: ty + 'px'
			});
		},

		clickMoveMap:function(link, rel) {
			var type = /map-move-([a-z]+)/i.exec(rel)[1];
			var x = 100, y = 100;
			switch (type) {
				case 'n':  x=0; break;
				case 'ne': x*=-1; break;
				case 'e':  y=0; x*=-1; break;
				case 'se': x=y*=-1; break;
				case 's':  x=0; y*=-1; break;
				case 'sw': y*=-1; break;
				case 'w':  y=0; break;
			}

			this.moveMap(x, y);
			return true;
		},

		setMap:function(link, rel) {
			var src = link.href;
			this.map.attr('src', src);
			this.moveMap(10000, 10000);
			return true;
		},

		printMap:function(link, rel) {
			if(!this.printFrame) {
				this.printFrame = document.createElement('iframe');
				document.body.appendChild(this.printFrame);
				this.printFrame.className = 'printFrame';
			}

			this.printFrame.contentWindow.location.replace(
				NS.getProperty('URL_PRINT_MAP') + '?' + this.map.src
			);
		}
	};

	/**
	 * Bind to NS.initialize
	 */
	NS.subscribe('initialize', function() {
		NS.addApplication('voorzieningen', new Voorzieningen());
	});
});
