
// redirect to Flash version if Flash Player 9+ is available
if ( swfobject.hasFlashPlayerVersion('9') && window.location.href.indexOf('client-login') == -1 ) {
	window.location.href = './flash/';
}


$(function(){

	$('A[rel="external"]').attr('target', '_blank');
	
	
	$('.services-list .title').hover(function(){
		$(this).next().show(0);
	}, function(){
		$(this).next().hide(0);
	}).click(function(e){
		e.preventDefault();
		$('.services-list .details').hide(0);
		$(this).next().show(0);
	});
	
	
	var newsHeightTotal = $('.news').height();
	var newsHeightItems = 0;
	$('.news DIV').click(function(){
		var href = $(this).attr('rel');
		window.open(href, '_blank');
	}).find('A').click(function(e){
		e.stopPropagation();
	}).end().each(function(){
		var $this = $(this);
		$this.data('height', $this.height() + 20)
			.hide(0)
			.css({
				'visibility'	: 'visible',
				'height'		: '0px',
				'paddingBottom'	: '0px',
				'opacity'		: 0
			});
		this.className = 'older';
	});
	
	initNews($('.news DIV:first-child'));
	
	$('.news-older').click(function(e){
		e.preventDefault();
		
		var older = $('.news DIV.older:first');
		
		$('.news-newer').show(0);
		if ( older.next().size() == 0 ) {
			$(this).hide(0);
		}
		
		if ( older.size() ) {
			var olderHeight = older.data('height');
			newsHeightItems += olderHeight;
			
			while ( newsHeightItems > newsHeightTotal ) {
				var newer = $('.news DIV.newer:last').next();
				if (newer.size() == 0) {
					newer = $('.news DIV:first-child');
				}
				newsHeightItems -= newer.data('height');
				hideNewsItem(newer, 'newer');
			}
			
			showNewsItem(older);
		}
	});
	
	$('.news-newer').click(function(e){
		e.preventDefault();
		
		var newer = $('.news DIV.newer:last');
		
		$('.news-older').show(0);
		if ( newer.prev().size() == 0 ) {
			$(this).hide(0);
		}
		
		if ( newer.size() ) {
			var newerHeight = newer.data('height');
			newsHeightItems += newerHeight;
			
			while ( newsHeightItems > newsHeightTotal ) {
				var older = $('.news DIV.older:first').prev();
				if (older.size() == 0) {
					older = $('.news DIV:last-child');
				}
				newsHeightItems -= older.data('height');
				hideNewsItem(older, 'older');
			}
			
			showNewsItem(newer);
		}
	});
	
	var $work = $('#workPhotos');
	if ( $work.size() > 0 ) {
		
		var animating = false;
		
		var workWidth = 800 * $('#workPhotos LI').size();
		$('#workPhotos UL').css('width', workWidth + 'px');
		$('#workPhotos LI:not(:first)').css('opacity', '0.15').addClass('next');
		
		$('#workDescription LI:first').show(0);
		
		
		$('#workMenu A').click(function(e){
			e.preventDefault();
			
			var $this = $(this);
			var filterClass = $this.attr('href').replace('#', '');
			
			$this.parent().parent().find('A').removeClass();
			$this.addClass('active')[0].blur();
			
			$('#workPhotos LI').hide(0).css('opacity', '0.15');
			$('#workPhotos .' + filterClass).show(0).removeClass('prev').addClass('next');
			$('#workPhotos UL').css('left', 0);
			$('#workPhotos LI:visible:first').removeClass('next').find('A').click();
			
			$('#sideProjects LI').hide(0);
			$('#sideProjects .' + filterClass).show(0);
			$('#sideProjects').mousemove();
		});
		
		
		$('#workPhotos A').click(function(e){
			//e.preventDefault();
			
			if ( animating ) {
				return;
			}
			animating = true;
			
			var $this	= $(this);
			var $parent	= $this.parent();
			var $scroll	= $parent.parent();
			var leftPos	= $scroll.position().left - 50;
			var direction = 0;
			var multiplier = 1;
			
			window.location.hash = $this.attr('href');
			
			var descriptionID = $this.attr('href').replace('#', '');
			
			$('#workDescription LI').hide(0);
			$('#workDescription .' + descriptionID).show(0);
			
			if ( $parent.hasClass('next') ) {
				multiplier = $parent.prevUntil(':not(.next)').size();
				direction = -1 * (multiplier + 1);
			} else if ( $parent.hasClass('prev') ) {
				direction = 1;
				multiplier = $parent.nextUntil(':not(.prev)').size();
				direction = 1 * (multiplier + 1);
			}
			
			$parent.removeClass('next prev')
				.nextAll(':visible').removeClass('prev').addClass('next').fadeTo(400, 0.15).end()
				.prevAll(':visible').removeClass('next').addClass('prev').fadeTo(400, 0.15).end();
			
			$parent.fadeTo(400, 1);
			
			leftPos += 800 * direction;
			
			$scroll.animate({
				'left'	: leftPos
			}, 400, function(){
				animating = false;
			});
			
		});
		
		var hash = window.location.hash;
		
		if ( hash ) {
			$('#workPhotos A[href="' + hash + '"]').click();
		}
	}
	
	
	$('#sideProjects').hover(function(){
		var $this = $(this);
		var $list = $this.find('UL');
		var $label = $this.find('SPAN');
		
		$label.animate({
			'left' : $this.width()
		}, 200);
		
		$list.animate({
			'left' : 0
		}, 200);
	}, function(){
		var $this = $(this);
		var $list = $this.find('UL');
		var $label = $this.find('SPAN');
		
		var $thisWidth = $this.width();
		
		$label.animate({
			'left' : $thisWidth - $label.width() - 20
		}, 200);
		
		$list.animate({
			'left' : $thisWidth
		}, 200);
	}).mousemove(function(e){
		
		var $this = $(this);
		var $list = $this.find('UL');
		var containerHeight = $this.height();
		var listHeight = $list.height() + 30;
		
		var heightDiff = containerHeight - listHeight;
		
		var posTop = 0;
		
		if ( heightDiff > 0 ) {
			posTop = heightDiff / 2;
		} else {
			
			var delta = e.pageY || 0;
			posTop = delta;
			
			posTop = delta * heightDiff / containerHeight + 15;
			
		}
		
		$list.css('top', posTop + 'px');
	});
	
	
	$('#sideProjects A').click(function(e){
		//e.preventDefault();
		
		var href = $(this).attr('href').replace('./', '');
		$('#workPhotos A[href="' + href + '"]').click();
	});
	
	
	$('#workPhotos').mousewheel(function(e, intDelta){
		if (e.preventDefault) {
			e.preventDefault();
		}
		
		var $this = $(this);
		//var $current = $this.find('A:not(.next):not(.prev)');
		//console.log(intDelta);
		var $next;
		
		if (intDelta < 0) {
			$next = $this.find('LI.next:visible:first');
		} else if (intDelta > 0) {
			$next = $this.find('LI.prev:visible:last');
		}
		
		if ( $next ) {
			if ( $next.size() ) {
				$next.find('A').click();
			}
		}
	});

	
	$('A.social.newsletter').click(function(e){
		e.preventDefault();
		
		$('#newsletterForm').toggle()
			.find('LABEL').removeClass('error').end()
			.find('INPUT').val('').end()
			.find('SPAN').remove();
	});
	
	if ( $('#newsletterForm SPAN').size() > 0 ) {
		setTimeout(function(){
			$('A.social.newsletter').click();
		}, 4000);
	}
	
	
	
	
	
	
	
	
	
	function initNews( item ) {
		var iHeight = item.data('height');
		
		if ( newsHeightItems + iHeight < newsHeightTotal ) {
			newsHeightItems += iHeight;
			
			showNewsItem(item);
			
			var $next = item.next();
			
			if ( $next.size() ) {
				initNews($next);
			}
		}
	}
	
	function showNewsItem(item) {
		item[0].className = '';
		item.delay(200).animate({
			'height'	: item.data('height')
		}, 400, function(){
			item.animate({
				'opacity'	: 1
			}, 400);
		});
	}
	
	function hideNewsItem(item, className) {
		item[0].className = className;
		item.css({'opacity'	: 0}).animate({
			'height'	: 0
		}, 400);
	}

})
