
$(document).ready(function() {
	if($('.jqzoom').length){
		var options = {
		zoomWidth: 200,
		zoomHeight: 200,
		xOffset: 10,
		yOffset: 0,
		position: "right",
		title:false	
		}	
		
		$(".jqzoom").jqzoom(options);
	}

	if($('a.zoom').length && $().retina){
		$('a.zoom').retina();
	}

	//show other essays in this category
	$('#explore_other_heading').click(function(){
		$('#other_essay_holder').slideToggle('slow', function() {
		});
			
	});
	
	
	/* for client search in sidebar */
	/* set initial */
	$('#q').val(' search ');
	$('.autocomplete_choices').hide();
	
	$('#q').focus(function(){
			$('#q').val('');
	});


	$('#q').keyup(function(){
			
		if($(this).val().length>1){ 
			$.get('ajax_search_list.php',{ q : $(this).val()},function(data){
				$('.autocomplete_choices').show().html(data);
			});
		}				
				
	});
	
	$('.hdr-search-button').hover(
		function(){
			$(this).attr("src","http://www.historygrandrapids.org/i/search_icon_hover.png");
		},
		function(){
			$(this).attr("src","http://www.historygrandrapids.org/i/search_icon.png");
		}
		);


/* PAGINATION PODCAST PAGE */

	//how many items to show (2 rows of items bars each has 3 floated podcast items)
	var show_per_page = 3;
	
	//get number of item bars
	var number_of_items = $('.item_bar').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 which store the page numbers
	$('#current_page').val(0);
	$('#show_per_page').val(show_per_page);

	var navigation_html = '';
	var current_link = 0;
	
	
	navigation_html += '<a class="previous_link" href="javascript:previous();">Prev<\/a>';
	
	while(number_of_pages > current_link){
		navigation_html += '<a class="page_link" 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();">Next<\/a>';

	$('#pagination').html(navigation_html);

	//add active_page class to the first page link
	$('#pagination .page_link:first').addClass('active_page');

	//hide all the elements inside content div
	$('.item_bar').css('display', 'none');

	//and show the first n (show_per_page) elements
	$('.item_bar').slice(0, show_per_page).css('display', 'block');


	$('.page_link, .previous_link, .next_link').click(function(){
		$(this).blur();
	});
	
	/* hide prev link on page load */
	$('.previous_link').css({'color' : '#F5EFE1', 'borderColor' : '#F5EFE1'});
		$('.previous_link').hover(function(){
			$(this).css({'cursor' : 'default'});
		});
	
});// close document ready


function thisMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

//function to call prototype ajax for each subject load
function callAjaxUpdater(parent, subjectId){
	// remove previously opened
	if($('ol#ajaxed')){
		$('ol#ajaxed').remove();
	}
	
	$.ajax({
		url: url = 'get_items_by_subject.php?subject='+subjectId,
		success: function(html){
		$("#"+subjectId).append(html);
		}
	});
		
	return false;
}


/* PODCAST PAGINATION FUNCTIONS */
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, number_of_pages){
	//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 pagination-wrapper div, get specific items and show them
	$('.item_bar').css('display', 'none').slice(start_from, end_on).fadeIn(3000);

	/*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);

	/* HIDE PREV and NEXT */
	
	if($('.active_page').text()==1){
		$('.previous_link').css({'color' : '#F5EFE1', 'borderColor' : '#F5EFE1'});
		$('.previous_link').hover(
			function(){
				$(this).css({'cursor' : 'default'});
				$('.previous_link').css({'color' : '#F5EFE1', 'borderColor' : '#F5EFE1'});
			}
		);
	}else{
		$('.previous_link').css({'color' : '#000', 'borderColor' : '#C2B592'});
		$('.previous_link').hover(
			function(){
				$(this).css({'cursor' : 'pointer', 'color' : '#888', 'borderColor' : '#C2B592'});
			},
			function(){
				$(this).css({'cursor' : 'default', 'color' : '#000', 'borderColor' : '#C2B592'});
			}		
		);
	}

	if( $('.active_page').next('.next_link').length==true){
		
		$('.next_link').css({'color' : '#F5EFE1', 'borderColor' : '#F5EFE1'});
		$('.next_link').hover(
			function(){
			$(this).css({'cursor' : 'default'});
			$('.next_link').css({'color' : '#F5EFE1', 'borderColor' : '#F5EFE1'});
		});
	
	}else{
		$('.next_link').css({'color' : '#000', 'borderColor' : '#C2B592'});
		$('.next_link').hover(
			function(){
				$(this).css({'cursor' : 'pointer', 'color' : '#888', 'borderColor' : '#C2B592'});
			},
			function(){
				$(this).css({'cursor' : 'default', 'color' : '#000', 'borderColor' : '#C2B592'});
			}
		);
	}
	
	// keeps the pagination in same place 
	$(window).scrollTop($('a#top').position().top) ;

}
