// jQuery is conflicting with the MailChimp subscribe script so reassign $ to jQuery
var $ = jQuery.noConflict();

var time = {
	
        init : function() {	
		
			var pageId = $("body").attr("id");
		
			time.newsletterSignup();
			
			switch (pageId) {
				case "homepage":
			 		time.columnBalanceHome();
					// init 'their edinburgh' slideshow
					time.initScrollableGallery();
					// init foursquare map, requires maps script in inc/scripts_paul.cfm to be uncommented
					//time.initFoursquareMap();
				break;
				case "theiredinburgh":
					// init 'their edinburgh' slideshow
					time.initScrollableGallery();
				break;
			}
			
			// round corners
			$('input').corner('round 4px');
		
		},
		
		columnBalanceHome : function() {	
		
			$('#newsblog').height(($('#whatsOn').outerHeight()+$('#yourEdinburgh').outerHeight())-$('#quiz').outerHeight()-12);
		
		},
		
		initGalleriffic : function() {
			
			// We only want these styles applied when javascript is enabled
			$('div#galleryThumbs').css({'width' : '466px', 'float' : 'left','display' : 'block'});
			$('div#gallery').css('display', 'block');

			// Initially set opacity on thumbs and add
			// additional styling for hover effect on thumbs
			var onMouseOutOpacity = 0.52;
			$('#galleryThumbs ul.thumbs li').opacityrollover({
				mouseOutOpacity:   onMouseOutOpacity,
				mouseOverOpacity:  1.0,
				fadeSpeed:         800,
				exemptionSelector: '.selected'
			});
			
			// Initialize Advanced Galleriffic Gallery
			var gallery = $('#galleryThumbs').galleriffic({
				delay:                     10000,
				numThumbs:                 8,
				preloadAhead:              8,
				enableTopPager:            false,
				enableBottomPager:         true,
				maxPagesToShow:            5,
				imageContainerSel:         '#slideshow',
				controlsContainerSel:      '#controls',
				captionContainerSel:       '#gallery_caption',
				loadingContainerSel:       '#loading',
				renderSSControls:          false,
				renderNavControls:         true,
				playLinkText:              'Play Slideshow',
				pauseLinkText:             'Pause Slideshow',
				prevLinkText:              '&lsaquo; Previous',
				nextLinkText:              ' Next &rsaquo;',
				nextPageLinkText:          'Next &rsaquo;',
				prevPageLinkText:          '&lsaquo; Prev',
				enableHistory:             false,
				autoStart:                 true,
				syncTransitions:           true,
				defaultTransitionDuration: 1600,
				onSlideChange:             function(prevIndex, nextIndex) {
					// 'this' refers to the gallery, which is an extension of $('#thumbs')
					this.find('ul.thumbs').children()
						.eq(prevIndex).fadeTo(800, onMouseOutOpacity).end()
						.eq(nextIndex).fadeTo(1300, 1.0);
				},
				onPageTransitionOut:       function(callback) {
					this.fadeTo(0, 0.0, callback);
				},
				onPageTransitionIn:        function() {
					this.fadeTo(800, 1.0);
				}
			});
		
		},
		
		initScrollableGallery : function() {

			$("#galleryThumbs").scrollable({
				circular: true					   
			}).navigator();

				$(".items img").click(function() {

				// see if same thumb is being clicked
				if ($(this).hasClass("active")) { return; }

				// calclulate large image's URL based on the thumbnail URL, remove "_thumb" from filename
				var url = $(this).attr("src").replace("_thumb", "_450x300");

				// get handle to element that wraps the image and make it semi-transparent
				var wrap = $("#galleryImage").fadeTo("medium", 0.5);

				// the large image
				var img = new Image();

				// grab the caption and holder element
				var captionHolder = $("#galleryCaption .caption");
				var caption = $(this).next(".caption").html();

				// call this function after it's loaded
				img.onload = function() {

					// make wrapper fully visible
					wrap.fadeTo("fast", 1);

					// change the image
					wrap.find("img").attr("src", url);

					// change the caption
					captionHolder.html(caption);

				};

				img.src = url;

				// activate thumn
				$(".items img").removeClass("active");
				// as this is on a loop, find 'duplicated' thumbs and also apply active class to them
				var currentThumb = $(this).attr("src");
				$(".items img[src='"+currentThumb+"']").addClass("active");

				// when page loads simulate a "click" on the first image
				//}).filter(":first").click();
			});
			
			// rollovers for thumbnails
			$(".items img").hover(function() {
				$(this).addClass("over");
			}, function() {
				$(this).removeClass("over");
			});

		},
		
		newsletterSignup : function() {	
		
			$('a#signMeUp').click(function() {
				$('#mc-embedded-subscribe-form').submit();
				return false;
			});
		
		},
		
		initFoursquareMap : function() {
			var geoXml = new GGeoXml("http://feeds.foursquare.com/history/3X1QSCYH5ZPS1V5O3DAKN25LS12FCA55.kml");
			var map = new GMap2(document.getElementById('foursquareMap'));
			var edinburgh = new GLatLng(55.9485572,-3.2096673);
			map.setCenter(edinburgh, 15);
			// setup 10 random points
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var lngSpan = northEast.lng() - southWest.lng();
			var latSpan = northEast.lat() - southWest.lat();
			map.addOverlay(geoXml);
			
		}
};

$(document).ready(time.init);
