Cronica.Home = {
	
	BASE: '',
	
	city_locations: [],
	
	has_selection: false,
	
	initialize: function() {
		
		var i = new Image();
		i.src = '/c/static/default/img/home/map2.png';
		
		j.getJSON(Cronica.Home.BASE+'/mapdata',function(r) {
			for (var i = 0; i<r.length; i++) {
				Cronica.Home.map.put(r[i]);
			}
		});
		
		window.searcher = new Cronica.Home.Search();
		
		j(this.city_locations).each(function() {
			Cronica.Home.map.archive_point(this.id, parseFloat(this.x), parseFloat(this.y));
		});
		
		j('#home-filter h2 a').click(function() {
			var node = j(this);
			if (!node.parents('.home-filter-list').hasClass('selected')) {
				
				if (node.attr('href')=='#bezirke') {
					
					j('#archive-info').fadeOut(200);
					
					j('#map').removeClass('map2').addClass('map1');
					j('#map .points').fadeIn(200);
					j('#map .archive-points').fadeOut(200);

					j('#district').addClass('selected');
					j('#district ul, #municipality, #city').slideDown(200);

					j('#archive').removeClass('selected');
					j('#archive ul, #collection').slideUp(200);
					
				}
				else {

					if (j('.archive-points.selected').length>0) j('#archive-info').fadeIn(200);
					
					j('#map').removeClass('map1').addClass('map2');
					j('#map .archive-points').fadeIn(200);
					j('#map .points').fadeOut(200);

					j('#district').removeClass('selected');
					j('#district ul, #municipality, #city').slideUp(200);
					
					j('#archive').addClass('selected');
					j('#archive ul, #collection').slideDown(200);
				}
			}
		});
		
		
		var that = this;
		j('#archive-info, #archive-info-bottom').click(function() {
			Cronica.hud.open(that);
			j('#cronica-hud-content').text('').hide();
			j.get('/c/collections/index/'+j('#archive li.selected').attr('name'), function(r) {
				j('#cronica-hud-content').get(0).innerHTML = r;
				j('#cronica-hud-content').fadeIn(200);
				j('#collection-info .collections a:first').trigger('click');
				
				j('#collection-info .logo').ifixpng();
			});
		});
		
	},
	
	
	// hud delegate
	did_close: function() {
		j('#cronica-hud-content').get(0).innerHTML = '';
	},
	
	get: function(type,id) {
		if (type=='c') {
			window.searcher.get(type,id,function() {
				Cronica.Home.map.point();
			});
		}
		else {
			window.searcher.get(type,id);
		}
	},


	district: {
		
		load: function(id, callback) {
						
			Cronica.Home.has_selection = true;
						
			Cronica.Home.archive.clear();
			
			j('#municipality').get(0).innerHTML = '';
			j('#city').get(0).innerHTML = '';
			
			Cronica.Home.map.point();
			
			j('#district li').removeClass('selected').filter('[name='+id+']').addClass('selected');
			j('#map .point').removeClass('selected');
			
			j.get(Cronica.Home.BASE+'/municipalities/'+id, function(r) {

				j('#municipality').get(0).innerHTML = r;
				
				j('#municipality li').each(function() {
					var n = j(this).attr('name');
					j('#map .points [name='+n+']').addClass('selected');
				});
				
				if (callback) {
					callback();
				}
			});
		},
		
		clear: function() {

			j('#municipality').get(0).innerHTML = '';
			j('#city').get(0).innerHTML = '';
			
			Cronica.Home.map.point();
			
			j('#district li').removeClass('selected');
			
			j('#map .point').removeClass('selected');
		}
	},
	
	municipality: {
		
		load: function(id, callback) {
			
			Cronica.Home.archive.clear();

			j('#city').get(0).innerHTML = '';
			
			j('#map .point').removeClass('selected').filter('[name='+id+']').addClass('selected');
			j('#municipality li').removeClass('selected').filter('[name='+id+']').addClass('selected');
			
			Cronica.Home.map.point();

			j.get(Cronica.Home.BASE+'/cities/'+id, function(r) {

				j('#city').get(0).innerHTML = r;

				if (callback) {
					callback();
				}
			});

		}
		
	},
	
	city: {
		load: function(id, callback) {
			
			Cronica.Home.archive.clear();

			j('#city li').removeClass('selected').filter('[name='+id+']').addClass('selected');
		}
	},
	
	archive: {
		load: function(id, callback) {
						
			Cronica.Home.has_selection = true;

			j('.archive-points .archive-point').removeClass('selected');
			j('.archive-points [name=archive'+id+']').addClass('selected');
						
						
			j('#archive-info').hide();
			j('#archive-info-bottom').hide();

			Cronica.Home.district.clear();
			
			j('#collection').get(0).innerHTML = '';
			
			j('#map .point').removeClass('selected').filter('[name='+id+']').addClass('selected');
			j('#archive li').removeClass('selected').filter('[name='+id+']').addClass('selected');
			
			j.get(Cronica.Home.BASE+'/collections/'+id, function(r) {
	
					
				j('#archive-info .info').get(0).innerHTML = j('#archive li.selected a').text();
				j('#archive-info-bottom span').get(0).innerHTML = j('#archive li.selected a').text();
	
				j('#collection').get(0).innerHTML = r;
	
				if (callback) {
					callback();
				}

				var right 	= (560 - parseInt(j('#map .archive-point.selected').css('left')) + 7) + 'px';
				var bottom	= (parseInt(j('#map .archive-point.selected').css('bottom')) - 2) + 'px';
				
				j('#archive-info').css({bottom: bottom, right: right}).show();	
				j('#archive-info-bottom').show();			
			});
		},
		
		clear: function() {
			j('.archive-points .archive-point').removeClass('selected');
			j('#archive-info').hide();
			j('#archive-info-bottom').hide();
			j('#collection').get(0).innerHTML = '';
			j('#archive li').removeClass('selected');
		}
	},
	
	collection: {
		load: function(id, callback) {	

			Cronica.Home.district.clear();

			j('#collection li').removeClass('selected').filter('[name='+id+']').addClass('selected');
		}
	},
	
	mapclick: function(district_id, municipality_id) {
		Cronica.Home.district.load(district_id, function() {
			Cronica.Home.municipality.load(municipality_id);
		});
		Cronica.Home.get('m',municipality_id);
	},
	
	map: {
		
		base: {
			x: 692400,
			y: 114300
		},
		
		factor: 255,
			
		archive_point: function(name, x, y) {
			
			if (isNaN(x) || isNaN(y) || x <= 0.0 || y <= 0.0) return; 
			
			x = parseInt((x-this.base.x)/this.factor);
			y = parseInt((y-this.base.y)/this.factor);

			j('.archive-points').get(0).innerHTML += '<div name="'+name+'" class="archive-point" style="left:'+x+'px; bottom:'+y+'px;"> </div>';
		},
				
		point: function() {
			
			if (j('#map').hasClass('map1')) {
			
				if (j('#city .selected a').length == 0) {
					j('#map .target').fadeOut();
					// j('#map .target').animate({
					// 	left: '60px',
					// 	bottom: '50px'
					// });
					j('#map .point').fadeIn();
					return;
				}
			
				eval("var coord = "+j('#city .selected a').attr('name')+";");
			
				var x = coord.x;
				var y = coord.y;
			
				if (x==0 && y==0) {
					j('#map .target').fadeOut();
					// j('#map .target').animate({
					// 	left: '60px',
					// 	bottom: '50px'
					// })
					j('#map .point').fadeIn();
				}
				else {
				
					x = parseInt((x-this.base.x)/this.factor);
					y = parseInt((y-this.base.y)/this.factor);
				
					j('#map .target').animate({
						left: x+'px',
						bottom: y+'px'
					});
					j('#map .target').fadeIn();
					j('#map .point.selected').fadeOut();
				}
			}
		},
		
		put: function(data) {
			var x = data.label_coord_x;
			var y = data.label_coord_y;

			x = parseInt((x-this.base.x)/this.factor);
			y = parseInt((y-this.base.y)/this.factor);
			
			var diffX = -5;
			var diffY = -8;
			
			j('#map .points').get(0).innerHTML += '<div class="point" name="'+data.id+'" style="left:'+(x+diffX)+'px; bottom:'+(y+diffY)+'px;">'
				+'<a href="#" onclick="Cronica.Home.mapclick('+data.district_id+','+data.id+'); return false;">'+data.name+'<\/a><\/div>';
						
				// x = 760040.75;
				// y = 133530.45;
				// x = parseInt((x-this.base.x)/this.factor);
				// y = parseInt((y-this.base.y)/this.factor);
				// 
				// j('#map').append('<div class="point" style="background-color: red; width: 2px; height: 2px; left:'+x+'px; bottom:'+y+'px;">'+'<\/div>');
				// 
				// 
				// x = 728806.31;
				// y = 122248.17;
				// x = parseInt((x-this.base.x)/this.factor);
				// y = parseInt((y-this.base.y)/this.factor);
				// 
				// j('#map').append('<div class="point" style="background-color: red; width: 2px; height: 2px; left:'+x+'px; bottom:'+y+'px;">'+'<\/div>');

				// x = 757693.81;
				// y = 210498.17;
				// 
				// x = parseInt((x-this.base.x)/this.factor);
				// y = parseInt((y-this.base.y)/this.factor);
				// 
				// j('#map').append('<div class="point" style="background-color: red; width: 2px; height: 2px; left:'+x+'px; bottom:'+y+'px;">'+'<\/div>');
				// 
				// 
				// x = 830318.81;
				// y = 168348.17;
				// 
				// x = parseInt((x-this.base.x)/this.factor);
				// y = parseInt((y-this.base.y)/this.factor);
				// 
				// j('#map').append('<div class="point" style="background-color: red; width: 2px; height: 2px; left:'+x+'px; bottom:'+y+'px;">'+'<\/div>');
		}
		
	}
		
}

Cronica.Home.Search = Cronica.Search.extend({
		
	init: function() {
		this.left 	= 0;
		this.result	= new Cronica.Search.Result();
		
		j('#scroller').click(function(e) {
			if (j(e.target).hasClass('img')) {
				Cronica.Search.Viewer.show(e.target);
			}
		});
	},
	
	reset: function() {
		this.index = 0;
		j('#results').text('').css('left','0em');
		this.result = new Cronica.Search.Result();
		j('#next').hide();
		j('#prev').hide();
	},

	val: {

		text: function() { return '' },
		tag: function() { return ''; },
		filter_tag: function() { return ''; },
		city: function() { return ''; },
		filter_city: function() { return ''; }
	},
	
	add_pages: function(from, len) {
		var html = '';
		for (idx = from; idx<len; idx++) {
			var style = (idx+1==len ? 'style="display:none;"':'');
			var left = Cronica.SEARCH_CONTAINER_WIDTH * idx;
			html += '<div class="page page'+idx+'" style="display: none; left: 0em;">'
			+'<div class="img-container top1 left1"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left2"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left3"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left4"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left5"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left6"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left7"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left8"><div '+ style +' class="img"> <\/div><\/div>'
			+'<div class="img-container top1 left9"><div '+ style +' class="img"> <\/div><\/div>'
			+'<\/div>';
		}
		j('#results').get(0).innerHTML += html;
	},
	
	load_filter: function() {

	},
	
	
	get: function(type,id,callback) {
		
		this.request_param = 'type='+type+'&id='+id;
				
		j('#scroller').addClass('loading');
		
		if (this._lock) return;
		
		this._lock = true;
		
		
		this.reset();
		this.get_page(0,callback);
				
		this._lock = false;
		
	},
	
	
	request_param: '',
	get_page: function(nr,callback) {
				
		if (this.req) {
			//this.req.abort();
		}
		
		var args = [];
		
		if (!this.result.stat) {
			args.push('stat=true');
		}
		args.push('page='+nr);
		
		args.push(this.request_param);
		

		var that = this;

		this.req = j.getJSON(Cronica.Home.BASE+'/json'+'?'+args.join('&'), function(r) {
			that.did_get(r);
			if (callback) callback();
		});
		
	},
	
	did_get: function(r) {
				
		j('#scroller').removeClass('loading');
		j('#filter').removeClass('loading');
		
		this.req = null;

			
		if (r.stat) {
			this.did_get_stat(r.stat);
		}
		
		this.update_display();
		
		
		var idx = 0;
		j('.page'+r.index+' .img-container').each(function() {

			if (idx < r.rows.length && !j(this).data('info')) {
				var row = r.rows[idx];

				

				var t = j(this).find('.img');
				t.addClass('ok');
				
				t.data('info',row);
				setTimeout(function() {
					t.css('background-image','url('+row.path_thumb+')');
				},100);
				t.show();
			}
			idx++;
		});
		
		j('.page'+r.index).addClass('loaded');
		
		if (this._load_filter) {
			this.load_filter();
		}
		
	}
	
});

j(document).ready(function() {
	Cronica.Home.initialize();
	

	setTimeout(function() {
		if (!Cronica.Home.has_selection) {

			var c = 0, idx = 0;

			c = j('#district ul li').length;
			idx = Math.ceil(Math.random()*c) - 1;
			var i1 = j('#district ul li:eq('+idx+')').attr('name');

			Cronica.Home.district.load(i1, function() {	
				c = j('#municipality ul li').length;
				idx = Math.ceil(Math.random()*c) - 1;

				var i2 = j('#municipality ul li:eq('+idx+')').attr('name');
				Cronica.Home.municipality.load(i2, function() {
					Cronica.Home.get('m',i2);
				});
			});
		}
	}, 1500);
});