// on dom ready stuff
$(function() {
	// vertical menu
	var curr = $('#menu-left p.on');
	if (curr.length)
		$('#menu-left').data('curr', curr);
	$('#menu-left > a').click(function() {
		var next = $(this).next();
		if (next.length && next[0].tagName == 'P') {
			var curr = $('#menu-left').data('curr');
			if (!curr || curr[0] != next[0]) {
				if (curr)
					curr.slideToggle();
				$('#menu-left').data('curr', next);
			}
			else
				$('#menu-left').removeData('curr');

			next.slideToggle();
			$(this).blur();
			return false;
		}
	});

	// photo gallery
	if ($('#photos-nav').length) {
		var hash = location.href.replace(/^[^#]+/, '').replace('#', '');
		if (hash) {
			var tmp = $('#photo-large .' + hash);
			if (tmp.length) {
				var pos = tmp.attr('id').split('-');
				focusPhoto(parseInt(pos[1], 10) + 1);
			}
		}

		$('#photo-large').find('img').each(function() {
			if (this.complete)
				$(this).css('visibility', 'visible').prev().remove();
			else
				$(this).load(function() {
					$(this).prev().remove();
					$(this).css({ opacity: 0, visibility: 'visible' }).animate({ opacity: 1 });
				});
		});

		$('#photos-nav').find('a').click(function() {
			var pos = $('#photo-large').data('pos');
			var noPhotos = $('#photo-large').find('img').length;
			
			pos = !pos ? 1 : parseInt(pos, 10);
			pos += $(this).attr('href') == '#prev' ? -1 : 1;
			if (pos == 0 || pos > noPhotos) return false;

			focusPhoto(pos);

			$(this).blur();
			return false;
		});
	}

	// center gallery nav vertically
	if ($('#gallery-nav').length) {
		var h = $('#gallery-nav').height();
		$('#gallery-nav').css({ marginTop: Math.round((226 - h - 6) / 2) });
	}

	// custom menu
	$('.your-menu-op').click(function() {
		$.ajax({
			parent: $(this).parent().css({ fontWeight: 'bold', color: '#f00' }),
			url: $(this).attr('href'),
			success: function(data) {
				var op = this.url.indexOf('add') != -1 ? 'add' : 'remove';
				if (op == 'add')
					this.parent.text('[done!]').animate({ opacity: 1 }, 1500).fadeOut(1000);
				else {
					var tr = this.parent.parents('tr').eq(0), next = tr.next(), prev = tr.prev();
					if (prev.find('td:eq(0)').hasClass('course')) {
						if (!next.length && !prev.prev().length)
							$('#menu').html('<div align="center"><br><br><strong>Your menu contains no items.</strong></div>');
						else {
							if (!next.length || next.find('td:eq(0)').hasClass('course'))
								prev.remove();
							tr.remove();
						}
					}
					else
						tr.remove();
				}
				
				var tmp = data.split('-');
				if (tmp[1] != '0')
					$('#your-menu').show().find('span').text(tmp[1]);
				else
					$('#your-menu').hide();
			}
		});
		$(this).replaceWith($(this).attr('href').indexOf('add') != -1 ? 'adding...' : 'removing...');
		return false;
	});
});

$(window).load(function() {
	// adjust height and footer image
	checkHeight();
	
	// home slideshow
	if (typeof(slideshowPhotos) != 'undefined' && slideshowPhotos[0].length > 0)
		$.slideshow({
			objects: ['#slideshow-imgs', '#slideshow-titles'],
			photos: slideshowPhotos,
			ratios: [[600, 311], [105, 16]]
		});
});

// adjust height
function checkHeight() {
	$(window).unbind('resize');

	$('#bttm-bg').css({ height: 'auto', backgroundPosition: 'center bottom' });
	var heightW = $(window).height();
	var heightBg = $(document.body).height();
	if (heightBg < heightW) {
		$('#bttm-bg').css('height', heightW);
		heightBg = heightW;
	}
	if (heightBg < 1200)
		$('#bttm-bg').css('backgroundPosition', 'center 400px');

	setTimeout('$(window).resize(checkHeight)', 10);
}

// preload support
var imgPreloader = {};
imgPreloader.images = [];
imgPreloader.queue = function() {
	for (var i = 0; i < arguments.length; i++) {
		this.images.push(new Image());
		this.images[this.images.length - 1].src = arguments[i];
	}
}

// home slideshow
$.slideshow = function(options) {
	var opt = options, counts = [0, 0];

	for (var i = 0; i < opt.photos.length; i++)
		for (var j = 1; j < opt.photos[i].length; j++)
			imgPreloader.queue(opt.photos[i][j]);

	$.slideshow.next = function() {
		var img = [], cont, i;

		// insert next image everywhere
		for (i = 0; i < opt.photos.length; i++) {
			cont = $(opt.objects[i]);
			if (cont.find('img').length < 2) {
				counts[i] = (counts[i] + 1) % opt.photos[i].length;
				img[i] =  $('<img>').prependTo(cont).css({ width: 0, height: opt.ratios[i][1] }).attr('src', opt.photos[i][counts[i]]);
			}
			else
				img[i] = cont.find('img:eq(0)');
		}

		// check if all images were loaded
		for (i = 0; i < opt.no; i++)
			if (!img[i][0].complete) {
				setTimeout($.slideshow.next, 100);
				return;
			}

		// start animations
		for (i = 0; i < opt.photos.length; i++) {
			cont = $(opt.objects[i]);
			img[i].animate({ width: opt.ratios[i][0] }, 1500);
			cont.find('img:eq(1)').css({ left: 'auto', left: 0, height: opt.ratios[i][1] }).animate({ width: 0 }, 1500, function() {
				$(this).remove();
			});
		}

		setTimeout($.slideshow.next, 4000);
	}

	setTimeout($.slideshow.next, 2000);
}

function focusPhoto(pos) {
	var noPhotos = $('#photo-large').find('img').length;
	var img = $('#photo-large').find('img').eq(pos - 1);

	// animation and serialization
	$('#photo-large').animate({ scrollLeft: (pos - 1) * 462 }, { queue: false, duration: 1000 });
	$('#photo-large').data('pos', pos);

	// nav
	$('#photos-nav').find('a:eq(0)').css('visibility', pos > 1 ? 'visible' : 'hidden');
	$('#photos-nav').find('a:eq(1)').css('visibility', pos < noPhotos ? 'visible' : 'hidden');
	$('#photos-nav').find('strong').text(pos);

	if (!img.attr('id')) return;

	// correct hash
	location.href = '#' + img.attr('class');

	// focus correct desc and desc full
	$('.desc').add('.desc-full').hide();
	var id = img.attr('id').split('-');
	$('#desc' + id[2]).show();
	$('#desc-full' + id[2]).show();
}