$('html').addClass('hasjs').addClass('loading');

$(document).ready(function() {
	//setup initial javascript settings
	setup(650);
	
	$(window).load(function(){
		$('html').removeClass('loading');
	});
	
		
	//nav accordions
	$('#mnav li ul:not(.open)').slideUp(0);
	$('#mnav li a:not(#mnav li li a)').click(function(){
		if( $(this).parent('li').children('ul').is(':visible') ) {
				$(this).parent('li').children('ul').slideUp(500);
		} else {
				$('#mnav li ul:visible').slideUp(500);
				$(this).parent('li').children('ul').slideDown(500);
		}
		return false;
	});
	//end nav accordions
	
	//scroll based on where you're clicking
	$('#mnav li li a, #logo a, .related a, .homenav a, .main .content a, .contact').live('click', function(){
		var oghref = $(this).attr('href').split('#');
		
		var href = '#' + oghref[1];
		scrollIt(href, 800);
		
		if($(this).parents('div').hasClass('related') || $(this).html() == "projects »"){
				$('#mnav li:not(:eq(2)) ul').slideUp(500);
				$('#mnav li:last-child ul').slideDown(500);
		}
		
		if($(this).parents('div').hasClass('related') || $(this).html() == "news »"){
				$('#mnav li:not(:eq(2)) ul').slideUp(500);
				$('#mnav li:last-child ul').slideDown(500);
		}
		
		if($(this).html() == "services »"){
				$('#mnav li:not(:eq(1)) ul').slideUp(500);
				$('#mnav li:nth-child(2) ul').slideDown(500);
		}
		
		if($(this).html() == "history »" || $(this).html() == "contact us »"){
				$('#mnav li:not(:eq(0)) ul').slideUp(500);
				$('#mnav li:nth-child(1) ul').slideDown(500);
		}
		
		
		return false;
	});
	
	//Principals navigation
	$('#principals li a ').click(function(){
		var href = $(this).attr('href');
		showHidePrincipals(href, $(this));
		return false;
	});
	
	//reset padding on resoze
	$(window).resize(function(){
		setWindow($('#panelwrapper'));
	});
	
	//add next buttons and functions
	addNextButtons();
	addPrevButtons();
	$('.panel').hover(function(){
		$(this).find('.next, .prev').show();
	}, function(){
		$(this).find('.next, .prev').hide();
	});
	$('.nextbutton').live('click', function(){
		var href= '#' + $(this).parents('.panel').next().attr('id');
		scrollIt(href, 800);
		return false;
	});
	$('.prevbutton').live('click', function(){
		var href= '#' + $(this).parents('.panel').prev().attr('id');
		scrollIt(href, 800);
		return false;
	});

		
});

//functions
function setup(panelWidth) {

	$('#rap').append('<div id="leftSide"></div><div id="rightSide"></div>');
	setWindow($('#panelwrapper'));
	
	
	//check for url hash on page load
	if(window.location.hash == ''){
		$('#panelwrapper').scrollTo($('#experience'), 0);
		//$('.panel:not(#experience), .prepanel, .postpanel').fadeTo(0, .3);
		href="#experience";
		scrollIt(href, 0);
	} else {
		var href = window.location.hash.replace('_', '');
		$('#mnav li li a').each(function(){
			if($(this).attr('href') == href){
				$(this).parent('li').parent('ul').addClass('open').slideDown(0);
			}
		})
		scrollIt(href, 0);
	}
	
	//check if we need scrollbars
	getScrollbar($('#principals .desc'), 161);
	getScrollbar($('.services .desc, .about .desc:not(#principals .desc)'), 360);
	getScrollbar($('.projects .desc, .news .desc, .news .wide-desc'), 350);
	getScrollbar($('#history .desc'), 400);
	getScrollbar($('#history.desc'), 400);

	
	//set the first principal to show
	showHidePrincipals('#principals div:eq(0)', $('#principals li:first-child a'));
}

//scrolls to panel based on href sent
function scrollIt(href, speed) {
	var w = $('body').data('padding')
	$('#panelwrapper').scrollTo($(''+href+''), speed, {offset: {top: 0, left: -w}, easing:'easeOutCirc'});
	setNav(href);
}


//sets navigation classes on page load and scroll
function setNav(href) {
	$('#mnav .selected').removeClass('selected');
	$('.homenav .selected').removeClass('selected');


	$('.homenav a').each(function(){
		if( $(this).attr('href') == href ) {
			$(this).addClass('selected');
		}
	});
	
	$('#mnav li li a').each(function(){
		if( $(this).attr('href') == href ) {
			$(this).parents('li').addClass('selected');
		}
	});

}

function showHidePrincipals(href, that) {
	$('#principals .selected').removeClass('selected');
	that.parent().addClass('selected');
	//use the index of the selected li to position the triangle under it
	index = that.parent().prevAll().length;
	var triPosition = (index*139) + 6;
	$('#principals ul').css('background-position', triPosition + 'px bottom');
	$('#principals .principal:not('+ href +')').hide(0);
	$(href).show(0);
}

//this function sets the element to the window dimensions
function setWindow(container) {
	var ww = $('#rap').width();
	//alert(ww);
	var wh = $('#rap').height();
	var pad = ((ww - 840)/2) + 190;
	if (document.all && !window.opera && !window.XMLHttpRequest) {
		//IE6 stuff
		
	}
	$('#rap').css('width', ww + 'px');
	$('body').data('padding', pad);
	container.css('padding-left', pad);
	//console.log(ww);
	var sides = (ww - 840)/2;
	$('#rightSide, #leftSide').css({'width': sides + 'px', 'height' : wh + 'px'});
	}
	
function addNextButtons(){
	//preload buttons
	jQuery.preLoadImages("/wp-content/themes/kdg/i/b/next.png");
	$('<div class="next"><a class="nextbutton" href="#next"><img src="/wp-content/themes/kdg/i/b/next.png"/></a><div>').appendTo('.panel:not(.main.panel, .projects:last, .news:last, .about:last, .services:last)').hide();
}

function addPrevButtons(){
	//preload buttons
	jQuery.preLoadImages("/wp-content/themes/kdg/i/b/prev.png");
	$('<div class="prev"><a class="prevbutton" href="#prev"><img src="/wp-content/themes/kdg/i/b/prev.png"/></a><div>').appendTo('.panel:not(.main.panel, .projects:first, .news:first,  .about:first, .services:first)').hide();
}

function getScrollbar(element, maxHeight) {
	element.each(function(){
		//set last element to have 0 margin just in case the margin pushes it over the scroll limit
		$(this).children(':last').css('margin-bottom', '0px');
		
		//check and add scrollbars
		if( $(this).height() > maxHeight ){
			$(this).wrap('<div class="scroller">').parent().append('<div class="slidewrap"><div class="slider"></div></div>');
			
			//set up variables for the slider
			var containHeight = maxHeight;
			var fullHeight = $(this).height();
			var difference = containHeight - fullHeight;
			var proportion = containHeight/fullHeight;		//get difference between container and inner heights
			//console.log(containHeight);
			//console.log(fullHeight);
			
			$(this).parents('.scroller').find('.slider').slider({ 
				orientation: 'vertical',
				range: 'max',
				min: 0,
				max: 100,
				value: 100,
				slide: function(event, ui) {
					$(this).parents('.slidewrap').siblings('.desc').css('top' , (-( -difference - ((ui.value/100) * -difference))) );
				}	 
			});
			
			var sliderH = containHeight * proportion;
			$(this).parents('.scroller').find('.ui-slider-handle').css('height', sliderH).css('margin-bottom', -sliderH/2);
			$(this).parents('.scroller').find('.slider').css('height', (containHeight-sliderH)).css('margin-top', sliderH/2);	
		}
	});
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
