// JavaScript Document

function create_blog( args )
{
	//console.log( args );
	//console.log( post );
	if( !document.getElementById )
		fail( 'epic' );
	else
	{	
		i=0;
		document.getElementById('blog_content').innerHTML = '';
		//-m,d,t,e,g
		while( args['m'+i] != null && args['d'+i] != null && args['t'+i] != null && args['e'+i] != null && args['g'+i] != null )
		{
			/*
			<div class="blog-post-date">
      	<!-- date -->
				<a href="<?= $bp['guid'] ?>" title="<?= $bp['post_title'] ?>" ><img src="images/dates/date_<?= substr( $bp['post_date'], 8, 2 ) ?>.gif" alt="" /></a><br />
				<a href="<?= $bp['guid'] ?>" title="<?= $bp['post_title'] ?>" ><img src="images/months/months_<?= substr( $bp['post_date'], 5, 2 ) ?>.gif" alt="" /></a>
			</div>
			<div class="blog-post">
				<div class="blog-post-title"><a href="<?= $bp['guid'] ?>" title="<?= $bp['post_title'] ?>"><?= strlen($bp['post_title']) > 61? substr($bp['post_title'],0,61).'...' : $bp['post_title'] ?></a></div>
				<div class="blog-post-content"><?= $bp['post_excerpt'] ?></div>
      </div>
      <div style="clear:both;"></div>
			*/
			blog_post_wrapper = document.createElement( 'DIV' );
				blog_post_wrapper.id = 'blog_post_wrapper_'+i;
				document.getElementById('blog_content').appendChild( blog_post_wrapper );
			blog_post_date = document.createElement( 'DIV' );
				blog_post_date.id = 'blog_post_date_'+i;
				blog_post_date.className = 'blog-post-date';
				document.getElementById('blog_post_wrapper_'+i).appendChild( blog_post_date );
			day_link = document.createElement( 'a' );
				day_link.id = 'blog_day_link_'+i;
				day_link.setAttribute('href',args['g'+i]);
				day_link.setAttribute('title',args['t'+i]);
				//day_link.innerHTML = '<img src="images/dates/date_' + args['d'+i].substr( 5, 2 ) + '.gif' + '" alt="" />';
				document.getElementById('blog_post_date_'+i).appendChild( day_link );
			day_img = document.createElement( 'IMG' );
				day_img.src = 'images/dates/date_' + args['d'+i] + '.gif';
				day_img.setAttribute('alt','');
				document.getElementById('blog_day_link_'+i).appendChild( day_img );
			curr_html = document.getElementById('blog_post_date_'+i ).innerHTML;
				curr_html += '<br />';
				document.getElementById('blog_post_date_'+i).innerHTML = curr_html;
			month_link = document.createElement( 'a' );
				month_link.id = 'blog_month_link_'+i;
				month_link.setAttribute('href',args['g'+i]);
				month_link.setAttribute('title',args['t'+i]);
				document.getElementById('blog_post_date_'+i).appendChild( month_link );
			month_img = document.createElement( 'IMG' );
				month_img.src = 'images/months/months_' + args['m'+i] + '.gif';
				month_img.setAttribute('alt','');
				document.getElementById('blog_month_link_'+i).appendChild( month_img );
				
			blog_post_content = document.createElement( 'DIV' );
				blog_post_content.id = 'blog_post_'+i;
				blog_post_content.className = 'blog-post';
				document.getElementById('blog_post_wrapper_'+i).appendChild( blog_post_content );
			blog_post_title = document.createElement( 'DIV' );
				blog_post_title.id = 'blog_post_title_'+i;
				blog_post_title.className = 'blog-post-title';
				document.getElementById('blog_post_'+i).appendChild( blog_post_title );
			title_link = document.createElement( 'a' );
				title_link.id = 'blog_title_link_'+i;
				title_link.setAttribute('href',args['g'+i]);
				title_link.setAttribute('title',args['t'+i]);
				title_link.innerHTML = args['t'+i].length > 61? args['t'+i].substr( 0, 61 )+'...' : args['t'+i];
				document.getElementById('blog_post_title_'+i).appendChild( title_link );
			blog_post_excerpt = document.createElement( 'DIV' );
				blog_post_excerpt.id = 'blog_post_content_'+i;
				blog_post_excerpt.className = 'blog-post-content';
				blog_post_excerpt.innerHTML = args['e'+i];
				document.getElementById('blog_post_'+i).appendChild( blog_post_excerpt );	
			
			clear_div = document.createElement( 'DIV' );
				clear_div.className = 'artgum';
			document.getElementById('blog_post_wrapper_'+i).appendChild( clear_div );
			
			i++;
		}
	}
}

function dis_blog( li )
{
	if( document.getElementById )
	{
		li.className = li.className == 'item button'? 'item button dissed' : 'item button';
		//console.log( document.getElementById('li_sort_recent').className );
		//console.log( document.getElementById('li_sort_recent').className.match('dissed')? 'matched' : 'not matched' );
		update_blog( document.getElementById('blog_li_sort_recent').className.match('dissed')? 'random' : 'recent' );
	}
}

function update_blog( arg )
{
	//console.log( arg );
	if( document.getElementById )
	{
		if( arg == null )
			arg = 'recent';
		if( arg == 'recent' )
		{
			document.getElementById('blog_li_sort_random').className = 'item button sort dissed';
			document.getElementById('blog_li_sort_recent').className = 'item button sort';
		}
		else
		{
			document.getElementById('blog_li_sort_recent').className = 'item button sort dissed';
			document.getElementById('blog_li_sort_random').className = 'item button sortsed';
		}
		//-determine dis
		if( document.getElementById('blog-users') )
		{
			lis = Array();
			lis = document.getElementById('blog-users').getElementsByTagName('li');
			run = lis.length;
			dissed = '';
			for( i=0; i<run; i++ )
			{
				if( lis[i].className.match('dissed') )
				{
					spans = Array();
					spans = lis[i].getElementsByTagName('span');
					dissed += spans[0].innerHTML + '|';
				}
			}
			if( dissed.length > 1 )
				dissed = dissed.substr( 0, dissed.length-1 );
			//console.log( dissed );
		}
		
		//$('#blog-menu').hide();
		
		//-post using jquery
		$.post( 'scripts/fetch_blog.php', { request_id : 'mp_home', num: 2, dis: dissed, sort: arg }, function(data){ create_blog(data); }, 'json' );
	}
}

