// we have this out of the function below so we can overwrite in the page if we need to
var currentPage;

function trace() {
	if (typeof(console) != 'undefined') {
		console.debug.apply(console.debug, arguments);
	} else {
		alert(arguments[0]);
	}
}

/* Menu Rollover Code */
$(function(){
	
	/* Mouse Interaction Code */
	var active = $('#menu LI.active');
	
	var activeId = $('#menu').children('.active').attr('id');
	if (activeId) {
		$('#main').addClass(activeId.replace(/^menu/, '').toLowerCase());
	}
	
	//active.removeClass('active');
	
	var possies = {
		menuNews:['-270px','94px'],
		menuCredits:['-233px','57px'],
		menuGallery:['-229px','53px'],
		menuEquipment:['-194px','18px'],
		menuContact:['-222px','46px']
	};
	
	var interval;
	
	$('#menu').children()
		.addClass('animated')
		.append('<div class="invisispot"></div>');
		
	makeActive(active, false);
	
	$('#menu LI').hover(function(){
			makeActive($(this));
	}, function(){
			var $this = $(this);
			interval = setTimeout(function(){
				makeInActive($this);
			}, 10);
	});
	
	$('#menu').hover(function(){
		
	}, function(){
		if (active.length){
			clearTimeout(interval);
			makeActive(active);
		}
	});
	
	function makeActive($elements, animate) {
		if (typeof($elements) == 'undefined') return;
		makeInActive($elements.siblings());
		$elements
			.not('.over')
			.addClass('over')
			.filter('.animated').each(function(){
				var y = $(this).css('backgroundPositionY');
				if (!y) y = $(this).css('backgroundPosition').split(' ')[1];
				$(this).css('backgroundPosition',possies[this.id][0]+' '+y+'');
				if (animate === false) {
					$(this).css({backgroundPosition:'-176px '+y+''});
				} else {
					var speed = 150;
					$(this).stop().animate({backgroundPosition:'(-176px '+y+')'}, {duration:speed});
				}
			})
			.end();
	}
	
	function makeInActive($elements) {
		if (typeof($elements) == 'undefined') return;
		$elements
			.filter('.over')
			.removeClass('over')
			.filter('.animated').each(function(){
				var y = $(this).css('backgroundPositionY');
				if (!y) y = $(this).css('backgroundPosition').split(' ')[1];
				$(this).css('backgroundPosition',possies[this.id][1]+' '+y+'');
				var speed = 150;
				$(this).stop().animate({backgroundPosition:'(0px '+y+')'}, {duration:speed});
			}).end();
	}
	
	
	/* Scroll to submenu item */
	$('#menu UL LI.here').each(function(){
		$(this).parent().scrollTo(this);
	});
	
});

/*
$(function(){
	$('#header').click(function(){
		window.location.href = 'index.html';
	}).css('cursor', 'pointer');
});*/


// hook up Brigham's motion reel
$(function(){
	if ($('#motionReelData').length) {
		var motionReel = $('#motionReelData').metadata({type:'attr',name:'data'});
		if (motionReel) {
			$('#menuGalleryMotion').children('A')
				.attr('href', '/files/videos/'+motionReel.path)
				.attr('rel', 'videolightbox')
				.attr('data', '{width:'+motionReel.width+',height:'+motionReel.height+'}')
				.attr('title', motionReel.caption);
		}
	}
});


/* open external + pdf links in a new window */
$(function(){
	$('A, AREA').filter(function(){
		return (!this.target && (this.href.indexOf(window.location.hostname) == -1 || this.href.match(/\.pdf$/i)));
	}).attr('target', 'blank');
});

/* prevent flicker on menu images */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}
	

$(function(){
	$('BODY').removeClass('nojs').addClass('js');
});



$(function(){
	
	var original = 'brigham edgar';
	var words = $.shuffle([
		'me grab hagrid',
		'rabid germ hag',
		'i bag hard germ',
		'brag raged him',
		'id harm beggar',
		'big grade harm',
		'bad ham rigger',
		'ham badger rig',
		'drama herb gig'
	]);
	
	var rawText = 'brighamedr';
	var rawWidths = [49,36,18,51,46,51,67,51,49,37];
	var charData = {};
	var x = 0;
	
	$.each(rawWidths, function(i, val){
		charData[rawText.charAt(i)+'0'] = {x:x, width:val};
		x += val;
	});
	
	$.each(rawWidths, function(i, val){
		charData[rawText.charAt(i)+'1'] = {x:x, width:val};
		x += val;
	});
	
	var cnt = -1;
	
	var divs;
	
	/// create the divs
	(function(){
		$('#logo h1').remove();
		var i, num = original.replace(/\s/,'').length;
		for (i=0;i<num;i++) {
			$('#logo').append('<div class="letter"></div>');
		}
		divs = $('#logo').children();
		morphTo(original, false);
	})();
	
	$('#logo').click(function(){ 
		window.location.href = '/';
	});
	
	$('#logo').hover(function(){
		morphTo(words[++cnt % words.length]);
	}, function(){
		morphTo(original);
	});
	
	function morphTo(word, animate) {
		var c = 0, letter = 0;
		$.each(word.split(''), function(i, v) {
			if (v == ' ' && ++c) return;
			var m = charData[v+(c%2)];
			letter++;
			if (animate !== false) {
				$('#logo :nth-child('+(letter)+')').animate({width:m.width, backgroundPosition:'(-'+m.x+'px 0px)'});
			} else {
				$('#logo :nth-child('+(letter)+')').css({width:m.width, backgroundPosition:'-'+m.x+'px 0px'}, {duration:200});
			}
		});
	}
	
});