
$().ready(function() {
    var tabs = [];
    var tab_dict = {};
    var tabContainers = [];
    var p=$('.pagination_control').get(0);
   var numPages=(Math.floor($(".copy>*").length/10))+1;
	var classesToRemove="";
 	for(var i=1; i<=numPages; i++)
	{
		$('<li class="select_'+i+'"><a href="#page_'+i+'">'+i+'</a></li>').insertBefore(".pagination_control .next");
		classesToRemove+=" page_"+i;
	};
 
  

   $(".copy>*").each(function(i,el)
	{
		$(el).removeClass(classesToRemove);
		$(el).addClass("page_"+(Math.floor(i/10)+1));
$(el).removeAttr("style");

	});

    e=$(p).find('a');

    $(e).each(function () {
	    hs=this.hash.slice(1);
	    if(!(hs in tab_dict))	
	    {
            tab_dict[hs]=this;
	    }
    });


    function hash_handle(hash)
	{
	if(hash)
	{
		if(hash=="all")
		{
			$(".copy>*").show();
			$(".pagination_control li").hide().filter(".paginate").show();
			$("h1,.standfirst").show();

		}
		else
		{
	        $(".copy>*").hide().filter("."+hash).show();
		$('.pagination_control li').show().filter(".paginate").hide();
		var pg=hash.split('_');
		pg=pg[1];
		
		var next_pg=Number(pg)+1;
		var prev_pg=Number(pg)-1;
		if(pg!=1)
		{
			$("h1,.standfirst").hide();
			$('.pagination_control .prev a').attr('href','#page_'+prev_pg);
			$('.pagination_control .prev').show();
			}
		else
		{
			$("h1,.standfirst").show();
			$('.pagination_control .prev').hide();
	
		}
		$('.pagination_control .paginate a').attr('href','#page_'+pg);
		next_pg='page_'+next_pg;
		if(!(next_pg in tab_dict))
		{
			$(".pagination_control .next").hide();
		}
		else
		{
			$('.pagination_control .next a').attr('href','#'+next_pg);
			$(".pagination_control .next").show();
		}
       		 $(".pagination_control li").removeClass('selected').filter(".select_"+pg).addClass('selected');
		$('.pagination_control .page_num').html(pg);
		$("html,body").animate({scrollTop:0},1);
		}
	}
	else
	{
		hash_handle("page_1");
	}
	return false;
	};	


    	$.history.init(hash_handle);
		$(".pagination_control li a").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			$.history.load(hash);
			return false;
		});
/*
	if(location.hash)
	{
		hash_handle(location.hash);
	}
	else
	{
		hash_handle("page_1");
	}
*/
	
});


