/*******************************************************************************************/

/*
 * LayersOSIdentitiesAccordion
 */


function LayersOSIdentitiesAccordion(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	
	//console.info(this._displayable);
	
	var me = this;
		
	if( $('#layersOS_identities_content').find('li').length == 0 ){
		me.loadIdentityList();
	}			
						
	this._referal._displayable.find('#layersOS_identities').show();
	
}

LayersOSIdentitiesAccordion.prototype._referal;
LayersOSIdentitiesAccordion.prototype._displayable;


LayersOSIdentitiesAccordion.prototype.loadIdentityList = function(){
	
	var me = this;
	
	$('#layersOS_identities_content').slideUp('normal',function(){    				
		
		try {
			// Create logout button
			new LayersOSLogoutButton(me._referal, $('#layersOS_identities_content') );
		
			var identitiesArray = os.getCurrentUser().getIdentities();
	
			// Get all identities from current user and create an identityButton for each, except for the current identity.            				
			for (identity in identitiesArray){
				var tmpIdentity = identitiesArray[identity];
	
				if (tmpIdentity._displayName != os.getCurrentIdentity()._displayName) {
					new LayersOSIdentityButton(me._referal, $('#layersOS_identities_content'), tmpIdentity._displayName, tmpIdentity._imageURL );				
				}														 
			}		
		
			$('#layersOS_identities_content').show();
			
		} catch (e){
			console.info(e);
		}	
										
	});
}

/*******************************************************************************************/

/*
 * LayersOSLayerListAccordion
 */


function LayersOSLayerListAccordion(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	this._layerIterator = new LayerIterator();
	this._itemsPerPage = 3;
	
	var me = this;
	
	if( this._displayable.find('li').length == 0 ){			
		os.getLayersOverThisPage();		
	}		
								
}

LayersOSLayerListAccordion.prototype._referal;
LayersOSLayerListAccordion.prototype._displayable;
LayersOSLayerListAccordion.prototype._layerIterator;
LayersOSLayerListAccordion.prototype._itemsPerPage;


LayersOSLayerListAccordion.prototype.loadList = function(direction){	
		var me = this;

		try {					
			$('#layersOS_item_container').empty();
																						
			if (direction == 'next'){				
				var i = 0;		
				
				while ( i < me._itemsPerPage && me._layerIterator.hasNextLayer() && !me._layerIterator._isRetrieving ) {
					var tmp = me._layerIterator.nextLayer();
					if (tmp != undefined) {
						new LayersOSLayerItem(me._referal, $('#layersOS_item_container'), tmp);
						i = i + 1;
					}
				}	
			}
			
			else if (direction == 'previous'){
				var i = 1;
				while ( i <= me._itemsPerPage) {					
					new LayersOSLayerItem(me._referal, $('#layersOS_item_container'), me._layerIterator.previousLayer());
					i = i + 1;					
				}	
			}											
							
			
			$('#layersOS_layers_over_page').find('.layersOS_list_loading').hide();																
			$('#layersOS_layers_over_page').show();
			$('#layersOS_layers_over_page_content').show();
			
		}catch(e){
			console.error(e);
			//throw(e);
		}
		
		
}

LayersOSLayerListAccordion.prototype.updateLayerIterator = function(layerIterator){
	
	var me = this;
	
	try{						
		this._layerIterator = Utils.prototype.mergeIterators(this._layerIterator, layerIterator);
		
		this._layerIterator._isRetrieving = false;
		var noResultsHTML = '<li class="layersOS_list_layer_empty">' + STRING_list_empty + '</li></ul>';
						
		if (this._layerIterator._count == 0) {
			this._displayable.html(noResultsHTML);			
		}
		else {
			var me = this;
			
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
						
			$('#layersOS_layers_over_page').slideUp('normal', function(){
				try{								
					me.loadList('next');	
				}catch(e){										
					//Show no results.
					me._displayable.html(noResultsHTML);									
				}		
			
			});				
		}
		
		var pagingButtons = $('#layersOS_layers_over_page_content').find('.layersOS_paging_prev_next');
				
		if (this._layerIterator._count > this._itemsPerPage  && pagingButtons.length == 0) {			
			new LayersOSLayerPaginationButtons(this._referal, $('#layersOS_layers_over_page_content'),this);
		}
		
	}catch(e){			
		console.error(e);
		throw e;
	}

}

LayersOSLayerListAccordion.prototype.getCurrentIndex = function(){
	return this._layerIterator._index;
}

LayersOSLayerListAccordion.prototype.getTotalItems = function(){
	return this._layerIterator._count;
}

/*******************************************************************************************/

/*
 * LayersOSLayerListLoggedAccordion
 */


function LayersOSLayerListLoggedAccordion(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	this._layerIterator = "";
	this._itemsPerPage = 3;
	
	this.dockAccordion();
	
	var me = this;
		
	if( this._displayable.find('li').length == 0 ){			
		try{
			//os.getLayersOverThisPageByMyFriends();
			os.getLayersOverThisPageNotFriends();
		}catch(e){
			alert(e);
			console.error(e);
		}
				
	}		
								
}

LayersOSLayerListLoggedAccordion.prototype._referal;
LayersOSLayerListLoggedAccordion.prototype._displayable;

LayersOSLayerListLoggedAccordion.prototype._myLayerIterator;
LayersOSLayerListLoggedAccordion.prototype._myFriendsLayerIterator;
LayersOSLayerListLoggedAccordion.prototype._othersLayerIterator;

LayersOSLayerListLoggedAccordion.prototype._itemsPerPage;


LayersOSLayerListLoggedAccordion.prototype.loadList = function(direction, itemContainer, type){	
		var me = this;
		var workingIterator = this._myLayerIterator;
		
		var itemsDiv = itemContainer.children(":first");
		
		switch (type){
			case 'myLayers':
				workingIterator = this._myLayerIterator;
				break;
			
			case 'myFriendsLayers':
				workingIterator = this._myFriendsLayerIterator;
				break;
			
			case 'otherLayers':
				workingIterator = this._othersLayerIterator;
				break;
		}
		
		
		try {					
			itemsDiv.empty();
																						
			if (direction == 'next'){				
				var i = 0;			
										
				while ( i < me._itemsPerPage && workingIterator.hasNextLayer() && !workingIterator._isRetrieving) {
					console.info(workingIterator._index);
					var tmp = workingIterator.nextLayer();
					if (tmp != undefined){
						new LayersOSLayerItem(me._referal, itemsDiv, tmp);					
						i = i + 1;
					}								
				}
				

			}
			
			else if (direction == 'previous'){
				var i = 1;
				while ( i <= me._itemsPerPage) {					
					new LayersOSLayerItem(me._referal, itemsDiv, workingIterator.previousLayer());
					i = i + 1;					
				}	
			}											
										
			$('#layersOS_layers_over_page').find('.layersOS_list_loading').hide();																
			$('#layersOS_layers_over_page').show();
			itemContainer.show();			
			
		}catch(e){				
			throw(e);
		}
		
		
}

LayersOSLayerListLoggedAccordion.prototype.updateLayerIterator = function(layerIterator, type){

	try{
		
		var noResultsHTML = '<li class="layersOS_list_layer_empty">' + STRING_list_empty + '</li></ul>';
		
		var itemContainer = $('#layersOS_layers_over_page_myfriends_content');
		var loadingToHide;
		
		switch (type){
			case 'myLayers':
				this._myLayerIterator = Utils.prototype.mergeIterators(this._myLayerIterator, layerIterator);
				this._myLayerIterator._isRetrieving = false;
				workingIterator = this._myLayerIterator;				
				itemContainer = $('#layersOS_layers_over_page_mylayers_content');
				loadingToHide = $('#myLayers .layersOS_list_loading');
				break;
			
			case 'myFriendsLayers':
				this._myFriendsLayerIterator = Utils.prototype.mergeIterators(this._myFriendsLayerIterator, layerIterator);
				this._myFriendsLayerIterator._isRetrieving = false;
				workingIterator = this._myFriendsLayerIterator;				
				itemContainer = $('#layersOS_layers_over_page_myfriends_content');
				//loadingToHide = $('#otherLayers .layersOS_list_loading');
				break;
			
			case 'otherLayers':
				this._othersLayerIterator = Utils.prototype.mergeIterators(this._othersLayerIterator, layerIterator);
				this._othersLayerIterator._isRetrieving = false;
				workingIterator = this._othersLayerIterator;
				itemContainer = $('#layersOS_layers_over_page_others_content');
				loadingToHide = $('#otherLayers .layersOS_list_loading');
				break;
		}
								
		if (workingIterator._count == 0) {			
			itemContainer.html(noResultsHTML);
			itemContainer.show();
		}else {
			var me = this;			
			itemContainer.slideUp('normal', function(){
				try{
					me.loadList('next', itemContainer, type);	
				}catch(e){										
					//Show no results.
					itemContainer.html(noResultsHTML);
					itemContainer.show();												
				}		
			
			});				
		}
		loadingToHide.hide();
					
		if (workingIterator._count > this._itemsPerPage) {			
			new LayersOSLayerMultiplePaginationButtons(this._referal, itemContainer, this, workingIterator, type);
		}
		
	}catch(e){			
		console.error(e);
		throw e;
	}

}

LayersOSLayerListLoggedAccordion.prototype.dockAccordion = function(){
	var me=this;

	try{
		$('#layersOS_layers_over_page').accordion({
			header: 'div.layersOS_list_title',
			active: 1,
			autoHeight: false,
	        changestart: function(event, ui){
				try{
					me._displayable.find('.layersOS_list_layer_icons').attr("style", "display:none");
					//$(this).find('.layersOS_list_layer_icons').attr("style", "display:none");
					// load content if empty. Content is empty if there are no 'li' elements inside.
				
					console.info( ui.newContent.find('li') );
					
					console.info(ui.newContent.attr("id"));
					
					if( ui.newContent.find('li').length == 0 ) {																		
						try{					
							switch ( ui.newContent.attr("id") ){
								case "otherLayers":							
									os.getLayersOverThisPageNotFriends();
									break;
								
								case "myLayers":							
									os.getMyLayersOverThisPage();
									break; 
							}														
													
						}catch(e){
							alert("357");
							console.error(e);
							throw e;
						}								
						
					}else{
						ui.newContent.find(".layersOS_list_loading").hide();
					}
				}catch(e){
					alert("361");
					throw e;
				}
	
			},
	        change: function(ui, event){
				try{
					// show layer icons			
		            $('#layersOS_layers_over_page .layersOS_list_layer_icons,.layersOS_list_layer_myicons').attr("style", "display:block");
				}catch(e){
					alert("371");
					throw e;
				}
	        }
	    });//.find('.layersOS_list_loading').attr("style", "display:none");
	  
	}catch(e){
		alert("383");
		alert(e);
	}
    
}


LayersOSLayerListLoggedAccordion.prototype.getCurrentIndex = function(){
	return this._layerIterator._index;
}

LayersOSLayerListLoggedAccordion.prototype.getTotalItems = function(){
	return this._layerIterator._count;
}

/*******************************************************************************************/

/*
 * LayersOSMyFriendsLayersAccordion
 */


function LayersOSMyFriendsLayersAccordion(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	this._layerIterator = "";
	this._itemsPerPage = 3;
	
	var me = this;
	
	if( this._displayable.find('li').length == 0 ){			
		var layers = os.getLayersByMyFriends();
		if(layers != undefined){
			this.updateLayerIterator(layers);
		}
	}		
								
}

LayersOSMyFriendsLayersAccordion.prototype._referal;
LayersOSMyFriendsLayersAccordion.prototype._displayable;
LayersOSMyFriendsLayersAccordion.prototype._layerIterator;
LayersOSMyFriendsLayersAccordion.prototype._itemsPerPage;


LayersOSMyFriendsLayersAccordion.prototype.loadList = function(direction){

	var me = this;
	console.info(this._layerIterator);

	try {					
		$('#layersOS_myfriends_other_item_container').empty();
															
		if (direction == 'next'){				
			var i = 0;
								
			while ( i < me._itemsPerPage && me._layerIterator.hasNextLayer() && !me._layerIterator._isRetrieving) {				
				var tmp = me._layerIterator.nextLayer();
				if(tmp != undefined){
					new LayersOSLayerItem(me._referal, $('#layersOS_myfriends_other_item_container'), tmp);					
					i = i + 1;
				}
			}
		}
		
		else if (direction == 'previous'){
			var i = 1;
			while ( i <= me._itemsPerPage) {
				new LayersOSLayerItem(me._referal, $('#layersOS_myfriends_other_item_container'), me._layerIterator.previousLayer());
				i = i + 1;					
			}	
		}											
						
		$('#layersOS_layers_myfriends').find('.layersOS_list_loading').hide();																
		$('#layersOS_layers_myfriends').show();
		$('#layersOS_layers_myfriends_content').show();
		
	}catch(e){
		console.error(e);
		//throw(e);
	}
}


LayersOSMyFriendsLayersAccordion.prototype.updateLayerIterator = function(layerIterator){
console.info("LayersOSMyFriendsLayersAccordion.prototype.updateLayerIterator");

	var me = this;

	
	try{
		tmp2 = Utils.prototype.mergeIterators(me._layerIterator, layerIterator);
		me._layerIterator = tmp2;
		
		me._layerIterator._isRetrieving = false;
		var noResultsHTML = '<li class="layersOS_list_layer_empty">' + STRING_list_empty + '</li></ul>';
		
		
		if (me._layerIterator._count == 0) {
			me._displayable.html(noResultsHTML);			
		}
		else {
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();	
			
			$('#layersOS_layers_myfriends').slideUp('normal', function(){
				try{						
					me.loadList('next');	
				}catch(e){										
					//Show no results.
					me._displayable.html(noResultsHTML);									
				}
			});				
		}
		
		var pagingButtons = $('#layersOS_layers_myfriends_content').find('.layersOS_paging_prev_next');
		console.info(pagingButtons);
		
		if (me._layerIterator._count > me._itemsPerPage && pagingButtons.length == 0) {
			new LayersOSLayerPaginationButtons(me._referal, $('#layersOS_layers_myfriends_content'), me);
		}
		
	}catch(e){			
		console.error(e);
		throw e;
	}

}

LayersOSMyFriendsLayersAccordion.prototype.getCurrentIndex = function(){
	return this._layerIterator._index;
}

LayersOSMyFriendsLayersAccordion.prototype.getTotalItems = function(){
	return this._layerIterator._count;
}

/*******************************************************************************************/

/*
 * LayersOSCommentsAccordion
 */


function LayersOSCommentsAccordion(referal, displayable){
	this._referal = referal;
	this._displayable = displayable;
	this._itemsPerPage = 3;
	this._currentIndex = 0;
	this._submitedComment = new Comment();
	this._submitedComment._displayName = os.getCurrentIdentity()._firstName + " " + os.getCurrentIdentity()._lastName;
	
	if (this._submitedComment._displayName == ""){
		this._submitedComment._displayName = os.getCurrentIdentity()._displayName;
	}
	
	// Create the container of the comments
	this._displayable.append(this._referal._templates.getCommentsBoxHTML());
			
	// Load comments into the container		
	this.updateCommentsArray();
	
	// Enable submit button
	this.enableCommentForm();
	
}

LayersOSCommentsAccordion.prototype._referal;
LayersOSCommentsAccordion.prototype._displayable;
LayersOSCommentsAccordion.prototype._itemsPerPage;
LayersOSCommentsAccordion.prototype._commentsArray;
LayersOSCommentsAccordion.prototype._currentIndex;
LayersOSCommentsAccordion.prototype._submitedComment;


LayersOSCommentsAccordion.prototype.loadList = function(direction, displayable){
	
	var me = this;	
	
	$('#layersOS_comments_list').empty();
																						
	if (direction == 'next'){						
		
		var i = 0;				
		while ( i < me._itemsPerPage && me._currentIndex < me._commentsArray.length ) {			
			var tmpComment = this._commentsArray[me._currentIndex];
			$('#layersOS_comments_list').append(me._referal._templates.getCommentItemHTML(tmpComment));
			me._currentIndex = me._currentIndex + 1;
			i = i + 1;				
		}		
	}
	
	else if (direction == 'previous'){
		var i = 1;
		me._currentIndex = me._currentIndex - me._itemsPerPage - 1;
		
		while ( i <= me._itemsPerPage) {
			var tmpComment = this._commentsArray[me._currentIndex];
			$('#layersOS_comments_list').append(me._referal._templates.getCommentItemHTML(Utils.prototype.createHyperlinks(tmpComment)));	
			i = i + 1;
			me._currentIndex = me._currentIndex + 1;
		}
				
	}
	
	$('#layersOS_comments_box').find('.layersOS_list_loading').hide();																	
	$('#layersOS_comments_list').show();
	try {
		$('#layersOS_comments_list_container').show();
	}catch(e){
		//Do nothing
	}
	
				
}

LayersOSCommentsAccordion.prototype.updateCommentsArray = function(){

	try {
		this._commentsArray = os.getCurrentLayer()._comments;
			
		var noResultsHTML = '<li class="layersOS_list_layer_empty">' + STRING_list_empty + '</li></ul>';
		
		if (this._commentsArray.length == 0) {
			$('#layersOS_comments_list').html(noResultsHTML);			
		}
		else {
			var me = this;			
			me._displayable.find('.layersOS_list_layer_icons').fadeOut();
			$('#layersOS_comments_list').slideUp('normal', function(){
				try{								
					me.loadList('next', me._displayable);	
				}catch(e){										
					//Show no results.
					console.info(e);
					$('#layersOS_comments_list').html(noResultsHTML);									
				}		
			
			});				
		}
				
		if (this._commentsArray.length > this._itemsPerPage) {					
			new LayersOSLayerPaginationButtons(this._referal, $('#layersOS_comments_list_container'), this);
		}	
		
	}catch(e) {
		console.info(e);
	}
}

LayersOSCommentsAccordion.prototype.getCurrentIndex = function(){
	return this._currentIndex;
}

LayersOSCommentsAccordion.prototype.getTotalItems = function(){
	return this._commentsArray.length;
}

LayersOSCommentsAccordion.prototype.enableCommentForm = function(){
	var me = this;
	$('#layersOS_comment_form').submit(
		function(){
				
			try{
				var comment = $('#layersOS_comment_form [name=message]').val();
				
				if(comment != "" && comment != undefined){
					me._submitedComment._text = comment;
					me._submitedComment._identity = os.getCurrentIdentity()._displayName;
					$('#layersOS_comment_form [name=message]').val("");
					me._displayable.find('#layersOS_email_submit')
										.addClass('layersOS_dock_item_disabled')
										.attr('disabled', 'true');
					os.commentLayer(comment);
				}
			}catch(e){
				console.error(e);
			}
			return false;	
		}
	);
}

LayersOSCommentsAccordion.prototype.updateLastComment = function(){	
	this._commentsArray.push(this._submitedComment);
	$('#layersOS_comments_list').append(this._referal._templates.getCommentItemHTML(this._submitedComment));
	this._displayable.find('#layersOS_email_submit')
									.removeClass('layersOS_dock_item_disabled')
									.removeAttr('disabled');	
}

