$(function(){
	var me = this;
	this.map = $('#map');
	this.map.find('div.station').not('.selected').addClass('clickable');
	this.stations = $('#content div.stations');
	this.moscowList = this.stations.find('div.moscow');
	//this.moscowList.find('div').addClass('clickable');
	this.othersList = this.stations.find('div.others');
	//this.othersList.find('p').not('.map_link').addClass('clickable');
	//this.othersList.find('h3').addClass('clickable');
	
	
	this.stations.find('p.switcher span').click(function(){
		if($(this).is('.pseudo_link')){
			$(this).parent().find('span').toggleClass('selected').toggleClass('pseudo_link').removeClass('hover');
			me.stations.find('>div').toggleClass('hidden');
			me.map.find('>div').toggleClass('hidden');
		}
		
	});
	
	this.stations.find('p.switcher span').hover(
		function(){
			if($(this).is('.pseudo_link')){
				$(this).addClass('hover');
			}
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	
	this.map.find('div.station').hover(
		function(){
			if(!$(this).is('.selected')){
				$(this).addClass('hover');
				var objClass = $(this).attr('class').split(' ')[1];
				var listClass = $(this).parent().attr('class');
				me.stations.find("> div[class*="+listClass+"]").find("> div[class*="+objClass+"]").find('span.pseudo_link').addClass('hover');
			}
		},
		function(){
			$(this).removeClass('hover');
			var objClass = $(this).attr('class').split(' ')[1];
			var listClass = $(this).parent().attr('class');
			if(listClass=='moscow'){
				me.moscowList.find("> div[class*="+objClass+"]").find('>p >span').removeClass('hover');
			} else {
				me.othersList.find("> div[class*="+objClass+"]").find('>h3 >span').removeClass('hover');
			}
		}
	);
	
	this.map.find('div.moscow div.station').click(function(){
		if(!$(this).is('.selected')){
			
			$(this).parent().find('div.station').removeClass('selected hover').addClass('clickable');
			$(this).addClass('selected').removeClass('clickable hover');
			var objClass = $(this).attr('class').split(' ')[1];
			var target = me.moscowList.find("div[class*="+objClass+"]");
			var that = this;
			this.endTop = target.get(0).offsetTop;
			this.startTop = $(window).scrollTop();
			me.moscowList.find('div').removeClass('selected hover').find('p > span').addClass('pseudo_link');
			// function stopclock() {
			// 			  if (timerID){
			// 			  	clearInterval(timerID);
			// 			  }
			// 			  timerID = null;
			// 			}
			
			target.addClass('selected').find('p > span.pseudo_link').removeClass('pseudo_link hover');
			//$(window).scrollTop(iTop);
			
			// k = (iTop - a)/10;
			// function slideUp(){
			// 	a += k;
			// 	$(window).scrollTop(a);
			// 	if(a>=iTop){
			// 		clearInterval(timerID);
			// 	}
			// }
			// function slideDown(){
			// 	a += k;
			// 	$(window).scrollTop(a);
			// 	if(a<iTop){
			// 		clearInterval(timerID);
			// 	}
			// }
			// if(k<0){
			// 	a=$(window).scrollTop();
			// 	k = (iTop - a)/10;
			// 	timerID = setInterval(slideDown, 10);
			// 	timerID = null;
			// }
			// if(k>=0){
			// 	a=$(window).scrollTop();
			// 	k = (iTop - a)/10;
			// 	timerID = setInterval(slideUp, 10);
			// 	timerID = null;
			// }
			// target.addClass('selected').find('p > span.pseudo_link').removeClass('pseudo_link hover');
			
			var fly = new Tween(window, '', EEQ.linear, 0, 100, 6);
			fly.onMotionChanged = function(obj){
				$(obj).scrollTop((that.endTop*this.position/100) + (that.startTop*(100 - this.position))/100);
			};
			// fly.onMotionFinished = function(obj){
			// 	$(obj).parent().addClass('hidden');
			// 	main.animeEnd = true;
			// 	if(main.animeStart && main.animeEnd){
			// 		main.switcherShow(objClass);
			// 	}
			// 	main.animeStart = false;
			// 	main.animeEnd = false;
			// };
			
		}
	});
	
	this.map.find('div.others div.station').click(function(){
		if(!$(this).is('.selected')){
			$(this).parent().find('div.station').removeClass('selected hover').addClass('clickable');
			$(this).addClass('selected').removeClass('clickable hover');
			var objClass = $(this).attr('class').split(' ')[1];
			var target = me.othersList.find("div[class*="+objClass+"]");
			//var iTop = target.get(0).offsetTop;
			var that = this;
			this.endTop = target.get(0).offsetTop;
			this.startTop = $(window).scrollTop();
			
			me.othersList.find('div').removeClass('selected hover').find('h3 > span').addClass('pseudo_link');
			target.addClass('selected').find('h3 > span.pseudo_link').removeClass('pseudo_link hover');
			//$(window).scrollTop(iTop);
			var fly = new Tween(window, '', EEQ.linear, 0, 100, 6);
			fly.onMotionChanged = function(obj){
				$(obj).scrollTop((that.endTop*this.position/100) + (that.startTop*(100 - this.position))/100);
			};
			
		}
	});
	
	this.moscowList.find('p > span').click(function(){
		if($(this).is('.pseudo_link')){
			me.moscowList.find('p > span').addClass('pseudo_link');
			$(this).removeClass('pseudo_link hover');
			var papa = $(this).parents('div:first');
			var objClass = papa.attr('class').split(' ')[0];
			me.moscowList.find('> div').removeClass('selected hover');
			papa.addClass('selected');
			// var iTop = papa.get(0).offsetTop;
			// $(window).scrollTop(iTop);
			me.map.find('div.moscow div.station').removeClass('selected hover').addClass('clickable');
			me.map.find("div.moscow > div[class*="+objClass+"]").addClass('selected').removeClass('clickable hover');
		}
	});
	
	
	
	this.moscowList.find('p > span').hover(
		function(){
			if($(this).is('.pseudo_link')){
				$(this).addClass('hover');
				var papa = $(this).parents('div:first');
				var objClass = papa.attr('class').split(' ')[0];
				me.map.find("div.moscow > div[class*="+objClass+"]").addClass('hover');
			}
		},
		function(){
			if($(this).is('.pseudo_link')){
				$(this).removeClass('hover');
				var papa = $(this).parents('div:first');
				var objClass = papa.attr('class').split(' ')[0];
				me.map.find("div.moscow > div[class*="+objClass+"]").removeClass('hover');
			}
		}
	);
	
	this.othersList.find('h3 > span').click(function(){
		if($(this).is('.pseudo_link')){
			me.othersList.find('h3 > span').addClass('pseudo_link');
			$(this).removeClass('pseudo_link hover');
			var papa = $(this).parents('div:first');
			me.othersList.find('> div').removeClass('selected hover');
			papa.addClass('selected');
			var objClass = papa.attr('class').split(' ')[0];
			// var iTop = papa.get(0).offsetTop;
			// $(window).scrollTop(iTop);
			me.map.find('div.others div.station').removeClass('selected hover').addClass('clickable');
			me.map.find("div.others > div[class*="+objClass+"]").addClass('selected').removeClass('clickable hover');
		}
	});
	
	this.othersList.find('h3 > span').hover(
		function(){
			if($(this).is('.pseudo_link')){
				$(this).addClass('hover');
				var papa = $(this).parents('div:first');
				var objClass = papa.attr('class').split(' ')[0];
				me.map.find("div.others > div[class*="+objClass+"]").addClass('hover');
			}
		},
		function(){
			if($(this).is('.pseudo_link')){
				$(this).removeClass('hover');
				var papa = $(this).parents('div:first');
				var objClass = papa.attr('class').split(' ')[0];
				me.map.find("div.others > div[class*="+objClass+"]").removeClass('hover');
			}
		}
	);
});