$(window).load(ready);

function ready(){
	$('.story-nav').each( function(){
		$(this).click( function(){
			story_activate(this);
		});
	});
	
	$('#story-nav').click( function(){
		$('#story-rotator').attr('name', 'stop');
	});
	
	// center the image vertically within the image block
	$('.story').each( function() {
		img_block = $(this).find('a.story-image img');
		img_block.removeAttr('height');
		img_block.removeAttr('width');
		img_w = img_block.width();
		container_w = img_block.parent().width();
		if( img_w > container_w )
			img_block.css(
				'marginLeft', 
				'-' + Math.round( (img_w - container_w) / 2 ) +'px'
			);
		container_div = img_block.parent();
		container_div.css('backgroundImage', 'url("'+img_block.attr('src')+'")');
		img_block.hide();
		$(this).click( function(){
			$('#story-rotator').attr('name', 'stop');
		});
	});
	
	story_rotator(1);
	
	$('ul.category-desc li').each( function(){
		$(this).click( function(){
			location.href = $(this).find('a').attr('href');
		});
		
		$(this).mouseover( function(){
			$(this).addClass('over');
		});
		
		$(this).mouseout( function(){
			$(this).removeClass('over');
		});
		
	});
	
	$('#add-fav').click( function(){
		$('#add-fav').animate({
			'marginRight': '-=15px'
		},
		{	duration: 1,
			easing: 'easeOutElastic'
		}).animate({
			'marginRight': '+=15px'
		},
		{	duration: 750,
			easing: 'easeOutElastic'
		});
		
		$.get('?', 
		{ 
			'fav-action': 'add',
			'c_id': $(this).attr('name') 
		},
		function(data){
			$('#add-fav').addClass('added');
			$('#add-fav').html('Added');
		});
	});
	
	$('.fav-content-item').mouseover( function(){
		$(this).addClass('fav-content-item-over');
	});
	$('.fav-content-item').mouseout( function(){
		$(this).removeClass('fav-content-item-over');
	});
	
	$('#fav-container').sortable(
	{
		cursorAt: 		'top',
		revert:			false,
		update: 		function(event, ui)
		{
			$.post('?fav-action=order', 
			{ 
				'order': $('#fav-container').sortable('serialize')
			},
			function(data){
			});	

			$('#fav-container .changed').animate({
				'top': '-=20px'
			},
			{	duration: 75,
				easing: 'easeOutElastic'
			}).animate({
				'top': '+=20px'
			},
			{	duration: 750,
				easing: 'easeOutElastic'
			});
		},
		change:		function(event, ui)
		{
			$('.fav').removeClass('changed');
			$('.ui-sortable-helper').addClass('changed');
		}
		
	}
	);
	
	$('.fav').mouseover( function(){
		control_bar = $(this).find('.fav-control');
		control_bar.show();
	});
	
	$('.fav').mouseout( function(){
		control_bar = $(this).find('.fav-control');
		control_bar.hide();
	});
	
	$('.fav-remove').click( function(){
		cat_parent = $(this).parent().parent();
		cat_name = cat_parent.find('.fav-title a').text();
		if( !confirm('Remove '+cat_name+' from your favorites?') )
			return;
			
		$.get('?', 
		{ 
			'fav-action': 'remove',
			'c_id': $(this).attr('name') 
		},
		function(data){
			$(cat_parent).hide('fast', function(){ $(this).remove(); });
		});		
	});
	
	$('.fav-content-item-text').click( function(){
		location.href = $(this).prev().find('a').attr('href');
	});
	
	$('#login-help').click( function(){
		$('#public-login-help').toggle('fast');
	});
	
	$('#login-submit').mouseover( function(){
		$(this).addClass('over');
	});
	
	$('#login-submit').mouseout( function(){
		$(this).removeClass('over');
	});

	$('.member-login-false').click( function(){
		this_h = $('#member-login-form').css('height');				  
		$('#member-login-form').css({
			'height'	: '1px',
			'display'	: 'block'
		});
		$('#member-login-form').animate({
			'height': this_h
		},
		{	duration: 1000,
			easing: 'easeOutExpo'
		});
		return false;
	});
	$('#nav-member').css('display', 'block'); // ie fix. who knows..
	
	$('#public-site').click( function(){
		if( !confirm('Logout of the Members Area and switch to the Public Site?'))
			return;
		$.get($('#logout').attr('href'), function(data){
			location.href = $('#public-site').attr('href').replace('#', '');
		});
		return false;
	});
	
	$('#login-submit').click( function(){
		if( $('#user_login').val() == '' || $('#user_pass').val() == ''){
			alert('Please enter your username and password to login');
			return false;
		}
	});
	
	if( $('#cntdwn').length ){
		var year2010=new dcountup("January 1, 2010 00:00:00", "days")
		
		year2010.oncountup=function(result){
			var mycountainer=document.getElementById("cntdwn")
			mycountainer.innerHTML=result['days']+" <sup>d</sup> "+result['hours']+" <sup>h</sup> "+result['minutes']+" <sup>m</sup> "+result['seconds']+" <sup>s</sup>";
		}
	}
	
	if( $('#dc-weather').length ){
		$('#dc-weather').find('strong a').text('Washington, DC Weather');
	}
	
	$('#cat-rss').mouseover( function(){
		$('#cat-rss-help').stop(true, true).animate({opacity: "show", top: "-10"}, "slow");
	});
	
	$('#cat-rss-help').mouseleave(function() {
		$('#cat-rss-help').animate({opacity: "hide", top: "-15"}, "fast");
	});
	
	if( $('#form-action').length > 0 ){
		$('#form-action').validate({
			errorClass: "warn"
		});
		$("#phone").mask("(999) 999-9999");
	}
}

function story_rotator(x){
	story = $('#story_'+x);
	time = ( parseInt( story.attr('wait') ) * 1000 ) || ( parseInt( story.text().length ) * 60 );

	rotator = setTimeout(function(){
		if( $('#story-rotator').attr('name') == 'stop'){
			clearTimeout(rotator);
			return;
		}
		x++;
		if( !$('#story_'+x).length)
			x = 1;

		story_activate('#story-nav-'+x)
			
		story_rotator(x);
	 }, 
	 	time
	 );

}

function story_activate(element){
	$(element).each( function(){
		story_num = $(this).text();
		$('.story').hide('fast');
		$('#story_' + story_num).show('fast');
		$('.story-nav').each( function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
	});
}


/***********************************************
* Dynamic CountUp script- Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function dcountup(startingdate, baseunit){
	this.currentTime=new Date()
	this.startingdate=new Date(startingdate)
	this.timesup=false
	this.baseunit=baseunit
	this.start()
}

dcountup.prototype.oncountup=function(){} //default action for "oncountup"

dcountup.prototype.start=function(){
	var thisobj=this
	this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
	var timediff=(this.currentTime-this.startingdate)/1000 //difference btw target date and current date, in seconds
	var oneMinute=60 //minute unit in seconds
	var oneHour=60*60 //hour unit in seconds
	var oneDay=60*60*24 //day unit in seconds
	var dayfield=Math.floor(timediff/oneDay)
	var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
	var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
	var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
	if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
		hourfield=dayfield*24+hourfield
		dayfield="n/a"
	}
	else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
		minutefield=dayfield*24*60+hourfield*60+minutefield
		dayfield=hourfield="n/a"
	}
	else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
		var secondfield=timediff
		dayfield=hourfield=minutefield="n/a"
	}
	var result={days: dayfield, hours:hourfield, minutes:minutefield, seconds:secondfield}
	this.oncountup(result)
	setTimeout(function(){thisobj.start()}, 1000) //update results every second
}
