$(document).ready(
    function() {
    	$.cookieBar({
            message: 'This site uses cookies. By continuing to use ePubs, you are giving consent to cookies being used.',
            policyButton: true,
            policyText: 'Our cookie policy',
            policyURL: '/privacy-cookies#cookie-policy'
		});

    	//fix issue where IE doesn't scroll into view the selected item
    	
		if (navigator.appVersion.indexOf("MSIE 7.")) {
			$("select[multiple='multiple']").each(function(){
				$(this).find(':selected:first').attr('selected', 'selected');
			});
		}


        $(".linkButton, .pfButton").hover(
            function () {
                $(this).addClass("ui-state-hover");
            },
            function () {
                $(this).removeClass("ui-state-hover");
            }
        );

		//deposit by doi/deposit by bibtex show/hide
        $("#mainForm\\:worksetsEdit, #mainForm\\:controlledKeywordsEdit").hide();
        $(".depositSectionShowHide").each(function(){
        	$(this).click(function(event){
        		event.preventDefault();

        		var sectionShowHideId = $(this).attr("id");
        		sectionShowHideId = sectionShowHideId.replace("ShowHide", "");

        		var panel = $("#mainForm\\:" + sectionShowHideId);
        		panel.toggle();

        		if (panel.is(":visible")) {
        			$(this).text('hide');
        			state = "true";
        		} else {
        			$(this).text('show');
        			state = "false";
        		}
        	});
        });

		//hide filter checkbox
        $(".clearFilter").hide();

	    //show clear filter if filters set
		if ($('#appliedFilters > ul > li:first').length > 0) {
			if ($('#appliedFilters > ul > li:first').text() != "(None)") {
				$('.clearFilter').show();
			}
		}

        //edit form/deposit by doi/deposit by bibtex, add required text if stfc is selected as organisation
		$("#mainForm\\:organisations").find(":selected").each(function(){
			if ($(this).text() == 'Science and Technology Facilities Council') {
				$(".departments > .fieldHelp").html('<span class="required">* Required</span>');

				//reporting year
				$(".reportingYearHelp > span").attr("class", "required");
				$(".reportingYearHelp > span").html('* Required');
			}
		});

		$("#mainForm\\:organisations").change(function(){
			$("option:selected", this).each(function () {
				if ($(this).text() == 'Science and Technology Facilities Council') {
					$(".departments > .fieldHelp").html('<span class="required">* Required</span>');

					//reporting year
					$(".reportingYearHelp > span").attr("class", "required");
					$(".reportingYearHelp > span").html('* Required');

				} else {
					$(".departments > .fieldHelp").html('');

					//reporting year
					$(".reportingYearHelp > span").attr("class", "recommended");
					$(".reportingYearHelp > span").html('* Recommended');
				}
			});
		});
    }

);

function setReportingYearRequiredHelp(){
	$("#mainForm\\:organisations").find(":selected").each(function(){
		if ($(this).text() == 'Science and Technology Facilities Council') {
			$(".reportingYearHelp > span").attr("class", "required");
			$(".reportingYearHelp > span").html('* Required');
		} else {
			$(".reportingYearHelp > span").attr("class", "recommended");
			$(".reportingYearHelp > span").html('* Recommended');
		}
	});
}


function openHelpWindow(pageId) {
	var settings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:1, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL: '/help/context/' + pageId, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
	var windowFeatures =    'height=' + settings.height +
	',width=' + settings.width +
	',toolbar=' + settings.toolbar +
	',scrollbars=' + settings.scrollbars +
	',status=' + settings.status +
	',resizable=' + settings.resizable +
	',location=' + settings.location +
	',menuBar=' + settings.menubar;


	settings.windowName = 'Help';
	settings.windowURL = '/help/context/' + pageId;
	centeredY = (screen.height - settings.height)/2;
	centeredX = (screen.width - settings.width)/2;
	window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();

}

