// configuration and variables
var animspeed = 'normal';
var img_path = 'http://media2.richmond.com/media/_res/img/'

var dropdown_show = '';

// reuseable functions
function manage_topnav(){
	$('#mainnav li.open').removeClass('open');
	if(dropdown_show != ''){
		$(dropdown_show).addClass('open');
	}
}

// apply code to page
$(function(){
	// page setup items
	$('body').addClass('with-js');
	$('#sr_a,#sr_b').addClass('columnPost');
	$('ul,ol').each(function(){
		$('li:first',this).addClass('li-first');
		$('li:last',this).addClass('li-last');
	});
	$('hr').wrap('<div class="hr"><\/div>');
	// dropdown menus
	$('#mainnav ol>li').each(function(){
		if($('ul',this).length<1){
			$(this).addClass('no-child');
		}
	});
	$('#mainnav a').hover(
		function(){ dropdown_show = $(this).parents('li[id]'); },
		function(){ dropdown_show = ''; }
	);
	var timer_mainnav = setInterval('manage_topnav()',100);
	// weather and traffic tabs
	$('#weathertraffic>ul').tabs();
	// most popular rail module tabs
	$('#most_popular div.tabbed>ul').tabs();
	// most popular stories/blogs module tabs
	$('#slist-tabbed div.tabbed>ul').tabs();
	// zebra list alternating colors 
	$('ul.zebra').each(function(){
		$('li:nth-child(odd)',this).addClass('odd');
	});
	// comment list alternating colors
	$('ol.commentlist li:nth-child(odd)').addClass('odd');
	// things to do form auto-submit
     $('.ttd select').bind('change',function(){ 
		if (this.value != 0) window.location='http://events.richmond.com/search?st='+(($(this).attr('class')=='')?'event':$(this).attr('class'))+'&cat='+this.value;
	});
	// things to do form text fields
	$('.event-keywords').bind(
		'focus',
		function(){
			if($(this).val()=='Event Keywords'){
				$(this).val('');
			}
		}
	);
	$('.event-keywords').bind(
		'blur',
		function(){
			if($(this).val()==''){
				$(this).val('Event Keywords');
			}
		}
	);
	$('.event-location').bind(
		'focus',
		function(){
			if($(this).val()=='Richmond, VA'){
				$(this).val('');
			}
		}
	);
	$('.event-location').bind(
		'blur',
		function(){
			if($(this).val()==''){
				$(this).val('Richmond, VA');
			}
		}
	);
	// search box options
	$('#globalsearch input').bind(
		'focus',
		function(){
			$('#globalsearch').addClass('open');
		}
	);
	$('#globalsearch .searchclose img').bind(
		'click',
		function(){
			$('#globalsearch').removeClass('open');
		}
	);
	$('body').bind(
		'click',
		function(){
			if($('#globalsearch #searchopts:visible')[0]){
				$('#globalsearch').removeClass('open');
			}
		}
	);
	$('#globalsearch').bind(
		'click',
		function(e){ e.stopPropagation(); }
	); 
	// slideshows
	$('.slideshow').each(function(i){
		$(this).attr('id','slideshow_'+i);
		$('#slideshow_'+i+' ul.slides').cycle(
			{
				fx:'scrollHorz', // animation effect
				speed:500, // .5 second transition
				timeout:8500, // 8 seconds of display + .5 second transition
				pager:'#slideshow_'+i+' .slide-state',
				next:'#slideshow_'+i+' .slide-next img',
				prev:'#slideshow_'+i+' .slide-prev img',
				pagerAnchorBuilder: function(idx,slide){
					return '<a href="#"><img src="'+img_path+'blank.gif" width="10" height="10" alt="" /><\/a>';
				}
			}
		);
	});
	$('.slide-state a').bind(
		'focus',
		function(){ this.blur(); }
	);
	// play/pause button in slideshow on homepage.
	$('.slide-playpause').toggle(
		function(){
			$(this).css('background-position','0 0');
			$(this).parents('.slideshow').find('ul.slides').cycle('pause');
		},
		function(){
			$(this).css('background-position','0 -17px');
			$(this).parents('.slideshow').find('ul.slides').cycle('resume');
		}
	);
	// randomize background position in right rail modules to make them look more organic
	$('#sr_b div.module,div.bar').each(function(){
		var thePosition = '{x}% {y}%';
		thePosition = thePosition.replace('{x}',Math.floor(Math.random()*21)*5).replace('{y}',Math.floor(Math.random()*21)*5);
		$(this).css('background-position',thePosition)
	});
	// making hot topics tab an actual tab if not previously selected.
	$('#mg_section-tabs ul.tabs li:first').click(function(){
		if (!$(this).hasClass('active')) {
			$('#mg_section-tabs ul.tabs li.active').removeClass('active');
			$(this).addClass('active');
			if ($('#mg_section-tabs .sections').css('display')=='none') $('#mg_section-tabs .sections').slideDown(100);
		}
		return false;
	});
});