/*
 * lucy
 * v1110 List.js
 */



var List = new Class({

	Extends : Filters,
	
	// vars

	/////

	// ** contructor **  //////////////////////////////

	initialize : function(options){
		
		this.parent(options);
		
		this.initList();
		
		if($('add-rows')){
			$('add-rows').addEvent('click',(function(event){
				//this.deteleFile(event.target.getProperty('id'));
				var url = baseUrl + '/ads/list-get-next';
				return this.sendRequest(url, 'post', this.getNextResponse.bind(this));
				
			}).bind(this));
		}	
		
	
		
		this.initOrderTh();
		
	},

	//* end contructor **  //////////////////////////////
	
	// ** getNextResponse ** //////////////////////////////
	getNextResponse : function (response){
		//alert(response); 
		//byo.overlay.hideLoading(); 
		//return false;
		
		response= this.decodeAjaxResponse(response);
		
		if(response.success){
			
			this.appendAd(response.items,true);
			$$('span#viewing').set('text', response.session['viewing'])
			if((response.session['viewing'] >= response.session['totalRows']) || response.countItems == 0) $('add-rows').setStyle('display','none');
			
		}else{
			alert('error');
		}
		
		byo.overlay.hideLoading(); 
		return;
		
	},
	//* end getNextResponse **  //////////////////////////////
	
	
	// ** initOrderTh /////////////////////////////////////////
	
	initOrderTh : function (){
		
		$$('.list thead th').addEvent('click',(function(event){
			var el = null;
			if(event.target.getProperty('id')  == null ){
				el = event.target.getParent('th');
			}else{
				el = event.target;
			}
			
			var classes = (el.getProperty('class')).split(' ');
			
			
			
			if(classes[1] == 'activeorder'){
				
				if(classes[2] == 'desc'){ 
					el.removeClass('desc'); 
					el.addClass('asc'); 
					this._orderType = 'ASC';
				}
				
				if(classes[2] == 'asc'){ 
					el.removeClass('asc'); 
					el.addClass('desc'); 
					this._orderType = 'DESC';
				}
				//if(classes[2] == 'asc') el.removeClass('asc'); el.addClass('desc'); var orderType = 'DESC';
				
				//alert(this._orderType + ' ' + classes[2]);
				
			}else{
				
				$$('.list thead th').removeClass('activeorder');
				$$('.list thead th').removeClass('desc');
				$$('.list thead th').removeClass('asc');
				
				el.addClass('activeorder desc');
				this._orderType = 'DESC';
			}
			
			var orderColumn = el.getProperty('id');
			
			var url = baseUrl + '/ads/order-list';
			return this.sendRequest(url,'post', this.getOrderResponse.bind(this),'oCl=' + orderColumn + '&oTp=' + this._orderType);
			
			
		}).bind(this));
	},
	
	//* end initOrderTh **  //////////////////////////////
	
	// ** getOrderResponse /////////////////////////////////////////
	
	getOrderResponse : function (response){
		//alert(response); 
		//byo.overlay.hideLoading(); 
		//return false;
		
		$$('table.list tbody tr').dispose();
		
		response= this.decodeAjaxResponse(response);
		
		if(response.success){
			
			$$('span#viewing').set('text', response.session['viewing']);
			
			if($('add-rows')){
				if((response.session['viewing'] >= response.session['totalRows']) || response.countItems == 0) {
					$('add-rows').setStyle('display','none');
				}else if(response.session['viewing'] < response.session['totalRows']){
					$('add-rows').setStyle('display','block');
				}
			}
			
			this.appendAd(response.items,false);
			
		}else{
			alert('error');
		}
		
		byo.overlay.hideLoading(); 
		return;
		
	}
	//* end getOrderResponse **  //////////////////////////////
	
});
