// JavaScript Document

$(document).ready(function(){
	$('body').addClass('hasJS');
	
	 
	   //Add text-size widget
	   $('#textSizer').append('<div id="fontResizer"></div>')
		fontSize("#fontResizer", "body", 12, 14, 18);

		//Add custom select box 
		$("#selectToggle").click(function(){
		  $("#selectBox").slideToggle("slow");
		  $(this).toggleClass("open");
		  return false;
		});
		
		$("#selectBox").hover(staysOpen,closeSelect);
		function closeSelect(event) {
			if($("#selectToggle").hasClass("open")) {
			 $("#selectBox").slideToggle("slow");
			 $("#selectToggle").removeClass("open");
			 }
		}	
		
		$(".search").hover(closeSelect);
		$(".menu ul").hover(closeSelect);
		
		//Close drop Panels if you hover the select box.
		$("#selectToggle").mouseover(function(){
		  $(".panel").hide();
		  $("li.navTab").removeClass("active");
		 });
		
		
		
		
		$("#masthead").mouseover(function() {
		  $(".panel").hide();
		  $("li.navTab").removeClass("active");	
		});

		$("#container").hover(staysOpen,close);
	   
	   function close(event) {	   		
	   		$(".panel").hide();
	   		$(".menu ul a").removeClass("active");
	   	}
		   	
		//Set class for external links
                $('a[href]:not([href^="http://ci.kent.wa.us"]):not([href^="http://www.ci.kent.wa.us"]):not([href^="http://kent.wsol.net"]):not([href^="#"]):not([href^="javascript:"]):not([href^="/"]):not([href^="mailto:"])').addClass('external'); 
		//$('a:not([href^="http://ci.kent.wa.us"]):not([href^="http://www.ci.kent.wa.us"]):not([href^="http://kent.wsol.net"]):not([href^="#"]):not([href^="javascript:"]):not([href^="/"])').addClass('external');



		//Hide all of our Drop down panels	
		$(".panel").hide();

        //first remove all hover states on tabs, then add the active class to hovered tab 	   
	    $("li.navTab").hover(hoverTab,staysOpen);
         
         function hoverTab(event) {
           $(".menu ul a").removeClass("active"); 
	       $(event.target).addClass("active"); 	  
         }
         function staysOpen(event) {
         }
             
         //when you hover over the text, pop open the panel. grab the panel id from the title attribute
	     $("a.nav").hover(function(){  
	       	var panelId = $(this).attr("title");
	       	$(".panel").hide();
	       	$(panelId).show();
	       	//$(panelId).slideToggle("fast");
	       	$(panelId).hover(staysOpen,close);
	     	
		   
		   //when you lose panel hover, hide the panel
		   function close(event) {	   		
		   		$(".panel").hide();
		   		$(".menu ul a").removeClass("active");
		   	}
		   
		   
		   function staysOpen(event) {	
		   		
		   }
	     });
});


$(window).load(function(){	     
	     
	// Accordion Panel	
    $("div.more").each(function() {
    	$(this).css("height", $(this).height() + "px");
    	$(this).hide();
    });

    $(".accordion div > h3").css("cursor", "pointer");
    $(".readMore").css("cursor", "pointer");
    $('<a class="toggleSprite"> +/- </a>').prependTo('div.readMore');
    $(".accordion div > h3").hover(
		function() {
		    $(this).css({ color: "#e12f29" }); //mouseover
		},
		function() {
		    $(this).css({ color: "#000000" }); // mouseout
		}
	);

    $("#mainContent .accordion div > h3").click(function() {
        $(this).siblings("div.more").slideToggle(1000);
		$(this).siblings("div.readMore").children("a.toggleSprite").toggleClass("close");
    });
    

    $(".readMore").click(function() {
        $(this).siblings("div.more").slideToggle(1000);
		$(this).children("a.toggleSprite").toggleClass("close");
    });
	
	// Tab Switchers
    $(function() {
        var tabContainers = $('div.tabContainer > div');

        $('div.tabs ul.tabNavigation a').click(function() {
            tabContainers.hide().filter(this.hash).show();

            $('div.tabs ul.tabNavigation a').removeClass('selected');
            $(this).addClass('selected');

            return false;
        }).filter(':first').click();
    });
	     
});


// --------------------- JQUERY DIV PAGINATION --------------------------

$(document).ready(function(){

	//how much items per page to show
	var show_per_page = 3;
	//getting the amount of elements inside content div
	var number_of_items = $('#content').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();">PREVIOUS STORIES</a>';
	var current_link = 0;
	while(number_of_pages > current_link){
		navigation_html += '<a class="page_link" href="javascript:go_to_page(' + current_link +')" longdesc="' + current_link +'"></a>';
		current_link++;
	}
	navigation_html += '<a class="next_link" href="javascript:next();">NEXT STORIES</a>';

	$('#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
	$('#content').children().css('display', 'none');

	//and show the first n (show_per_page) elements
	//$('#content').children().slice(0, show_per_page).css('display', 'block');
	$('#content').children().slice(0, show_per_page).fadeIn("slow");
	
	//HIDE PREVIOUS LINK ON LOAD
	$('.previous_link').hide();

});

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
	//$('#content').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
	$('#content').children().css('display', 'none').slice(start_from, end_on).fadeIn("slow");

	/*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);
	
	
	//how much items per page to show
	var show_per_page = 3;
	//getting the amount of elements inside content div
	var number_of_items = $('#content').children().size();
	//calculate the number of pages we are going to have
	var number_of_pages = Math.ceil(number_of_items/show_per_page);
	
    //HIDE THE NEXT BUTTON ON LAST PAGE
	if(parseInt($('#current_page').val()) + 1 == parseInt(number_of_pages))
	{
       $('.next_link').hide();
	}
	else
	{
	    $('.next_link').show();
	}
	
	$('.previous_link').show();
		
	//HIDE THE PREVIOUS BUTTON ON FIRST PAGE
	if(parseInt($('#current_page').val()) == 0)
	{
       $('.previous_link').hide();
	}
	else
	{
	    $('.previous_link').show();
	}
}

// --------------------- SEARCH BOX --------------------------
// CLEAR ELEMENT TEXT
function ClearElementText(text, elementId) {
if ($('#' + elementId).val() == text)
  $('#' + elementId).val("");
}

// RE-ENTER ELEMENT TEXT ON BLUR
function SetElementText(text, elementId) {
if ($('#' + elementId).val() == "")
  $('#' + elementId).val(text);
}



