$(document).ready(function() {
	
	if ($.browser.msie) {
		$('.mobile').css({'display':'none'});
		$('#top_text .k').css({'color':'#333'});
		/*
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css({'z-index', zIndexNumber});
			zIndexNumber -= 10;
		});
		*/
	}

	// mobile menu
	$('#menu-m select').change(function(){
		window.location = $(this).val();
	});
	
	// noteboxes
	$('.note_box .slidee').hide();
	$('.slider').bind('click', function(e) {
		e.preventDefault();
		if ($(this).is('.open')) {
			$(this).parent().animate({"opacity": "0.2"}, 400);
		} else {
			$(this).parent().animate({"opacity": "1.0"}, 400);
		}		
		$(this).toggleClass('open');
		$(this).next('.slidee').slideToggle('400');
		_gaq.push(['_trackEvent', 'Content', 'Notebox', $(this).text()]);
	});
	
	
	// --- TRANSLATION BLOCKS --- //
	// translations
	$('.translation .tr_block .en').hide();
	$('.toggle_en').bind('click', function(e) {
		e.preventDefault();
		$(this).toggleClass('open');
		$(this).parent().next('.tr_block').children('.en').toggle('blind','','400');
		_gaq.push(['_trackEvent', 'Content', 'Translation', $(this).attr('title')]);
	});
	
	// open gloss
	$('.translation .tr_block .gl_hide').hide();
	$('.toggle_gl_full').bind('click', function(e) {
		e.preventDefault();
		// turn off 'pop-up'
		$(this).next('.toggle_gl_pop').removeClass('open');
		$(this).parent().next('.tr_block').children('.tr_word').children('.k').unbind('mouseenter mouseleave');
		$(this).parent().next('.tr_block').children('.tr_word').children('.k').removeClass('pop-up');
		$(this).parent().next('.tr_block').children('.tr_word').children('.gl_hide').removeClass('pop-up');
		// === CLOSE ===
		if( $(this).is('.open') ) {
			// turn off button
			$(this).removeClass('open');
			// hide balloons first
			$(this).parent().next('.tr_block').children('.tr_word').children('.gl_hide').slideToggle('400', function(){
				// then condense source text
				$(this).prev('.k').removeClass('open', '400');
			});
			// remove lines
			$(this).parent().next('.tr_block').children('.br').removeClass('open');
		// === OPEN ===
		} else {
			// turn on button
			$(this).addClass('open');
			// expand source text first
			$(this).parent().next('.tr_block').children('.tr_word').children('.k').addClass('open', '400', function(){
				// then show balloons
				$(this).next('.gl_hide').slideToggle('400');
			});
			// add lines
			$(this).parent().next('.tr_block').children('.br').addClass('open');
		}
		_gaq.push(['_trackEvent', 'Content', 'Gloss-Open', $(this).attr('title')]);
	});
	
	// pop-up gloss
	$('.toggle_gl_pop').bind('click', function(e) {
		e.preventDefault();
		// turn off 'open' button
		$(this).prev('.toggle_gl_full').removeClass('open');
		$(this).parent().next('.tr_block').children('.tr_word').children('.gl_hide').slideUp('400');
		// === CLOSE ===
		if( $(this).is('.open') ) {
			// turn off button
			$(this).removeClass('open');
			// hide balloons 
			$(this).parent().next('.tr_block').children('.tr_word').children('.gl_hide').hide();
			$(this).parent().next('.tr_block').children('.tr_word').children('.k').unbind('mouseenter mouseleave');
			// condense source text
			$(this).parent().next('.tr_block').children('.tr_word').children('.k').removeClass('open pop-up', '400');
			// remove lines
			$(this).parent().next('.tr_block').children('.br').removeClass('open');
		// === OPEN ===
		} else {
			// turn on button
			$(this).addClass('open');
			// expand source text
			$(this).parent().next('.tr_block').children('.tr_word').children('.k').addClass('open pop-up', '400');
			// add pop-up class to balloons
			$(this).parent().next('.tr_block').children('.tr_word').children('.gl_hide').addClass('pop-up');
			// show balloons
			$(this).parent().next('.tr_block').children('.tr_word').children('.k').hover(
				function() {
					$(this).addClass('over');
					$(this).next('.gl_hide').css({display: 'table-cell'});
				},
				function() {
					$(this).removeClass('over');
					$(this).next('.gl_hide').css({display: 'none'});
				}
			);	
			// show lines
			$(this).parent().next('.tr_block').children('.br').addClass('open');
		}
		_gaq.push(['_trackEvent', 'Content', 'Gloss-Popup', $(this).attr('title')]);
	});	
});
