/*
	functions.site.js
	JQuery / DOM Interaction Functions
	Creator: Matt Kircher
*/

/* GENERAL */
function setupPage(){
	
	//IE PNG Fix
	if(! $.support.leadingWhitespace){
		$('img').each(function(){
			$(this).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).attr('src')+'", sizingMethod="crop");');
			$(this).css('-ms-filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+$(this).attr('src')+'", sizingMethod="crop");');
		});
	}
	
	//share and follow
	$('.home').find('.shareinpost').remove();
	
	//classes
	$('#functional-nav li:last-child').addClass('end_nav');
	$('#content, #main-content, .grid, .wp-paginate, #sidebar, #email-signup #email-signup-form, #connect-nav, #gutter-nav, #gutter').addClass('clearfix');
	$('a.button').parent().addClass('clearfix');
	
	//headers
	$(':header + .subhead').each(function(){	$(this).prev().addClass('leadin_header');	});
	
	//navigation manipulation
	if( $('#sub-nav').length ){
		
		$('#main-content').addClass('has_nav');
		
		if( $('.single').length ){
			$('#main-nav li.current-menu-parent').parents('#main-nav *').addClass('current-menu-ancestor');
			$('#sub-nav li.current-menu-parent').parents('#sub-nav *').addClass('current-menu-ancestor');
		}
		
		if( $('#sub-nav li.current_page_item').length ){
			$('#sub-nav > ul > li.current-page-ancestor').siblings().remove();
		} else {
			$('#sub-nav > ul > li.current-menu-ancestor').siblings().remove();
		}
		$('#sub-nav > ul > li.current-menu-item').siblings().remove();
		$('#sub-nav > ul > li.current-post-ancestor').siblings().remove();
		
		$('#sub-nav').show();
	}
}

function initMasthead(){
	if($('#masthead-items').length){
		
		$('#masthead').append('<ul id="masthead-ticker"></ul>');
		$('#masthead-items > li').each(function(index){
			$(this).addClass('masthead'+index);
			
			var content = $(this).find('.feature_link').parent().clone();
			$(content).find('.content, img').remove().end().find('a').text('Read now!');			
			
			$('#masthead-ticker').append('<li class="ticker masthead'+index+'">'+$(content).html()+'</li>');
			$('#masthead-ticker li.masthead'+index+' a').click(function(){ return false; });
		});
		$('#masthead-ticker').addClass('clearfix');
		
		$('#masthead-ticker li.masthead0').addClass('on');
		
		if($.support.leadingWhitespace){
			//ALL OTHER BROWSERS
			$('#masthead-items > li:eq(0)').show().animate({ opacity:100, left:'0px' }, 700);
		} else {
			//IE ONLY
			$('#masthead-items > li:eq(0)').show();
		}	
		
		$('#masthead .next_button').click(function(){
			
			if(! $.support.leadingWhitespace){
				
				//IE ONLY
				$('#masthead-items > li:eq(0)').hide().parent().append( $('#masthead-items > li:eq(0)') );
				$('#masthead-ticker li').removeClass('on');
				$('#masthead-ticker li.'+$('#masthead-items > li:eq(0)').attr('class') ).addClass('on');
				$('#masthead-items > li:eq(0)').show();
				
			} else {
				
				//ALL OTHER BROWSERS
				$('#masthead-items > li:eq(0)').stop().animate({ opacity:0, left:'25px' }, 700, function(){
					$(this).parent().append($(this));
					
					$('#masthead-ticker').fadeOut(500, function(){
						$(this).find('li').removeClass('on');
						$('#masthead-ticker li.'+$('#masthead-items > li:eq(0)').attr('class') ).addClass('on');
						$(this).fadeIn(500);
					});
					
					$('#masthead-items > li:eq(0)').css({ opacity:0, left:'-25px' }).show().animate({ opacity:100, left:'0px' }, 700);
				});	
			}
			return false;
		});
		
		$('#masthead .prev_button').click(function(){
			
			if(! $.support.leadingWhitespace){
				
				//IE ONLY
				$('#masthead-items > li:eq(0)').hide().parent().prepend( $('#masthead-items > li:last-child') );
				$('#masthead-ticker li').removeClass('on');
				$('#masthead-ticker li.'+$('#masthead-items > li:eq(0)').attr('class') ).addClass('on');
				$('#masthead-items > li:eq(0)').show();
				
			} else {
				
				//ALL OTHER BROWSERS
				$('#masthead-items > li:eq(0)').stop().animate({ opacity:0, left:'-25px' }, 700, function(){
					$('#masthead-items').prepend( $('#masthead-items > li:last-child') );
					
					$('#masthead-ticker').fadeOut(500, function(){
						$(this).find('li').removeClass('on');
						$('#masthead-ticker li.'+$('#masthead-items > li:eq(0)').attr('class') ).addClass('on');
						$(this).fadeIn(500);
					});
					
					$('#masthead-items > li:eq(0)').css({ opacity:0, left:'25px' }).show().animate({ opacity:100, left:'0px' }, 700);
				});
			}
			return false;
		});
		
		$('#masthead .pause_button').click(function(){
			
			$(this).hide();
			clearInterval(i);
			
			if($.support.leadingWhitespace){
				$('#masthead .next_button, #masthead .prev_button').fadeIn(500);
			} else {
				$('#masthead .next_button, #masthead .prev_button').show();	
			}
			return false;
		});
		
		var i = setInterval(function(){
			$('#masthead .next_button').trigger('click');
		}, 7000);
	}
}

function getLatestTweet(){
	
	$("#twitter-feed").tweet({
            username: "DrNickUSA",
            avatar_size: 48,
            count: 1,
	     join_text: 'auto',
	     auto_join_text_default: "",
            auto_join_text_ed: "",
            auto_join_text_ing: "",
            auto_join_text_reply: "",
            auto_join_text_url: "",
            loading_text: "Loading latest tweet&hellip;"
        });	
}

/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});

