// on dom ready stuff
$(function() {
	// top menu hover
	$('#menu-top a').hover(
		function() {
			if ($(this).hasClass('on')) return;
			$(this).scrollTop(37);
		},
		function() {
			if ($(this).hasClass('on')) return;
			$(this).scrollTop(0);
		}
	);
		
	// regenerate captcha on click
	$('.captcha').click(function() {
		var img = $(this);
		img.attr('src', img.attr('src').replace(/\?.*/, '') + '?' + (new Date()));
		return false;
	});
	
	/* portfolio gallery */
	if ($('#box-photo.portfolio').length)
		PortfolioPhotos.init();
		
	/* project scroller */
	ProjectScroller.init();
});

// on load stuff
$(window).load(function() {
	/* home slideshow */
	if ($('#box-photo.slideshow').length)
		ProjectSlideshow.preloadNext();
		
	/* stretch sidebar */
	if ($('#content').height() > $('#sidebar').height())
		$('#sidebar .bg').height($('#content').height() - 70);
});

// home project slideshow
var ProjectSlideshow = {
	idx: 0,
	
	preloadNext: function() {
		this.idx = (this.idx + 1) % this.projects.length;
		this.img = new Image();
		this.img.src = this.folder + this.projects[this.idx].photo;
		this.tile = 0;
		
		setTimeout('ProjectSlideshow.rotate()', 3000);
	},
	
	rotate: function() {
		this.tile++;
		if (this.tile == 5)
			$('#box-photo .t5 span.label').html('<strong>' + this.projects[this.idx].client + '</strong>' + this.projects[this.idx].label);
		
		var a = $('#box-photo .t' + this.tile);
		$('<span>')
			.addClass('effect')
			.prependTo(a)
			.css({ opacity: 0, backgroundImage: 'url(' + this.img.src + ')' })
			.animate({ opacity: 1 }, 300, function() {
				$(this).parent().css('backgroundImage', $(this).css('backgroundImage'));
				$(this).remove();
				
			});
		a.attr('href', '/portfolio/' + this.projects[this.idx].client_rw).attr('title', this.projects[this.idx].client);
		
		if (this.tile < 6)
			setTimeout('ProjectSlideshow.rotate()', 300);
		else
			ProjectSlideshow.preloadNext();
	}
	
}

// portfolio photos support
var PortfolioPhotos = {
	
	init: function() {
		this.noPhotos = $('#box-photo a.tile').length;
		
		$('#box-photo .nav .pages a')
			.hover(function() {
				var a = $(this);
				a.parent().find('a').removeClass('on');
				a.addClass('on');
				$('#box-photo .glider').stop().animate({ scrollLeft: (a.text() - 1) * 752 });
			})
			.click(function() {
				return false;
			});
			
		$('#box-photo .nav .large a').click(function() {
			PortfolioPhotos.navLarge($(this).hasClass('next') ? 1 : -1);
			return false;
		});
		
		$('#box-photo a.tile').click(function() {
			PortfolioPhotos.loadLarge($(this).attr('rel'));
			return false;
		});
		
		$('#portfolio-large p').click(function() {
			$(this).css('display', 'none');
			var a = $('#box-photo a.tile[rel=' + PortfolioPhotos.idx + ']');
			var pos = a.position();
			$('#portfolio-large').find('img').animate({ width: 292, height: 170, marginTop: 24, marginLeft: -21	, opacity: 0 });
			$('#portfolio-large').animate({ width: 250, height: 218, marginTop: pos.top, marginLeft: pos.left }, function() {
				$('#portfolio-large').css('display', 'none').find('img').remove();
			});
			
			$('#box-photo .nav .large').animate({ height: 0 });
			$('#box-photo .nav .pages').animate({ height: 27 });
			
			return false;
		});
		$('#portfolio-large').click(function() {
			if (!$(this).find('img').length) return;
			PortfolioPhotos.navLarge(1);
		});
	},
	
	loadLarge: function(idx) {
		this.idx = idx;
		var a = $('#box-photo a.tile[rel=' + idx + ']');
		var url = a.css('backgroundImage').replace(/(^url\("?)|("?\)$)/g, '').replace('_t2', '');
		var pos = a.position();
		$('#portfolio-large')
			.css({ width: 250, height: 118, marginTop: pos.top, marginLeft: pos.left, display: 'block' })
			.prepend('<span>loading...</span>')
			.find('p').css('display', 'none');
		var img = $('<img>')
			.css({ width: 292, height: 170, marginTop: 24, marginLeft: -21, opacity: 0 })
			.attr('src', url)
			.appendTo($('#portfolio-large'))
			.attr('title', 'click for next photo');
		
		if (img[0].complete)
			this.openLarge();
		else
			img.load(function() {
				PortfolioPhotos.openLarge();
			});
	},
	
	openLarge: function() {
		$('#box-photo .nav .pages').animate({ height: 0 });
		$('#box-photo .nav .large').animate({ height: 27 });
		$('#box-photo .nav .large a.prev').css('display', this.idx == 1 ? 'none' : 'inline');
		$('#box-photo .nav .large a.next').css('display', this.idx == this.noPhotos ? 'none' : 'inline');
		
		$('#portfolio-large')
			.animate({ width: 752, height: 437, marginTop: 0, marginLeft: 0 })
			.find('p').css('display', 'block')
			.parent().find('span').remove();
		$('#portfolio-large').find('img')
			.animate({ width: 752, height: 437, marginTop: 0, marginLeft: 0, opacity: 1 });
	},
	
	navLarge: function(inc) {
		this.idx = (this.idx - 1 + inc) % this.noPhotos + 1;
		var img = $('#portfolio-large').find('img').stop();
		if (!$('#portfolio-large span').length)
			$('#portfolio-large')
				.css('backgroundImage', 'url(' + img.attr('src') + ')')
				.prepend('<span>loading...</span>');
		var img1 = $('<img>')
			.css('opacity', 0)
			.attr('src', $('#box-photo a.tile[rel=' + this.idx + ']').css('backgroundImage').replace(/(^url\("?)|("?\)$)/g, '').replace('_t2', ''))
			.attr('title', 'click for next photo');
		img.replaceWith(img1);
		if (img1[0].complete) {
			$('#portfolio-large').find('span').remove();
			img1.animate({ opacity: 1 }, function() {
				$('#portfolio-large').css('backgroundImage', 'none');
			});
		}
		else
			img1.load(function() {
				$('#portfolio-large').find('span').remove();
				$(this).animate({ opacity: 1 }, function() {
					$('#portfolio-large').css('backgroundImage', 'none');
				});
			});
			
		$('#box-photo .nav .large a.prev').css('display', this.idx == 1 ? 'none' : 'inline');
		$('#box-photo .nav .large a.next').css('display', this.idx == this.noPhotos ? 'none' : 'inline');
		
		var page = Math.ceil(this.idx / 6);
		$('#box-photo .glider').scrollLeft((page - 1) * 752);
		$('#box-photo .nav .pages a').removeClass('on');
		$('#box-photo .nav .pages a[rel=' + page + ']').addClass('on');
	}
	
}

// project scroller
var ProjectScroller = {
	
	init: function() {
		var noPhotos = $('#scroller div a').length;
		this.scrollTop = 0;
		this.scrollMax = (noPhotos - 1) * 200 - 35;
		
		$('#scroller .nav')
			.hover(
				function() {
					ProjectScroller.start($(this).hasClass('down') ? -1 : 1);
				},
				function() {
					ProjectScroller.stop();
				}
			)
			.click(function() {
				$(this).blur();
				return false;
			});
	},
	
	start: function(inc) {
		this.scrollInc = inc * 3;
		this.scrollInt = setInterval('ProjectScroller.run()', 20);
	},
	
	stop: function(inc) {
		clearInterval(this.scrollInt);
	},
	
	run: function() {
		var scrollTop = this.scrollTop + this.scrollInc;
		$('#scroller .nav.up').css('display', scrollTop >= this.scrollMax ? 'none' : 'block');
		$('#scroller .nav.down').css('display', scrollTop <= 0 ? 'none' : 'block');
		if (scrollTop < 0 || scrollTop > this.scrollMax) {
			this.stop();
			return;
		}
		this.scrollTop = scrollTop;
		$('#scroller div').scrollTop(this.scrollTop);
	}
	
}
