
// var chatTag='small-talk';	// not currently used

var pageContent=Array();
var currentPage=1;



$(function() {


singleColWidth();

// saveSeen();

/*
|----------------------------------------------------------------------------------------------------------------------------------
| display avatars
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

var updownSrc=$('#upDown').find('img').attr('src');

$('#upDown').click(function(){	
		
	if ($('#avatars').css('height')=='150px'){
	
		var h=parseInt(($('#avatars').find('a').length/20+1)*47);
		$('#avatars').animate({height: h+ "px"}, 500 );
		$(this).find('a').attr('src',updownSrc.replace('down','up'))
	}else{
	
		$('#avatars').animate({height: "150px"}, 500 );
		$(this).find('a').attr('src',updownSrc);

	}
})





/*
|----------------------------------------------------------------------------------------------------------------------------------
| Next Button Hijack
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

$('#next').livequery('click', function(event) { 	

	pageContent[currentPage]=$('.innerContainer').html();

	currentPage++;

	if (typeof(pageContent[currentPage])!='undefined'){

		loadContent();
		preFetchContent();

	}else{
		getPageContent($(this).attr('href'));
	}

	saveSeen();

	return false;
})

/*
|----------------------------------------------------------------------------------------------------------------------------------
| Prev Button Hijack
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

$('#previous').livequery('click', function(event) { 	

	if (currentPage>0 && typeof(pageContent[currentPage-1])!='undefined'){
		
		currentPage--;
		loadContent();
		
		return false;
	}
})

/*
|----------------------------------------------------------------------------------------------------------------------------------
| getPageContent
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

function getPageContent(url){

	$.ajax({
	   url: url + '/ajaxPage/y',
	   success: function(response){
			if (response){
				pageContent[currentPage]=response;
				loadContent();
				preFetchContent();
			}
	   }
   });	
}


function loadContent(){
	$('.innerContainer').html(pageContent[currentPage]);
	$('#avatars>a').removeClass('active');

	$('.author').each(function(){
		var id=$(this).attr('id').replace('auth_','');
		$('#avatar_'+id).addClass('active');
	});
}



/*
|----------------------------------------------------------------------------------------------------------------------------------
| preFetchContent
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

function preFetchContent(){

	if (typeof(pageContent[currentPage+1]=='undefined') && typeof($('#next').attr('href'))!='undefined'){

		$.ajax({
		   url: $('#next').attr('href') + '/ajaxPage/y',
		   success: function(response){
				if (response){
					pageContent[currentPage+1]=response;
				}
		   }
	   });	
	}
}




/*
|----------------------------------------------------------------------------------------------------------------------------------
| hilight avatars
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/


$('#categories>li>a').hover(function(){	

	if ($(this).attr('rel')) $('.' + $(this).attr('rel')).addClass('hilight');
	
},function(){

	if ($(this).attr('rel')) $('.' + $(this).attr('rel')).removeClass('hilight');
})



/*
|----------------------------------------------------------------------------------------------------------------------------------
| display avatars
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/

$('.more').livequery('click', function(event) { 	
	
	var s=$(this).attr('id').split('_');
	var more=$('#authorPage_' + s[1]).val()*1+1;

	var obj=$(this);

	var url=siteUrl + 'ajax/y/more/' +  more + '/authID/' + s[1];

	$.ajax({
	   type: 'POST',
	   url: url,
	   success: function(response){
			if (response){
				$('#authorPage_' + s[1]).val(more);
				obj.before('<div id="page_' + s[1] +'_' + more + '" style="display:none;" class="page">' + response +'</div>');	
				
				singleColWidth();

				$('#page_' + s[1] +'_' + more).slideDown('slow');

			}
	   }
   });		
})



/*
|----------------------------------------------------------------------------------------------------------------------------------
| display delete tag button
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('.tags').find('.tag').livequery(function(){ 
		$(this).mouseover(function(){	
			$('#delTag').remove();
			$(this).after('<a href="javascript:void(0)" id="delTag" title="delete this tag" rel="' + $(this).attr('id') +'">[x]</a>');
		})
	})


/*
|----------------------------------------------------------------------------------------------------------------------------------
| click delete tag button
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('#delTag').livequery(function(){ 
		$(this).click(function(){

			var s=$(this).attr('rel').split('_');
			var thisLinkID=$(this).attr('rel');

			var parameters='ajax=deleteTag'
			parameters=parameters+'&tagID=' + s[2];
			parameters=parameters+'&id=' + s[1];

			$.ajax({
			   type: 'POST',
			   data: parameters,
			   url: siteUrl,
			   success: function(response){
					if (response){
						$('#delTag').remove();
						$('#' + thisLinkID).remove();
					}
					return false;
			   }
		   });		

			return false;
		})
	})






	
/*
|----------------------------------------------------------------------------------------------------------------------------------
| click item on predictive list for tags
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('#predictiveList>a').livequery(function(){ 
		$(this).click(function(){

			$('#addTagBox').val($(this).html());
			$("#addTagButton").trigger('click');
			return false;
		})
	})




/*
|----------------------------------------------------------------------------------------------------------------------------------
| show a video
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('.video').find('img').livequery(function(){ 
		$(this).click(function(){
			var obj=$(this).parent();
			obj.fadeOut('slow',function(){
				obj.html(obj.find('.vid').html());
				obj.fadeIn('slow');
			})
			
		})
	})







/*
|----------------------------------------------------------------------------------------------------------------------------------
| click add a tag button
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('#addTagButton').livequery(function(){ 
		$(this).click(function(){

			var tagLine=$(this).parent().parent().find('.tags');
			var thisLinkID=tagLine.attr('id');

			var parameters='ajax=addTag'
			parameters=parameters+'&tag=' + $('#addTagBox').val();
			parameters=parameters+'&id=' + $(this).attr('rel');

			$.ajax({
			   type: 'POST',
			   data: parameters,
			   url: siteUrl,
			   success: function(response){
					if (response){
						$('#addTag').remove();

						var s=response.split('|'); // tag , itemID, tagID
						$('#tags_'+s[1]).append('<a href="'+siteUrl+'tag/'+s[0]+'/"  class="tag" id="tag_'+s[1]+'_'+s[2]+'">'+s[0]+'</a>');
						$('#tags_'+s[1]).show();
					}
					return false;
			   }
		   });		

			return false;
		})
	})




/*
|----------------------------------------------------------------------------------------------------------------------------------
| diplay the predictive text list
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
	$('#addTagBox').livequery(function(){ 
		$(this).keyup(function(e){

			if(e.keyCode == 13) {
				$("#addTagButton").trigger('click');
				return;
			}


			var val=$(this).val();
			var obj=$(this);
			
			$('#predictiveList').remove();

			if(val.length>0){

				var parameters='ajax=predictiveText'
				parameters=parameters+'&val=' + $('#addTagBox').val();

				$.ajax({
				   type: 'POST',
				   data: parameters,
				   url: siteUrl,
				   success: function(response){
						if (response){
							obj.after(response);		
						}
				   }
			   });		
			}
		})
	})





/*
|----------------------------------------------------------------------------------------------------------------------------------
| mark an item as chat
|----------------------------------------------------------------------------------------------------------------------------------
|	
|	skip this for now
|
*/
	$('#chatButton').livequery(function(){ 
		$(this).click(function(){
			$('#addTagBox').val(chatTag);
			$("#addTagButton").trigger('click');
		})
	})




/*
|----------------------------------------------------------------------------------------------------------------------------------
| display the add-tag box
|----------------------------------------------------------------------------------------------------------------------------------
|
|

$('.date').livequery(function(){ 
	$(this).mouseover(function(){
	
		if (!$(this).parent().find('#addTag').length){

			$('#addTag').remove();
			$(this).after('<div id="addTag" style="display:none"><input type="text" id="addTagBox" class="addTagBox" /><input type="button" value="add tag" class="tagbtn" id="addTagButton" rel="'+$(this).attr('rel')+'" /></div>'); // <input type="button" value="'+chatTag+'" id="chatButton" class="tagbtn" />
			$('#addTag').slideDown();
		}
		return false;
		})					
	})

*/

})		

/*
|----------------------------------------------------------------------------------------------------------------------------------
| singleColWidth
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function singleColWidth(){

	if ($('.author').length==1){

		$('.more').before('<div class="clear"></div>');

		$('.author').css('width','620px');
		$('.author').css('margin-left','180px');
		$('.feed').css('width','300px');
		$('.feed').css('float','left');

		$('.feed:even').css('margin-right','20px');
		$('.feed:even').css('clear','both');

	}

}






/*
|----------------------------------------------------------------------------------------------------------------------------------
| saveSeen
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function saveSeen(){


	var seen=getCookie('seen');
	var seenFeeds=Array();
	var id;
	var authID;
	var str='';


	if (seen!=null)	{
		var s=seen.split(':');
		for (i=0;i<s.length ;i=i+2 ){
			seenFeeds[s[i]]=s[i+1];		// an array where the key is the author id, the value is the highest feed ID
		}
	}
	
	$('.author').each(function(){
		
		s=$(this).attr('id').split('_');	
		authID=s[1];

		s=$(this).find('.feed:0').attr('id').split('_');		// this is the id of the feed with the highest date.
		id=s[1];

		if (typeof(seenFeeds[authID])=='undefined'){
			seenFeeds[authID]=id;
		}else	if (seenFeeds[authID]<id){
			seenFeeds[authID]=id;
		}
	})

	for (var key in seenFeeds) {
		str=str+ (str.length>0?':':'') + key + ':' + seenFeeds[key];
	}

	setCookie('seen',str,100);
}



/*
|----------------------------------------------------------------------------------------------------------------------------------
| reload
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function reload(){

	$('.reload').livequery('click', function(event) { 
	
		var id=$(this).attr('id').split('_');
		
		$('.wait').remove();

		$(this).after('<img src="/feeds/images/wait.gif"  class="wait wait2" />');

		$.ajax({
		   type: 'POST',
		   url: siteUrl + 'admin/reload/y/feed_id/' + id[1],
		   success: function(response){
				window.location.href=window.location.href;			
		   }
	   });		
	})
}


/*
|----------------------------------------------------------------------------------------------------------------------------------
| badPic
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function badPic(){

	$('.block').livequery('click', function(event) { 	

		if (confirm('are you sure you want to block this image?')){

			var src=$(this).parent().find('img').attr('src');

			$(this).parent().find('img').remove();

			var parameters='ajax=badPic'
			parameters=parameters+'&src=' + src;

			$.ajax({
			   type: 'POST',
			   url: siteUrl,
			   data: parameters,
			   success: function(response){

			   }
		   });
		}

	})
}





/*
|----------------------------------------------------------------------------------------------------------------------------------
| trim
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function trim(str){
	
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	
	return s;
}




/*
|----------------------------------------------------------------------------------------------------------------------------------
| cookie functions
|----------------------------------------------------------------------------------------------------------------------------------
|
|
*/
function setCookie(name,value,days){
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function deleteCookie(name) {
	setCookie(name,"",-1);
}