/*

site.js - jQuery behaviours common to all pages in the site

*/


var currentMap = "";
var map, link;

(function($) {  // anonymous function closure for $ portability
  
  

  $(document).ready( function() {
    
    
    // setup the menus
    
    $('.tm').menu();
    
    // Preload images (menu)
    $.preloadImages("resources/images/menu.png");
    
    $('.carousel').carousel({
      auto: 7000,
      duration: 2500
    });

    $('.carousel')
      .bind("carousel.beforescroll", function(event, currentItem, newItem) {
        // fade the current content out
        var carousel = $(this);
        var currentContent = $(currentItem).find(".content");
        var newContent = $(newItem).find(".content");
        
        newContent.hide(); // hide the new content, so that it may be faded in
        
        $(this).queue( function() { currentContent.animate({top: ( currentContent.metadata().top || 100 ) + 10, opacity: "toggle"},{ duration: "normal", complete: function() { carousel.dequeue();  } } ); } );
      })
      .bind("carousel.afterscroll", function(event, currentItem) {
        var carousel = $(this);
        var currentContent = $(currentItem).find(".content");
        $(this).queue( function() { currentContent.css({top: (currentContent.metadata().top || 100)}).fadeIn({ duration: "normal", complete: function() { carousel.dequeue();  } } ); } );
      });
      
    
    $('.carousel .content')
      .mouseenter( function(event) {
        $(this).closest(".carousel").carouselPause();
      })
      .mouseleave( function(event) {
        $(this).closest(".carousel").carouselPlay();
      });


    $('.content-reveal').contentReveal();
    $('.mb-content-reveal').mbcontentReveal();
    
	$("#main-image img").live("click", function(){
		$("#main-image .scroll").animate({"left": "-500px"}, 600, 'easeInOutExpo');
	});
	
	$('.activities-list li').hover(function(){
		$(".overlay", this).stop().animate({top:'-55px'},{queue:false,duration:160});
	}, function() {
		$(".overlay", this).stop().animate({top:'90px'},{queue:false,duration:160});
	});
    
    
    $("p.view-hours a").click(function(){
    	$("#main-image .scroll, #map").animate({"left": "-450px"}, 600, 'easeInOutExpo', function(){
    		$("#openinghours").animate({"left": "50px"}, 600, 'easeInOutExpo');
    	});
	    return false;
	});
    
    $("p.view-map a").click(function(){
    
    	currentMap = this.hash;
    
    	if(!$(this).hasClass("on")){
	    	map = new GMap2(document.getElementById('map'));
	    	$(this).addClass("on");
	    }
    	
    	$("#main-image .scroll, #openinghours").animate({"left": "-450px"}, 600, 'easeInOutExpo', function(){
			$('.club-gallery .on').removeClass('on');
	    	$("#map").animate({"left": "50px"}, 600, 'easeInOutExpo', function(){
				
				var bondi = new GLatLng(-33.893068,151.249632);
				var wentworth = new GLatLng(-33.865373386680076, 151.21037006378174);
				var parkstreet = new GLatLng(-33.873459,151.208791);
				
    			if(currentMap == '#map-bondi-junction'){
    				map.setCenter(bondi, 16);
				    var markers = [];
				    marker = new GMarker(bondi);
				    map.addOverlay(marker);
				    markers[0] = marker;
				    currentMap = "bondi";
    			}
    			else if(currentMap == "#map-bligh-street"){
	    			
    				map.setCenter(wentworth, 16);
				    var markers = [];
				    marker = new GMarker(wentworth);
				    map.addOverlay(marker);
				    markers[0] = marker;
				    currentMap = "wentworth";
    			} else if(currentMap == "#map-park-street"){    				
    				map.setCenter(parkstreet, 16);
				    var markers = [];
				    marker = new GMarker(parkstreet);
				    map.addOverlay(marker);
				    markers[0] = marker;
				    currentMap = "parkstreet";
    			}
    		});
    	});
    	return false;
    });
    
    
    $(".club-gallery a").click(function(){
    	return false;
    });
    
    $(".club-gallery a").hover(function(){
	
		link = this;		
		if(!$(this).hasClass("on")){	            
            $("#main-image .scroll, #map, #openinghours").animate({"left": "-450px"}, 400, 'easeInOutExpo', function(){
    			var img = new Image();
        		$(img).load(function () {    	
            	
	            	$('#main-image .scroll').empty().append(img);
	            	$("#main-image .scroll").animate({"left": "50px"}, 600, 'easeOutExpo');	
	            	
	            	//$('.polaroid-gallery .active').removeClass('active');
		            $('.club-gallery .on').removeClass('on');
					$(link).addClass("on");
		        }).error(function () {
		            // notify the user that the image could not be loaded
		        }).attr('src', link.href);
            });	            
		}
    	return false;
    });
    
    $(".slide-container ul li:not(.on)").css({"display" : "none"});
    
    
    /* SLIDE FUNCTION */
    $(".slide-links a").click(function(){
    	var link = this;
		
		if(!$(this).hasClass("on")){
            $(".slide-container ul").animate({"left": "-"+($(".slide-container li").width()+50)+"px"}, 400, 'easeInExpo', function(){
            	var  whichSlide = $(link).attr('id').substr(5);
            	$('.slide-container .on').removeClass('on');
            	$(".slide-container ul li").css({"display" : "none"})
            	$("#slide-"+whichSlide).addClass("on").css({"display" : "block"});
            	$(".slide-container ul").animate({"left": "0"}, 600, 'easeOutExpo');	
            });
            
            $('.slide-links .on').removeClass('on');
			$(link).addClass("on");	        
		}
    	return false;
    });
    /* END SLIDE FUNCTION */
    
    /* FADE FUNCTION */
    $(".fade-content li:not(.on)").hide();
    $('.activity .col-2 li').css({'position' : 'absolute','left' : '0px', 'top' : '0px'});
    
    
    $(".fade-links a").click(function(){
    	var  whichSlide = $(this).attr('id').substr(5);

		if(!$(this).hasClass("on")){
			$(".fade-content li.on").fadeOut(600,function(){	

				$("#content_"+whichSlide).addClass("on");
			});
			
			setTimeout(function(){
					$("#content_"+whichSlide).fadeIn(600);
			}, 600);
			
	        $('.fade-links .on').removeClass('on');
			$(this).addClass("on");	        
		}
    	return false;
    });
    /* END FADE FUNCTION */
    
    /* START LIGHTWINDOW */
    $("a.apply-online").colorbox({width:"480px", height:"470px", iframe:true});
    $("a.register-online, a.lightbox, a.freeze-form").colorbox({width:"480px", height:"560px", iframe:true});
    $("a.newsletter").colorbox({width:"480px", height:"280px", iframe:true});
    /* END LIGHTWINDOW */
    
  });
  
  
})(jQuery);
