$(document).ready(function(){

	/* Navigation */
	
	/*$('#nav-main a').mouseenter(function(){
		$(this).parent().prev().find('a').css({backgroundPosition:'right bottom'});
	})
	.mouseleave(function(){
		$(this).parent().prev().find('a').css({backgroundPosition:''});
	})*/
	
	/*$('#nav-main a:last')
		.css({backgroundImage:'none'})
		.mouseenter(function(){
			$(this).parent().parent().css({backgroundColor:'#79a237'})
		})
		.mouseleave(function(){
			$(this).parent().parent().css({backgroundColor:''})
		})*/
		
	$('#nav-main a:last').css({backgroundImage:'none'})
	
	// Make the total width of nav links equal the width of nav bar by changing padding.
	
	var nNavsWidth = 0;
	$('#nav-main a').each(function(){
		nNavsWidth += $(this).innerWidth();
	});
	var nAddPadding = 0;
	var nAddPadding = ($('#nav-main').width() - nNavsWidth) / ($('#nav-main a').size()*2);
	var nPaddingLeft = Number(String($('#nav-main a').css('padding-left')).replace('px',''));
	var nPaddingRight = Number(String($('#nav-main a').css('padding-right')).replace('px',''));
	
	if ( $.browser.mozilla ) {
		$('#nav-main a').css({
			paddingRight:19.25,
			paddingLeft:9.25
		});
	} else {
		$('#nav-main a').css({
			paddingRight:nPaddingRight+nAddPadding,
			paddingLeft:nPaddingLeft+nAddPadding
		});
	}
	
	/* Articles Home */
	
	$('.article-home:last').css({borderBottom:'none'});
	
	// Animating in .error whenever it is present
	
	$('.error').show('medium');
	
	// Archives Page
	
		$('li.archives-issue').mouseenter(function(){
			$(this).css({backgroundColor:'#e4ffb9'});
		}).mouseleave(function(){
			$(this).css({backgroundColor:''});
		})
	
		// Hide issues list on page load
		$('ul.archives-issue').hide();
		
		// Toggle Showing issue lists on month list click
		$('li.archives-month')
			.css({cursor:'pointer'})
			
			.toggle(
				function(){
					$(this).next().show('medium');
				},
				function(){
					$(this).next().hide('medium');
				}
			)
			
	/* Left Column Right Border */
	
	if ( $('.content .c1').height() < $('.content .c2').height() ) {
		$('.content .c1').css('height',$('.content .c2').height());
	}
	
	/* Middle Column Right Border */
	
	if ( $('.content .c1').height() > $('.content .c2').height() ) {
		$('.content .c1-2').css('height',$('.content .c1').height()-30);
	}
			
	// Making sure logo links correctly, its breaking probably because of the absolute positioned date below the logo.
	
	$('#head-logo .c1 span')
		.css('cursor','pointer')
		.click(function(){
			window.location.href = $(this).parent().find('a').attr('href');
		});
		
	// Add green circle to home current issue cover image
	
	$('a#current-issue').prepend('<div></div><!-- Green Circle -->');
	
	/* ===== Logo Rotator ==================== */
	
	/* pseudo code:
	* turn off all but first logo
	* create JS array based on anchors in the div
	* set current index to 0
	* start a timer which will fade out any visible logos and fade in the next logo based on current index ++
		* but if current index equals array length - 1, reset current index to 0
	*/
	
	//if ( $('#logo-rotator').length != 0 ) {
	
		var aLogos = Array();
		
		$('#logo-rotator a').each(function(){
			aLogos.push($(this))
			$(this).css({opacity:'0',display:'none'})
		})
		
		var nCurRotation = 0;
		$('#logo-rotator a:first').css({opacity:'100',display:'block'})
		
		var myRotator = setInterval(myRotatorInterval, 5000)
		
		function myRotatorInterval(){
			if ( aLogos.length == 1 ) { return false; }
			aLogos[nCurRotation].animate({opacity:0}, 100, function(){
				$(this).css({display:'none'})
				if ( nCurRotation == aLogos.length - 1 ) {
					nCurRotation = 0
				} else {
					nCurRotation++
				}
				aLogos[nCurRotation].css({display:'block'})
				aLogos[nCurRotation].animate({opacity:100}, 100)
			})
		}
		
	//} // if size
	
	/* ===== Ad click tracking ====================== */
	
	$('a[rel^="ad["]').click(function(event){
		event.preventDefault()
		var url = $(this).attr('rel')
		url = url.replace('ad[', '')
		url = url.replace(']', '')
		window.open('http://www.therealreporter.com/?/adclick/'+url);
	})
	
});
