var lsCplTreeClass = new Class({
					   
	Implements: Options,	
	
	options: {
		img: {
			path: 		'images/',			
			normal:  	'ui-check-box-check.png', 
			deleted: 	'ui-check-box-uncheck.png'
		},
		classes: {
			normal: 	'checked',
			deleted:  	'unchecked'		
		}		
	},
	url_add: DIR_WEB_ROOT+'/include/ajax/coupleAdd.php',			
	url_response: DIR_WEB_ROOT+'/include/ajax/coupleResponse.php'		
		
	,
	
	initialize: function(options){		
		this.setOptions(options);		
		this.make();		
		this.aCoupleNew=[];
		this.iCurrentShowFormCouple=0;	
		this.iCoupleIdLastView=null;	
		this.countNewCouple=0;
		this.docScroller = new Fx.Scroll(document.getDocument());	
		//this.hideCoupleForm(this.iCurrentShowFormCouple);
	},

	make: function(){
		var thisObj = this;
		var aImgFolding=$$('img.folding');
		aImgFolding.each(function(img, i){
			var divCouple = img.getParent('div').getChildren('div.couple-children')[0];			
			if (divCouple && divCouple.getChildren('div.couple')[0]) {
				thisObj.makeImg(img);
			} else {
				img.setStyle('display','none');
			}
		});		
	},
	
	makeImg: function(img) {
		var thisObj = this;
		img.setStyle('cursor', 'pointer');
		img.setStyle('display','inline');
		img.addClass(this.options.classes.closeImg);
		img.removeEvents('click');
		img.addEvent('click',function(){
			thisObj.toggleNode(img);		
		});
	},
	
	/*
	toggleNode: function(img) {	
		var b = img.hasClass(this.options.classes.closeImg);		
		if (b) {				
			this.collapseNode(img);
		} else {					
			this.expandNode(img);
		}
	},
	
	expandNode: function(img) {				
		var thisObj = this;
		img.setProperties({'src': this.options.img.path + this.options.img.closeName});
		img.removeClass(this.options.classes.openImg);
		img.addClass(this.options.classes.closeImg);		  
		var divCouple = img.getParent('div').getChildren('div.couple-children')[0];		
		
		divCouple.removeClass(thisObj.options.classes.hidden);
		divCouple.addClass(thisObj.options.classes.visible);		
	},
	collapseNode: function(img) {
		var thisObj = this;
		img.setProperties({'src': this.options.img.path + this.options.img.openName});
		img.removeClass(this.options.classes.closeImg);
		img.addClass(this.options.classes.openImg);		    
		var divCouple = img.getParent('div').getChildren('div.couple-children')[0];		
		
		divCouple.removeClass(thisObj.options.classes.visible);
		divCouple.addClass(thisObj.options.classes.hidden);		
	},
	
	expandNodeAll: function() {
		var thisObj = this;
		var aImgFolding=$$('img.'+this.options.classes.openImg);		
		aImgFolding.each(function(img, i){			
			thisObj.expandNode(img);
		});
	},
	
	collapseNodeAll: function() {
		var thisObj = this;
		var aImgFolding=$$('img.'+this.options.classes.closeImg);		
		aImgFolding.each(function(img, i){			
			thisObj.collapseNode(img);
		});
	},*/
	
	setIdCoupleLast: function(id) {
		this.idCoupleLast=id;
	},
	
	addCouple: function(formObj,male_number, female_number, user_mail, groups) {
		var thisObj=this;
		formObj=$(formObj);		
		var tempparams = formObj
		for(var i=0; i<groups.length; i++){
			//window.alert("group_"+groups[i]);
			if($("group_"+groups[i]).checked) { // если группа отмечена, отправляем запрос
				tempparams['group_id'] = groups[i];
				JsHttpRequest.query(        	
					thisObj.url_add,
					{'male_number': male_number ,
					 'female_number': female_number ,
					 'user_mail': user_mail ,
					 'group_id': groups[i] },
					function(result, errors) {
						if (!result) {
							//thisObj.enableFormCouple();
							msgErrorBox.alert('Ошибка','Что-то пошло не так');  
							return;         
						}      
						if (result.bStateError) {        			
							//thisObj.enableFormCouple();        			
							msgErrorBox.alert(result.sMsgTitle,result.sMsg);
						} else {
							msgNoticeBox.alert("Регистрация", "Пара зарегистрирована");
							//thisObj.responseNewCouple(targetId,targetType,$('update-couples'),result.sCoupleId,true);        			   								
						}                           
					},
					true
				);
			}
		}
		/*	

      	$('form_couple_text').addClass('loader');		
      	$('form_couple_text').setProperty('readonly',true);		*/
	},
	
	enableFormCouple: function() {
		//$('form_couple_text').removeClass('loader');
		//$('form_couple_text').setProperty('readonly',false);
	},
	
	addCoupleScroll: function(coupleId) {
		this.aCoupleNew.extend([coupleId]);
		this.setCountNewCouple(this.countNewCouple+1);
	},
	
	toggleCouple: function(coupleId) {
		var aCheckBox=$('a_'+coupleId);
		var trContent=$('row_'+coupleId);
		if (!aCheckBox) {
			return false;
		}
		
		var thisObj=this;			
		JsHttpRequest.query(
        	DIR_WEB_ROOT+'/include/ajax/coupleToggle.php',
        	{ idCouple: coupleId },
        	function(result, errors) {         		 
            	if (!result) {
                	msgErrorBox.alert('Error','Please try again later');           
        		}      
        		if (result.bStateError) {        			
                	msgErrorBox.alert(result.sMsgTitle,result.sMsg);
        		} else {   
        			msgNoticeBox.alert(result.sMsgTitle,result.sMsg);     			
        			aCheckBox.removeClass('checked').removeClass('unchecked');
        			if (result.bState) {
        				aCheckBox.addClass('unchecked');
        				trContent.addClass("del");
        			} else {
        				aCheckBox.addClass('checked');
        				trContent.removeClass("del");
        			}       			        								
        		}                           
	        },
        	true
       );
       return true;
	},
	
	toggleCoupleForm: function(idCouple) {
		if (!$('reply_'+this.iCurrentShowFormCouple) || !$('reply_'+idCouple)) {
			return;
		} 
		divCurrentForm=$('reply_'+this.iCurrentShowFormCouple);
		divNextForm=$('reply_'+idCouple);
				
		var slideCurrentForm = new Fx.Slide(divCurrentForm);
		var slideNextForm = new Fx.Slide(divNextForm);
		
		$('couple_preview_'+this.iCurrentShowFormCouple).set('html','').setStyle('display','none');
		if (this.iCurrentShowFormCouple==idCouple) {
			slideCurrentForm.toggle();
			//$('form_couple_text').focus();
			return;
		}
		
		slideCurrentForm.slideOut();
		divNextForm.set('html',divCurrentForm.get('html'));
		divCurrentForm.set('html','');		
		divNextForm.setStyle('display','block');
		slideNextForm.hide();
		
		slideNextForm.slideIn();
				
		//$('form_couple_text').focus();
		$('form_couple_text').setProperty('value','');
		$('form_couple_reply').setProperty('value',idCouple);
		this.iCurrentShowFormCouple=idCouple;
	},
	
	hideCoupleForm: function(idCouple) {
		if ($('reply_'+idCouple)) {
			//this.enableFormCouple();
			$('couple_preview_'+this.iCurrentShowFormCouple).set('html','').setStyle('display','none');
			var slideForm = new Fx.Slide('reply_'+idCouple);							
			slideForm.hide();
		}
	},
	
	preview: function() {
		ajaxTextPreview('form_couple_text',false,'couple_preview_'+this.iCurrentShowFormCouple);		
	},
	
	goToParentCouple: function(obj) {
		var idCpl = obj.href.substr(obj.href.indexOf('#')+8);
		var objCplParent=$('couple_id_'+idCpl);
		var objCpl=obj.getParent('div.couple');
		objCplParent.getElement('.goto-couple-child').removeClass('hidden');
		objCplParent.getElement('.goto-couple-child a').href = '#couple' + objCpl.id.substr(11);
		this.docScroller.setOptions({ 			
			offset: {'y': 0}
 		});
		this.docScroller.toElement(objCplParent);
		return false;
	},
	
	goToChildCouple: function(obj) {
		var idCpl = obj.href.substr(obj.href.indexOf('#')+8);
		var objCplChild=$('couple_id_'+idCpl);
		var objCpl=obj.getParent('div.couple');
		objCpl.getElement('.goto-couple-child').addClass('hidden');
		this.docScroller.setOptions({ 			
			offset: {'y': 0}
 		});
		this.docScroller.toElement(objCplChild);
		return false;
	}
});


var lsCplTree;
var formCoupleSlide;

window.addEvent('domready', function() {  	
    lsCplTree = new lsCplTreeClass({
    	img: {
    		path: DIR_STATIC_SKIN+'/images/'
    	},
		classes: {
			normal: 	'checked',
			deleted:  	'unchecked'		
		}	
    });
});

