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 == 'Search the WSWS') {
		element.value = '';
	}
}

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

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 initLightbox() {
	if (!$('#content').is('.width72,.category')) {
		$('.lightbox').lightbox({fitToScreen: true});
	}
}

function initSidebarAccordion() {
	// contact us comment field
	$('#fm_comments.fmtextarea').focus(function(){
		if ($(this).val() == 'Enter your comments') {
	    	$(this).val('');
		}
	});
	$('.fmtextarea.sidebar').blur(function(){
		if ($(this).val() == '') {
	    	$(this).val('Enter your comments');
		}
	});
	
	// contact us email field	
	$('#fm_email.fmtext').focus(function(){
		if ($(this).val() == 'Email Address') {
	    	$(this).val('');
		}
	});
	$('#fm_email.fmtext').blur(function(){
		if ($(this).val() == '') {
	    	$(this).val('Email Address');
		}
	});
	
	// Newsletter email field	
	$('#newsletter_email').focus(function(){
		if ($(this).val() == 'Email Address') {
	    	$(this).val('');
		}
	});
	$('#newsletter_email.sidebar').blur(function(){
		if ($(this).val() == '') {
	    	$(this).val('Email Address');
		}
	});
	
}

function appendAppeal() {
	var html = '<p><b>The World Socialist Web Site needs your support!</b><br /><a href="/donate.shtml">Click here for a special message from US SEP Chairman David North &raquo;</a></p>';
	if (!$('#content').is('.width72,.category')) {
		$('#content').append('<div id="attention">' + html + '</div>');
	} else {
		if ($('#content').is('.width72')) {
			$('#content.width72').prepend('<div id="attention" style="margin:0 1em 2em 0; display:none;">' + html + '</div>');
			$('#attention').animate({opacity:1.0},1000).slideDown(1000);
		}
	}
}

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);
		}
	}
}

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



/*Variable for opening random sidebar panels */  
	var rand_no = Math.floor((3+1)*Math.random()) + 0;


/*Script for Expanding 'more meetings' */
  $(document).ready(function(){
  	  $("#morelink").show()
	  $("#extrameetings").hide()
      $("#moremeetings").hover(function(){
      $("#morelink").hide();
      $("#extrameetings").show({ 
      } );
      return false;
      });
  });


/*Script for animating the sidebar */
  $(document).ready(function(){
    $("#accordion").accordion({
      event: 'click',
      active: rand_no,
      collapsible: true,
      autoHeight: false
    });
    $("#accordion > h3").focus(function () {
      $(this).blur();
    });
  });
  

/*Function for pulling random sidebar image ads */
  
function random_imglink(){
  var rotatingimages=new Array()
  rotatingimages[1]="http://petrov.dreamhosters.com/sidebarimages/redstar.jpg"
  rotatingimages[2]="http://petrov.dreamhosters.com/sidebarimages/rogovin.jpg"

  var imagelinks=new Array()
  imagelinks[1]="http://mehring.com/showBook.php?book_id=RED_STAR_OVER_RUSSIA"
  imagelinks[2]="http://mehring.com/showBook.php?book_id=STALINS_TERROR_1937_TO_1938_CLOTH"

  var ry=Math.floor(Math.random()*rotatingimages.length)

  if (ry==0)
     ry=1
     document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+rotatingimages[ry]+'" border=0></a>')
}


/*Contact form active highlighting - currently disabled*/
/*
		$(document).ready(function()
		{
			$('input').focus(function(){
				$(this).parents('span').addClass("over");
			}).blur(function(){
				$(this).parents('span').removeClass("over");
			});
		});
*/



// Function for appending pdf generation to article tools

/*
  	$(document).ready(function(){
  		var template = $('meta[name=template]').attr("content")
  		if( template == "post.htm") {
  			var appendpdf = '<li id="pdfversion"><a id="pdfversion" class="PDF" href="#">PDF</a></li>'
			$("ul.tools").append(appendpdf);  	  	
  		}  	
  	});
*/


// Function for opening  new window with print version
$(function(){
	var curdocurl = window.location;
    $('a#pdfversion').click(function(){
        window.open("http://intsse.com/rss-to-pdf/makepdf_modded.php?url=" + curdocurl);
        return false;
    });
});


// Function for This Week in History tabs

  $(document).ready(function(){
    $("#thisweekinhistory").tabs({event: 'click', fx:{opacity: 'toggle'}})
    .tabs('select', rand_no);
  });
  
// Function for rotating sidebar ads
function rotating_sidebar(){
  var rotatingimages=new Array()
  rotatingimages[1]="/images/sidebar/rotating/redstar.jpg"
  rotatingimages[2]="/images/sidebar/rotating/rogovin.jpg"
  rotatingimages[3]="/images/sidebar/rotating/Revolution_betrayed.jpg"
  rotatingimages[4]="/images/sidebar/rotating/essentialmarx.png"
  rotatingimages[5]="/images/sidebar/rotating/marxism_history.jpg"


  var imagelinks=new Array()
  imagelinks[1]="http://mehring.com/showBook.php?book_id=RED_STAR_OVER_RUSSIA"
  imagelinks[2]="http://mehring.com/showBook.php?book_id=STALINS_TERROR_1937_TO_1938_CLOTH"
  imagelinks[3]="http://mehring.com/showBook.php?book_id=REVOLUTION_BETRAYED"
  imagelinks[4]="http://mehring.com/showBook.php?book_id=ESSENTIAL_MARX"
  imagelinks[5]="http://mehring.com/showBook.php?book_id=MARXISM_HISTORY_SOCIALIST_CONSCIOUSNESS"

  var ry=Math.floor(Math.random()*rotatingimages.length)
  if (ry==0)
     ry=1
     document.write('<h3 style="width:186px;">From Mehring Books</h3><div id="mehringad" style="background-color:#cecece;background-repeat: no-repeat;background-image:url(/images/sidebar/rotating/background.png);"><a href='+'"'+imagelinks[ry]+'"'+'><div id="mehringimage" style=""><img src="'+rotatingimages[ry]+'" border=0></div></a></div>')
}

// Function for small rotating sidebar ads
function rotating_sidebar_small(){
  var rotatingimages=new Array()
  rotatingimages[1]="/images/sidebar/rotating/redstar.jpg"
  rotatingimages[2]="/images/sidebar/rotating/rogovin_scaled.jpg"
  rotatingimages[3]="/images/sidebar/rotating/Revolution_betrayed_scaled.jpg"
  rotatingimages[4]="/images/sidebar/rotating/essentialmarx_scaled.png"
  rotatingimages[5]="/images/sidebar/rotating/marxism_history_scaled.jpg"


  var imagelinks=new Array()
  imagelinks[1]="http://mehring.com/showBook.php?book_id=RED_STAR_OVER_RUSSIA"
  imagelinks[2]="http://mehring.com/showBook.php?book_id=STALINS_TERROR_1937_TO_1938_CLOTH"
  imagelinks[3]="http://mehring.com/showBook.php?book_id=REVOLUTION_BETRAYED"
  imagelinks[4]="http://mehring.com/showBook.php?book_id=ESSENTIAL_MARX"
  imagelinks[5]="http://mehring.com/showBook.php?book_id=MARXISM_HISTORY_SOCIALIST_CONSCIOUSNESS"

  var ry=Math.floor(Math.random()*rotatingimages.length)
  if (ry==0)
     ry=1
     document.write('<div id="mehringad" style=""><h3>Mehring Books</h3><a href='+'"'+imagelinks[ry]+'"'+'><div id="mehringimage_small" style=""><img src="'+rotatingimages[ry]+'" border=0></div></a></div>')
}

/* Hover effect for Mehring Ads */

$(document).ready(function(){
    
    $("#mehringad").hover(
      function () {
        $("#mehringbuyme").show("slow")
        $("#mehringimage").animate({ 
        opacity: .9,
        marginLeft: "0.5in",
        borderWidth: "10px"
      }, 500 )
      }, 
      function () {
        $("#mehringbuyme").hide("slow")
                $("#mehringimage").animate({ 
        width: "100%",
        opacity: 1,
        marginLeft: "0in",
        borderWidth: "10px"
      }, 500 )

      }
    );
  });

