function addDOMLoadEvent(f){if(!window.__ADLE){var n=function(){if(arguments.callee.d)return;arguments.callee.d=true;if(window.__ADLET){clearInterval(window.__ADLET);window.__ADLET=null}for(var i=0;i<window.__ADLE.length;i++){window.__ADLE[i]()}window.__ADLE=null};if(document.addEventListener)document.addEventListener("DOMContentLoaded",n,false);/*@cc_on @*//*@if (@_win32)document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");var s=document.getElementById("__ie_onload");s.onreadystatechange=function(){if(this.readyState=="complete")n()};/*@end @*/if(/WebKit/i.test(navigator.userAgent)){window.__ADLET=setInterval(function(){if(/loaded|complete/.test(document.readyState)){n()}},10)}window.onload=n;window.__ADLE=[]}window.__ADLE.push(f)}

var currentLocation;
var tm;
var locations;
var win;

var dragStart = {
	time: 0,
	x: 0,
	y: 0
}
var dragStop = {
	time: 0,
	x: 0,
	y: 0
}

function externalLinks() {
	$$('a[rel=external]').setProperty('target', '_blank');
}

var Location = new Class({
	
	moveHere: function() {
		tm.moveToLocation(this);
	},
	
	initialize: function(name,x,y) {
		this.name = name ? name : '';
		this.x = x ? x : 0;
		this.y = y ? y : 0;
		this.div = new Element('div', {
			'class': 'location'
		});
		this.div.injectInside($('map'));
		this.div.setStyles({
			'left': this.x - 30,
			'top': this.y - 30
		});
		this.text = new Element('span', {
			'text': this.name
		});
		this.text.injectInside(this.div);
		
	}
	
});

var Map = new Class({
	
	Implements: Options,
	
	options: {
		rows: 0,
		cols: 0,
		wrapper: null,
		container: null
	},
	
	map: null,
	
	initialize: function(map, options) {
		
		this.setOptions(options);
		this.map = $(map);
		this.section = new Array();
		if(!this.options.container && this.map) 
			this.options.container = this.map.getParent();
		if(!this.options.wrapper && this.options.container) 
			this.options.wrapper = this.options.container.getParent();
		this.width = this.map.getSize().x;
		this.height = this.map.getSize().y;
		this.sectionHeight = (this.height / this.options.rows).toInt();
		this.sectionWidth = (this.width / this.options.cols).toInt();
		this.firstUse = false;
		
		this.cookie = new Hash.Cookie('map', {path: '/', duration: 1});
		
		this.resetPosition();
			
		if(coords = this.cookie.get('coords')) {
			dimension = this.cookie.get('dimension');
			x = coords.x + (-this.options.container.getStyle('left').toInt() + dimension.left);
			y = coords.y + (-this.options.container.getStyle('top').toInt() + dimension.top);
			this.move(x,y);
		}
		else {
			this.move(3435, 5286);
		}
			
		this.fxMove = new Fx.Morph(this.map, {duration: 'long', 'link': 'cancel', transition: Fx.Transitions.Quad.easeOut});
	},
	
	resetPosition: function(){
		this.wrapperWidth = this.options.wrapper.getSize().x;
		this.wrapperHeight = this.options.wrapper.getSize().y;
		this.wrapperOffsetX = this.width - this.wrapperWidth;
		this.wrapperOffsetY = this.height - this.wrapperHeight;
			
		this.options.container.setStyles({
			'width': 2 * this.width - this.wrapperWidth,
			'height': 2 * this.height - this.wrapperHeight,
			'top': - this.height + this.wrapperHeight,
			'left': - this.width + this.wrapperWidth
		});
	},
	
	setPositionCookie: function(x,y) {
		settings = {
			coords : {
				'x' : x ? x : this.map.getStyle('left').toInt(),
				'y' : y ? y : this.map.getStyle('top').toInt()
			},
			dimension : {
				'width' : this.options.container.getSize().x,
				'height' : this.options.container.getSize().y,
				'top' : this.options.container.getStyle('top').toInt(),
				'left' : this.options.container.getStyle('left').toInt()
			}
		}
		this.cookie.extend(settings);
	},
	
	generate: function() {
		//this.map.empty();
		var k = 0;
		var lis = $$('#map li');
		for(i=0; i<this.options.rows; i++) {
			this.section[i] = new Array();
			for(j=0; j<this.options.cols; j++) {
				this.section[i][j] = lis[k++];
			}
		}
	},
	
	draw: function(x,y){
		
		var startX, endX, startY, endY;
		if(!x) x = this.map.getStyle('left').toInt();
		if(!y) y = this.map.getStyle('top').toInt();
		y -= window.getScroll().y;
		startX = ((this.wrapperOffsetX - x) / this.sectionWidth).toInt();
		startY = ((this.wrapperOffsetY - y) / this.sectionHeight).toInt();
		endX = startX + (this.wrapperWidth / this.sectionWidth).toInt() + 1;
		endY = startY + (this.wrapperHeight / this.sectionHeight).toInt() + 1;
		if(endY >= this.options.rows) endY = this.options.rows - 1;
		if(endX >= this.options.cols) endX = this.options.cols - 1;
		for(i=startY; i<=endY; i++)
			for(j=startX; j<=endX; j++)
				this.section[i][j].setStyle('background-image', 'url(templates/images/map/map_' + (i*this.options.cols+j+1) + '.gif)');
					
	},
	
	move: function(x,y) {
		this.map.setStyles({
			'top': y,
			'left': x
		});
		this.setPositionCookie(x,y);
	},
	
	moveToLocation: function(location) {
		if(!win) return;
		currentLocation = location;
		pos = location.div.getPosition(win);
		x = this.map.getStyle('left').toInt() - pos.x + win.getSize().x / 2 - 35;
		y = this.map.getStyle('top').toInt() - pos.y + win.getSize().y / 2 - 35;
		this.draw(x,y);
		
		this.fxMove.start({
			'top': y,
			'left': x
		}).chain(function(){
			tm.setPositionCookie(x,y);
		});
	},
	
	setLocation: function(location) {
		pos = location.div.getPosition(win);
		x = this.map.getStyle('left').toInt() - pos.x + win.getSize().x / 2 - 35;
		y = this.map.getStyle('top').toInt() - pos.y + win.getSize().y / 2 - 35;
		this.map.setStyles({
			'top': y,
			'left': x
		});
		this.setPositionCookie(x,y);
	},
	
	getContainer: function() {
		return this.options.container;
	},
	
	getPosition: function() {
		return {
			x: this.map.getStyle('left').toInt(),
			y: this.map.getStyle('top').toInt()
		};
	},
	
	inertia: function() {
		var dragtime = dragStop.time - dragStart.time;
		//if(dragtime < 1000) return;
		var dragx = dragStart.x - dragStop.x;
		var dragy = dragStart.y - dragStop.y;
		var dragdistance = Math.sqrt(Math.pow(dragx, 2) + Math.pow(dragy, 2));
		var velocity = dragdistance / dragtime * 1000;
		var fx = new Fx.Morph(this.map, {duration: dragtime*0.7, 'link': 'cancel', transition: Fx.Transitions.Quad.easeOut});
		alert('distance: '+dragdistance.toInt()+'\nms: '+dragtime.toInt()+'\nvelocity: '+velocity.toInt()+' px/sec');
	}
	
});


window.addEvents({
	'domready': function(){
	
			win = $('window-large') ? $('window-large') : ($('window-small') ? $('window-small') : null);
						
			tm = new Map('map', {
				'wrapper': $('map-wrapper'),
				'container': $('map-container'),
				'rows': 25,
				'cols': 25
			});
			tm.generate();
			tm.draw();
			
			locations = new Array();
			locations['unirii'] = new Location('Piata Unirii', 5715, 3200);
			locations['desero'] = new Location('desero', 5820, 4435);//6900, 2940);
			locations['gara_nord'] = new Location('Gara Timisoara Nord', 4440, 3750);
			locations['universitate'] = new Location('Universitatea', 5800, 4100);
			locations['stadion'] = new Location('Stadionul Dan Paltinisanu', 6540, 4710);
			locations['judetean'] = new Location('Spitalul Judetean', 6350, 4930);
			locations['centru'] = new Location('Piata Victoriei', 5470, 3640);
			locations['traian'] = new Location('Piata Traian', 6940, 3280);
			locations['maria'] = new Location('Piata Maria', 5090, 3960);
			locations['sinaia'] = new Location('Piata Sinaia', 4920, 4120);
			locations['gara_est'] = new Location('Gara Timisoara Est', 6650, 2520);
			locations['gauss'] = new Location('Gauss', 5410, 3440);
			
			locations['gauss'].text.setStyle('background', 'none');
			
			currentLocation = locations['unirii'];
			
			new Drag.Move('map', {
				'container': tm.getContainer(),
				'onDrag': function(){
					tm.draw();
				},
				'onStart': function() {
					//dragStart.time = new Date().getTime();
					//position = tm.getPosition();
					//dragStart.x = position.x;
					//dragStart.y = position.y;
				},
				'onComplete': function(){
					tm.setPositionCookie();
					//dragStop.time = new Date().getTime();
					//position = tm.getPosition();
					//dragStop.x = position.x;
					//dragStop.y = position.y;
					//tm.inertia();
				}
			});
			
			new Drag.Move('map', {
				'container': tm.getContainer(),
				'handle': win,
				'onDrag': function(){
					tm.draw();
				},
				'onComplete': function(){
					tm.setPositionCookie();
				}
			});
			
			$$('.drag').addEvents({
				'mousedown': function(e) {
					this.setStyle('cursor','url(templates/images/closedhand.cur), move');
				},
				'mouseup': function(e) {
					this.setStyle('cursor','url(templates/images/openhand.cur), default');
				},
				'mouseover': function(e) {
					this.setStyle('cursor','url(templates/images/openhand.cur), default');
				}
			});
					
	},
	
	'load': function(e) {
			
			if(page == 'contact' || page == 'index') {
				(function(){tm.moveToLocation(locations['gauss']); }).delay(1000);
			}
			
	},
	
	'resize': function(e) {
		tm.resetPosition();
		tm.draw();
		tm.setPositionCookie();
	}

});

addDOMLoadEvent(externalLinks);