var TrollSpotters = function() {
	var _gallery_width = 385;

	function init() {
		$(document.body).addClass('js');
		
		pngFix();
		attachPopups();
		attachOverlays();
		attachTabs();
		attachSliderGallery();
		attachLazyLoad();
		attachTracking();
		attachOutboundLinks();
	}
	
	function pngFix() {
		if (typeof(jQuery.fn.pngFix) == 'function') {
		    $(document).pngFix({
			blankgif: '/img/blank.gif'
		    });
		}
	}
	
	function attachPopups() {
		$("a[rel~='popup']").click(function(e) {
			e.preventDefault();
			window.open(
				$(this).attr('href'),
				'skanepopup',
				'location=1,status=0,scrollbars=1,width=550,height=400'
			); 
		});
		
		$("a.prizePop").click(function(e) {
			e.preventDefault();
		});
		
		$("a.prizePop").mouseenter(function(e) {
			$(this).parent().parent().find(".prizeDetail").hide();
			$(this).parent().children(".prizeDetail").fadeIn('fast');
			$(this).parent().children(".prizeDetail").css({'z-index': 20});
			
		});
		
		$("a.prizePop").mouseleave(function(e) {
			$(this).parent().children(".prizeDetail").fadeOut('fast');
			$(this).parent().children(".prizeDetail").css({'z-index': 1});
		});
		
	}

	function attachOverlays() {
	    $("a[rel='overlay']").click(function(e) {
		e.preventDefault();
		launchOverlay($(this).attr('href'));
	    });

	    $('.overlay-close').click(function(e) {
		e.preventDefault();
		$('#overlay .overlay-content').html('');
		$('#overlay').hide();
	    })
	}

	function launchOverlay(link) {
	    $.ajax({
		url: link,
		success: function(data) {
		    $('#overlay .overlay-content').html(data);
		    $('#overlay').show();
		}
	    });
	}
	
	function popupPrize(caller) {
		console.debug($(caller).parent());
	}

	function attachTabs() {
		// Show first tab in each set
		$(document).find($('.tabCta a:first').attr('href')).show();
		
		$('.tabCta a').click(function(e) {
			e.preventDefault();
			$($(this).attr('href')).siblings('.tab').hide();
			$($(this).attr('href')).show();
		});
	}
	
	function attachSliderGallery() {
		$('.galleryCta a').click(function(e) {
			e.preventDefault();

			// Add active class to current element
			$(this).parents('.galleryCta')
				    .find('.active').removeClass('active');
			$(this).addClass('active');
			
			var galleryOffset = parseInt(
			    $(this).attr('href').replace('#gallery-', '')
			);
			var scrollAmount = 
			    ((galleryOffset-1)-((galleryOffset-1)*2))
			    * _gallery_width;

			$(this).parents('.galleryCta').prev('.gallery').find('ul').animate({left: scrollAmount});
			$(this).parents('.galleryCta').next('.gallery-content').find('.wrapper').animate({left: scrollAmount});
			
		});
	}

	function attachLazyLoad() {
	    $("img:not(.no-lazyload").lazyload({
		    effect: "fadeIn"
	    });
	}

	function attachOutboundLinks() {
		$("a[rel~='outbound']").click(function(e) {
			e.preventDefault();
			window.open($(this).attr('href'));
		});
	}

	function attachTracking() {
	    $('.track').click(function() {
		track('page', {
		    'url': $(this).attr('href')
		});
	    });

	    $('.track-event').click(function() {
		var endPos = $(this).attr('rel').length;
		if ($(this).attr('rel').indexOf(' ') > 0) {
		    endPos = $(this).attr('rel').indexOf(' ');
		}
		var action = $(this).attr('rel').substring(0, endPos);

		track('event', {
		   'action': action,
		   'url': $(this).attr('href')
		});
	    });
	}

	function attachTracking() {
	    $('.track').click(function() {
		track('page', {
		    'url': $(this).attr('href')
		});
	    });

	    $('.track-event').click(function() {
		var endPos = $(this).attr('rel').length;
		if ($(this).attr('rel').indexOf(' ') > 0) {
		    endPos = $(this).attr('rel').indexOf(' ');
		}
		var action = $(this).attr('rel').substring(0, endPos);

		track('event', {
		   'action': action,
		   'url': $(this).attr('href')
		});
	    });
	}

	function track(type, trackingData) {
	    if (typeof(pageTracker) == 'undefined' ||
		typeof(pageTracker._trackPageview) == 'undefined' ||
		typeof(pageTracker._trackEvent) == 'undefined') {
		    return false;
	    }

	    switch(type) {
		case 'page':
		    if (typeof(trackingData.url) != 'undefined') {
			 pageTracker._trackPageview(trackingData.url);
		    }
		    break;
		case 'event':
		    if (typeof(trackingData.url) != 'undefined' &&
			typeof(trackingData.action) != 'undefined') {
			    pageTracker._trackEvent(
				    trackingData.action,
				    trackingData.url
			    );
		    }
		    break;
	    }
	}
	
	return {
		init: init,
		launchOverlay: launchOverlay
	}
}();

$(document).ready(function(){
	TrollSpotters.init();
});
