(function(){
	$.archive = {};
	$.archive.data = [];
	$.archive.cdata = [];
	$.archive.cyear = 0;
	$.archive.sortOrder ={
		year : true,
		station : true,
		type : true
	};
	$.archive.titleList =[];
	$.archive.snipet = function (d){
		return '<div class="archive_item"><ul><li class="col year_col">'+d.year+'</li><li class="col station_col"><p>'+d.station+'</p></li><li class="col type_col"><p>'+d.type+'</p></li><li class="col title_col"><p>'+d.title+'</p></li><li class="col etc_col"><p>'+d.etc+'</p></li></ul></div>';
	}
				
	$.archive.searchFor = function(str,to){
		setTimeout(function(){
			var items = "";
			$.archive.cdata = [];
			$.each($.archive.data,function(){
					if(this[to] == str){
						$.archive.cdata.push(this);
						items += $.archive.snipet(this);
					}
			});
			setTimeout(function(){
				$.archive.build(items);
			},0);
		},0);
	}
	
	$.archive.search = function(str){
		setTimeout(function(){
			var items = "";
			$.archive.cdata = [];
			//var reg = new RegExp(str);
			var len = $.archive.data.length;
			for(var i = 0; i< len ; i++){
				var t = $.archive.titleList[i];
				//var res = reg.exec(t);
				//if(!res){continue};
				if(t.indexOf(str) > -1){
					$.archive.cdata.push($.archive.data[i]);
					items += $.archive.snipet($.archive.data[i]);
				}
			}
			setTimeout(function(){
				$.archive.build(items);
			},0);
		},0);
	}
	
	$.archive.build = function(items){
		$.archive.container.innerHTML = "";
		$("#archive_item_container").append(items);
	}
	
})();


$(document).ready(function(){
		$.archive.container = document.getElementById("archive_item_container");
		
		$(".selector").hide();
		$(".selector a").live("click",function(e){
				var t = $(this).text();
				var p = $(this).parent().parent();
				var to = p.parent().attr("id").split("_")[0];
				p.slideToggle();
				setTimeout(function(){
					$.archive.searchFor(t,to);
				},0);
				return false;
		});
		
		$("#year_head p span,#station_head p span,#type_head p span").click(function(){
				var t = $(this).parent().siblings(".selector");
				
				$(".selector:visible").not(t).slideToggle();
				t.slideToggle();
		}).css("cursor","pointer");
		
		$("#year_head p,#station_head p,#type_head p").css("cursor","pointer").click(function(e){
				if(e.target == this){
					$(".up").not($(this)).removeClass("up");
					$(this).toggleClass("up");
					var type = $(this).parent().attr("id").split("_")[0];
					var flg = $.archive.sortOrder[type];
					if(type == "year"){
						$.archive.cdata.sort(function(a,b){
							if(flg){
								return a[type] - b[type]
							}else{
								return b[type] - a[type] 
							}
						});
					}else{
						$.archive.cdata.sort(function(a,b){
							if(flg){
								return a[type].charCodeAt(0) - b[type].charCodeAt(0)
							}else{
								return b[type].charCodeAt(0) - a[type].charCodeAt(0)
							}
						});
					}
					$.archive.sortOrder[type] = !$.archive.sortOrder[type];
					var items = "";
					$.each($.archive.cdata,function(){
						items += $.archive.snipet(this);
					});
					setTimeout(function(){
						$.archive.build(items);
					},0);
				}
		})
		$("#search_btn").click(function(){
			$(".up").removeClass("up");
			$(".selector:visible").slideToggle();
			if($("#search_input").val() == "") {
				setTimeout(function(){
					$.archive.searchFor($.archive.cyear,"year");
				},0);
			}else{
				$.archive.search($("#search_input").val());
			}
		});
		
		$("#search_input").keydown(function(e){
			if(e.keyCode ==13){
				$("#search_btn").trigger("click");
			}
		})
		/*
		$("#search_input").keyup(function(){
			$("#search_btn").trigger("click");
		})
		*/
		$.getScript("data.php",function(){
					
				$.archive.data = index_data.index;
				var items = "";
				var tmp_y = 0;
				$.each($.archive.data,function(){
						if(this.year > tmp_y){
							tmp_y = this.year
						}
				});
				$.archive.cyear = tmp_y;
				$.each($.archive.data,function(){
						$.archive.titleList.push(this.title);
						if(this.year == tmp_y){
							$.archive.cdata.push(this);
							items += $.archive.snipet(this);
						}
				});
				$("#archive_item_container").append(items);
				
				items = "";
				$.each(index_data.years,function(){
					items += '<li><a href="#">'+this+'</a></li>';
				});
				$("#year_head .selector").append(items);
				
				items = "";
				$.each(index_data.stations,function(){
					items += '<li><a href="#">'+this+'</a></li>';
				});
				$("#station_head .selector").append(items);
				
				items = "";
				$.each(index_data.types,function(){
					items += '<li><a href="#">'+this+'</a></li>';
				});
				$("#type_head .selector").append(items);
				items = "";
		});				   
		
})