// JavaScript Document
jQuery(document).ready(function($) {
	//Hide Admin Bar Buttons
	$("#wp-admin-bar-comments, #adminbarsearch-wrap, #wp-admin-bar-wpseo-kwresearch").hide();
	
	//Search Box (remove default text on focus, then put back in)
	$('#s').focusin(function(){
		var sv = $('#s').attr('value'); 
		if( sv == 'Search' ){ $(this).attr('value', '');}
	});
	$('#s').focusout(function(){
		var sv = $('#s').attr('value'); 
		if( !sv ){ $(this).attr('value', 'Search');}
	});
	
	//SubMenu Animation
	$('#Menu>ul:not(.stick)>li').hover(function(){
		$(this).find('ul:first').css({"z-index": "100",visibility: "visible",display: "none"}).show(400);
	},function(){
		$(this).find('ul:first').css({"z-index": "1",visibility: "hidden"});
	}); 
	
	//Apply active class on parent when hovering on submenu
	$('nav li li a').hover(function(){
			$(this).parent().parent().parent().addClass('active');
		}, function(){
			$(this).parent().parent().parent().removeClass('active');
	});
	
	//Apply array of colors to Menu
	var MenuColors = new Array('orangebg','greenbg', 'yellowbg', 'tealbg', 'redbg');
	$('#Menu>ul>li>a').each(function(index){
		$(this).addClass(MenuColors[index]).parent().addClass(MenuColors[index]).attr('id', 'Item'+(index+1));
		$(this).parent().find('ul').addClass(MenuColors[index]);
	});
	//Apply array of colors to Secondary menu
	var SecColors = new Array('greybg','bluebg', 'purplebg');
	$('#SecondaryMenu span').each(function(index){
		$(this).addClass(SecColors[index]);
	});
	
	//Apply current menu color to heading block section & Content
	var Current = $('.current_page_parent a').attr('class');
	if( !Current ) var Current = $('.current_page_item a').attr('class');
	var Item = $('.current_page_item').attr('id');
	if( !Item ) var Item = $('.current_page_parent').attr('id');
	//$('header section').addClass(Current).addClass(Item);
	//$('#Content article').addClass(Current+'content');

	//Set the Header Section height to be at least the height of the current page submenu
	var CurSubHeight = $('.current_page_item ul').height();
	if( !CurSubHeight ) var CurSubHeight = $('.current_page_parent ul').height();
	var CurSecHeight = $('header section').height();
	if( CurSubHeight > CurSecHeight){
		$('header section').css('height', (CurSubHeight-20)+'px');
	}
	
	//Awards clickable menu show n hide
	$('article.awards>div:not(:first)').hide();
	$("nav#Awards a").click(function(){
			var showID = $(this).attr('href');
			$('article.awards>div').hide();
			$(showID).fadeIn();
			return false;
	});
	
	//Add Padding to Header Section to avoid menu items
	if( $('.stick').length > 0 ){
		//check if child or parent
		if( $('.current_page_item ul').length > 0 ){
			var isParent = true;
		}else{
			var isChild = true;	
		}
		if( isParent ){
			var CurSubPos = $('ul.stick .current_page_item ul').position();
		}else{
			var CurSubPos = $('ul.stick .current_page_parent ul').position();
		}
		if( CurSubPos.left < 180 ){
			$('header section').css('padding-left', CurSubPos.left+210+'px');		
		}else{
			$('header section').css({'text-align':'right', 'padding-right':'40px'});
			$('header section .desc').css({'float':'left', 'margin-left':'-20px', 'text-align':'left'});
		}
	}
	
	$('#Slides').cycle();
	/*Footer Fix to move to bottom on short pages
	var ContainerH = $('#Container').height();
	var WindowH = $(window).height();
	if( ContainerH < WindowH ){
		$('#Container').height(WindowH);
		$('footer').css({'position':'absolute', 'bottom':0, 'left':0});	
	}
	*/
	
	//SCROLLING MENU
	/*var awardH = $('#Awards li').size();
	if( awardH < 14 ){
		$('.scrollup, .scrolldown').hide();	
	}else{
		
		var mouseisdown = false;
	
		$('.up').mousedown(function(event) {
			mouseisdown = true;
			ScrollUp();
		}).mouseup(function(event) {
			mouseisdown = false;
		});
		  
		 $('.dn').mousedown(function(event) {
			mouseisdown = true;
			ScrollDown();
		}).mouseup(function(event) {
			mouseisdown = false;
		}); 
		  
		$(document).mousedown(function(event) { //This is to make sure the event stops if the mouse is no longer over the control
		}).mouseup(function(event) {
			mouseisdown = false;
		});
	}
	
	function ScrollUp(){
	
	  //var topVal = $('.up').parents(".container").find(".content").css("top").replace(/[^-\d\.]/g, ''); //Use this for multiple boxes
	  var topVal = $(".content").css("top").replace(/[^-\d\.]/g, ''); //remove the px from the current top val
		topVal = parseInt(topVal);
	  console.log(topVal);
	  if(topVal < 0){ //This is to stop it when it reaches the top
	  $('.up').addClass('disable').parents(".container").find(".content").stop().animate({"top":topVal + 50  + 'px'},'slow');
		if (mouseisdown)
	setTimeout(ScrollUp, 400);  
	  }
	}
	
	
	function ScrollDown(){
	 
	  //var topVal = $('.up').parents(".container").find(".content").css("top").replace(/[^-\d\.]/g, ''); 
	  var topVal = $(".content").css("top").replace(/[^-\d\.]/g, '');
		topVal = parseInt(topVal);
	  console.log($(".content").height()+ " " + topVal);
	  if(Math.abs(topVal) < ($(".content").height() - $(".container").height() + 10)){ //This is to limit the bottom of the scrolling - add extra to compensate for issues
	  $('.up').parents(".container").find(".content").stop().animate({"top":topVal - 50  + 'px'},'slow');
		if (mouseisdown)
	setTimeout(ScrollDown, 400);
	  }
	
	}
	
	$('.container').css('overflow', 'hidden');/*.mousewheel(function(event, delta, deltaX, deltaY) {
		console.log('bla'+ delta, deltaX, deltaY);
	  
	  if(delta < 0){ //Scroll down
		  var topVal = $('.up').parents(".container").find(".content").css("top").replace(/[^-\d\.]/g, '');
		console.log('lol');
		topVal = parseInt(topVal);
		$('.up').parents(".container").find(".content").stop().animate({"top":topVal + (deltaY * 10)  + 'px'},'slow');
	  }
		if(delta > 0){ //Scroll up
		  var topVal = $('.up').parents(".container").find(".content").css("top").replace(/[^-\d\.]/g, '');
		topVal = parseInt(topVal);
		$('.up').parents(".container").find(".content").stop().animate({"top":topVal - (-deltaY * 10)  + 'px'},'slow');
	  }
		
	});*/
	
	/*Award Portfolio
	function AwardsFolio(awardID){
		$(awardID+" .thumbs").click(function() {
			var image = $(this).attr("href");
			$(awardID+' .main').hide();
			$(awardID+' .main').fadeIn('slow');
			$(awardID+' .main').html('<img src="' + image + '"/>');
			return false;
		});
	}
	AwardsFolio('#Award1');
	AwardsFolio('#Award2');*/
 
});
