/**
* @author Kaiser Shahid (initially)
*/

if ( !AARP.Bulletin ) AARP.Bulletin = {};

// NEW [2008-06-16 | kaiser]
AARP.Bulletin.chomp = function( str, len )
{
	var chunks = str.trim().split( /\s+/g );
	var c = 0;
	var buff = '';
	
	for ( var i = 0; i < chunks.length; i++ )
	{
		if ( c + chunks[i].length < len )
		{
			buff += chunks[i] + ' ';
			c += chunks[i].length;
		}
	}
	
	return buff.trim();
}

// used in moreinnews component
AARP.Bulletin.moreinnews = {
	tpl_article: "<li><a href='#{url}'>#{title}</a><br/><small style='color: #aaa'>#{source}</small></li><br/>"
	, tpl_channel: "<div class='marginBottom10'><h3><a href='#{url}'>#{title}</a></h3><p>#{description} <a href='#{url}'>More &raquo;</a></p><br/>"
		+ "<p class='newsSource negTop10'>#{source}</p></div><br/>"
	, tpl_state: "<h3><a href='#{url}'>#{title}</a></h3><br/>"
		+ "<p class='posted'><span>#{source}</span> Posted on #{posted_on}</p><br/><p>#{description}</p><br/>"
	, limit: 5
	, style: '1col'
	, pull: function()
	{
		var path = '';
		var start = 0;
		
		// will use CQ's selector functionality to limit pull
		var limit = 5;
		if ( arguments[0] )
			limit = arguments[0];
		this.limit = limit;
		
		if ( AARP.page.is_authoring )
		{
			path = '/author';
		}
		else
		{
			path = '/publish';
		}
		
		path += '/etc/mostrecentarticles';
		if ( AARP.page.site == 'dotorg' ) path += '/home';
		else path += '/bulletin';
		
		for ( var i = 0; i < AARP.page.pathc_effective.length; i++ )
		{
			path += '/' + AARP.page.pathc_effective[i];
		}
		
		//if ( AARP.page.pathc_effective[0] != 'states' ) path += '/articles';
		if ( AARP.page.pathc_effective.length > 0 ) path += '/articles';
		path += '.' + limit + '.ajax.html?';
		
		// NEW [2008-02-28 | kaiser]: 404 being generated in publish instance, so need to try FQD url
		var port = ''; if ( AARP.page.port != '80' ) port = ':' + AARP.page.port;
		path = 'http://' + AARP.page.host + port + path;
		if ( AARP.page.url.match( /publish/ ) ) path = path.replace( "/author", "/publish" );
		
		if ( AARP.page.parameters['debugNow'] ) document.write(path);
		var aj = new Ajax.Request( path, { onComplete: AARP.Bulletin.moreinnews.onComplete } );
	}
	, onComplete: function( obj )
	{
		var json = obj.responseText;
		if ( AARP.page.parameters['debugNow'] ) alert( 'status=' + obj.status + "\n" + json );
		
		try
		{
			eval( json );
			AARP.Bulletin.moreinnews.formatter( recent_articles );
		}
		catch ( e )
		{
			if ( AARP.page.parameters['debugNow'] ) alert( "Exception: " + e );
		}
	}
	, formatter: function( recent_articles )
	{
		var buff = '';
		if ( AARP.page.querystring.match( /debug/ ) )
		{
			alert(AARP.Bulletin.moreinnews.style + " :: " + AARP.page.pathc_effective);
		}
		if ( recent_articles.length > 0 )
		{
			var max = AARP.Bulletin.moreinnews.limit
			if ( AARP.Bulletin.moreinnews.style == 'channel' )
			{
				var TPL = new Template( AARP.Bulletin.moreinnews.tpl_channel );
				if ( recent_articles.length < max ) max = recent_articles.length;
			
				buff1 = '';
				buff2 = '';
				for ( var i = 0; i < max; i++ )
				{
					var len = recent_articles[i].title.length;
					recent_articles[i].title = AARP.Bulletin.chomp( recent_articles[i].title, 65 );
					if ( recent_articles[i].title.length < len )
						recent_articles[i].title += '&hellip;';
					
					len = recent_articles[i].description.length;
					recent_articles[i].description = AARP.Bulletin.chomp( recent_articles[i].description, 75 );
					if ( recent_articles[i].description.length < len )
						recent_articles[i].description += '&hellip;';
					
					if ( i % 2 == 0 )
						buff1 += TPL.evaluate( recent_articles[i] );
					else
						buff2 += TPL.evaluate( recent_articles[i] );
				}
				$( 'channelInfoColOne' ).innerHTML = buff1;
				$( 'channelInfoColTwo' ).innerHTML = buff2;
			}
			else if ( AARP.Bulletin.moreinnews.style == 'state' )
			{
				var TPL = new Template( AARP.Bulletin.moreinnews.tpl_state );
				if ( recent_articles.length < max ) max = recent_articles.length;
				
				for ( var i = 0; i < max; i++ )
				{
					recent_articles[i]['posted_on'] = Date.format( '%l:%m%p', recent_articles[i]['date_created'] );
					buff += TPL.evaluate( recent_articles[i] );
				}
				
				$( 'moreinnewsBody-state' ).innerHTML = buff;
			}
			else
			{
				var TPL = new Template( AARP.Bulletin.moreinnews.tpl_article );
				if ( recent_articles.length < max ) max = recent_articles.length;
			
				buff += '<ul>';
				for ( var i = 0; i < max; i++ )
					buff += TPL.evaluate( recent_articles[i] );
				buff += '</ul>';
				
				// TODO: if we're at root, what can we link to for More?
				if ( AARP.page.pathc_effective.length > 0 )
				{
					var t = '/' + AARP.page.pathc.join( '/' );
					if ( t.match( /states/ ) ) t += '/articles';
					
					if ( AARP.page.is_authoring ) t += '.html';
					else t += '/';
					
					buff += "<a href='" + t + "'>More &raquo;</a>";
				}
				
				$( 'moreinnewsBody' ).innerHTML = buff;
			}
		}
	}
}

/*
Dynamic character counter for <textarea> and <input> tags
Added by Yonas Hassen (15 July 2008)
-------------------------------------------------------*/

function getObject(obj) {
	var theObj;
	if(document.all) 
	{
    	if(typeof obj=="string") 
		{
      		return document.all(obj);
    	} 
		else 
		{
      		return obj.style;
    	}
  	}
  
	if(document.getElementById) 
	{
    	if(typeof obj=="string") 
		{
      		return document.getElementById(obj);
    	} 
		else 
		{
      		return obj.style;
    	}
  	}
	return null;
}

function toCount(entrance,exit,text,characters) {
	var entranceObj=getObject(entrance);
	var exitObj=getObject(exit);
	var length=characters - entranceObj.value.length;
  	
	if(length <= 0) 
	{
    	length=0;
   	 	text='<span class="disable">You have reached your character limit.</span>';
    	entranceObj.value=entranceObj.value.substr(0,characters);
  	}
  	
	exitObj.innerHTML = text.replace("{CHAR}",length);
}

/* end: Dynamic character counter */