var filter_live;
var Filter_live;
Filter_live = new Class({
	live_cd: null,
	initialize: function() {
		var _this = this;
		// Get all concerned links for Filter 
		var elements = $(document.body).getElements('*[rel=video_list_live_filter]').combine($(document.body).getElements('*[rel=pagination]'));
		
		elements.each(function(element) {
			element.addEvent('click', function(e) {
				e.stop();
				//	Start the process
				_this.element_click(element);
			});
		});
	},


	element_click: function(element){
		_this = this;
		_this.live_cd.clear_delayed('all');

//		$('clipsloader').setStyle('display', 'inline');
		query = new Request({
			url: element.get('href'), 

			onSuccess: function(xml){
//			$('clipsloader').setStyle('display', 'none');

				_this.update_content(query.response['xml'], element.get('href'));
				//	Reload the count down
				_this.live_cd.cd_initialize();
			},
			
			onFailure: function() {
//				$('clipsloader').setStyle('display', 'none');
				return false;
			}
		}).send();
	},
	

	update_content: function(xml, url){
		_this = this;
		var clip_target = $('clip_list_ul');
		var filter_target = $('clipsfilter');
		var pagination_top = $('tab_clip_list').getElement('.pagination_top');
		var pagination_bottom = $('tab_clip_list').getElement('.pagination_bottom');
		var error_target = $('left_col');
				
/*
 *	The data bellow can be added in the xml in website controleur display_clip_list methode
 *	That methode need to be moved in a model ASAP
 *
 *		var eID = xml.getElementsByTagName('eID');
 *		var counter = xml.getElementsByTagName('counter');
 *		var data = xml.getElementsByTagName('data');
 */

		//	Error check
		var error = xml.getElementsByTagName('error');
		if(error[0].childNodes[0].nodeValue == 1){
			var errorCtn = xml.getElementsByTagName('errorCtn');
			error_target.set('html', errorCtn[0].childNodes[0].nodeValue);
		}
		else{
			//	Update content
			var clips = xml.getElementsByTagName('clip');
			clip_target.empty();
			for(i=0; i<clips.length;i++){
				var clip = clips[i].childNodes[0].nodeValue;
			
				var newLi = new Element('li', {'class': 'clip_list_element channel_brd_bt_color'});	//	Creat new clip item
				newLi.innerHTML = clip;											//	Add data in clip item
				newLi.inject(clip_target);										//	Add clip item in the page
			}
			
			//	Update filters
			filter_target.empty();
			var filter_value = _this.getNodeValueFromName(xml, 'filter');
			filter_target.innerHTML = filter_value;
	
	
			//	Update pagination
			var pagination_value = _this.getNodeValueFromName(xml, 'pagination');
			pagination_top.empty();
			pagination_top.innerHTML = pagination_value;
	
			pagination_bottom.empty();
			pagination_bottom.innerHTML = pagination_value;
			
			
			//	Reaload script on item
			_this.initialize();
			
			//	Update google
			_this.googleUpdate(url);
		}		
		
		//	Update displayed url
//		window.location.replace(url);

		
	},

	getNodeValueFromName: function(node, name) {
		var value = "";
		
		try {
			value = node.getElementsByTagName(name)[0].childNodes[0].nodeValue;
		} catch(e) {}
		
		return value;
	},
	
	googleUpdate: function(url){
		if(pageTracker) {
			pageTracker._trackPageview(url);
		}
	},

	changeAnchor: function(anchor){
		// Si l'url possde dŽjˆ une ancre
		if (window.location.href.match(new RegExp('#.*$'))){
			window.location = window.location.href.replace(
				new RegExp('#.*$'),
				'#' + anchor
			);
		}
		// Sinon, il suffit de l'ajouter ˆ la fin
		else{
			window.location = window.location.href + "#" + anchor
		}
		
		return window.location;
	},
	
	addRef: function(ref_obj){
		_this = this;
		_this.live_cd = ref_obj;
	},
	
	end: function(){}
});