// +-------------------------------------------------------------+
// | Zaplife Channels javascript file
// | All rights reserved. Copyright 2005-2009 Zaplife
// |
// +-------------------------------------------------------------+

var channelPhotoView = 0;
var commentReplyMessage = '';
var commentReplyToUserId = '';
var archiveVisible = 1;

// Used to load channel entries into a page
function loadChannelEntries(type, id, container, page, year, month, paginationContainer) {
	if (typeof page == "undefined") {
		var page = 1;
	}
	/*
	if (typeof scroll == "undefined") {
		var scroll = true;
	}
	*/
	if (typeof year == "undefined") {
		var year = '';
	}
	if (typeof month == "undefined") {
		var month = '';
	}
	if (typeof paginationContainer == "undefined") {
		var paginationContainer = '';
	}
	
	if ($(paginationContainer)) {
		startLoader(paginationContainer, 'bottom');
	} else {
		startLoader(container);
	}
	if ($('entryNavigation')) {
		startLoader('entryNavigation');
	}
	
	var vars = {
		url: webPath + 'ajax_channels.php', 
		method: 'get',
		evalScripts: true,
		//update: $(container),
		data: 'action=load_channel_entries&id=' + id + '&type=' + type + '&page=' + page + '&year=' + year + '&month=' + month + sessionVar,
		onComplete: function (responseTree, responseElements, responseHTML, responseJavaScript) {
			if ($(paginationContainer)) {
				$(paginationContainer).destroy();
				$(container).adopt(responseTree);
			} else {
				$(container).set('html', responseHTML);
			}
			startSlideshow.update();
			if ($('entryNavigation')) {
				loadEntryNavigation();
			}
			if (trimmer) {
				trimmer.loadChunks();
    			trimmer.doTrim(trimmer.currentStep);
			}
			/* Is this used anymore? Its done on viewchannel inline
			if (selectedEntryId != '' && scroll) {
				setTimeout("scrollToElement('e" + selectedEntryId + "')", 1000); //Give time to let content load such as you tube movies
			}
			*/
		}
	};
	var myAjax = new Request.HTML(vars).send();
}

//Used to load channel navigation into a page
function loadEntryNavigation() {
	$('entryNavigation').set('html', $('entryNavigationMenuHtml').get('html'));
}

/*
function loadEntryNavigation(type, id, page, year, month) {
	if (typeof page == "undefined") {
		var page = 1;
	}
	if (typeof year == "undefined") {
		var year = '';
	}
	if (typeof month == "undefined") {
		var month = '';
	}
	
	startLoader('entryNavigation');
	
	var vars = {
		url: 'ajax_channels.php', 
		method: 'post',
		//update: $(container),
		data: 'action=load_entry_navigation&' + 'channel_id=' + id + '&type=' + type + '&page=' + page + '&year=' + year + '&month=' + month,
		onComplete: function (responseTree, responseElements, responseHTML, responseJavaScript) {
			$('entryNavigation').set('html', responseHTML);
		}
	};
	var myAjax = new Request.HTML(vars).send();
}
*/


function toggleEntryComments(entryId, newComment, form) {

	if ($('entryCommentsWrapper' + entryId).get('html') == '') {
		loadEntryComments(entryId, newComment, form);
	} else {
		//$('entryComments' + entryId).slide('toggle');
		$('entryCommentsWrapper' + entryId).empty();
	}

	//$('entryComments' + entryId).get('slide').open
}

function loadEntryComments(entryId, newComment, form) {
	
	if (typeof newComment != "undefined" && typeof newComment != "undefined") { //Case when we want to have toggle

		var entryCommentsFx = new Fx.Slide('entryCommentsWrapper' + entryId, {
			mode: 'vertical',
			onComplete: function() {
			//Must add this when you got two slides, otherwise the second remains hidden
			if(this.wrapper.offsetHeight != 0)
				this.wrapper.setStyle('height', 'auto');
			}
		});
		entryCommentsFx.hide();
		
		if (form) {
			var action = 'load_entry_comments_form';
		} else {
			var action = 'load_entry_comments';
		}
		
		var vars = {
			url: webPath + 'ajax_channels.php', 
			method: 'get',
			data: 'action=' + action + '&entry_id=' + entryId + '&write=' + form + sessionVar,
			//update: $('entryCommentsWrapper' + entryId),
			onComplete: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				$('entryCommentsWrapper' + entryId).set('html', responseHTML);
				if (form) {
					if (newComment) {
						$('newEntryComment' + entryId).slide('show');
						$('newEntryCommentLinkWrapper' + entryId).setStyle('display', 'none');
					} else {
						$('newEntryComment' + entryId).slide('hide');
						$('newEntryCommentLinkWrapper' + entryId).setStyle('display', '');
						$('newEntryCommentLink' + entryId).addEvent('click', function(e){
							e = new Event(e);
							$('newEntryComment' + entryId).slide('toggle');
							e.stop();
						});
					}
				}
				this.slideIn();
			}.bind(entryCommentsFx)
		}
		
	} else { //Case when we just want to update comments without toggle
		var vars = {
			url: webPath + 'ajax_channels.php', 
			method: 'get',
			data: 'action=load_entry_comments&entry_id=' + entryId + sessionVar,
			//update: $('entryComments' + entryId),
			onComplete: function (responseTree, responseElements, responseHTML, responseJavaScript) {
				$('entryComments' + entryId).set('html', responseHTML);
			}
		}		
	}
    var myAjax = new Request.HTML(vars).send();
}

/*
function loadEntryComments(entryId) {
	var pars = 'action=load_entry_comments&entry_id=' + entryId;
    var myAjax = new Request.HTML({url: 'ajax_channels.php', method: 'get', data: pars, update: $('entryComments' + entryId)}).send();
}
*/

function addComment(entryId) {
	if ($('commentMessage' + entryId).value == '' || langDefaultCommentMessage == $('commentMessage' + entryId).value || commentReplyMessage == $('commentMessage' + entryId).value) {
		doError('commentMessage' + entryId, langCommentMessageError);
	} else {
		var vars = {
			url: webPath + 'ajax_channels.php', 
    		method: 'post',
    		data: 'action=add_comment&' + $('newEntryCommentForm' + entryId).toQueryString() + '&reply_to_user_id=' + commentReplyToUserId + sessionVar,
    		onComplete: function () {
    			loadEntryComments(entryId);
				$('newEntryComment' + entryId).slide('toggle');
				$('commentMessage' + entryId).value = langDefaultCommentMessage;
    		}
    	};
    	var myAjax = new Request(vars).send();
	}
	return false;
}

function removeComment(id, entryId) {
    var vars = {
		url: webPath + 'ajax_channels.php', 
    	method: 'get',
    	data: 'action=remove_comment&' + 'comment_id=' + id + '&entry_id=' + entryId + sessionVar,
    	onComplete: function () {
    		loadEntryComments(entryId);
    	}
    };
    var myAjax = new Request(vars).send();
}

function replyToComment(entryId, replyId, username) {
	commentReplyMessage = langDefaultCommentReplyMessage + ' ' + username;
	$('commentMessage' + entryId).value = commentReplyMessage;
	commentReplyToUserId = replyId;
	$('newEntryComment' + entryId).slide('toggle');
}


function toggleChannelView(type, id) {
	if (channelPhotoView == 0) {
		channelPhotoView = 1;
		$('showPhotoView').setStyle('display', 'none');
		$('showEntryView').setStyle('display', '');
		
		loadPhotoThumbs(type, id, 'channelEntries');
		
		//$('mainCardTitle').setHTML(sentText);
	} else {
		channelPhotoView = 0;
		$('showEntryView').setStyle('display', 'none');
		$('showPhotoView').setStyle('display', '');
		
		loadChannelEntries(type, id, 'channelEntries');
		
		//$('mainCardTitle').setHTML(inboxText);
	}
}

function toggleSubscription(id, close, entryId) {

	if (typeof close == "undefined") {
		var close = false;
	}
	
	if (typeof entryId == "undefined") {
		var entryId = '';
	}

	var vars = {
		url: 'ajax_channels.php', 
    	method: 'get',
    	data: 'action=subscription&channel_id=' + id + sessionVar,
    	onComplete: function () {
    		if (close) {
    				$('channel' + id).setStyle('display', 'none');
    		} else {
				if ($('showSubscribe' + entryId).getStyle('display') == 'none') {
					$('showSubscribe' + entryId).setStyle('display', '');
					$('showUnsubscribe' + entryId).setStyle('display', 'none');
				} else {
					$('showSubscribe' + entryId).setStyle('display', 'none');
					$('showUnsubscribe' + entryId).setStyle('display', '');
				}
			}
    	}
    };
    var myAjax = new Request(vars).send();
    
}

function toggleChannelInfo(id) {
	if ($('channel' + id + 'Info2').getStyle('display') == 'none') {
		new Fx.Tween($('channel' + id + 'Info1'), {
			onComplete: function () { 
				$('channel' + id + 'Info1').setStyle('display', 'none');
				$('channel' + id + 'Info2').fade('hide');
				$('channel' + id + 'Info2').setStyle('display', '');
				$('channel' + id + 'Info2').fade('in'); 
			} 
		}).start('opacity', 1, 0);
	} else {
		new Fx.Tween($('channel' + id + 'Info2'), {
			onComplete: function () { 
				$('channel' + id + 'Info2').setStyle('display', 'none');
				$('channel' + id + 'Info1').fade('hide');
				$('channel' + id + 'Info1').setStyle('display', '');
				$('channel' + id + 'Info1').fade('in'); 
			} 
		}).start('opacity', 1, 0);
	}
}

function showArchiveMenu(action) {
	if (action == 'older') {
		var nextArchive = archiveVisible + 1;
	
		if ($('archive' + nextArchive)) {
			$('archive' + archiveVisible).setStyle('display', 'none');
			$('archive' + nextArchive).fade('hide');
			$('archive' + nextArchive).setStyle('display', '');
			$('archive' + nextArchive).fade('in'); 
			
			if (archiveVisible == 1) {
				$('newerArchive').setStyle('display', '');
			}
			
			archiveVisible = nextArchive;
			nextArchive = archiveVisible + 1;
			if (!$('archive' + nextArchive)) {
				$('olderArchive').setStyle('display', 'none');
			}
		}
	} else {
		var nextArchive = archiveVisible - 1;
	
		if ($('archive' + nextArchive)) {
			$('archive' + archiveVisible).setStyle('display', 'none');
			$('archive' + nextArchive).fade('hide');
			$('archive' + nextArchive).setStyle('display', '');
			$('archive' + nextArchive).fade('in'); 
						
			if (nextArchive == 1) {
				$('newerArchive').setStyle('display', 'none');
			}
			
			$('olderArchive').setStyle('display', '');
			
			archiveVisible = nextArchive;
		}
	}
}

