/*
Copyright (c) 2008, Security2day
Список карточек пользователя.
*/

if ( typeof YAHOO.security2day == "undefined" ) {
	YAHOO.namespace ("security2day");
}

if ( typeof YAHOO.security2day.Cards == "undefined" ) {
	
	/*
	Record = {
		Label: "",
		Data: "",
		Type: text|password|webaddress|address
	}
	Card = {
		Records: [],
		Name: "",
		Note: "",
		ID: "",
		Catalog: ""
	}
	*/
	YAHOO.security2day.Cards = function() {};
	
	YAHOO.security2day.Cards.prototype.list = [];
	YAHOO.security2day.Cards.prototype.tempCard = "undefined";
	YAHOO.security2day.Cards.prototype.editCardIndex = -1;	// >0 - значит редактируется карточка с данным индексом
	
	YAHOO.security2day.Cards.prototype.init = function( ) {
		this.list 			= [];
		this.tempCard 		= "undefined";
		this.editCardIndex 	= -1;
	};	
	
	YAHOO.security2day.Cards.prototype.loadCards = function( cards, sort_by ) {
		this.list = cards;
	};
	
	// сортировка карточек
	// варианты: name, date
	YAHOO.security2day.Cards.prototype.sortCards = function( sort_by ) {
	
		var buf_card;
		var exchange; // поменять карточки местами
		
		// пузырёк
		for (i=0; i<this.list.length-1; i++) {
		
			for (j=0; j<this.list.length-1-i; j++) {
			
				exchange = false;				
				switch (sort_by) {
					case "name":
							if (this.list[j].Name.toLowerCase() > this.list[j+1].Name.toLowerCase()) {
								exchange = true;
							}
							break;
					case "date": 
							if (this.list[j].ID > this.list[j+1].ID ) {
								exchange = true;
							}						
							break;						
//					default: 
				}			
		
				if (exchange)
				{
					buf_card 		= this.list[j+1];
					this.list[j+1] 	= this.list[j];
					this.list[j] 	= buf_card;
				}
			}
		}
		
		if ( sort_by == "date" )
		{
			for (i=0; i<this.list.length-1; i++) {
				if (this.list[i].ID > this.list[i+1].ID)
				{
					buf_card 		= this.list[i+1];
					this.list[i+1] 	= this.list[i];
					this.list[i] 	= buf_card;
				}
			}
			return;
		}
				
	};	
	
	YAHOO.security2day.Cards.prototype.newCard = function( catalog_id ) {
		
		// в каждый момент можно редактировать только 1 карточку
		if (this.editCardIndex!=-1) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Добавлена карточка при редактировании другой", "error", "YAHOO.security2day.Cards");
			}
			return;
		}		
		
		if (YAHOO.security2day.debugging) {
			YAHOO.log("Добавлена карточка", "info", "YAHOO.security2day.list");
		}
		this.list.push( this.getNewCard(catalog_id) );
		this.editCardIndex 	= this.list.length-1;
		this.tempCard		= "undefined";
	}
	
	YAHOO.security2day.Cards.prototype.editCard = function( cardIndex ) {
		
		if (this.editCardIndex!=-1) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Попытка редактирования карточки параллельно с другой", "error", "YAHOO.security2day.Cards");
			}
			return;
		}
		if (cardIndex > this.list.length) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Редактирование карточки с недопустимым индексом: "+cardIndex, "error", "YAHOO.security2day.Cards");
			}
			return;
		}		
		var editCard = this.list[cardIndex];
		
		this.tempCard = this.getNewCard();
		this.tempCard.Name 	= editCard.Name;
		this.tempCard.Note 	= editCard.Note;
		this.tempCard.ID 	= editCard.ID;
		this.tempCard.Catalog 	= editCard.Catalog;
		for( i=0; i<editCard.Records.length; i++ ) {
			this.tempCard.Records[i] = this.getNewRecord();
			
			this.tempCard.Records[i].Label = editCard.Records[i].Label;
			this.tempCard.Records[i].Data = editCard.Records[i].Data;
			this.tempCard.Records[i].Type = editCard.Records[i].Type;
		}
		
		this.editCardIndex = cardIndex;		
	}	
	
	YAHOO.security2day.Cards.prototype.newCardRecord = function() {	
	
		// подразумевается, что добавление записей происходит только при редактировании карточки
		if (this.editCardIndex<0) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Добавление записи в нередактируемую карточку", "error", "YAHOO.security2day.Cards");
			}
			return;
		}
		
		this.list[this.editCardIndex].Records.push( this.getNewRecord() );
		
		if (YAHOO.security2day.debugging) {
			YAHOO.log("Добавлена запись в карточку: " + this.editCardIndex, "info", "YAHOO.security2day.Cards");
		}		
	}		
		
	YAHOO.security2day.Cards.prototype.applyEditCard = function() {
		this.tempCard		= "undefined";
		this.editCardIndex	= -1;
	}
	
	YAHOO.security2day.Cards.prototype.cancelEditCard = function() {
		
		if (this.editCardIndex<0) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Отмена изменений карточки в тот момент, когда ни одна не редактируется", "error", "YAHOO.security2day.Cards");
			}
			return;
		}
		
		if ( this.tempCard == "undefined" ) {
			// редактируемая карточка была новой, удаляем её
			delete this.list[ this.editCardIndex ];
			this.list.length--;
			this.editCardIndex	= -1;
		} else {
			// редактировалась имеющаяся карточка
			this.list[ this.editCardIndex ] = this.tempCard;
			this.tempCard		= "undefined";
			this.editCardIndex	= -1;
		}

	}	
	
	YAHOO.security2day.Cards.prototype.removeCard = function( cardIndex ) {
	
		if (cardIndex<0 || cardIndex > this.list.length) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Удаление карточки с недопустимым индексом: "+cardIndex, "error", "YAHOO.security2day.Cards");
			}
			return;
		}		
		this.list.splice(cardIndex,1);				
	}	
	
	YAHOO.security2day.Cards.prototype.removeEditCardRecord = function( recordIndex ) {	
	
		// подразумевается, что удаление записей происходит только при редактировании карточки
		if (this.editCardIndex<0) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Удаление записи из нередактируемой карточки", "error", "YAHOO.security2day.Cards");
			}
			return;
		}
		if (recordIndex<0) {
			if (YAHOO.security2day.debugging) {
				YAHOO.log("Удаление несуществующей записи: " + recordIndex, "error", "YAHOO.security2day.Cards");
			}
			return;
		}		
		
		//delete this.list[this.editCardIndex].Records[recordIndex];
		//this.list[this.editCardIndex].Records.length--;
		this.list[this.editCardIndex].Records.splice(recordIndex,1);
	}
	
	YAHOO.security2day.Cards.prototype.getEditCardIndex = function() {	
		return this.editCardIndex;
	}
	
	YAHOO.security2day.Cards.prototype.getEditCardRecordsCount = function() {	
		return this.list[this.editCardIndex].Records.length;
	}	
	
	YAHOO.security2day.Cards.prototype.getNewCard = function(catalog_id){
		var newCard = {
			Records: [],
			Name: "New Card",
			Note: "",	
			ID: "",
			Catalog: catalog_id
		};
		return newCard;
	}
	
	YAHOO.security2day.Cards.prototype.getNewRecord = function() {
		var newRecord = {			
			Label: "",
			Data: "",
			Type: RECORD_TYPE_TEXT	
		};
		return newRecord;
	}
	
	YAHOO.security2day.Cards.prototype.addNewCard = function(){
		this.list.push( this.getNewCard() );
	}
	
}
