$(document).ready(function()
{
	$('#languages .selector a').click(function()
	{
		$(this).parent().toggleClass('selected');
		var langlist = $ ('#langlist');
		$('#langlist').toggle();
	});

	$('#scoutForm').attr('target', '_blank');

	$('.datePicker').datePicker({clickInput : true}).dpSetSelected(new Date().asString());

	$('.jsBoxAjax .headline').each(function(i)
	{
		var self = $(this);
		self.css('cursor', 'pointer');

		if(!self.hasClass('expanded'))
		{
			self.addClass('collapsed').parent().find('.content').hide();
		}

		self.click(function()
		{
			var self = $(this);

			if(self.hasClass('expanded'))
			{
				self.removeClass('expanded').addClass('collapsed').parent().find('.content').hide('slide', { direction: "up" }, 600);
			}
			else if (self.hasClass ('loaded') && self.hasClass ('collapsed'))
			{
				self.removeClass('collapsed').addClass('expanded').parent().find('.content').show('slide', { direction: "up" }, 600);
			}
			else
			{
				var uid = self.attr('id').split('_')[1];
				self.find ('.icon').addClass ('loading');
				self.parent ().find ('.content').load (rpcaddress + '&getentry=1&uid=' + uid, function ()
				{
					$ (this).parent ().find ('.headline').addClass ('loaded').find ('.icon').removeClass ('loading');
					$ (this).show ('slide', { direction : 'up' }, 600);
					// fancyBox
					$ ('.thumblist a[rel=sdb_'+ uid +']').fancybox
					({
						'titleShow' : false
					});
				});
				self.addClass ('expanded').removeClass ('collapsed');
			}
		});
	});

	$('.jsBox .headline').each(function(i) {
		$(this).css('cursor', 'pointer');

		if(!$(this).hasClass('expanded')) {
			$(this).addClass('collapsed').parent().find('.content').hide();
			$(this).parent().find('.fileList').hide();
		}

		$(this).click(function(ev) {
			var self = $(this);
			if(self.hasClass('expanded')) {
				self.removeClass('expanded').addClass('collapsed').parent().find('.content, .fileList').hide('slide', { direction: "up" }, 600);
			}
			else {
				self.toggleClass('collapsed expanded').parent().find('.content, .fileList').show('slide', { direction: "up" }, 600);
			}
		});

	});

	$('.news-morelink').click(function() {
		$(this).parent().parent().find('.news-text').toggleClass('hidden');
		var text = $(this).html();
		$(this).html($(this).attr('rel')).attr('rel', text);
	});

	var host = location.href.split('//')[1].split('/')[0];

	$('a').each(function () {
		var self = $(this);
		var href = self.attr('href');

		if (typeof href == 'undefined') { return false; }

		if (href.indexOf('http:') == 0 && href.indexOf(host) == -1) {
			self.live ('click', function () {
				window.open(this.href);
				return false;
			});
		}

	});
});

function putText(el, text){
	if(el.value == '') el.value = text;
}

function rmText(el, text){
	if(el.value == text) el.value = '';
}

