
function changeFontSize(val) {
	var oldFontSize = $('body').css('font-size');
	var newFontSize = parseInt(oldFontSize) + val;
	$('body').css('font-size', newFontSize+'px');
}

function focusSearchField(element) {
	if(element.value == 'WSWS durchsuchen') {
		element.value = '';
	}
}

function blurSearchField(element) {
	if(element.value == '') {
		element.value = 'WSWS durchsuchen';
	}
}

function openShareWindow(url,title) {
	var options = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=710,height=400';
	shareWindow = window.open(url,'shareWindow',options);
	shareWindow.focus();
}

function openPrintWindow(url) {
	var options = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=710,height=510';
	printWindow = window.open(url,'printWindow',options);
	printWindow.focus();
	// printWindow.print();
}

function initLanguages() {
	if ($('#language').length > 0) {
		$('#language').change(function(){
			var url = $('#language option:selected').attr('value');
			if (url != '#') {
				window.location.href = url;
			}
		});
	}
}

function initNavigation() {
	resetFirst = $('#navigation > ul > li > a.active');
	resetSecond = $('#navigation > ul > li > ul.active');
	resetThird = $('#navigation > ul > li > ul > li > a.active');
	$('#navigation > ul').hover(
		function(){
			void(0);
		},
		function(){
			$('#navigation > ul > li > a').removeClass('active');
			$('#navigation > ul > li > ul').removeClass('active');
			resetFirst.addClass('active');
			resetSecond.addClass('active');
			resetThird.addClass('active');
		}
	);
	$('#navigation > ul > li').hover(
		function(){
			$(this).siblings('li').children('a').removeClass('active');
			$(this).siblings('li').children('ul').removeClass('active');
			$(this).children('a').addClass('active');
			$(this).children('ul').addClass('active');
		},
		function(){
			$(this).children('a').removeClass('active');
			$(this).children('ul').removeClass('active');
			resetFirst.addClass('active');
			resetSecond.addClass('active');
			resetThird.addClass('active');
		}
	);
	$('#navigation > ul > li > ul > li').hover(
		function(){
			$(this).siblings('li').children('a').removeClass('active');
			$(this).siblings('li').children('ul').removeClass('active');
			$(this).children('a').addClass('active');
			$(this).children('ul').addClass('active');
		},
		function(){
			$(this).children('a').removeClass('active');
			$(this).children('ul').removeClass('active');
		}
	);
}

function initArticleTools() {
	if ($('#tools').length > 0) {
	
		$('#tools > ul > li > a.print').attr('target','_blank');
		$('#tools > ul > li > a.print').attr('href',function(){
			var url = $(this).attr('href');
			url += '&amp;url='+encodeURIComponent(location.href);
			return url;
		});
		
		/*
		$('#tools > ul > li > a.print').click(function(){
			var url = $(this).attr('href');
			openPrintWindow(url);
			return false;
		});
		*/

		$('#tools > ul > li > a.email').attr('href',function(){
			var url = $(this).attr('href');
			url += '&url='+encodeURIComponent(window.location.href);
			url += '&title='+encodeURIComponent(document.title);
			return url;
		});
		
		$('#tools > ul > li > a.feedback').attr('href',function(){
			var url = $(this).attr('href');
		 	url += '?url='+encodeURIComponent(window.location.href);
		 	url += '&title='+encodeURIComponent(document.title);
		 	return url;
		 });
		
		$('#tools > ul > li').hover(
			function(){
				if ($(this).children('ul').length > 0) {
					$(this).children('ul').addClass('active');
				}
			},
			function(){
				if ($(this).children('ul').length > 0) {
					$(this).children('ul').removeClass('active');
				}
			}
		 );
		 
		 $('#tools > ul > li > ul > li > a.share-facebook').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'sharer.php?u='+encodeURIComponent(location.href);
		 	url += '&amp;t='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-reddit').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'submit?url='+encodeURIComponent(location.href);
		 	url += '&amp;title='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-digg').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'submit?phase=2&amp;bodytext=&amp;tags=';
		 	url += '&amp;url='+encodeURIComponent(location.href);
		 	url += '&amp;title='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 // Yahoo! Buzz
		 
		 $('#tools > ul > li > ul > li > a.share-delicious').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'post?v=2&amp;notes=&amp;tags=';
		 	url += '&amp;url='+encodeURIComponent(location.href);
		 	url += '&amp;title='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-newsvine').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += '_wine/save?popoff=1&amp;tags=';
		 	url += '&amp;u='+encodeURIComponent(location.href);
		 	url += '&amp;blurb='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-furl').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'storeIt.jsp';
		 	url += '?t='+encodeURIComponent(document.title);
		 	url += '&amp;u='+encodeURIComponent(location.href);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-propeller').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'submit/';
		 	url += '?U='+encodeURIComponent(location.href);
		 	url += '&amp;T='+encodeURIComponent(document.title);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-livejournal').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'update.bml';
		 	url += '?url='+encodeURIComponent(location.href);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-blogger').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'blog-this.g';
		 	url += '?u='+encodeURIComponent(location.href);
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-wordpress').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'snap';
		 	return url;
		 });
		 
		 $('#tools > ul > li > ul > li > a.share-google').attr('href',function(){
		 	var url = $(this).attr('href');
		 	url += 'mark?op=add';
		 	url += '&amp;bkmk='+encodeURIComponent(location.href);
		 	url += '&amp;title='+encodeURIComponent(document.title);
		 	return url;
		 });

	}
}

function initArchiveBox() {
	if ($('#archivebox').length > 0) {
		$('#archivebox').hover(
			function(){
				$(this).children('ul').addClass('active');
			},
			function(){
				$(this).children('ul').removeClass('active');
			}
		);
	}
}

function initMediaBox() {
	if ($('#mediabox').length > 0) {
		var mediaCount = $('#mediabox > div.stage > div.panel > a').length;
		var panelCount = Math.ceil(mediaCount/3);
		var panelWidth = $('#mediabox > div.stage > div.panel').width();
		$('#mediabox > div.stage').css('position','relative');
		$('#mediabox > div.stage').css('overflow','hidden');
		$('#mediabox > div.stage > div.panel').css('position','absolute');
		$('#mediabox > div.stage > div.panel').css('width',panelCount*panelWidth);
		// var panelHeight = $('#mediabox > div.stage > div.panel').height();
		// $('#mediabox > div.stage').height(panelHeight);
		
		$('#mediabox > div.pages > a').each(function(i){
			$(this).click(function(){
				var newLeft = -(i*panelWidth);
				$('#mediabox > div.stage > div.panel').animate({
					left: newLeft
				},500);
				$('#mediabox > div.pages > a').removeClass('active');
				$(this).addClass('active');
				return false;
			});
		});
	}
}

function initLangtabs() {
	if ($('#langtabs').length > 0) {
		$('#langtabs > ul.tabnav > li').hover(
			function(){
				if ($(this).attr('rel') != 'next' && $(this).attr('rel') != 'prev') {
					$(this).siblings('li').children('a').removeClass('active');
					$(this).siblings('li').children('ul').removeClass('active');
					$(this).children('a').addClass('active');
					$(this).children('ul').addClass('active');
				}
			},
			function(){
				void(0);
			}
		);
		$('#langtabs > ul.tabnav > li[rel=next] > a').click(function(){
			$(this).parent('li[rel=next]').parent('ul.tabnav').hide();
			$(this).parent('li[rel=next]').parent('ul.tabnav').next('ul.tabnav').show();
			return(false);
		});
		$('#langtabs > ul.tabnav > li[rel=prev] > a').click(function(){
			$(this).parent('li[rel=prev]').parent('ul.tabnav').hide();
			$(this).parent('li[rel=prev]').parent('ul.tabnav').prev('ul.tabnav').show();
			return(false);
		});
	}
}

function switchStyles() {
	if (window.location.search != "") {
		var params = window.location.search;
		if (params.search(/style=white/) != -1) {
			$('head').append('<link rel="stylesheet" type="text/css" href="./css/sidebar_white.css" />');
		}
		if (params.search(/color=/) != -1) { 
			var color = '';
			if (params.search(/color=green/) != -1) {
				color = '#009900';
			} else if (params.search(/color=brown/) != -1) {
				color = '#AD532F';
			} else if (params.search(/color=pink/) != -1) {
				color = '#D1008B';
			} else if (params.search(/color=purple/) != -1) {
				color = '#8F1AB6';
			} else if (params.search(/color=orange/) != -1) {
				color = '#FF6600';
			}
			$('#content > .columnwidth34borderleft > .editorial').css('border-color',color);
			$('#content > .columnwidth34borderleft > .editorial > h3').css('background-color',color);
			$("#sidebar > .column > .special > h3[rel='color']").css('background-color',color);
		}
	}
}

function initLightbox() {
	if (!$('#content').is('.width72,.category')) {
		$('.lightbox').lightbox({fitToScreen: false});
	}
}

function appendAppeal() {
	var html = '<P>&nbsp;</P>';
	html += '<P style="padding:1em; border:1px dotted #999999;"><I>Liebe Leserinnen und Leser, die WSWS benötigt Eure aktive Unterstützung und bittet Euch am Aufbau eines WSWS-Förderkreises teilzunehmen. <a href="/de/spenden/foerderkreis.shtml">Hier geht es zu weiteren Informationen.</a></I></P>';
	$('#content:not(.width72,.category)').append(html);
}

$(document).ready(function(){
	initLanguages();
	initNavigation();
	initArticleTools();
	initArchiveBox();
	initMediaBox();
	initLangtabs();
	//appendAppeal();
});
