$(document).ready(function(){

	//Check if elements are on a page
	jQuery.fn.exists = function(){return jQuery(this).length>0;}
	
	//Primary Navigation
	function megaHoverOver(){
		$(this).find('a').addClass('active');
		$(this).find('.sub').stop().fadeTo('fast', 1).show();

		(function($) {
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				columns = 2;
				$(this).find('ul.items').each(function() {
					maxWidth = ($(this).outerWidth(true) * columns);
					rowWidth += $(this).outerWidth(true);					
					if(rowWidth > maxWidth) {
						rowWidth = maxWidth;								
					}					
				});
			};
		})(jQuery); 
		
		$(this).calcSubWidth();
		$(this).find('.sub').css({'width' : rowWidth});
		
		$(this).find('.sub').masonry({
			itemSelector: '.items',
			columnWidth: 250
		});		
	}

	function megaHoverOut(){		
		$(this).find('a').removeClass('active');
		$(this).find('.sub').stop().fadeTo(200, 0, function() {
			$(this).hide();	
		});		
	}
	
	var config = {   
		sensitivity: 1,   
		interval: 0, 
		over: megaHoverOver,
		timeout: 0,
		out: megaHoverOut   
	};
	
	$('ul.primary-nav li.drop').hoverIntent(config);
	$('ul.primary-nav li .sub').css({'opacity':'0'});
	
	//News Button
	$('.news-btn').append('<span class="hover">Continue Reading...</span>').each(function () {
		var $span = $('> span.hover', this).css('opacity', 0);
		$(this).hover(function () {
			$span.stop().fadeTo(400, 1);
			}, function () {
				$span.stop().fadeTo(300, 0);
		});
	});
	
	//Isotope
	var $container = $('ul.isotope-elements');
	
	if ($container.exists()) {
	
		$container.isotope({
			animationEngine: 'jquery',
			itemSelector : 'ul.isotope-elements li',
			layoutMode : 'straightDown',
			getSortData : {
				resource : function ( $elem ) {
					return $elem.find('.resource').text();
				},	
				announcement : function ( $elem ) {
					return $elem.find('.announcement').text();
				}	
			},
			animationOptions: {
				duration: 500,
				easing: 'easeInOutCirc',
				queue: false
			},
			sortAscending : false
		});
	
		var $optionSets = $('ul.option-set'),
		$optionLinks = $optionSets.find('a');
		
		$optionLinks.click(function(){
			var $this = $(this);
			if ( $this.hasClass('selected') ) {
				return false;
			}
			var $optionSet = $this.parents('.option-set');
			$optionSet.find('.selected').removeClass('selected');
			$this.addClass('selected');
			var options = {},
			key = $optionSet.attr('data-option-key'),
			value = $this.attr('data-option-value');
			value = value === 'false' ? false : value;
			options[ key ] = value;
			if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
				changeLayoutMode( $this, options )
			} else {
				$container.isotope( options );
			}	
			return false;
		});
	}

	//Login
	$('a.login').click(function(e) {
		e.preventDefault();
		if( $('#login-inner').hasClass('active') ) {
			$('#login-inner').animate({
				top: 0
			}, 500, 'easeInOutQuart', function() {
				$(this).removeClass('active');	
				$('.login-button').removeClass('active');			
			});
		} else {
			$('#login-inner').animate({
				top: 195
			}, 500, 'easeInOutQuart', function() {
				$(this).addClass('active');
				$('.login-button').addClass('active');
			});

		}
	});	
	
	//Featured Slides	
	$('.featured-image-desc').css('filter', 'alpha(opacity=100)');
	$('#featured-article-wrapper').slides({
		preload: true,
		preloadImage: '/imgages/ajax-loader.gif',
		container: 'featured-slides-container',
		generatePagination: true,
		paginationClass: 'featured-pagination',
		next: 'featured-article-next',
		prev: 'featured-article-prev',
		play: 10000,
		pause: 2000,
		hoverPause: true,
		slideSpeed: 800,
		slideEasing: 'easeInOutQuart',
		animationComplete: function() {
			$.newBG = $('#featured-article-inner').find('.activeSlide').children('.featured-image-holder').children('img').attr('src');
			$.newTitle = $('#featured-article-inner').find('.activeSlide').children('.featured-image-holder').children('img').attr('alt');
			$('.featured-image-desc').fadeOut(300);
			$('li.bgs-current').find('img').fadeOut(500, function() {
				$('li.bgs-current').find('img').attr('src', $.newBG);
				$('.featured-image-desc').children('p').html($.newTitle);
			});			
			$('li.bgs-current').find('img').delay(500).fadeIn(500, function() {
				$('.featured-image-desc').fadeIn(300);
			});			
		}
	});
	
	$('.featured-close-btn').click(function(e) {
		e.preventDefault();
		$('ul.featured-pagination').fadeToggle(500);
		$('#featured-article-inner').fadeToggle(500, function() {
			$('.featured-close-btn').toggleClass('active');			
		});
	});
	
	//Captions
	$('.featured-article-left img').jcaption({
		 wrapperClass: 'featured-caption'
	});	
	$('.content-left-std img.image-left').jcaption({
		wrapperClass: 'cap-image-left'
	});
	$('.content-left-std img.image-right').jcaption({
		wrapperClass: 'cap-image-right'
	});
	
	//Tables
	$("tr:odd").addClass("alt")
	$('#form-table tr:odd').removeClass('alt');
	$('#form-table tr:even').removeClass('alt');
	$('#login-form tr:odd').removeClass('alt');
	
	//Forms
	$(".select").selectbox({
		effect: 'fade'
	});
	
	//Colourbox
	$(".error-report").colorbox({
		iframe: true,
		fixed: true,
		innerWidth: 850, 
		innerHeight: 600
	});
	$(".image-gallery").colorbox({
	    photo: true,
		rel: 'image-gallery',
		transition: 'elastic',
		speed: 500,
		fixed: true,
		title: true
	});
	
	//Clubs Nav
	if ($("ul#clubs").exists()) {
		$(this).treeview({
			animated: 'normal',
			collapsed: true,
			unique: true
		});
	}

	//Login Redirect
	if ( $('#login-inner').find('.wlpeMessage').length ) {
		window.location.href = "http://www.rotaryd9710.org.au/user-management/secure-login.html";
		console.log('redirected');
	} else {
		return false;
		console.log('not redirected');
	}

	
});
