var DISPLAY_ITEMS = 25;

var searchKeys = {
		'todofuken':0,
		'kaiinkubun':0,
		'gyoushu':0
};

var cases;

    var currentPage = 1;

    var numberOfAllResults = 0;
    
    var numberOfShowItems = 0;
    
    var numberOfPages = 0;

$(function(){
	cases = $('table tbody tr');
    
	init();
	
    
})

function init(){
	getSearchKeys();
	countAllItems()
	
}


var sufix = '';

function getSearchKeys(){
	
	
	var thisPageLocation = location.href;
	var pageTopLevelCategory ="";
	
	if (thisPageLocation.split('#')[1]) {
		var a = thisPageLocation.split('#')[1];
		var b = a.split('&');
		var final = {};
		$.each(b, function(x, y){
			var temp = y.split('=');
			final[temp[0]] = temp[1];
		});
		$.each(final,function(i,item){
			if (i) {
				$('#' + i + ' option').eq(item).attr('selected','selected');
			}
		})
		
	}
	
	
	if(thisPageLocation.match('/todofuken/'))
		pageTopLevelCategory = 'todofuken';
	else if(thisPageLocation.match('/kaiinkubun/'))
		pageTopLevelCategory = 'kaiinkubun';
	else if(thisPageLocation.match('/gyoushu/'))
		pageTopLevelCategory = 'gyoushu';
	else
		pageTopLevelCategory = 'all';
	
	if(pageTopLevelCategory!='all'){
	$('#' + pageTopLevelCategory).change(function(e){
		var url = $(this[this.selectedIndex]).attr('value');
		getSearchKeysFromURL();
		if (url != 'null') {
			
			
			location.href = url + 'index.html#' + sufix;
		}else{
			location.href = '/member/index.html#' + sufix;
		}
	})
	
	$('select').not('#' + pageTopLevelCategory).change(function(e){
		cases.removeClass('nomatch')
		numberOfAllResults = 0;
		getSearchKeys2();
		countAllItems();
	})
	
	
	
	}else{

	
	$('select').change(function(e){
		var url = $(this[this.selectedIndex]).attr('value');
		if (url != 'null') {
			getSearchKeysFromURL();
			
			location.href = url + 'index.html#' + sufix;
		}else{
		cases.removeClass('nomatch')
		numberOfAllResults = 0;
		getSearchKeys2();
		countAllItems();
		}
	})
	}
	
	$.each($('select'), function(i, item){
			var condition = $(item).attr('name');
            var selectItem = item.options.selectedIndex;
            searchKeys[condition] = selectItem;
    })
}

function getSearchKeys2(){
	$.each($('select'), function(i, item){
			var condition = $(item).attr('name');
            var selectItem = item.options.selectedIndex;
            searchKeys[condition] = selectItem;
    })
}


function getSearchKeysFromURL(){
		var searchKeys = {};
		$.each($('select'), function(i, item){
				var condition = $(item).attr('name');
	            var selectItem = item.options.selectedIndex;
	            searchKeys[condition] = selectItem;
	    })
		
		$.each(searchKeys,function(i,item){
			sufix = sufix + (i + '=' + item + '&');
		})
		
		
	}


var isAllHit = true;

function countAllItems(){
		isAllHit = true;
		$.each(searchKeys,function(key,value){
			if (isAllHit) {
			
				if (value != 0) {
				//console.log(cases.eq(numberOfAllResults).find('.' + key).text())
					if (cases.eq(numberOfAllResults).find('.' + key).text() == $($('#' + key + ' option')[value]).text()) {
					//isAllHit = true;
					}
					else {
						isAllHit = false;
						cases.eq(numberOfAllResults).addClass('nomatch');
					}
				}
			}
		})
		
		
		numberOfAllResults++;
		if (cases.eq(numberOfAllResults).length > 0) {
		var t = setTimeout("countAllItems()", 1);
		}else {
			t = null;
			countHitItems()
		}
}

function countHitItems(){
	numberOfShowItems = $('table tbody tr').not('.nomatch').length;
	$('#result .number').text(numberOfShowItems);
	initPagenation();
}


function initPagenation(){
			
			$('p.pagenavi a,p.pagenavi strong.selected').remove();
			
			currentPage = 1;
			
        if (numberOfShowItems > 25) {
			
            numberOfPages = Math.floor(numberOfShowItems / 25) + 1;
            
        if (numberOfPages > 1) {
                for (i = 0; i < numberOfPages; i++) {
					if(i==0)
                    $('p.pagenavi').append('<strong class="selected"><a href="#">' + (i + 1)  + '</a></strong>')
					else
                    $('p.pagenavi').append('<a href="#">' + (i + 1)  + '</a>')
                }
            }
        }else{
        	$('p.pagenavi').append('<strong class="selected"><a href="#">1</a></strong>')
        }
        
        

	visibleCurrentPage(0)
	
}

function visibleCurrentPage(pageNum){
	$('table tbody').hide();
	$('table tbody tr').hide();
	for(i=0;i<25;i++){
		$('table tbody tr').not('.nomatch').eq(i + pageNum*25).show();
	}
	$('table tbody').show();
	
	$('.pagenavi a').click(function(e){
				e.preventDefault();
			
			var targetPage = $(this).text() - 1;
			
			var tmpAnchor = $('strong.selected').html();
			
			$('strong.selected').replaceWith(tmpAnchor);
			
			$.each($('.pagenavi'),function(i,item){$(item).find('a').eq(targetPage).wrap('<strong class="selected"></strong>')});
			
			visibleCurrentPage(targetPage)
		});
}


function search(){
	getSearchKeys();
	
	countAllPages();
	
}
