var evaluationAjax = '/fileadmin/templates/scripts/php/evaluations.ajax.php';

var evaluations = {

	evals: new Array(),

	bodySize: new Array(),

	registerEval: function(id, stars){
		
		tmp = $(id);
		$(id).setAttribute('id', 'backup_'+id );
		tmp.setAttribute('id', id );
		document.getElementsByTagName('body')[0].appendChild(tmp);
		this.evals[id] = {
			id: id,
			stars: stars,
			comments:''
		};
		return false;
	},

	openEval: function(id, hideLayer){

		if($('evaluationTextswitchRules')){
	  	$('evaluationTextswitchRules').hide();
	  }
		if(this.evals[id].alreadyEvaluated){
			errorText = 'Es kann nur einmal je Element bewertet werden! ' + '<div class="buttonDefault" style=""><a href="javascript:void(0)" onclick="evaluations.reset(\''+id+'\',\''+hideLayer+'\'); return false;">OK</a></div>';
			$(id+'_errorEvaluations').update(errorText).show();
		} else {
			this.hideOthers();
		}
		evaluations.loadComments(id, 0, 50);
		this.showOverlay(hideLayer);
		bodySize = this.bodySize;
		this.setCentered(id, bodySize);
		$(id).show();
		
		return false;
	},

	loadComments:function(id, start, count){
// 		for(i=start;i<(start+count);i=i+1){
		//$(id+'_comments').update('');
		this.evals[id].commentStart = start;
		this.evals[id].commentCount = count;
		$(id+'_comments').innerHTML =  'saved'+this.evals[id].comments;
			ajax = new Ajax.Updater($(id+'_comments'), evaluationAjax+'?'+Math.random(), {
				parameters:{
					start:start,
					count:count,
					view:'comments',
					values:id
				},
				method:'post',
				onComplete:function(transport){
				  evaluations.evals[id].comments = $(id+'_comments').innerHTML;
				},
				evalScripts:true
			});
// 		}
	},

	setStars: function(star, id){
		for(i=1; i<= this.evals[id].stars; i++){
			if(star >= i){
				$(id+'_star_'+i).className = 'evalstar-on';
			} else {
				$(id+'_star_'+i).className = 'evalstar-off';
			}
			
			if(star == i){
				$('star_'+i+'_msg'+id).className = 'evalmsg-on';
			} else {
				$('star_'+i+'_msg'+id).className = 'evalmsg-off';
			}
		}
		this.evals[id].starSet = star;
		$(id+'_star_'+star).blur();
		
		if($('internetstar_stars')){
			$('internetstar_stars').className = 'internetstar-on';
		}
		
		return false;
	},
	
	setInternetstar: function(source_id, star_id){
		if($('internetstar'+star_id)){
			if($F(source_id+star_id) != ''){
				if(star_id == '_comment' || star_id == '_shop_argument'){
					$('internetstar'+star_id).className = 'internetstar-on';
					$('internetstar'+star_id+'2').className = 'internetstar-on';
					$('internetstar'+star_id+'3').className = 'internetstar-on';
				}else if(star_id == '_conclusion'){
					$('internetstar'+star_id).className = 'internetstar-on';
					$('internetstar'+star_id+'2').className = 'internetstar-on';
				}else{
					$('internetstar'+star_id).className = 'internetstar-on';
				}
			}else{
				if(star_id == '_comment' || star_id == '_shop_argument'){
					$('internetstar'+star_id).className = 'internetstar-off';
					$('internetstar'+star_id+'2').className = 'internetstar-off';
					$('internetstar'+star_id+'3').className = 'internetstar-off';
				}else if(star_id == '_conclusion'){
					$('internetstar'+star_id).className = 'internetstar-off';
					$('internetstar'+star_id+'2').className = 'internetstar-off';
				}else{
					$('internetstar'+star_id).className = 'internetstar-off';
				}
			}
		}
		return false;
	},
	
	saveEval: function(id, hideLayer){
		
		eval_id = id;
		starSet = this.evals[id].starSet;
		comment = $F(id+'_comment');
		username = $F(id+'_username');
		title = ($(id+'_title') ? $F(id+'_title') : '');
		pro = ($(id+'_pro') ? $F(id+'_pro') : '');
		contra = ($(id+'_contra') ? $F(id+'_contra') : '');
		conclusion = ($(id+'_conclusion') ? $F(id+'_conclusion') : '');
		cost_performance = ($(id+'_cost_performance') ? $F(id+'_cost_performance') : '');
		assortment = ($(id+'_assortment') ? $F(id+'_assortment') : '');
		advice = ($(id+'_advice') ? $F(id+'_advice') : '');
		service = ($(id+'_service') ? $F(id+'_service') : '');
		repair_shop = ($(id+'_repair_shop') ? $F(id+'_repair_shop') : '');
		shop_argument = ($(id+'_shop_argument') ? $F(id+'_shop_argument') : '');
		shop_service = ($(id+'_shop_service') ? $F(id+'_shop_service') : '');
		shop_delivery = ($(id+'_shop_delivery') ? $F(id+'_shop_delivery') : '');
		tags = ($(id+'_tags') ? $F(id+'_tags') : '');

		ajax = new Ajax.Request( evaluationAjax, {
			parameters: {
				eval_id: eval_id,
				stars: starSet,
				comment: comment,
				username: username,
				title: title,
				pro: pro,
				contra: contra,
				conclusion: conclusion,
				cost_performance: cost_performance,
				assortment: assortment,
				advice: advice,
				service: service,
				repair_shop: repair_shop,
				shop_argument: shop_argument,
				shop_service: shop_service,
				shop_delivery: shop_delivery,
				tags: tags
				
			},
			method: 'post',
			onComplete: function(transport) {
				if (transport.responseText.match(/Fehler/)){
				  errorText = transport.responseText;
			    errorText = '' + errorText ;
			    	  //+ ' <div class="buttonDefault" style=""><a href="javascript:void(0)" onclick="evaluations.reset(\''+id+'\'); return false;">OK</a></div>';
				  $(id+'_errorEvaluations').update(errorText);
				  $(id+'_errorEvaluations').show();
		    } else {
			    $(id+'_errorEvaluations').update(transport.responseText);

					evaluations.reset(id, hideLayer);
					evaluations.setStarsAfter(id);
					evaluations.removeForm(id);
				
		    }
			}
		});


		return false;
	},

	reset: function(id, hideLayer) {
		//alert('reset');
		$(id+'_errorEvaluations').update('').hide();

		document.getElementsByClassName('communityInput').each(function(el){
			el.hide();
		});


		$('jsOverlay').hide();
		if(hideLayer){
			$(hideLayer).show();
		}
		
		return false;
	},

	setStarsAfter: function(id){

		ajax = new Ajax.Updater( $(id+'_link').parentNode, evaluationAjax, {
			parameters: {
				eval_id : id,
				view:'single'
			},
			method: 'post',
			onComplete: function(transport) {

			}
		});


		return false;


	},
	
	switchOff:function(id){
		var layer = id.split(",");
		for(i = 0; i < (layer.length); i++){
			$(layer[i]).hide();
		}
	},
	
	switchOn:function(id){
		var layer = id.split(",");
		for(i = 0; i < (layer.length); i++){
			$(layer[i]).show();
		}
	},
	
	switchClass:function(id){
		var layer = id.split(",");
		for(i = 0; i < (layer.length); i++){
			if($(layer[i]).className == 'evaluationTextswitch-on'){
				$(layer[i]).className = 'evaluationTextswitch-off';
			}else{
				$(layer[i]).className = 'evaluationTextswitch-on';
			}
		}
	},
	
	removeForm:function(id){
		// CHRISTOPH edit here
		$(id+'_form').remove();
		// edit end
		$(id).removeChild($(id+'_form'));
		headline = document.getElementsByClassName('communityInputHeadline', $(id))[0];
		new Insertion.After(headline, '');
		return false;
	},

	hideOthers : function() {
		$('jsOverlay').hide();
		document.getElementsByClassName('communityInput').each(function(el){
			Element.extend(el);
			el.hide();
		});
	},

	showOverlay : function(hideLayer) {
		$('jsOverlay').show();

		if(hideLayer){
			$(hideLayer).hide();
		}

		bodyObj = document.getElementsByTagName('body')[0];
		Element.extend(bodyObj);

		this.bodySize[0] = bodyObj.getWidth();
		this.bodySize[1] = bodyObj.getHeight();

		$('jsOverlay').setStyle({
			width: this.bodySize[0]+'px',
			height: this.bodySize[1]+'px',
			visibility: 'visible'
		});
	},

	setCentered : function(id, bodySize) {
		topPos = 50;

		if(document.body.scrollTop){
			topPos+=document.body.scrollTop;
		} else {
			topPos+=window.pageYOffset;
		}

		if(topPos < 100 || !topPos) topPos = 100;

		elementSize = new Array($(id).getWidth(), $(id).getHeight());

		leftPos = Math.round((bodySize[0]-elementSize[0])/2);
		leftPos = leftPos < 0 ? 10 : leftPos;

		$(id).setStyle({
			top: topPos+'px',
			left: leftPos+'px',
			width: '500px'
		});
	}
}
