function rotator() {
	var rotator = $('#main-page #rotator'), //Caching Objects
		rotatorstories = $('#rotator-stories .item', rotator),
		rotatornavitems = $('#rotator-nav .item', rotator);
	rotatornavitems.height((rotator.height()-$('#rotator-nav .more', rotator).outerHeight())/rotatornavitems.length-(rotatornavitems.outerHeight()-rotatornavitems.height()));
	
	t = setTimeout("rotator_rotate()",5000);
	
	rotatornavitems.each(function(index){
		$(this).click(function(){
			clearTimeout(t);
			$(this).addClass('active').siblings().removeClass('active');
			rotatorstories.filter('.active').removeClass('active').hide();
			rotatorstories.eq(index).addClass('active').show();
			t = setTimeout("rotator_rotate()",5000);
		});
	});
	rotatornavitems.eq(0).click();
	
}

function rotator_rotate() {
	rotatornavitems = $('#main-page #rotator #rotator-nav .item');
	if(rotatornavitems.filter('.active').next('.item').length){
		rotatornavitems.filter('.active').next('.item').click();
	}else{
		rotatornavitems.eq(0).click();
	}
	t = setTimeout("rotator_rotate(rotatornavitems)",5000);
}

function sidenav() {
	var sidenav = $('#side-bar #side-nav'),
	sidenavitems = $('ul .page_item', sidenav);
	sidenavitems.each(function(index){
		if($(this).children('.children').length){
			$('a:first',this).before('<div class="expand left"></div>');
			$('.expand', this).click(function(){
				if($(this).hasClass('active')){
					$(this).removeClass('active').siblings('.children').fadeOut();
				}else{
					$(this).addClass('active').siblings('.children').fadeIn();
				}
			});
		}
	});
	if($('#side-bar #side-nav .page_item.current_page_item').length){
		$('#side-bar #side-nav .page_item.current_page_item').children('.expand').click();
		$('#side-bar #side-nav .page_item.current_page_item').parent('.children').siblings('.expand').click();
	}
}

function galleryoverlay() {
	var gallery = $('#main-page .content #gallery.info'),
	galleryitems = $('.gallery-rotator li', gallery),
	gallerythumbnails = $('.gallery-thumbnail-overlay', gallery),
	galleryrotationcontrols = $('.control-arrow', gallery).parent(),
	gallerycontrols = $('.gallery-rotator-controls', gallery);

	galleryitems.parent().width(galleryitems.length*galleryitems.width());
	
	galleryitems.click(function(){
		gallery_slideshow_stop();
	});
	
	galleryrotationcontrols.eq(0).click(function(){
		if($(galleryitems).filter('.active').prev().length) {
			$(galleryitems).filter('.active').removeClass('active').prev().addClass('active');
			$('.counter .current-item', gallerycontrols).text($(galleryitems).filter('.active').index()+1);	
		}else{
			$(galleryitems).filter('.active').removeClass('active').parent().children().eq(galleryitems.length-1).addClass('active');
			$('.counter .current-item', gallerycontrols).text(galleryitems.length);
		}
	});
	
	galleryrotationcontrols.eq(1).click(function(){
		if($(galleryitems).filter('.active').next().length) {
			$(galleryitems).filter('.active').removeClass('active').next().addClass('active');
			$('.counter .current-item', gallerycontrols).text($(galleryitems).filter('.active').index()+1);
		}else{
			$(galleryitems).filter('.active').removeClass('active').parent().children().eq(0).addClass('active');
			$('.counter .current-item', gallerycontrols).text(1);
		}	
	});
	
	$('.thumbnails', gallerycontrols).click(function(){
		gallerythumbnails.show();
		gallery_slideshow_stop();
		$('.play', gallerycontrols).show().siblings('.pause').hide();
	});
	
	$('.play', gallerycontrols).click(function(){
		$(this).hide().siblings('.pause').show();
		t = setTimeout("gallery_slideshow()",5000);
	});
	
	$('.pause', gallerycontrols).click(function(){
		$(this).hide().siblings('.play').show();
		gallery_slideshow_stop();
	});
	
	$('li', gallerythumbnails).each(function(index){
		$(this).click(function(){
			$(galleryitems).removeClass('active').eq(index).addClass('active');
			$('.counter .current-item', gallerycontrols).text(index+1);
			gallerythumbnails.hide();
		});	
	});
	
	$('.popout', gallerycontrols).click(function(){
		if(!$('body #gallery-popout').length){
			$('body').append('<div id="gallery-popout-overlay"></div><div id="gallery-popout"><div class="gallery-popout-close"></div><div class="gallery-popout-image"><img src=""/></div></div>');
		}else{
			$('body #gallery-popout').show();	
		};
		$('body #gallery-popout-overlay').show();
		$('body #gallery-popout .gallery-popout-image img').attr('src',$(galleryitems).filter('.active').children('img').attr('src'));
		$('body #gallery-popout').css({
			'width' : $('body #gallery-popout .gallery-popout-image img').width(),
			'height' : $('body #gallery-popout .gallery-popout-image img').height(),
			'marginLeft' : '-'+($('body #gallery-popout .gallery-popout-image img').width()/2)+'px',
			'marginTop' : '-'+($('body #gallery-popout .gallery-popout-image img').height()/2)+'px' ,
		
		});
		$('body #gallery-popout-overlay').click(function(){
			$(this).hide();
			$('body #gallery-popout').hide();
		});
		gallery_slideshow_stop();
	});
}

function gallery_slideshow() {
	$('.control-arrow', gallery).parent().eq(1).click();
	t = setTimeout("gallery_slideshow()",5000);
}

function gallery_slideshow_stop() {
	if(t){
		clearTimeout(t);
	}
}

function fontreplace() {
	var mainmenulinks = $('#main-header #main-nav ul .page_item a'),
	sidenavitemslinks = $('#side-bar #side-nav ul .page_item a');
	rotator = $('#main-page #rotator');
	
	Cufon.set('fontFamily', 'akzidengrotesk').replace(mainmenulinks)(sidenavitemslinks)($('#main-page aside.info a'));
    Cufon.set('fontFamily', 'akzidengroteskce').replace($('#rotator-stories .item .info a',rotator))($('#rotator-nav .item',rotator))('h2,h3,h4')($('#rotator-stories .item .info a',rotator));
    
    sidenavitemslinks.parent().not('.current_page_item').children('a').hover(function(){
		$(this).css('color','#F89B24');
		Cufon.replace($(this), { 
			fontFamily: 'akzidengrotesk' ,
			color: '#F89B24'
		});
	},function(){
		Cufon.replace($(this), { 
			fontFamily: 'akzidengrotesk' ,
			color: '#074668'
		});
	});    
}

function menusubchildren() {
	var mainmenusubchildren = $('#main-header #main-nav ul .page_item .children .children');
	 mainmenusubchildren.parent().hover(function(){
	 	$(this).children('.children').css('left', $(this).outerWidth()+'px').show();
	 },function(){
	 	$(this).children('.children').hide();	
	 });
}

$(document).ready(function(){
	if($('#main-page #rotator #rotator-stories').length){
		rotator();
	}
	if($('#side-bar #side-nav').length){
		sidenav();
	}
	if($('#main-page .content #gallery.info').length){
		galleryoverlay();
	}	
	$('#main-page .facebook .wall').fbWall({
		id:'ImpactNWsince1966',
		accessToken:'206158599425293|259670c18806606c8cdb6349.1-5140349|SHFzfimKdrietIhWZhl5Y_Kff7Y',
		showGuestEntries:true,
		showComments:true,
		max:1,
		timeConversion:24
	});
	menusubchildren();
	fontreplace();
});


