/*	
/	Site-wide jQuery UI 
/	scripting and helper methods
/	by The Atom Group
----------------------------------*/
(function ($) {
	if ($('.column-container').length > 0) {
		var tallest = 0,
			group = $('.column-container > div'),
			height;
		group.each(function () {
			height = $(this).height();
			if (height > tallest) tallest = height;
		});
		group.css('height', tallest + 'px');
	}

	if ($('.project-images-wrapper').length > 0) {
		$(window).load(function () {
			var images = $('.project-images img');
			if (images.length > 1) images.parents('.project-images').parents('.project-images-wrapper').find('.prev, .next').css('visibility', 'visible');
			$('.project-images img').each(function () {
				if ($(this).height() < 533) {
					var halfHeight = ($(this).height() / 2);
					$(this).css({
						'top': '50%',
						'margin-top': -(halfHeight) + 'px'
					});
				}
				if ($(this).width() < 710) {
					var halfWidth = ($(this).width() / 2);
					$(this).css({
						'left': '50%',
						'margin-left': -(halfWidth) + 'px'
					});
				}
			}).css('visibility', 'visible');
		});
	}

	if ($('.marquee img').length > 0) {
		setInterval(function () {
			var $active = $('.marquee img.active');
			if ($active.length == 0) $active = $('.marquee img:last');
			var $next = $active.next().length ? $active.next() : $('.marquee img:first');

//			var $sibs = $active.siblings(),
//                        rndNum = Math.floor(Math.random() * $sibs.length),
//                        $next = $($sibs[rndNum]);

			$active.addClass('last-active');

			$next.css({ opacity: 0.0 })
				.addClass('active')
				.animate({ opacity: 1.0 }, 2000, function () {
					$active.removeClass('active last-active');
				});
		}, 5000);

	}

	if ($('.post').length) {
		$('.post-content a img').parents('a').fancybox();

	}
})(jQuery);


// Slider Scripting
var slider = function initSlider() {
	//scrollpane parts
	var scrollPane = jQuery('.nav-wrapper');
	var scrollContent = jQuery('.inner');
	var scrollItems = jQuery('.image-nav div.card a');

	//build slider
	var count = 0;
	scrollItems.each(function () {
		if (jQuery(this).hasClass('current')) return false;
		count++;
	});

	var val = 0;
	if (count == 0) val = (count * 118) / scrollContent.width() * 100;
	else if (count == (jQuery('div.card').length - 1)) val = 100;
	else if (count > 20) val = ((count * 118) + 118) / scrollContent.width() * 100;
	else val = ((count * 118) + 59) / scrollContent.width() * 100;

	var scrollbar = jQuery(".scroll-bar").slider({
		value: val,
		slide: function (e, ui) {
			if (scrollContent.width() > scrollPane.width()) { scrollContent.css('margin-left', Math.round(ui.value / 100 * (scrollPane.width() - scrollContent.width())) + 'px'); }
			else { scrollContent.css('margin-left', 0); }
		}
	});
	scrollContent.css('margin-left', Math.round(val / 100 * (scrollPane.width() - scrollContent.width())) + 'px');

	//append icon to handle
	var handleHelper = scrollbar.find('.ui-slider-handle')
		.mousedown(function () {
			scrollbar.width(handleHelper.width());
		})
		.mouseup(function () {
			scrollbar.width('100%');
		})
		.append('<span class="nav-handle"></span>')
		.wrap('<div class="ui-handle-helper-parent"></div>').parent();

	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - (proportion * scrollPane.width());
		scrollbar.css({
			width: handleSize,
			'margin-left': -handleSize / 2
		});
	}

	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
		var percentage = Math.round(leftVal / remainder * 100);
		scrollbar.slider("value", percentage);
	}
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
		var showing = scrollContent.width() + parseInt(scrollContent.css('margin-left'));
		var gap = scrollPane.width() - showing;
		if (gap > 0) {
			scrollContent.css('margin-left', parseInt(scrollContent.css('margin-left')) + gap);
		}
	}

	//change handle position on window resize
	jQuery(window)
		.resize(function () {
			resetValue();
			reflowContent();
		});
	//init scrollbar size
	//setTimeout(sizeScrollbar, 10); //safari wants a timeout
};

// init slider
(function ($) {
	var imageNav = $('.image-nav');
	if (imageNav.find('img').length >= 6) {
		var wrap = imageNav.find('.nav-wrapper .inner'),
			cards = imageNav.find('.nav-wrapper .card'),
			newWidth = 0;
		cards.each(function () { 
			newWidth += ($(this).width() + 3);
		});
		wrap.css({
			'width': newWidth + 'px',
			'float': 'left'
		});
		var newSlider = new slider();
	}
	else { imageNav.addClass('no-slider'); }
})(jQuery);

/* Gallery */
jQuery.fn.gallery = function (_options) {
	// defaults options	
	var _options = jQuery.extend({
		duration: 600,
		autoSlide: false,
		slideElement: 1,
		effect: false,
		fadeEl: 'ul',
		switcher: 'ul > li',
		disableBtn: false,
		next: 'a.link-next, a.btn-next, a.next',
		prev: 'a.link-prev, a.btn-prev, a.prev',
		circle: true
	}, _options);

	return this.each(function () {
		var _hold = jQuery(this);
		if (!_options.effect) var _speed = _options.duration;
		else var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _sliderEl = _options.slideElement;
		var _wrap = _hold.find(_options.fadeEl);
		var _el = _hold.find(_options.switcher);
		var _next = _hold.find(_options.next);
		var _prev = _hold.find(_options.prev);
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth(true);
		var _wrapHolderW = Math.ceil(_wrap.parent().width() / _w);
		if (((_wrapHolderW - 1) * _w + _w / 2) > _wrap.parent().width()) _wrapHolderW--;
		if (_timer) var _t;
		var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		if (!_options.effect) var rew = _count - _wrapHolderW + 1;
		else var rew = _count;

		if (!_options.effect) _wrap.css({ marginLeft: -(_w * _active) });
		else {
			_wrap.css({ opacity: 0 }).removeClass('active').eq(_active).addClass('active').css({ opacity: 1 }).css('opacity', 'auto');
			_el.removeClass('active').eq(_active).addClass('active');
		}
		if (_options.disableBtn) {
			if (_count < _wrapHolderW) _next.addClass(_options.disableBtn);
			_prev.addClass(_options.disableBtn);
		}

		function fadeElement() {
			_wrap.eq(_last).animate({ opacity: 0 }, { queue: false, duration: _speed });
			_wrap.removeClass('active').eq(_active).addClass('active').animate({
				opacity: 1
			}, { queue: false, duration: _speed, complete: function () {
				jQuery(this).css('opacity', 'auto');
			}
			});
			_el.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		function scrollEl() {
			_wrap.animate({ marginLeft: -(_w * _active) }, { queue: false, duration: _speed });
		}
		function toPrepare() {
			if ((_active == rew) && _options.circle) _active = -_sliderEl;
			for (var i = 0; i < _sliderEl; i++) {
				_active++;
				if (_active > rew) {
					_active--;
					if (_options.disableBtn && (_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
				}
			};
			if (_active == rew) if (_options.disableBtn && (_count > _wrapHolderW)) _next.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
		}
		function runTimer() {
			_t = setInterval(function () {
				toPrepare();
			}, _timer);
		}
		_next.click(function () {
			if (_t) clearTimeout(_t);
			if (_options.disableBtn && (_count > _wrapHolderW)) _prev.removeClass(_options.disableBtn);
			toPrepare();
			return false;
		});
		_prev.click(function () {
			if (_t) clearTimeout(_t);
			if (_options.disableBtn && (_count > _wrapHolderW)) _next.removeClass(_options.disableBtn);
			if ((_active == 0) && _options.circle) _active = rew + _sliderEl;
			for (var i = 0; i < _sliderEl; i++) {
				_active--;
				if (_active < 0) {
					_active++;
					if (_options.disableBtn && (_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
				}
			};
			if (_active == 0) if (_options.disableBtn && (_count > _wrapHolderW)) _prev.addClass(_options.disableBtn);
			if (!_options.effect) scrollEl();
			else fadeElement();
			return false;
		});
		if (_options.effect) _el.click(function () {
			_active = _el.index(jQuery(this));
			if (_t) clearTimeout(_t);
			fadeElement();
			return false;
		});
		if (_timer) runTimer();
	});
}

jQuery(document).ready(function () {
	jQuery('div.project-images-wrapper').gallery({
		duration: 1200,
		effect: 'fade',
		fadeEl: 'div.project-images > img',
		next: 'span.next',
		prev: 'span.prev',
		switcher: 'div.project-images > img'
	});
});

