function echeck(str) 
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }

	 return true;				
}

function maximize( id )
{
	$('#frame_'+id).parent().find('.textContent div.minimize').css("display","none");
	$('#frame_'+id).parent().find('.textContent div.maximize').css("display","block");
	
	$('#frame_'+id).parent().find('div.cmd a:first img').attr('src', "img/cmd_minus.gif");
}

function minimize( id )
{
	$('#frame_'+id).parent().find('.textContent div.minimize').css("display","block");
	$('#frame_'+id).parent().find('.textContent div.maximize').css("display","none");
	
	$('#frame_'+id).parent().find('div.cmd a:first img').attr('src', "img/cmd_plus.gif");
}

function read( id )
{
	var urlLink = $('#url_'+id).attr('value');
	$('#frame_'+id).attr('src', "article_relations/read/"+urlLink);
	$('#check_'+id).attr('checked', false);
	$('#checked_'+id).attr('value','true');
	if( $('#check_'+id).parent().find('img').attr('id') != null )
	{
		$('#check_'+id).parent().find('img').attr('src','img/bg_unchecked.gif');
	}
	$('#frame_'+id).parent().find('div.cmd a:last').removeClass('gold');
	$('#frame_'+id).parent().find('div.cmd a:last').addClass('grey');
	
	maximize( id );
}

function unread( id )
{
	var urlLink = $('#url_'+id).attr('value');
	$('#frame_'+id).attr('src', "article_relations/unread/"+urlLink);
	$('#check_'+id).attr('checked', true);
	$('#checked_'+id).attr('value','');
	if( $('#check_'+id).parent().find('img').attr('id') != null )
	{
		$('#check_'+id).parent().find('img').attr('src','img/bg_checked.gif');
	}
	$('#frame_'+id).parent().find('div.cmd a:last').removeClass('grey');
	$('#frame_'+id).parent().find('div.cmd a:last').addClass('gold');
}

function updateRead( id )
{
	if($('#checked_'+id).attr('value') == "true")
	{
		unread( id );
	}
	else
	{
		read( id );
	}
}

function updateDisplay( id )
{
	if( $('#frame_'+id).parent().find('.textContent div.maximize').css("display") == "none" )
	{
		read( id );
	}
	else
	{
		minimize( id );
	}
}

function filterLength()
{
	var length = $('#itemsPerPage').attr('value');
	if(length=="")
	{
		window.location.href = "rss_feeds/filterLength";
	}
	else
	{
		window.location.href = "rss_feeds/filterLength/"+length;
	}
}

function filterCategory()
{
	var category = $('#sortBy').attr('value');
	if(category=="full")
	{
		window.location.href = "rss_feeds/filterCategory";
	}
	else
	{
		window.location.href = "rss_feeds/filterCategory/"+category;
	}
}

function changePopupShareUrl( title,url )
{
	$("#shareTitle").attr('value',title);
	$("#shareUrl").attr('value',url);
}

function showSendAFriend(title,url)
{
	//$("#"+caller.id).position().top
	//$("#sendAFriendForm").css("display","block");
	changePopupShareUrl(title,url);
	centerPopup();
	loadPopup();
}

function hideSendAFriend(caller)
{
	//$("#"+caller.id).position().top
	$("#sendAFriendForm").css("display","none");
}

function changePopupTagValues(linkURL,userID,b64)
{
	$("#ArticleRelationUrl").attr('value',linkURL);
	$("#ArticleRelationUserID").attr('value',userID);
	$("#popupAddTagForm").attr('action',($("#popupAddTagBase").attr('value')+"/"+b64));
}

function showAddTag(linkURL,userID,b64)
{
	//$('#addTag_'+id).css("display","block");
	//loadPopup("#addTag_"+id);
	changePopupTagValues(linkURL,userID,b64);
	centerPopup("#popupAddTag");
	loadPopup("#popupAddTag");
}

function hideAddTag()
{
	//$('#addTag_'+id).css("display","none");
	disablePopup();
}

function pagerFactory(idx, slide) 
{
	var s = idx > 2 ? ' style="display:none"' : '';
	return '<a href="#">'+(idx+1)+'</a>';
}

function validateEmailsAndSubmit()
{
	var from = echeck($('#contactArea_from_email').attr('value'));
	var to = echeck($('#contactArea_to_email').attr('value'));

	if(!from)
	{
	   $('#fromInvalid').css("visibility","visible");
	}
	else
	{
	   $('#fromInvalid').css("visibility","hidden");
	}
	if(!to)
	{
		$('#toInvalid').css("visibility","visible");
	}
	else
	{
		$('#toInvalid').css("visibility","hidden");
	}
	if(from && to)
	{
		$('#contactArea').submit();
	}
}

function makeFeedContentLinksExternal()
{
	$(".description a").each(function(){
		$(this).attr('target','_blank');
	});
}

var _itemsPerPageCombobox = null;
var _sortByCombobox = null;
var _cssStyleSelectJQ = null;
var _animationTypeSelectJQ = null;
var _animationType = "slide";

$(document).ready(function(){
	var comboboxSettings = {animationSpeed: 100};
	
	_itemsPerPageCombobox = $('#itemsPerPage').combobox(
	{
		  comboboxContainerClass: "comboboxContainer", 
		  comboboxValueContainerClass: "comboboxValueContainer", 
		  comboboxValueContentClass: "comboboxValueContent", 
		  comboboxDropDownClass: "comboboxDropDownContainer", 
		  comboboxDropDownButtonClass: "comboboxDropDownButton", 
		  comboboxDropDownItemClass: "comboboxItem", 
		  comboboxDropDownItemHoverClass: "comboboxItemHover", 
		  comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader", 
		  comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer", 
		  width: "194px",
		  onChange: function(){ filterLength(); }
	});

	_sortByCombobox = $('#sortBy').combobox(
	{
		  comboboxContainerClass: "comboboxContainer", 
		  comboboxValueContainerClass: "comboboxValueContainer", 
		  comboboxValueContentClass: "comboboxValueContent", 
		  comboboxDropDownClass: "comboboxDropDownContainer", 
		  comboboxDropDownButtonClass: "comboboxDropDownButton", 
		  comboboxDropDownItemClass: "comboboxItem", 
		  comboboxDropDownItemHoverClass: "comboboxItemHover", 
		  comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader", 
		  comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer", 
		  width: "98px",
		  onChange: function(){ filterCategory(); }
	});
	$('#slideshow').cycle(
	{
		fx:      'fade',
		timeout:  3000,
		prev:    '#prev',
		next:    '#next',
		pager:   '#nav',
		pagerAnchorBuilder: pagerFactory
	});	
	makeFeedContentLinksExternal();
});
