/*
Author: Joe Tan (joetan54@gmail.com)
*/

// boxes
jQuery(function($) {
	$('#content .box:odd, #content .box2:odd').addClass('odd-box').after('<div class="clear"></div>');
	$('#content .box:last, #content .box2:last').after('<div class="clear"></div>');
	
	$('#content a img').each(function(i, obj) {
		if (!$(obj).attr('align')) $(obj).attr('align', 'absmiddle');
	})
});

// font replace
jQuery(function($) {
	if (!$.browser.msie) $('blockquote').addClass('typeface-js');
	if (typeof _typeface_js != 'undefined') _typeface_js.renderDocument();
});

// drop down menu handlers
jQuery(function($) {
	var nav = $('#nav');
	nav.find('ul li.level-1').hover(
		function() { //over
			if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
			if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
			if ($(this).find('ul.level-2').hasClass('hover')) { return;} // already opened
			
			nav.data('menuDropDown', $(this).find('ul.level-2'));
			nav.data('menuTimer', setTimeout(function() {
				$('#nav ul.level-2').hide().removeClass('hover');
				$('#nav a.level-1').removeClass('hover');

				var menu = $('#nav').data('menuDropDown')
				$(menu).addClass('hover').slideDown(50);
				$(menu).prev().addClass('hover').show();
				
			}, 150));
			$(this).addClass('over');
		},
		function() { //out
			if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
			if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
			nav.data('menuTimerOut', setTimeout(function() {
				$('#nav ul.level-2').hide().removeClass('hover');
				$('#nav a.level-1').removeClass('hover');
			}, 1500));
			$(this).removeClass('over');
		}
	);
	nav.find('ul > li li, ul > li li a').mouseover(function() {
		var nav = $('#nav');
		if(nav.data('menuTimer'))    clearTimeout(nav.data('menuTimer'))
		if(nav.data('menuTimerOut')) clearTimeout(nav.data('menuTimerOut'))
	});
});


// our people
/*
jQuery(function($) {
	var curr = 0;
	$('.our-people #content2 > *').each(function(i, obj) {
		if (obj.tagName.toLowerCase() == 'h3') {
			if (curr > 0) $(obj).before('<a href="#" class="content-'+curr+' more expandable" contentID="'+curr+'">[more]</a>')

			if ($(obj).nextAll().length > 1) {
				curr = i;
				$(obj).attr('contentID', curr);
				//$(obj).addClass('expandable');
			}
			$(obj).next().addClass('first');
		} else if (curr > 0) {
			if (!$(obj).hasClass('first')) $(obj).hide();
		}
		
		$(obj).addClass('content-'+curr);
		
	});
	
	$('.our-people #content2 .expandable').click(function() {
		var contentID = '.content-' + $(this).attr('contentID');
		$(this).toggleClass('expanded')
		if ($(this).hasClass('expanded')) {
			$(contentID+':not(h3)').show(300);
			$(contentID+'.more').html('[hide]');
		} else {
			$(contentID+':not(h3, .first, .more)').hide(300);
			$(contentID+'.more').html('[more]');
		}
		
		return false;
	});

});
*/

// expanding widget
jQuery(function($) {
	var curr = 0;
	$('#content2 > *').each(function(i, obj) {
		if ($(obj).hasClass('expand')) {
			if (curr > 0) $(obj).before('<p><a href="#" class="content-'+curr+' more expandable" contentID="'+curr+'">[more]</a></p>')

			if ($(obj).nextAll().length > 1) {
				curr = i;
				$(obj).attr('contentID', curr);
				$(obj).addClass('expandable');
			}
			$(obj).next().addClass('first');
		} else if ($(obj).hasClass('stop')) {
			if (curr > 0) $(obj).before('<p><a href="#" class="content-'+curr+' more expandable" contentID="'+curr+'">[more]</a></p>')

			curr = 0;
		} else if (curr > 0) {
			if (!$(obj).hasClass('first') ) $(obj).hide();
		}
		
		$(obj).addClass('content-'+curr);
		
	});
	if (curr > 0) {
		$('#content').append('<p><a href="#" class="content-'+curr+' more expandable" contentID="'+curr+'">[more]</a></p>')
	}
	$('#content .expandable').click(function() {
		var contentID = '.content-' + $(this).attr('contentID');
		$(this).toggleClass('expanded')
		if ($(this).hasClass('expanded')) {
			$(contentID+':not(.expand)').show(300);
			$(contentID+'.more').html('[hide]');
		} else {
			$(contentID+':not(.expand, .first, .more)').hide(300);
			$(contentID+'.more').html('[more]');
		}
		
		return false;
	});

});


// misc formatting
jQuery(function($) {
	$('a[href$=pdf]').addClass('pdf');
	$('a[href*=/menus/]').addClass('menu');
	$('ul > li:last').addClass('last');

});