var TendmeNavigateList = {

	init: function() {
		// script for expanding and collapsing box for ratings
		var ref = this;
		
		var links = document.getElementsByClassName('expand');
		
		for(var a = 0; a < links.length; a++){
			links[a].onclick = function() { ref.expand(this); return false; }
		}
		
		// if there was a rating submitted change the text in the box and show it for 3 seconds
		var userIds = document.getElementsByClassName('userId');
		
		for (var b = 0; b < userIds.length; b++) {
			var flag = document.getElementsByClassName('ratingFlag')[0];
			
			if (flag && userIds[b].value == flag.value) {
				var lastIndex = userIds[b].id.lastIndexOf('_');
				var id = userIds[b].id.substr(lastIndex + 1);
				
				// hide rating form
				var hide = document.getElementById('form_rate_' + id);
				if (hide) {
					hide.style.display = "none";
				}
			
				// show "thanks for rating" text
				var show = document.getElementById('submitted_rate_' + id);
				if (show) {
					show.style.display = "inline";
				}
				
				// display the wohole box
				var obj = document.getElementById("boxSlide_edit_rating_" + id);
				obj.style.display = "block";
				
				// scroll to rating box if we have more then 2 boxes
				if (userIds.length > 2) {
					obj.scrollIntoView(true);
				}
				
				// after 3 seconds close it
				window.setTimeout("TendmeNavigateList.collapseRating(" + id + ")", 3000);
			}
		}
	},
	
	expand: function(link){
		var ref = this;
			
		// ref.collapseAll(); ??
		
		var newId = "boxSlide_"+link.id;
		
		var obj = document.getElementById(newId);
		
		new Effect.BlindDown(obj, {duration: 0.3});
		
		//ref.init();
		ref.initStars();
		ref.initCancel();
		ref.initSubmit();
		
		window.setTimeout("document.getElementById('" + newId + "').scrollIntoView(true)", 300);
	},
	
	setValue: function(star){
		var firstIndex = star.id.indexOf('_');
		var lastIndex = star.id.lastIndexOf('_');
		var value = star.id.substr(star.id.length-1);
		var id = star.id.substr(0, star.id.length-1);
		
		var hideid = star.id.substring(firstIndex+1, lastIndex);
		var hiddens = document.getElementsByClassName('hide_' + hideid);
	
		var hidden = hiddens[0];
		
		var values;
		var ids;
		
		var stars = document.getElementsByClassName('stars');
		
		for(var a = 0; a < stars.length; a++){
			stars[a].src="pics_content/stern_leer.gif";
		}
		
		for(var b = 0; b < stars.length; b++){
			//stars[b].src="pics_content/stern_voll.gif";
			values = stars[b].id.substr(star.id.length-1);
			ids = stars[b].id.substr(0, star.id.length-1);
			
			if (ids = id) {
				if (values <= value) {
					stars[b].src="pics_content/stern_voll.gif";
				}
			}
		}
		
		hidden.value=value;
	},
	
	initStars: function() {
		var ref = this;
		var stars = document.getElementsByClassName('stars');
		
		for(var a = 0; a < stars.length; a++){
			stars[a].onclick = function() {ref.setValue(this);};
		}
	},
	
	initCancel: function() {
		// rateSubmitab
		var ref = this;
		var id;
		var index;
		
		var cancel = document.getElementsByClassName('rateSubmitab');
		for(var b = 0; b < cancel.length; b++){
			index = cancel[b].id.lastIndexOf('_');
			id = cancel[b].id.substr(index + 1);
			cancel[b].onclick = function() {ref.collapseRating(id);};
		}
	},
	
	initSubmit: function() {
		var ref = this;
		
		// TODO send form
		
		var submit = document.getElementsByClassName('rateSubmit');
		for(var b = 0; b < submit.length; b++){
			submit[b].onclick = function() {ref.submitForm(this);};
		}
	},
	
	collapseRating: function(id) {
			
		// rate_box
		var box = document.getElementsByClassName('rate_box');
		for(var b = 0; b < box.length; b++){
			index = box[b].id.lastIndexOf('_');
			var boxid = box[b].id.substr(index + 1);
			
			if(boxid = id) {
				new Effect.BlindUp(box[b], {duration: 0.3});
			}
		}
		
		// reset text
		var hide = document.getElementById('notClicked_' + id);
		if (hide) {
			hide.style.display = "none";
		}
			
		var show = document.getElementById('clickStars_' + id);
		if (show) {
			show.style.display = "inline";
		}
	},
	
	submitForm: function(submit) {
		var index = submit.id.lastIndexOf('_');
		var id = submit.id.substr(index + 1);
		var ref = this;
		
		/*new Ajax.Request(document.getElementById('form_rate_box_' + id).action,
							{parameters: $('form_rate_box_' + id).serialize(true)});*/
							
		// , $('bean.description').serialize(true), $('bean.userIdOfProvider').serialize(true)
		
		
		// if the user don't have choosed a value we display only another text
		if (document.getElementById('rating.ratingValue_' + id).value == '') {
			var hide = document.getElementById('clickStars_' + id);
			if (hide) {
				hide.style.display = "none";
			}
			
			var show = document.getElementById('notClicked_' + id);
			if (show) {
				show.style.display = "inline";
			}
		}
		else {
			ref.collapseRating(id);
		
			//document.getElementById('form_rate_box_' + id).submit();
		}
	}
}

Tendme.addPageLoadModule(TendmeNavigateList);