
jQuery(document).ready(function() {
	// toggle background image of buttons
	jQuery('button, input:submit').hover(  
		function(){jQuery(this).addClass('hover')},
		function(){jQuery(this).removeClass('hover')}
	);
	
	jQuery('div.summary').css('display', 'none'); /* for ie 6 and 7 this is being done with css also - due to a bug */
	jQuery('.checkbox-toggle').hide();
	jQuery('fieldset.options fieldset.left').css('display','block');
	jQuery('p.options a.options-toggler').removeClass('active').parents('.options').eq(0).next().hide();
	jQuery('.articles-feed a.options-toggler').addClass('active').parents('.options').eq(0).next().show();
	jQuery('.display-options').each(function(){
		jQuery(this).attr('checked', true);
	});


	jQuery('a.options-toggler').click(function(){
		if(jQuery('.checkbox-toggle').is(':hidden'))	jQuery('.checkbox-toggle').show();
		else jQuery('.checkbox-toggle').hide();
		jQuery(this).toggleClass('active').parents('.options').eq(0).next().slideToggle({complete:function(){jQuery('.display-controls').toggle();}}); 
		return false;
	});

	jQuery('a.summary-toggler').click(function(){
		var summaryToggled=jQuery(this).toggleClass('active').parents('.nav').eq(0).next('div.summary-toggled').eq(0);
		var self=this;
		summaryToggled.slideToggle('fast');
		return false;
	});

	/* onload for citation actions */
	jQuery('input.citation-control').each(function(){
		jQuery(this).attr('checked', true);
		if (getBooleanRequestParameter ('citation',"true")=='false'){
			jQuery(this).attr('checked', false);
			jQuery('.display-options input.citation-control').each(function(){
				jQuery('.citation span').hide();
			});
		}
		if (jQuery(this).is(':checked')){
			/* articles page*/
			appendParameter('.next-link','citation','true');
			appendParameter('.previous-link','citation','true');
			appendParameter('.page-link', 'citation', 'true');
		}else{
			appendParameter('.next-link','citation','false');
			appendParameter('.previous-link','citation','false');
			appendParameter('.page-link', 'citation', 'false');
		}
	});

	/* onload for summary actions */
	jQuery('input.summary-control').each(function(){
		jQuery(this).attr('checked', false);
		jQuery('.display-options input.summary-control').each(function(){
			jQuery('p.summary-toggled').hide();
			jQuery('a.summary-toggler').removeClass('active');
		});
		if(getBooleanRequestParameter ('summary',"false")=='true'){
			jQuery(this).attr('checked', true);
			jQuery('.display-options input.summary-control').each(function(){
				jQuery('div.summary-toggled').show();
				jQuery('a.summary-toggler').addClass('active');
			});
		}
		if (jQuery(this).is(':checked')){
			appendParameter('.next-link','summary','true');
			appendParameter('.previous-link','summary','true');
			appendParameter('.page-link','summary','true');
		}else{
			appendParameter('.next-link','summary','false');
			appendParameter('.previous-link','summary','false');
			appendParameter('.page-link','summary','false');
		}
	});

/* Display options*/

	jQuery('.display-options input.abstract-control').click(function(){
		if (jQuery(this).is(':checked')) jQuery('.abstract').show()
		else jQuery('.abstract').hide()
	});

	jQuery('.display-options input.summary-control').click(function(){
		if (jQuery(this).is(':checked')){
			jQuery('div.summary-toggled').show();
			jQuery('a.summary-toggler').addClass('active');
			appendParameter('.next-link','summary','true');
			appendParameter('.previous-link','summary','true');
			appendParameter('.page-link','summary','true');
		}else{
			jQuery('div.summary-toggled').hide();
			jQuery('a.summary-toggler').removeClass('active');
			appendParameter('.next-link','summary','false');
			appendParameter('.previous-link','summary','false');
			appendParameter('.page-link','summary','false');
		}
	});
	
	jQuery('.display-options input.graphical-control').click(function(){
		if (jQuery(this).is(':checked')) jQuery('.graphical-abstract').show()
		else jQuery('.graphical-abstract').hide()
	});

	jQuery('.display-options input.citation-control').click(function(){
		if (jQuery(this).is(':checked')){
			jQuery('.citation span').show();
			appendParameter('.next-link','citation','true');
			appendParameter('.previous-link','citation','true');
			appendParameter('.page-link','citation','true');
		}else{
			jQuery('.citation span').hide();
			appendParameter('.next-link','citation','false');
			appendParameter('.previous-link','citation','false');
			appendParameter('.page-link','citation','false');
		}
	});

	/*generic functions to be used above */
	
	function appendParameter(object , paramName, value){
		jQuery(object).each(function(){
			var link = jQuery(this).attr('href');
			if(link.indexOf(paramName)>-1){
				if(value=='true'){
					link = link.replace(paramName+'=false',paramName+'=true');
				}else{
					link = link.replace(paramName+'=true',paramName+'=false');
				}
			}else{
				if(link.indexOf('?')>-1){
					link = link+'&'+paramName+'='+value;
				}else{
					link = link+'?'+paramName+'='+value;
				}
			}
			jQuery(this).attr('href',link);
		});
	};

	function getBooleanRequestParameter (name, defaultValue){
		var params = window.location.href;
		if(params.indexOf(name)>-1){
			var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
			return results[1] || defaultValue;
		}
		return defaultValue;
	};
});

