function DisplayFeed( url, block )
{
	alert(url);
	$.getFeed({
        	'url': url,
        	success: function(feed)
			{
				
				$('#'+block).append('<h2>' +
				'<a href="' +
				feed.link +
				'">' +
				feed.title +
				'</a>' +
				'</h2>');
			}		
		});
}


function ProcessTags()
{
	var maxRate = 2;
	$('.tag').each(
		function(index, elem){			
			var rate = elem.getAttribute('rate');
			if( rate > maxRate ) maxRate = rate;
		}
	);
	var logValue = Math.log(maxRate);
	
	$('.tag').each(
		function(index, elem){			
			var rate = elem.getAttribute('rate');
			var fontSize = 30*Math.log( rate ) / logValue;
			fontSize +=12;
//			console.log(logValue);
			elem.style.fontSize = fontSize+'px';
			$(elem).css('line-height', fontSize+'px');
		}
	);

}

function InitBlogs()
{
	AddTagsLink();
	BuildDesc();
	AddEventListners();
}

function AddTagsLink()
{
	$('.column .blogTags').each(function(){
		var text = $(this).html();
		if( !text ) return;
		text = text.split(',');
		$.each(text, function(i){
			var textLink = $.trim(this);			
			text[i] = '<a href="/tags/'+textLink+'">'+textLink+'</a>';
		} );
		$(this).html( text.join(', '));		
	});	
}

function AddEventListners()
{
	$('li.blogRow').mousemove( function(e){ DisplayDesc(this, e) } );
	$('li.blogRow').mouseout( function(e){ HideDesc(this,e) } );
}

function BuildDesc()
{
	$('li.blogRow').each( function(){
			title = $( '.blogRowTitle',this).text();
			date = $( '.blogRowDate',this).text();
			descObj = $( '.blogRowDesc',this);
			descObj.prepend( '<div class="DescTitle">'+title+'</div' );
			descObj.prepend( '<div class="DescDate">'+date+'</div' );
	});
}

function DrawMenu( selectMenu )
{
	if( !selectMenu ) selectMenu = 'Tags';
	var selObj;
	$('a.tab').each(
		function(index, elem) {
			if( elem.innerHTML == selectMenu) selObj = $(elem);
		}
	);
	
	if( selObj )
	{
		var menu = '<span class="highlighted tab"><a href="'+selObj.attr('href')+'">'+selObj.html()+'</a></span>';
		selObj.before(menu);
		selObj.remove();
	}
}


function DisplayBlogs()
{
	$('.column').hide();
	var viewType;
	if( $('#displayHidden').val() == 'display hidden' )
	{
		$('#displayHidden').val('display visible');
		viewType = 'hidden';
	}
	else{
		$('#displayHidden').val('display hidden');
		viewType = 'visible';
	}
//	 = $('#displayHidden').attr('checked') ?  : 'visible';
	var hiddenBlogs = GetHiddenId();
	$('.column').each( function() {
			var hiddenBlog = ( hiddenBlogs[this.id] ) ? true : false;
			AddButtons(this, hiddenBlog);			
			if( 
				( viewType == 'hidden' && hiddenBlog ) ||
				( viewType == 'visible' && !hiddenBlog )
			 )
			{
				$(this).show();
			}
	}); 	
}


function AddButtons( column, hide )
{
	$('#'+column.id+' .hideButton').empty()
	var showBtn = '<img  src="/img/show_1.png" onClick="ShowBlog(this)" disableClick="ShowBlog(this)" alt="unhide this blog" title="unhide this blog"/>';
	var hideBtn = '<img  src="/img/hide_1.png" disableClick="HideBlog(this)" onClick="HideBlog(this)" alt="hide this blog" title="hide this blog"/>';
	if ( hide )
	{
		showBtn = EnableButton(showBtn);
		hideBtn = DisableButton(hideBtn);
	}
	else
	{
		showBtn = DisableButton(showBtn);
		hideBtn = EnableButton(hideBtn);		
	}
	$(column).append(
		$('<div class="hideButton"></div>').append(showBtn, hideBtn)
	);
}


function GetHiddenId()
{
	var blogsCookie = get_cookie('blogs');
	var blogs = [];
	var hiddenBlogs = {};
	if( blogsCookie ) blogs =  blogsCookie.split(':');
	for (var blogI = 0; blogI < blogs.length; blogI++)
	{
		hiddenBlogs[ blogs[blogI] ] = true;
		$('#'+blogs[blogI]).hide();
	}	
	return hiddenBlogs;
}

function HideBlog(button)
{
	processBlog(button,'hide');
}

function ShowBlog(button)
{
	processBlog(button,'show');
}

function processBlog(button, action)
{
	if( !button ) return;
	var blogDiv;	
	if ( $(button) && $(button).parent() && $(button).parent().parent() )
	{
		blogDiv = $(button).parent().parent()[0];
	}
	else
	{
		return;
	}
		
	if( action == 'show' )
	{
		DisableButton( $(button).parent().children()[0] );
		EnableButton(  $(button).parent().children()[1] );		
	}
	else
	{
		DisableButton( $(button).parent().children()[1] );
		EnableButton(  $(button).parent().children()[0] );		
	}
	$(blogDiv).hide();
	SaveBlogCookie( blogDiv.id, action );	
}

function SaveBlogCookie( blogStr, action )
{
	var blogsCookie = get_cookie('blogs');	
		
	var blogs = [];
	if( blogsCookie ) blogs =  blogsCookie.split(':');
//	console.log(blogs);
	var  IsBlogNotExist = true;
	for( var blogI=0; blogI < blogs.length; blogI++ )
	{
		if( blogs[blogI]  == blogStr )
		{
			if( action = 'show' ) blogs.splice(blogI,1);				
			IsBlogNotExist = false;
			break;
		}
	}	
	if( IsBlogNotExist ) blogs.push(blogStr);	
	set_cookie('blogs', blogs.join(':'),2020,0,1,'/' );
	return;
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
	var cookie_string = name + "=" + escape ( value );
	if (exp_y) //delete_cookie(name)
	{
		var expires = new Date ( exp_y, exp_m, exp_d );
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if (path) cookie_string += "; path=" + escape ( path );
	if (domain) cookie_string += "; domain=" + escape ( domain );
	if (secure) cookie_string += "; secure";

	document.cookie = cookie_string;
}

// Retrieving cookies
function get_cookie(cookie_name)
{
	var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');
	if (results) return (unescape(results[1]));
	else return null;
}

function clear_cookie(name)
{
	var ThreeDays=3*24*60*60*1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime()-ThreeDays);
	document.cookie=name+"=;expires="+expDate.toGMTString();
}


function DisableButton( button )
{
	if( ! button ) return;
	button = $(button);
	button.attr('onClick', '');
	button.addClass( 'disableButton' );
	return button;	
}

function EnableButton( button )
{
	if( ! button ) return;
	button = $(button);
	button.attr('onClick', button.attr('disableClick') );
	button.removeClass( 'disableButton' );
	return button;
}

function DisplayDesc( row, e )
{
	windowBox = $('#helpBox');
	windowBox.empty();
	
	block = $('.blogRowDesc', row);
	
	newBlock = block.clone();
	
	windowBox.append( newBlock );
	newBlock.show();
	windowBox.show();
	
	var x = e.pageX;
	var y = e.pageY;
	x+=20;
	y+=20;

	var myWidth = window.innerWidth;
	var myHeight = window.innerHeight;

	if( typeof( window.innerWidth ) != 'number' )
	{
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;		
	}

	if( myHeight - e.clientY -  newBlock.height() - 65 < 0 )
	{
		y -= 70+newBlock.height();				
	}
	if( myWidth - e.clientX -  newBlock.width() - 80 < 0 )
	{
		x -= 70+newBlock.width();		
		
	}
	windowBox.css('left', x );
	windowBox.css('top', y );
}

function HideDesc( row, e )
{
	$('#helpBox').hide();
}

function DisplayAddForm()
{
	$('#addForm').fadeIn();
	if( ! $("#blogTags").attr('suggest') )	
		$("#blogTags").suggest(tagsList,{
				'sourceType' : 'array',
				'selectType': 'add',
				'minchars': -1				
			}
		);	
}

function HideAddForm()
{
	$('#addForm').fadeOut();
}

function SendNewBlog()
{
	var isError = false;
	$.each( ['userEmail', 'blogURL'], function(){
		var fieldName = this;
		if (!$('#' + fieldName).val()) {
			$('#' + fieldName).addClass('errorField');
			isError = true;
		}
		
		$('#'+fieldName).keyup( function(){
			if( $(this).val() ) $(this).removeClass('errorField');
		} );		
	} );
	
	if( isError ) return;
	
	var qstring ='';
	$.each( ['userName', 'userEmail', 'blogURL', 'blogTags'], function(){
		var fieldName = this;
		if( $('#'+fieldName).val() )
		{
			qstring += fieldName+'='+encodeURI( $('#'+fieldName).val() )+'&';
			$('#'+fieldName).val('')
		}
	});
	if( !qstring ) return;
	qstring = qstring.substr(0,qstring.length-1);
	$.ajax({
		   type: "POST",
		   url: "/add/",
		   data: qstring
 	});
	$('#addForm').fadeOut('fast', function(){
		$('#addThanks').fadeIn('fast');
		setTimeout( function(){
			$('#addThanks').fadeOut('fast');
		}, 2000); 
	});
}

///################################################################
