$(document).ready(function() {

 $('#txtSearchKeywords').click(function() {
 $(this).select();
 });

 if ($("#home").length> 0) {
 $("#home").css("background", "#FFFFFF url('/siteFiles/resources/images/backgrounds/bg-" + Math.ceil(Math.random() * 4) + ".jpg') no-repeat top center");
 }

 if ($("#header-logo").length> 0) {
 $("#header-logo").append('<a href="home.aspx"><img src="siteFiles/resources/images/logos/' + Math.ceil(Math.random() * 9) + '.png" /></a>');
 }

 if ($('#home-top-cycle-items').length> 0) {
 $('#home-top-cycle-items').cycle({
 fx: 'scrollLeft',
 speed: 500,
 timeout: 6000,
 cleartype: true,
 cleartypeNoBg: true,
 pager: '#home-top-cycle-pager-items-links',
 next: '#home-top-cycle-pager-next',
 prev: '#home-top-cycle-pager-prev',
 pagerAnchorBuilder: function(idx, slide) {
 return '<a href="#" class="item"><img src="/siteFiles/images/pager-item-spacer.png"/></a>';
 }
 });
 };
 
 function menuMouseOver(id) {
 var intX = $('#top-nav_' + id).position().left;
 var width = 472; //$('#top-nav-sub-' + id).width() + 2
 var fullWidth = 930; //$('#wrapper').width()
 var blnBig = intX> (fullWidth - width);
 var index = $('#top-nav_' + id).index();
 var left = 0;
 if (blnBig == true) {
 var offset = ((8 - index) * 15);
 left = 450 - offset;
 } else {
 left = intX - 50;
 }
 $('#top-nav-sub-' + id).css('left', left);
 $("#top-nav_" + id).addClass("top-nav-selected");
 $("#top-nav-sub-" + id).stop().fadeTo('fast', 1).show();
 }

 function menuMouseOut(id) {
 $("#top-nav_" + id).removeClass("top-nav-selected");
 $("#top-nav-sub-" + id).stop().fadeTo('fast', 0, function() {
 $(this).hide();
 });
 }

 function megaHoverOver(e) {
 var blnCallShowMenu = true;
 if ($(this).hasClass('top-nav-selected') == false) {
 if ($(this).attr("class") == "top-nav-sub") {
 var id = parseInt($(this).attr("id").replace("top-nav-sub-", ""));
 if ($(this).is(':visible') == true) { blnCallShowMenu = false; }
 } else {
 var id = parseInt($(this).attr("id").replace("top-nav_", ""));
 }
 if (e.pageY < NAV_SUB_TOP) {
 $(".top-nav-sub").hide();
 }
 if (blnCallShowMenu == true) {
 menuMouseOver(id);
 }
 }
 }

 function megaHoverOut(e) {
 if ($(this).attr("class") == "top-nav-sub") {
 var id = parseInt($(this).attr("id").replace("top-nav-sub-", ""));
 } else {
 var id = parseInt($(this).attr("id").replace("top-nav_", ""));
 }
 blnOutsideNavSub = false;
 blnOutsideNavItem = false;
 var intXPos = e.pageX;
 var intYPos = e.pageY;

 if (($(this).attr("class") == "top-nav-sub") || (intYPos < 122)) {
 var intNavItemTop = 94; //$('#top-nav_' + id).offset().top
 var intNavItemBottom = 112; //$('#top-nav_' + id).offset().top + $('#top-nav_' + id).height()
 var intNavItemLeft = $('#top-nav_' + id).offset().left;
 var intNavItemRight = $('#top-nav_' + id).offset().left + $('#top-nav_' + id).width();
 if ($('#top-nav-sub-' + id).length) {
 if (intYPos < NAV_SUB_TOP || intYPos> NAV_SUB_BOTTOM || intXPos < NAV_SUB_LEFT || intXPos> NAV_SUB_RIGHT) {
  blnOutsideNavSub = true;
 }
 }
 if (intYPos < intNavItemTop || intYPos> intNavItemBottom || intXPos < intNavItemLeft || intXPos> intNavItemRight) {
 blnOutsideNavItem = true;
 }
 if (blnOutsideNavSub == true && blnOutsideNavItem == true) {
 menuMouseOut(id);
 }
 }
 }

 var config = {
 sensitivity: 100,
 interval: 50,
 over: megaHoverOver,
 timeout: 0,
 out: megaHoverOut
 };


 // extra config - dimensions of revealed panel (for performance)
 var NAV_SUB_TOP = 123;
 var NAV_SUB_BOTTOM = 358;
 var NAV_SUB_LEFT = 486.5;
 var NAV_SUB_RIGHT = 956.5;

 // some intialisation for menu
 $('.top-nav li').hoverIntent(config);
 $('.top-nav-sub').hoverIntent(config);

 // navigation class set
 $('.content-left-navigation').children('li').addClass('level1');
 $('.content-left-navigation .level1').children('a').addClass('level1-link');
 $('.content-left-navigation .level1').children('ul').addClass('content-left-navigation-level2');
 $('.content-left-navigation-level2').children('li').addClass('level2');
 $('.content-left-navigation-level2 .level2').children('a').addClass('level2-link');
 $('.content-left-navigation-level2 .level2').children('ul').addClass('content-left-navigation-level3');
 $('.content-left-navigation-level3').children('li').addClass('level3');
 $('.content-left-navigation-level3 .level3').children('a').addClass('level3-link');

 function calHoverOver() {
 if ($(this).find('.calendar-event-more-event').length) {
 $(this).find('.calendar-event-more-event').stop().fadeTo('fast', 1).slideDown();
 }
 }

 function caloverOut() {
 $(this).find('.calendar-event-more-event').stop().fadeTo('fast', 0, function() {
 $(this).slideUp();
 });
 }

 var calconfig = {
 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher) 
 interval: 100, // number = milliseconds for onMouseOver polling interval
 over: calHoverOver, // function = onMouseOver callback (REQUIRED) 
 timeout: 300, // number = milliseconds delay before onMouseOut
 out: caloverOut // function = onMouseOut callback (REQUIRED) 
 };

 $('.calendar-event-more-link li').hoverIntent(calconfig);

 if ($('#whatson-main-options-view').length) {

 $('#whatson-main-options-view a').click(function() {

 $('#whatson-main-options-view a.selected').removeClass('selected');
 $(this).addClass('selected');
 $('.whatson-options-option input[type=checkbox]').attr('checked', false);
 var options = $(this).attr('id').replace('link', 'options');
 if ($('.whatson-options').length) {
 $('.whatson-options').slideUp(300);
 $('#' + options).delay(300).slideDown(350);
 } else {
 $('#' + options).slideDown(350);
 };

 });
 }

 if ($(".content-accordion").length> 0) {
 $(".content-accordion").accordion({
 header: 'h5',
 autoHeight: false,
 collapsible: true,
 active: false
 })
 }

 $("#befm10019 .button").val("Donate");
 $("#befm10022 .button").val("Donate");

 WebFontConfig = { // load custom font
 custom: { families: ['halle'],
 urls: ['/siteFiles/css/fonts.css']
 }
 };

 (function() { // google font api
 var wf = document.createElement('script');
 wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
 '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
 wf.type = 'text/javascript';
 wf.async = 'true';
 var s = document.getElementsByTagName('script')[0];
 s.parentNode.insertBefore(wf, s);
 })();

 $('#butDonate').bind('click', function(e) {
 if ($('#chkDonate_Monthly').attr('checked') == true) {
 window.location.href = 'regular-giving.aspx';
 }
 else if ($('#chkDonate_Once').attr('checked') == true) {
 window.location.href = 'donate.aspx';
 } else {
 window.location.href = 'what-we-do.aspx';
 }
 }); 


 function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i = 0; i < anchors.length; i++) {
 var anchor = anchors[i];
 if (anchor.getAttribute("href") &&
 anchor.getAttribute("rel") == "external")
 anchor.target = "_blank";
 }
 }
 window.onload = externalLinks;

});

function openArchive() {
 window.open("http://archive.halle.co.uk/", "archive", "menubar=0,resizable=0,scrollbars=1,width=580,height=580");
}





