function initCufon() {
	Cufon.replace('.menu ul li a', { fontFamily: 'avenir-book', hover: true });
	Cufon.replace('.gallery .text span', { fontFamily: 'avenir-book'});
	Cufon.replace('.gallery .text a', { fontFamily: 'avenir-book', hover: true });
	Cufon.replace('.mini-container .img-text span', { fontFamily: 'avenir-book'});
	Cufon.replace('.mini-container .img-text a', { fontFamily: 'avenir-book', hover: true });
	Cufon.replace('.link-box a', { fontFamily: 'avenir-book'});
	Cufon.replace('.wp-pagenavi .current', { fontFamily: 'avenir-book'});
	Cufon.replace('.wp-pagenavi .extend', { fontFamily: 'avenir-book'});
	Cufon.replace('.sidebar h2', { fontFamily: 'avenir-book'});
	Cufon.replace('.sidebar-nav ul li a', { fontFamily: 'avenir-book'});
	Cufon.replace('.box-post .title h3', { fontFamily: 'avenir-light'});
	Cufon.replace('.title-image ul li span', { fontFamily: 'avenir-book'});
	Cufon.replace('.title-image ul li a', { fontFamily: 'avenir-book'});
	Cufon.replace('.title-posted span', { fontFamily: 'avenir-book'});
	Cufon.replace('.box-button a', { fontFamily: 'avenir-book'});
	Cufon.replace('.comment-form .title h2', { fontFamily: 'avenir-book'});
	Cufon.replace('.posted span', { fontFamily: 'avenir-book'});
	Cufon.replace('.box-time span', { fontFamily: 'avenir-book'});
}

function initGallery() {
	jQuery('div.gallery').gallSlide({
		duration: 1000,
		autoSlide: 5000
	});
}

jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = jQuery(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('div.posts-wrapper');
		var _el = _hold.find('div.post');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		var _btn = jQuery('div.scroller').find('a');
		_btn.removeClass('active');
		_btn.eq(_active).addClass('active');
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			_btn.removeClass('active');
			_btn.eq(_active).addClass('active');
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();
		
		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
		_btn.click(function(){
			_active = _btn.index(jQuery(this));
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});
		_next.mouseenter(function(){
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			runTimer()
		});
		_prev.mouseenter(function(){
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			runTimer()
		});
		_wrap.mouseenter(function(){
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			runTimer()
		});
	});
}


jQuery(document).ready(function(){
	initCufon();
	initGallery();
	initLightbox();
});


function initLightbox() {
	// defaults options
	var _fadeSpeed = 500;
	var _linksSelector = 'a[rel*="lightbox"], a:has(img.small, img.medium)';
	var _lightboxID = '#simplelightbox';
	var _imageHolderID = '#simplelightbox-image';
	var _titleTextID = '#simplelightbox-title';
	var _frameHolder = '.png-box';
	var _navigationBar = '.nav';
	var _faderBackground = '#212020';
	var _closeLink = 'a.close';
	var _btnPrev = 'a.prev';
	var _btnNext = 'a.next';
	var _faderOpacity = 0.95;
	var _centerPosition = true;
	var _circleSlide = true;
	var _lightboxHTML = ' \
		<div id="simplelightbox" class="popup" style="display:none;"> \
			<div class="load-wrapper"> \
				<div class="png-box"> \
					<div id="simplelightbox-image" class="box-content"> \
					</div> \
				</div> \
				<a href="#" class="prev">Prev</a> \
				<a href="#" class="next">Next</a> \
			</div> \
			<div class="nav"> \
				<strong id="simplelightbox-title"></strong> \
				<a href="#" class="close">close</a> \
			</div> \
		</div>';

	// lightbox code
	var _isIE6 = (jQuery.browser.msie && jQuery.browser.version == '6.0' ? true : false);
	var _lightbox = jQuery(_lightboxHTML);
	_btnPrev = _lightbox.find(_btnPrev);
	_btnNext = _lightbox.find(_btnNext);

	if (!jQuery('div.lightbox-fader').length) _fader = jQuery('body').append('<div class="lightbox-fader"></div>');
	_fader = jQuery('div.lightbox-fader');
	jQuery('body').append(_lightbox);
	_imageHolderID = jQuery(_imageHolderID);
	_titleTextID = jQuery(_titleTextID);
	_frameHolder = _lightbox.find(_frameHolder);
	_navigationBar = _lightbox.find(_navigationBar).hide();

	if (_isIE6) {
		if (!jQuery('iframe.ie-fader').length) {
			_ieFader = jQuery('body').append('<iframe class="ie-fader" src="about:blank" width="10" height="10" scrolling="no" frameborder="0"></iframe>');
			_ieFader = jQuery('iframe.ie-fader')
			_ieFader.css({
				position:'absolute',
				visibility:'visible',
				display:'none',
				opacity:0,
				top:0,
				left:0
			});
		}
	}

	_lightbox.css({
		'zIndex':999,
		'visibility':'visible',
		'display':'none'
	});
	_fader.css({
		opacity:_faderOpacity,
		backgroundColor:_faderBackground,
		position:'absolute',
		visibility:'visible',
		display:'none',
		top:0,
		left:0,
		zIndex:998,
		textIndent: -9999
	}).text('&nbsp;');

	// lightbox opener code
	var _currentGroup;
	var _currentIndex;
	var _itemsCount;

	jQuery(_linksSelector).click(function(){
		var _opener = jQuery(this);
		_currentGroup = jQuery('a[rel="'+_opener.attr('rel')+'"]');
		_currentIndex = _currentGroup.index(jQuery(this));
		_itemsCount = _currentGroup.length;

		if(_opener.attr('rel').length == 0) {
			_itemsCount=0;
		}

		if(_itemsCount < 2){
			_btnPrev.css({display:'none'});
			_btnNext.css({display:'none'});
		} else {
			_btnPrev.css({display:'block'});
			_btnNext.css({display:'block'});
		}

		_fader.fadeIn(_fadeSpeed, function(){
			positionLightbox();
			var _img = new Image();
			_img.onload = function(){
				_imageHolderID.html('');
				_imageHolderID.append(_img);

				if(jQuery.browser.msie)
				setTimeout(function(){
					jQuery(_lightbox).css({width:jQuery(_img).width()+20});
				}, 50);

				var _height = jQuery(window).height();
				var _thisHeight = _lightbox.height();
				if(_thisHeight>_height) window.scroll(0,0);

				positionLightbox();
				_lightbox.fadeIn(_fadeSpeed,function(){
					_navigationBar.slideDown(_fadeSpeed);
					positionLightbox();
				});
				if (_isIE6) _ieFader.show();
			};
			_img.src = _opener.attr('href');
			_img.alt = '';

			var _title = _opener.find('img').attr('title');
			if(_title.length) _titleTextID.html(_title);
		});
		positionLightbox();
		return false;
	});

	// lightbox navigation
	_btnPrev.click(function(){
		prevSlide();
		return false;
	});
	_btnNext.click(function(){
		nextSlide();
		return false;
	});

	function nextSlide() {
		if(_currentIndex < _itemsCount-1) {
			_currentIndex++;
			switchSlide();
		} else if(_circleSlide) {
			_currentIndex = 0;
			switchSlide();
		}
	}
	function prevSlide() {
		if(_currentIndex > 0) {
			_currentIndex--;
			switchSlide();
		} else if(_circleSlide) {
			_currentIndex = _itemsCount-1;
			switchSlide();
		}
	}
	function switchSlide() {
		_frameHolder.animate({opacity:0},{duration:_fadeSpeed,queue:true,complete:function(){
			if(jQuery.browser.msie) _frameHolder.css({opacity:'auto'});
			var _img = new Image();
			_img.onload = function(){
				_imageHolderID.html('');
				_imageHolderID.append(_img);

				if(jQuery.browser.msie)
				setTimeout(function(){
					jQuery(_lightbox).css({width:jQuery(_img).width()+20});
				}, 50);

				var _height = jQuery(window).height();
				var _thisHeight = _lightbox.height();
				if(_thisHeight>_height) window.scroll(0,0);

				positionLightbox();
				_frameHolder.animate({opacity:1},{duration:_fadeSpeed,queue:true,complete:function(){
					if(jQuery.browser.msie) _frameHolder.css({opacity:'auto'});
					positionLightbox();
				}});
			};
			var _title = _currentGroup.eq(_currentIndex).find('img').attr('title');
			if(_title && _title.length) _titleTextID.html(_title);

			_img.src = _currentGroup.eq(_currentIndex).attr('href');
			_img.alt = '';
		}});
	}

	// misc event listeners
	_lightbox.find(_closeLink).click(function(){
		hideLightbox();
		return false;
	});
	_fader.click(function(){
		hideLightbox();
		return false;
	});
	jQuery(document).keydown(function (e) {
		if (!e) evt = window.event;
		if (e.keyCode == 27) {
			hideLightbox();
		}
	});

	// lightbox hide function
	function hideLightbox() {
		_lightbox.fadeOut(_fadeSpeed, function(){
			_fader.fadeOut(_fadeSpeed);
			_navigationBar.hide();
			if (_isIE6) _ieFader.hide();
		});
	}

	// lightbox positioning function
	function positionLightbox() {
		var _minWidth = jQuery('#main').outerWidth();
		var _height = jQuery(window).height();
		var _width = jQuery(window).width();
		var _thisHeight = _lightbox.outerHeight();
		var _page = jQuery('body');

		if (_lightbox.length) {
			if (_height > _page.innerHeight()) _fader.css('height',_height);
			else _fader.css('height',_page.innerHeight());

			if (_width < _minWidth) _fader.css('width',_minWidth);
			else _fader.css('width','100%');

			if (_height > _thisHeight) {
				if (!window.innerHeight) {
					_lightbox.css({
						position:'absolute',
						top: (document.documentElement.scrollTop + (_height - _thisHeight) / 2)+"px"
					});
				} else {
					_lightbox.css({
						position:'fixed',
						top: ((_height - _lightbox.outerHeight()) / 2)+"px"
					});
				}
			}
			else {
				_lightbox.css({
					position:'absolute',
					top: 0
				});
			}
			if (_width > _lightbox.outerWidth()) _lightbox.css({left:(_width - _lightbox.outerWidth()) / 2 + "px"});
			else _lightbox.css({position:'absolute',left: 0});

			if (_isIE6) {
				_ieFader.css({
					height:_fader.height(),
					width:_fader.width()
				});
			}
		}
	}

	jQuery(window).scroll(function(){
		positionLightbox();
	});
	jQuery(window).resize(function(){
		positionLightbox();
	});
}