/* 

	This is for random scripts to help the site preform better. Plugins should not go here.
	This site requires jQuery 1.4 or higher.

*/

// Site Functions


// Carousel Rotate 
function runCarousel(me, elem) {
	// Get New Object
	var obj = me;
	
	// Change Active State
	$('#carouselControls a').removeClass('active');
	$(elem).addClass('active');

	// Set New Background
	var newImg = $('#carouselImages .' + obj).attr('src');
	$('#backstretch img').hide().attr('src', newImg).fadeIn();

	// Set New Text
	$('.carouselText').hide();
	$('#' + obj).show();
}// end Carousel Function



// Functions for Top Nav
function navFlyDown() { $('ul.sub-menu').hide(); $('#headlinks ul li').removeClass('active'); $(this).find('ul.sub-menu').show(); $(this).addClass('active'); }
function navFlyUp() { $(this).find('ul.sub-menu').hide(); $(this).removeClass('active'); }

// Dom-Ready Events
$(document).ready(function(){

	/* Check for JS */
	$('html').removeClass('no-js');


	// Ad PDF Style
	//$("a[href$='.pdf']").addClass("pdf").wrapInner('<span />');
	
	/* Top nav Flyout */
	var config = {over: navFlyDown, timeout: 700, out: navFlyUp };
	$('#headlinks ul.menu > li').hoverIntent(config);
	
	
	$('#headlinks a[href^=#top]').addClass('notClick');
	$('#headlinks a[href^=#top]').click(function(){ return false; });
	
	
	
	// Footer Nav Last
	$('#footlinks a:last').addClass('last');
	
	
/*
	Script for Home Page Carousel
	
	Auto-Rotates and allows for click of element.
	
*/	

if($('#bodycontentHome').length > 0){

	// Setup
	$('.carouselText').hide();
	
	// Setup for Next/Prev
	$('#carouselControls a').click(function(){
		
		var me = $(this).attr('rel');
		var elem = $(this);
		
		runCarousel(me, elem);
		return false;
	});


	// Setup for Timer
	$(document).everyTime(15000, function() { 
	
		var tElem = $('#carouselControls a.active');
		
		var me = $(tElem).next().attr('rel');
		var elem = $(tElem).next();

		if(me == undefined)	{
			var me = $('#carouselControls a:first').attr('rel');
			var elem = $('#carouselControls a:first');
			runCarousel(me, elem);
		} else {
			runCarousel(me, elem);
		}
		
	});
	
	
	
	
}	
	
	
if($('.projectsNav').length > 0){

	var show_per_page = $('#show_per_page').val(); 
	var number_of_items = $('#projContent').children().size();
	var number_of_pages = Math.ceil(number_of_items/show_per_page);

	if(show_per_page < number_of_items) {
		
		var navigation_html = '<a class="previous_link" href="javascript:previous();">Prev</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 +'">'+ (current_link + 1) +'</a>';
			current_link++;
		}
		navigation_html += '<a class="next_link" href="javascript:next();">Next</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
		$('#projContent').children().css('display', 'none');
		
		//and show the first n (show_per_page) elements
		$('#projContent').children().slice(0, show_per_page).css('display', 'block');
	}

}	
	
	
	
	// Interrior Rotator
	$('#rotator').jshowoff({ speed:4000 });	
	
	
	$("#projContent a").click(function(){
		var link = $(this).attr('href');
		var uquer = $("#current_page").val();
		var quer = link.replace(/(\d+)"*$/, uquer);
		$(this).attr('href', quer);
		return true;
	});
	

}); // End DOC.READY




// Random Page 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){
	//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
	$('#projContent').children().css('display', 'none').slice(start_from, end_on).css('display', 'block');
	
	/*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);
}


$(document).ready(function(){
	var pts = $("#page_to_show").val();
	go_to_page(pts);
});
















