$(document).ready(function () {
/*	$('body').click(function() {
		if ($('#language-dropdown').is(':visible')) $('#language-dropdown').hide();
	});*/
	
	$('#lang_menu_link').click(function() {
		// $('#language-dropdown').toggle() not working for IE. check out:
		// http://stackoverflow.com/questions/975153/jquery-toggle-not-working-with-trs-in-ie
		var elem = $('#language-dropdown')[0];
		if(elem.style.display == 'none')
		     $('#language-dropdown').show();
		else
		{
		     $('#language-dropdown').hide(); 
		}
	});
	
	// login
	focusBlur('#login-email', 'Email');
	focusBlurPassword('#login_password', 'Password');
	
	// language dropdown
	// inspired from http://www.jankoatwarpspeed.com/post/2009/07/28/reinventing-drop-down-with-css-jquery.aspx
	$('#language-select').hide();
	$('#lang_menu_link').html($('#language-select option[selected=selected]').html() + '<small>▼</small>');
	var html = '<ul id="language-dropdown">';
	$('#language-select option').each(function() {
		html += '<li><a href="' + $(this).attr('value') + '">' + $(this).html() + '</a></li>';
	})
	html += '</ul>';
	$('#lang_menu_link').after(html);
	
	$('.switchable_hidden').hide();
	$('.switchable_trigger').each(function() {
		var tag = "Show".tagify('a', {'href': '#', 'class': 'switchable_link'});
		$(this).before(tag);
	});
	$('.switchable_link').click(function() {
		($(this).html() == 'Show') ? $(this).html('Hide') : $(this).html('Show');
		$(this).parent().children().filter('.switchable_hidden').toggle();
		return false;
	});
	
	/* HOMEPAGE */
	if ($('#frontpage')[0]) {
		$('.image').each(function() {
			$(this).before('<img class="zoom" src="images/zoom.png" width="33" height="32" alt="Zoom">');
		});

		// lightbox for thumbnails
		$("a[rel^='prettyPhoto']").prettyPhoto({
			theme: 'dark_square'
		});	
		
		// news ticker
		if (news) {
			var newsitems = 3;
			var li = '';
			// fill li
			for (var i=0; i<newsitems; i++) {
				li += news[i]['title'].truncate(55).tagify('a', {href: 'news.html'}).tagify('li');
			}
			
			$('#news_content').html(li);
			$('#news_content').innerfade({
				animationtype: 'slide',
				speed: 1000,
				timeout: 5000,
				containerheight: '1em'
			});
		}
	}
	
	/* NEWS */
	// if #news exist and variable news is in scope:
	if ($('#news')[0] && news) {
		var html = '';
		jQuery.each(news, function() {
			html += (this['title']).tagify('h3') + this['content'];
		});
		$('#news_content').html(html);
	}
	
	/* VIDEO GALLERY */
	if ($('#video_list')) {
		hideall();
		$('#landing').show();

		$('#triggers_video div').click(function (e) { 
			hideall();
			$('#' + $(this).attr('class')).show();
		});

	}
	
	$('#language-dropdown').hide();			// need this for language dropdown to work properly
});

function focusBlur(e, val) {
	showValue(e, val);
	
	$(e).focus(function() {
		if ($(this).attr('value') == val) {
			$(this).attr('value', '');
			$(e).removeClass('fade');
		}
	});
	$(e).blur(function() {
		showValue(e, val);
	});
}

function showValue(e, val) {
	if ($(e).attr('value') == '') {
		$(e).attr('value', val);
		$(e).addClass('fade');
	}
}

// e is the actual password field
function focusBlurPassword(e, val, suffix) {
	// "-suffix" is the temporary password field
	if (!suffix) suffix = '-tmp';
	showPasswordValue(e, val, suffix);
	
	$(e + suffix).focus(function() {
		$(e + suffix).hide();
		$(e).attr('value', '').show().focus();
	});
	$(e).blur(function() {
		showPasswordValue(e, val, suffix);
	});
}

function showPasswordValue(e, val, suffix) {
	if ($(e).attr('value') == '') {
		$(e).hide();
		$(e + suffix).attr('value', val).show();
	}
	else {
		$(e + suffix).hide();
		$(e).show();
	}
}

jQuery.extend(String.prototype, {
	// attr is {attr1:val1, attr2:val2, ..., attrn:valn}
	tagify: function(tag, attr) {
		var output = '<' + tag;
		if (attr) {
			jQuery.each(attr, function (i, val) {
				output += ' ' + i + '="' + val + '"';
			});			
		}
		output += '>' + this + '</' + tag + '>';
		return output;
	},

	truncate: function(length, chars) {
		if (!chars) chars = '...';
		return (length < this.length) ? this.substring(0, length) + chars : this;
	}
});

function hideall() {
	$('#landing').hide();
	$('#setup').hide();
	$('#or').hide();
	$('#grademark').hide();
	$('#peermark').hide();
}