loadGoogleCSE() //load custom search box from google
   $(document).ready(function(){

	flipElements(); //Flip boxes on about page



/* Animate Dots on main page */

                  $('menu.dots li a').live('click', function(e){
                                       var i = $(this).parent().index();
                                      var slider = $('.slider');

                                      $('.slider:visible').fadeOut(1200);
                                      slider.eq(i).fadeIn(1200);

                                      $('menu.dots .active').removeClass('active');
                                      $(this).addClass('active');

                                      e.preventDefault();
                                      });

/* Animate news slider on main page */

    $('.slider2').eq(0).animate({'margin-top':'-=1200px'}, 1000);
                  $('#right>div').live('click', function(){
						if (!($(this).hasClass('active'))){

                                                        var a = $(this).index();
                                                        var slider2 = $('article.slider2');
                                                   
                                                        $('article.slider2:visible').animate({'margin-top':'+=1200px'}, 1500);
                                                        $('article.slider2:visible').fadeOut(1);
                                                        slider2.eq(a).fadeIn(1);
                                                        slider2.eq(a).animate({'margin-top':'-=1200px'}, 1500);

                                                        $('menu#right>div.active').removeClass('active');
                                                	$(this).addClass('active'); 
                                                       
						} //end if clause    

                                      }); // end click function

                  $('#right>div').hover(function() {
			$(this).children('span').children('em').css({'textDecoration': 'underline', 'color':'#0b3dee'});
		}, function() { 
		$(this).children('span').children('em').css({'textDecoration': 'none', 'color': '#0b3d92'});}

                  ); // end hover function




/* About page slider */

var hash = window.location.hash;
var pageLoad = 0;
var slider3 = $('div.slider3');
var tabs = $('menu.sub-nav li a')
//check if hash tag exists in the URL
switch(hash){

	case '#who':
		slider3.eq(0).slideDown(2000, 'easeOutBack');
		tabs.eq(0).addClass('active');
		window.location.hash = '#'
		break;
	case '#what':
		tabs.eq(1).addClass('active');
		slider3.eq(1).slideDown(2000, 'easeOutBack');
		window.location.hash = '#'
		break;
	case '#when':
		tabs.eq(2).addClass('active');
		slider3.eq(2).slideDown(2000, 'easeOutBack');
		window.location.hash = '#'
		break;
	case '#where':
		tabs.eq(3).addClass('active');
		slider3.eq(3).slideDown(2000, 'easeOutBack');
		window.location.hash = '#'
		break;
	case '#how':
		tabs.eq(4).addClass('active');
		slider3.eq(4).slideDown(2000, 'easeOutBack');
		window.location.hash = '#'
		break;
	default:
		var pageLoad = 1;
		$('#about-help').delay(500).fadeIn(1000);
}
		//Flash tabs on click
               $('a.flashtabs').live('click', function(){
			flashtabs();
			setTimeout('flashtabs()', 600);
			setTimeout('flashtabs()', 1200);
			return false;
               });
		
		//Animate slider
		$('menu.sub-nav li a').live('click', function(e){
			var b = $(this);

			if (b.hasClass('active')) {
				e.preventDefault();
				return;
			}

			var a = $(this).parent().index();

			if  (pageLoad) {
				$('#about-help').fadeOut(500, function(){
				slider3.eq(a).slideDown(2000, 'easeOutBack');
				b.addClass('active');
				pageLoad = 0;
				});
			} else {
				$('div.slider3:visible').stop(true, true).slideUp(1500, 'easeInBack', function() {
				tabs.removeClass('active');
				slider3.eq(a).slideDown(2000, 'easeOutBack');
				b.addClass('active');  
				});
			}

			e.preventDefault();
		});

/*Help button animations */
		//Home page
		$('a#home-help').live('click', function(e){
			$('#news-slider').fadeOut(1000);	
			$('div.home-help').fadeIn(1000);
		});

		$('a#reset-home').live('click', function(e){
			$('div.home-help').fadeOut(1000);
			$('#news-slider').fadeIn(1000);				
		});

		//Events page
		$('a#events-help').live('click', function(e){
			$('#events-top').fadeOut(1000);	
			$('div.events-help').fadeIn(1000);
		});


		$('a#reset-events').live('click', function(e){
			$('div.events-help').fadeOut(1000);
			$('#events-top').fadeIn(1000);				
		});

		//About page
		$('a#help').live('click', function(e){
			if (pageLoad == 1){
				$('#about-help').fadeOut(200).fadeIn(400);
			} else {
				$('div.slider3:visible').stop(true, true).slideUp(1500, 'easeInBack', function() {
					$('menu.sub-nav li a.active').removeClass('active');
					pageLoad = 1
					$('#about-help').fadeIn(1000);
				});
				
			}
			e.preventDefault();
		});

/*Google search animation*/

//Bind click function to search results
$('td.gsc-table-cell-snippet-close').live('click', function() {
	window.open($(this).children().children().attr('href'));
	return false;
});

//move search button up on large screens
if (screen.width>=1200){
	$('#search1').css('top','41px');
}

//Measure screen size to determine whether menu needs to be pushed up
//and search box down

var moveAmount = '-25px';
if (screen.width>=1450){
var moveAmount = '0px';
$('#search').css('top','31px');
}

//Testing:	
//$('nav ul').animate({'margin-top':moveAmount}, 1000);

//Show search box
$('.gsc-search-show').live('click', function(){
	$('body').css('overflow-x','hidden');
	$(this).fadeOut(1000);
	$('#search').show().animate({'right':'20px'}, 1000);
	$('nav ul').animate({'margin-top':moveAmount}, 1000, function(){
		$('body').css('overflow-x','visible');
	});
	return false;
});

//Hide search box
$('.gsc-clear-button').live('click', function(){
	$('body').css('overflow-x','hidden');
	$('.gsc-search-button').delay(500).fadeIn(500);
	$('#search').animate({'right':'-420px'}, 1000, function(){
		$('#search').hide()
		$('body').css('overflow-x','visible');
	});
	$('nav ul').animate({'margin-top':'0px'}, 1000);
	return false;
});

//Jiggle search button
$('a.jiggle-search').live('click', function(){
	$('#search1').children('input').css('backgroundColor','red !important');
	var jiggles = 3; // how any jiggles?
	for (i=1;i<=jiggles;i++){
		var j = i * 150;
		setTimeout('jiggleFunc()', j);
	}
	var delay2 = j + 400;
	setTimeout('$(\'#search1\').children(\'input\').css(\'backgroundColor\',\'#f4f1d3 !important;\');', delay2)	
	return false;
});

/*fancybox animation*/
                        $("a.fancybox").fancybox({
                            'transitionIn' : 'fade',
                            'transitionOut' : 'fade',
                            'speedIn' : 1000,
                            'speedOut' : 400,
                            'centerOnScroll' : true,
			    'titleShow' : false,
                         });


                        $("a.fancybox2").fancybox({
							'type' : 'image',
                            'easingIn'	:  'easeOutBack',
                            'easingOut'	:  'easeInBack',
                            'transitionIn' : 'elastic',
                            'transitionOut' : 'elastic',
                            'speedIn' : 800,
                            'speedOut' : 800,
        	            	'titlePosition' : 'inside',
                            'centerOnScroll' : true,
							'cyclic' : true,
                         });

/*scrolling animation for links*/
  function filterPath(string) {
  return string
    .replace(/^\//,'')
    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
    .replace(/\/$/,'');
  };
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if ( $(this).hasClass('scrollable')
    &&  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 1000, function() {
            location.hash = target;
          });
        });
      }
    }
  });


  function scrollableElement(els) {   // use the first element that is "scrollable"
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  };

});

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/*various functions for use with areas above*/


function scrollcolor(i){
		$('menu.sub-nav li a').eq(i).toggleClass('active');	
		};
function flashtabs(){
				setTimeout('scrollcolor(0)', 100);
				setTimeout('scrollcolor(0)', 200);
				setTimeout('scrollcolor(1)', 200);
				setTimeout('scrollcolor(1)', 300);
				setTimeout('scrollcolor(2)', 300);
				setTimeout('scrollcolor(2)', 400);
				setTimeout('scrollcolor(3)', 400);
				setTimeout('scrollcolor(3)', 500);
				setTimeout('scrollcolor(4)', 500);
				setTimeout('scrollcolor(4)', 600);
};

function jiggleFunc(){
$('#search1').animate({'right':'-=5px'}, 50, function(){
	$(this).animate({'right':'+=10px'}, 50, function(){
		$(this).animate({'right':'-=5px'}, 50)
	});
});
};

function loadGoogleCSE(){
  google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
  google.setOnLoadCallback(function() {
    var customSearchControl = new google.search.CustomSearchControl(
      '006986151619831629873:hmyahpybpoa');

    customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
    var options = new google.search.DrawOptions();
    options.setAutoComplete(true);
    options.enableSearchboxOnly("http://www.rotarysouthamptonwest.co.uk/search.php");
    customSearchControl.draw('cse-search-form', options);
  }, true);
};

function flipElements(){
/* Flip elements for about page */
	
	$('.sponsorFlip').bind("click",function(){
		
		// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
		
		var elem = $(this);
		
		// data('flipped') is a flag we set when we flip the element:
		
		if(elem.data('flipped'))
		{
			// If the element has already been flipped, use the revertFlip method
			// defined by the plug-in to revert to the default state automatically:
			
			elem.revertFlip();
			
			// Unsetting the flag:
			elem.data('flipped',false)
		}
		else
		{
			// Using the flip method defined by the plugin:
			
			elem.flip({
				direction:'lr',
				speed: 500,
				onBefore: function(){
					// Insert the contents of the .sponsorData div (hidden from view with display:none)
					// into the clicked .sponsorFlip div before the flipping animation starts:
					
					elem.html(elem.siblings('.sponsorData').html());
				}
			});
			
			// Setting the flag:
			elem.data('flipped',true);
		}
	});
};

