$(document).ready(function() {

	$("ul.tabs").tabs("div.panes > div");
	var itemNumber = $('div.items a').size();
	if(itemNumber > 1){ //checks how many items are in the 'items' scroller, if greater than 1, autoscroll.
  		$("div.scrollable").scrollable({circular: true }).navigator().autoscroll(10000);
	}else{
  		$("div.scrollable").scrollable().navigator();
	}
	$('div.expert .row .card:first-child').addClass('first');
		// ==== Panel forms ==========
	var currNav;
	var currPanel;
	var activePanel;

	$('#social #newsletter, #footergetdaybook, #aboutheader #newsletter').click(function(){

		if($('#util-nav a').not($('#util-nav a#subscribe')).hasClass("active")){
			//other tab is open, close it then open subscribe one
				activePanel = $('#panels .active');
				$(activePanel).removeClass('active');
				$(activePanel).slideUp( 500, function() {
					$('#panels .active').removeClass('active');
					$('#util-nav a').removeClass('active');
					$('#util-nav a#subscribe').addClass('active');
					$('#panels #subscribe-panel').slideDown( 500 );
					$('#subscribe-panel').addClass('active');
				});
		}else{
			//no tabs are open, open the subscribe one.
			$('#util-nav a').removeClass('active');
			$('#util-nav a#subscribe').addClass('active');
			$('#panels #subscribe-panel').slideDown( 500 );
			$('#subscribe-panel').addClass('active');
			
		}
		if($(this).attr('id') == 'footergetdaybook'){
			$('html, body').animate({scrollTop:0}, 'slow');
		}
		return false;
	});

	$('.contributor').click(function(){
			//close subscribe panel, open contribute panel
				$('#subscribe-panel').removeClass('active');
				$('#subscribe-panel').slideUp( 500, function() {
					$('#panels .active').removeClass('active');
					$('#util-nav a').removeClass('active');
					$('#util-nav a#contribute').addClass('active');
					$('#panels #contribute-panel').slideDown( 500 );
					$('#contribute-panel').addClass('active');
				});
		return false;
	});
	

	$('#util-nav a').click(function () { 
		currNav = '#'+$(this).attr('id');
		currPanel = '#'+$(this).attr('id')+'-panel';
		
		if($(this).hasClass('active')){
			//panel is active, slide up
			$(currPanel).slideUp(500);
			$(this).removeClass('active');
		}else{
			// if a panel is open; close activePanel then open panel clicked (currPanel)
			if($('#util-nav a').not($(this)).hasClass("active")){
				$('#util-nav a.active').not(currNav).removeClass('active');
				activePanel = $('#panels .active');
				$(activePanel).removeClass('active');
				$(activePanel).slideUp( 500, function() {
					$('#panels .active').removeClass('active');
					$(currPanel).slideDown( 500 );
					$(currPanel).addClass('active');
		      	});
			} else{
				$('#util-nav a.active').not(currNav).removeClass('active');
				$(currPanel).slideDown( 500 );
				$(activePanel).removeClass('active');
				$(currPanel).addClass('active');
			}
			$(currNav).addClass('active');	
		}
		return false;					
    });

	$('a.panel-close').click(function () { 
		// check if IE 6-7
		
		iecatch = $.browser.msie + $.browser.version;
		if ((iecatch.indexOf("true7") != -1)) {
			$(this).parent().parent().slideUp( 500 );
		}else{
			$(this).parent().parent().slideUp( 500 );
 		}	
		$(this).parent().parent().removeClass('active');
		var btn = '#'+$(this).parent().parent().attr('id').replace('-panel','');
		$(btn).removeClass('active');
		return false;
    });

	/* hover state for secondary stories on home page */
	$('.blocks, #main-story').hover(function(){
		$(this).addClass('active');
		$(this).css('cursor', 'pointer');
	}, function(){
		$(this).removeClass('active');
		$(this).css('cursor', 'default');
	})
	
	$('#category-subscribe, #category-contribute').selectbox();
	
	



});
