// 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);
		}
	);
	
	// sidebar menu hover
	$('#sidebar .menu a')
		.hover(
			function() {
				if ($(this).hasClass('on')) return;
				$(this).find('img').css('marginLeft', -225);
			},
			function() {
				if ($(this).hasClass('on')) return;
				$(this).find('img').css('marginLeft', 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();
	
	/* stretch sidebar */
	if ($('#content').height() > $('#sidebar').height())
		$('#sidebar .bg').height($('#content').height() - 299);
		
	var diff = 543 - ($('#content').height() - 224);
	if (diff > 0)
		$('#wrapper').css('backgroundPosition', '0px -' + diff + 'px');
});

// on load stuff
$(window).load(function() {
	/* home slideshow */
	if ($('#box-photo.slideshow').length)
		ProjectSlideshow.preloadNext();
});

// 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].id;
		this.tile = 0;
		
		setTimeout('ProjectSlideshow.rotate()', 6000);
	},
	
	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', '/photo-gallery/' + 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);
	}
	
}

// location & directions supprt
function locationOps() {
	$('#location').find('.map').googleMap({ points: [[38.87218, -77.195383]] });
	
	$('#location').find('.directions :input[name=get]').click(function() {
		getDirections();
	});
	
	$('#location').find('.directions :input[name=from]').keypress(function(e) {
		if (e.keyCode == 13)
			getDirections();
	});
	
	$('#location').find('.print a').click(function() {
		$('#location').find('.map').googleMap('print', {
			panel: $('#location').find('.panel')
		});
		return false;
	});
}

function getDirections() {
	var from = $('#location').find('.directions :input[name=from]').attr('value');
	if (!from) {
		alert('Please fill in a from address!');
		return;
	}
	
	$('#location').find('.directions :input[name=get]')
		.attr('disabled', true)
		.attr('value', 'please wait...');
	
	$('#location').find('.print').css('display', 'none');
	$('#location').find('.panel').html('');
	
	$('#location').find('.map').googleMap('directions', {
		origin: from,
		destination: '7223 Roosevelt Ave, Falls Church, VA 22042',
		panel: $('#location').find('.panel'),
		success: function() {
			$('#location').find('.print').css('display', 'block');
		},
		complete: function() {
			$('#location').find('.directions :input[name=get]')
				.attr('disabled', false)
				.attr('value', 'get directions');
		}
	});
}

// cal
var Cal = {
	
	initOverview: function(currDate) {
		$('#cal .nav a.m').click(function() {
			Cal.setLoading('?date=' + $(this).attr('href').substr(1));
		});
		$('#cal .nav select').change(function() {
			$(this).blur();
			window.location.href = '#' + this.value;
			Cal.setLoading('?date=' + this.value);
		});
		$('#cal .overview .cell').click(function() {
			var date = $(this).attr('id').replace('d', '');
			window.location.href = '#entry-' + date;
			Cal.setLoading('/calendar-entry?date=' + date);
		});
		
		if (window.location.hash) {
			var hash = window.location.hash.substr(1);
			var date = hash.replace('entry-', '');
			if (date && date != currDate) {
				if (hash.indexOf('entry') == -1)
					Cal.setLoading('?date=' + hash);
				else
					Cal.setLoading('/calendar-entry?date=' + hash.replace('entry-', ''));
			}
		}
	},
	
	initEntry: function() {
		$('#cal .nav a.back').click(function() {
			Cal.setLoading('/calendar?date=' + $(this).attr('href').substr(1));
		});
		$('#cal .nav a.day').click(function() {
			Cal.setLoading('/calendar-entry?date=' + $(this).attr('href').substr(7));
		});
	},
	
	setLoading: function(url) {
		$('<div>')
			.prependTo($('#cal').css('opacity', 0.5))
			.css({ position: 'absolute', zIndex: 1 })
			.height($('#cal').height())
			.width($('#cal').width());
		$('#cal').load(url, function() {
			$('#cal').css('opacity', 1);
		});
	}
	
}
















