//CNA Scroll Effects


jQuery(function( $ ){
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */

	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'xy';

	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: 'body', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1500
	});

	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: 'body', // could be a selector or a jQuery object too.
		queue:true,
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
			// The 'this' contains the scrolled element (#content)
		}
	});



	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '.content-scroll', // Could be a selector or a jQuery object too.
		queue:true,
		duration:1500
	});

	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '.content-scroll', // could be a selector or a jQuery object too.
		queue:true,
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){
			// The 'this' is the settings object, can be modified
		},
		onAfter:function( anchor, settings ){
			// The 'this' contains the scrolled element (#content)
		}
	});


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

	$(function () {
		// IE6 doesn't handle the fade effect very well - so we'll stick with
		// the default non JavaScript version if that is the user's browser.
		if ($.browser.msie && $.browser.version < 7) return;

		$('#home .preparing li a')

		// remove the 'highlight' class from the li therefore stripping 
		// the :hover rule
		.removeClass('highlight')

		// within the context of the li element, find the a elements
		.find('span')

		// create our new span.hover and loop through anchor:
		.append('<span class="hover" />').each(function () {

			// cache a copy of the span, at the same time changing the opacity
			// to zero in preparation of the page being loaded
			var $span = $('> span.hover', this).css('opacity', 0);

			// when the user hovers in and out of the anchor
			$(this).hover(function () {
				// on hover

				// stop any animations currently running, and fade to opacity: 1
				$span.stop().fadeTo(500, 1);
			}, function () {
				// off hover

				// again, stop any animations currently running, and fade out
				$span.stop().fadeTo(500, 0);
			});
		});
	});

	
	// $('.sub-nav a').click(function() {
	//  	$('.sub-nav a').removeClass('current');
	//	$(this).addClass('current');
	//	});

	$(function() {
		$("#lava, #lava2").lavaLamp({
			fx: "linear", 
			speed: 250,
			click: function(event, menuItem) {
				return false;
			}
		});
	});
	
	checkModal = function() {
		if (!($('.whatever').height() == null)) {
			$('.whatever').height($modalsize - 12);
			if ($('.whatever .social').children().length < 1) {
				$('#simplemodal-container').css("padding-left","12px");
				$('#simplemodal-container h1.logo').css("left","25px");
			}
		} else {
				setTimeout(checkModal,100); // wait at least 100 ms and check again
		}
	}

	
	$(window).resize(function() {
		
		$('.inner').height((($(window).height()) - 362));
		$('.newslist .inner').height((($(window).height()) - 312));
		if ($(window).height() > 800) {
			$('.about-scroll').css({"overflow-y":"hidden", "min-height":"1000px"}); 
		}
		
		$('a.postpopup').click(function(){
			$modalsize = Math.round(($(window).height() * 0.80) - 85);
			id = $(this).attr('rel');
			$('<div id="ajax-popup"></div>').hide().appendTo('body').load('/ajax-handler/?id='+id).modal({overlayClose:true,minHeight:$modalsize,maxHeight:$modalsize,autoResize:true,
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('fast', function () {
						dialog.container.fadeIn('fast', function () {
							dialog.data.fadeIn('fast');
						});
					});
				},
				onShow: 
				function(dialog){
					checkModal();
				},
				onClose: function (dialog) {
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close();  
							});
						});
					});
				},
			});
			return false;
		});
		
		$('a.site-credits').click(function(){
			$modalsize = Math.round(($(window).height() * 0.80) - 85);
			$('<div id="ajax-popup"></div>').hide().appendTo('body').load('/site-credits').modal({overlayClose:true,minHeight:$modalsize,maxHeight:$modalsize,autoResize:true,
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('fast', function () {
						dialog.container.fadeIn('fast', function () {
							dialog.data.fadeIn('fast');
						});
					});
				},
				onShow: 
				function(dialog){
					checkModal();
				},
				onClose: function (dialog) {
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close();  
							});
						});
					});
				},
			});
			return false;
		});
		
	}).resize();


	jQuery(document).ready(function(){
		
		jQuery('#postPagination a').live('click', function(e){
			e.preventDefault();
			var link = jQuery(this).attr('href');
			jQuery('.content').html('Loading...');
			jQuery('.content').load(link+' #contentInner');

		});
		
		/*if ($('.newslist div.featured-entry').length < 4) {
			$('.news-wrap a.prev-news,.news-wrap a.next-news').hide();
		}
		$('.newslist .inner').width($('.newslist div.featured-entry').length * 220);
		$('.newslist').serialScroll({
			items:'div.featured-entry',
			prev:'.news-wrap a.prev-news',
			next:'.news-wrap a.next-news',
			step: 3,
			cycle: false,
			stop: true,
			force: false,
			constant: true,
			onBefore: function( e, elem, $pane, $items, pos ){
				if ( $items.length > 3 ) {
					$('.news-wrap a.prev-news,.news-wrap a.next-news').fadeIn();
				} 
				if ( pos == 0 ) {
					$('.news-wrap a.prev-news').fadeOut();
				} else if ( pos >= $items.length - 3 ) {
					$('.news-wrap a.next-news').fadeOut();
				}
			},
		});*/
		
		

	});


	/* makes subnav arrow follow hash changes caused by next/prev arrows */
	window.onhashchange = function() {
		$('.sub-nav li a').each(function(){
			if ($(this).attr("href") == location.hash) {
				$(this).mouseenter().click();
			}
		});
	};
	

});



