hhtUserratingCtrlClass = new Class ({
	
	init:function(){
		/*		
		$$('.hht_userrating_show_rating_form').addEvent('click', function(e)	{
			var e = new Event(e);
			var t = new Element (e.target);
			e.stop();
			hhtUserrating.getRatingForm(t);
		});

		$$('.hht_userrating_show_commenting_form').addEvent('click', function(e)	{
			var e = new Event(e);
			var t = new Element (e.target);
			e.stop();
			hhtUserrating.getCommentingForm(t);
		});

		$$('.hht_userrating_show_all_comments').addEvent('click', function(e)	{
			var e = new Event(e);
			var t = new Element (e.target);
			e.stop();
			hhtUserrating.getMoreComments(t);
		});
		*/
		
	},
	
	/*
	 * record rating
	 */
	
	getRatingForm:function(target){
		target = new Element (target);
		var params = {
			table : target.getProperty('hht:rating_table'),
			uid   : target.getProperty('hht:rating_uid'),
			lang  : target.getProperty('hht:rating_lang'),
			LL    : target.getProperty('hht:ll')
		};

		if (params.table && params.uid){
			hhtUserrating.callJson ('getRatingForm', params, hhtUserrating.showRatingForm );
		}
	},
	
	showRatingForm:function(result){
		$('lbAjaxContainer').innerHTML = result;
		if (thLightbox.options.closed){
			thLightbox.openLightBox('searchResults', null, '', false);
		}
		thLightbox.changeContent('lbAjaxContainer');

			// add form submit event
		$ES("#lbCenter .lightbox-content form.hht_userrating_form").each(function(el){
			el.addEvent('submit', hhtUserrating.submitRating );
		});
		
	},
	
	submitRating:function(e){
		var e = new Event(e);
		var t = new Element(e.target);
		e.stop();

			//find form values
		var params = { 	
			table : $E('input[name=table]', t).getValue(),
			lang  : $E('input[name=lang]',  t).getValue(),
			uid   : $E('input[name=uid]',   t).getValue(),
			LL    : $E('input[name=ll]',   t).getValue(),
			points: false
		};
		
		$ES('input[name=points]', t).each(function(el){
			if (el.checked == true) {
				params.points = el.value;
			} 
		}); 
		
		if (params.points !== false && params.table && params.uid){
			hhtUserrating.callJson ('saveRatingForm', params , hhtUserrating.showSubmitRatingResult );
		}		
	},
	
	showSubmitRatingResult:function(result){
		if (result.error){
			$('lbAjaxContainer').innerHTML = result.error;
			if (thLightbox.options.closed){
				thLightbox.openLightBox('searchResults', null, '', false);
			}
			thLightbox.changeContent('lbAjaxContainer');
			thLightbox.close.delay(500);
		}
		
		if (result.message){
			$('lbAjaxContainer').innerHTML = result.message;
			thLightbox.changeContent('lbAjaxContainer');
			
			// thLightbox.close.delay(500);
			hhtUserrating.refreshPage.delay(500);
		}
	},
	
	refreshPage:function(){
		var old_url = document.URL;
		if ( old_url.indexOf('&no_cache=1') > 0 ){
			window.location.reload();
		} else {
			if ( old_url.indexOf('?') > 0 ){
				window.location.href = old_url+"&no_cache=1";
			} else {
				window.location.href = old_url+"?no_cache=1";
			}
		}
	},
	
	/*
	 * record commenting
	 */
	
	getCommentingForm:function(target){
		target = new Element (target);
		var params = {
			table : target.getProperty('hht:commenting_table'),
			uid   : target.getProperty('hht:commenting_uid'),
			lang  : target.getProperty('hht:commenting_lang'),
			LL    : target.getProperty('hht:ll')
		};
		
		if (params.table && params.uid){
			hhtUserrating.callJson ('getCommentingForm',params, hhtUserrating.showCommentingForm );
		}
	},
	
	showCommentingForm:function(result){
		$('lbAjaxContainer').innerHTML = result;
		if (thLightbox.options.closed){
			thLightbox.openLightBox('searchResults', null, '', false);
		}
		thLightbox.changeContent('lbAjaxContainer');
			// add form submit event
		$ES("#lbCenter .lightbox-content form.hht_usercommenting_form").each(function(el){
			el.addEvent('submit', hhtUserrating.submitComment );
		});
	},
	
	submitComment:function(e){
		
		var e = new Event(e);
		var t = new Element(e.target);
		e.stop();
		
			// find form values
		
		var params = {
			table   : $E('input[name=table]', t).getValue(),
			lang    : $E('input[name=lang]',  t).getValue(),
			uid     : $E('input[name=uid]',   t).getValue(),
			comment : $E('textarea[name=comment]',t).getValue(),
			name    : $E('input[name=name]',  t).getValue(),
			email   : $E('input[name=email]', t).getValue(),
			url     : window.location.href,
			LL      : $E('input[name=ll]',    t).getValue()
		
		};
			//  
		hhtUserrating.callJson ('submitComment',params, hhtUserrating.showSubmitCommentResult );

	},
	
	showSubmitCommentResult:function(result){
		if (result.error){
			$('lbAjaxContainer').innerHTML = result.error;
			thLightbox.changeContent('lbAjaxContainer');
			thLightbox.close.delay(3000);
		}
		
		if (result.message){
			$('lbAjaxContainer').innerHTML = result.message;
			thLightbox.changeContent('lbAjaxContainer');
			thLightbox.close.delay(3000);
		}
		
	},
	
	getMoreComments:function(target){
		target = new Element (target);
		var params = { 
			table : target.getProperty('hht:commenting_table'),
			uid   : target.getProperty('hht:commenting_uid'),
			lang  : target.getProperty('hht:commenting_lang'),
			LL    : target.getProperty('hht:ll'),
			page  : 0		
		};
		
		hhtUserrating.callJson ('getMoreComments',params, hhtUserrating.showMoreComments );
	},
	
	showMoreComments:function(result){
		$('lbAjaxContainer').innerHTML = result.body;
		if (thLightbox.options.closed){
			thLightbox.openLightBox('searchResults', null, '', false);
		}
		thLightbox.changeContent('lbAjaxContainer', null , result.header);
		
			// add pagination events
		$ES("#lbCenter a.hht_usercommenting_pages").each(function(el){
			el.addEvent('click', hhtUserrating.getMoreCommentsPage );
		});
	},
	
	getMoreCommentsPage:function(e){
		var e = new Event(e);
		var t = new Element(e.target);
		e.stop();
		var params = { 
			table : t.getProperty('hht:commenting_table'),
			uid   : t.getProperty('hht:commenting_uid'),
			lang  : t.getProperty('hht:commenting_lang'),	
			page  : t.getProperty('hht:commenting_page'),
			LL    : t.getProperty('hht:ll')
		};
		hhtUserrating.callJson ('getMoreComments',params, hhtUserrating.showMoreComments );
	},
	
	
	/*
	 * helper funtions 
	 */
	
	
	callJson:function(method, param, callback){
		jsonServer.dispatchMethod(
			'hht_userrating',
			'tx_hhtuserrating_json::' + method,
			param,
			callback
		);	
	}
});

window.addEvent('domready', function()	{
	hhtUserrating = new hhtUserratingCtrlClass();
	hhtUserrating.init();
});