$(document).ready(function() {
   	$(".tax-dl_object_category aside h3").click(function(){
		var filterClose = $(this).hasClass("close");
		if (filterClose) {
			$(this).closest('.box').children('p').slideDown('fast');
			$(this).removeClass('close');
		} else {
			$(this).closest('.box').children('p').slideUp('fast');			
			$(this).addClass('close');			
		}
		return false;														  
	});
	
	$(".tax-dl_object_category aside .box").each(function() {
		$(this).find('p.checkbox').each(function() {
			
			if ($(this).find('input').is(":checked")) {
				$(this).closest('.box').children('h3').removeClass('close');
				$(this).closest('.box').children('p').show();
			}
		});	
	});		
	
	
	$(".tax-dl_object_category .btn.submit").click(function(){
		$('#frmFilter').submit();														  
	});			
	
	var menuParentLength = $('#menu-header-menu li.current-menu-parent').length;
	if (menuParentLength > 1) {
		var currentRef = $('body').attr('data-ref');
		var noRefItem;
		var accomodaties=new Array("appartementen","bed-en-breakfasts","vakantiehuizen");
		var menuItem = $('#menu-header-menu li.current-menu-parent');
		menuItem.each(function() {
			$(this).removeClass('current-menu-parent');	
			var href = $(this).find('a').attr("href").split('/');
			var type = href[(href.length)-2];
			if (inArray(currentRef,accomodaties)) {
				if (type == currentRef) {
					//alert('true');	
					$(this).addClass('current-menu-item');
				} else {
					//noRefItem = $(this);
				}
			} else {
				noRefItem = $(this);
			}
		});	
		if (noRefItem) {
			noRefItem.addClass('current-menu-item');
		}
	} else {
		$('#menu-header-menu li.current-menu-parent').removeClass('current-menu-parent').addClass('current-menu-item');
	}
	
	
	
	var leftOver = get_leftOver();
	setFooter(leftOver);
	
	//make sure the footer remains at the bottom when the user is resizing its window
	$(window).resize(function() {
			leftOver = get_leftOver();
			setFooter(leftOver);
	});
	
	function setFooter(leftOver) {
		var footerHeight = 	$('footer.main').height();
		if (leftOver > footerHeight) {
			$('footer.main').addClass('abs');
		} else {
			$('footer.main').removeClass('abs');
		}
		
	}
	
	function get_leftOver() {
		//we're probably gonna need these
		var windowHeight = $(window).height();
		var docHeight = $(document).height();
		var windowWidth = $(window).width();
		var docWidth = $(document).width();
		
		//place the footer always on the bottom of the window when the window height < docheight
		var topHeight = 	$('div.top').height();	
		var mainHeight = 	$('div.main').height();
		var footerHeight = 	$('footer.main').height();
		var totalHeight = topHeight + mainHeight;
		var leftOver = windowHeight - totalHeight;
		return leftOver;
	}
	
	var taxPage = $('body').hasClass("tax-dl_object_category");
	if (taxPage) {
		//how much items per page to show  
		var show_per_page = 10;  
		var holderEl = $('.holder');
		//getting the amount of elements inside content div  
		var number_of_items = holderEl.children().size();  
		//calculate the number of pages we are going to have  
		var number_of_pages = Math.ceil(number_of_items/show_per_page);  
	  
		//set the value of our hidden input fields  
		$('#current_page').val(0);  
		$('#show_per_page').val(show_per_page);  
	  
		//now when we got all we need for the navigation let's make it '  
	  
		/* 
		what are we going to have in the navigation? 
			- link to previous page 
			- links to specific pages 
			- link to next page 
		*/  
		var navigation_html = '<a class="previous_link" href="javascript:previous();">&nbsp;</a>';  
		var current_link = 0;  
		var last = '';
		while(number_of_pages > current_link){  
			if ((number_of_pages) == (current_link+1)) {
				last = ' last';							
			} else {
				last = '';	
			}
			navigation_html += '<a class="page_link'+ last + '" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'">'+ (current_link + 1) +'</a>';  
			current_link++;  
		}  
		navigation_html += '<a class="next_link" href="javascript:next();">&nbsp;</a>';  
	  
	  	if (number_of_pages > 1) {
			$('#page_navigation').html(navigation_html);  
		}
	  
		//add active_page class to the first page link  
		$('#page_navigation .page_link:first').addClass('active_page');  
	  
		//hide all the elements inside content div  
		holderEl.children().css('display', 'none');  
	  
		//and show the first n (show_per_page) elements  
		holderEl.children().slice(0, show_per_page).css('display', 'block');  	
	}
	
});  

function previous(){  
  
    new_page = parseInt($('#current_page').val()) - 1;  
    //if there is an item before the current active link run the function  
    if($('.active_page').prev('.page_link').length==true){  
        go_to_page(new_page);  
    }  
  
}  
  
function next(){  
    new_page = parseInt($('#current_page').val()) + 1;  
    //if there is an item after the current active link run the function  
    if($('.active_page').next('.page_link').length==true){  
        go_to_page(new_page);  
    }  
  
}  
function go_to_page(page_num){  
    //get the number of items shown per page  
    var show_per_page = parseInt($('#show_per_page').val());  
  
    //get the element number where to start the slice from  
    start_from = page_num * show_per_page;  
  
    //get the element number where to end the slice  
    end_on = start_from + show_per_page;  
  
    //hide all children elements of content div, get specific items and show them  
    $('.holder').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');  
    location.href = "#searchResult"; 
    /*get the page link that has longdesc attribute of the current page and add active_page class to it 
    and remove that class from previously active page link*/  
    $('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');  
  
    //update the current page input field  
    $('#current_page').val(page_num);  
}  

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}
