/* Verarbeiten von Bewertungfunktionen */
(function(jQuery)
{

	jQuery.fn.ratingSubmit = function(options) 
	{
		var defaults = {
			uploadFormId: ''
			,formId: ''
			,ratingIdField: ''
			,ratingValueFieldId: ''
			,ratingTitleFieldId: ''
			,ratingNameFieldId: ''
			,ratingTextFieldId: ''	
			,ratingPhotoFieldId: '' 
			,ratingRedirectOkayFieldId: ''		
			
		};
		
		var options = jQuery.extend(defaults, options);		
		
		return this.each(function() 
		{
			
			var aErrorText = new Array();
			
			if (jQuery("#"+options.ratingValueFieldId).val()==0) aErrorText[aErrorText.length] = '-> Rating\n';
			if (jQuery("#"+options.ratingTitleFieldId).val()=="") aErrorText[aErrorText.length] = '-> Überschrift\n';
			if (jQuery("#"+options.ratingTextFieldId).val()=="") aErrorText[aErrorText.length] = '-> Ihr Bewertungstext\n';
			
	            	if (aErrorText.length > 0){
	                	alert ("Folgende Felder sind nicht gesetzt:\n\n " + aErrorText.join(" ")+ ' \n Füllen Sie diese Felder bitte aus.');
		                return false;
			}			
			
			var FormData = jQuery("#"+options.formId).serialize();
			
			jQuery.ajax({
				url: '/ajax/rating/',
				data: FormData,
				type: 'POST',
				timeout: 30000,
				error: function(transport, type) 
				{
					alert(transport);
					jQuery("#" + options.dialogDiv).dialog('destroy');
					
				},
				success: function(transport) 
				{
					var data = eval('(' + transport + ')');
					
					if(data.error)
					{
						alert('Fehler beim Hochladen der Daten! ' + data.error);	
					}
					else
					{

						if (data.rating_id >0)
						{
							jQuery("#"+options.ratingIdField).val(data.rating_id);
							
							
							
							var picName = jQuery("#"+options.ratingPhotoFieldId).val();
							
							if (picName!="")
							{
								//mit Pic
							
								jQuery("#"+options.uploadFormId).submit();
									
								var FormData = 'picName=' + picName + '&rating_id=' + data.rating_id+ '&eintrag_id=' + data.eintrag_id;
								jQuery.ajax({
									url: '/ajax/ratingUpdate/',
									data: FormData,
									type: 'POST',
									timeout: 30000,
									error: function(transport, type) 
									{
										alert(transport);
									},
									success: function(transport) 
									{
										//alert(transport);
									}
								});
							}
							else
							{
								//ohne Pic
								window.location.href = jQuery("#"+options.ratingRedirectOkayFieldId).val();
							}
							return false;
						}
						else
						{
							alert('Fehler beim Speichern der Bewertung. Bitte wenden Sie sich an unseren Support, um weitere Informationen zu erhalten.');
							return false;
						}
					}
				}
			});
			return false;
		});
	};

	jQuery.fn.ratingAddPhoto = function(options) 
	{
		
		var defaults = {
			dialogDiv: null
			,title: 'Bewertungsfoto hochladen'
			,formId: ''
			,photoUploadLinkStatusFieldId: ''
			,photoUploadImageFieldId: ''
		};
		
		var options = jQuery.extend(defaults, options);
		
		return this.each(function() 
		{
			jQuery.ui.dialog.defaults.bgiframe=true;
                        jQuery("#" + options.dialogDiv).dialog({
                                modal: false,
                                closeOnEscape: true,
                                title: options.title ,
                                resizable: false,
                                width: 400,
                                height: 190,
                                beforeclose: function(event, ui)
                                {
                                        jQuery("#" + options.dialogDiv).dialog('destroy');
                                },
                                buttons:
                                {
                                        "Abbrechen": function() {jQuery("#" + options.dialogDiv).dialog('destroy');},
                                        "Speichern": function() 
                                        		{
                                        			jQuery("."+options.photoUploadLinkStatusFieldId).text('Die Datei "'+jQuery("#"+options.photoUploadImageFieldId).val() + '" wurde von Ihnen zur Übertragung ausgewählt. Das Hochladen der Datei wird beim Absenden des Formulares erfolgen. Dies kann ggf. einen Moment dauern.');
                                        			jQuery("#" + options.dialogDiv).dialog('destroy');
                                        		}
                                }
                        });
  		});
	};
	
	
	jQuery.fn.ratingShowPic = function(options) 
	{
		
		var defaults = {
			dialogDiv: '',
			dialogClass:'',
			title: 'Bewertungsfoto',
			picName: ''
		};
		
		var options = jQuery.extend(defaults, options);
		
		
		
		return this.each(function() 
		{
			jQuery.ui.dialog.defaults.bgiframe=true;
			
			jQuery("." + options.dialogClass).dialog('destroy');
			
                        jQuery("#" + options.dialogDiv).dialog({
                                modal: true,
                                closeOnEscape: true,
                                title: options.title ,
                                resizable: false,
                                width: 525,
                                beforeclose: function(event, ui)
                                {
                                        jQuery("#" + options.dialogDiv).dialog('destroy');
                                },
                                buttons:
                                {
                                        "Abbrechen": function() {jQuery("#" + options.dialogDiv).dialog('destroy');}
                                }
                        });
  		});
	};	

})(jQuery);





function vote( voting )
{
	document.getElementById('rating_value').value = voting;
}

function light_stars( selectedRating,commonStaticHost )
{
	var hiddenVote = document.getElementById('rating_value');
	if( !hiddenVote || !hiddenVote.value ) return true;
	var voted = hiddenVote.value;

	if( voted > 0 && selectedRating == 0 )
	{
		selectedRating = voted;
	}

	stars = new Array();
	grade = new Array();

	stars[0] = document.getElementById('star1');
	stars[1] = document.getElementById('star2');
	stars[2] = document.getElementById('star3');
	stars[3] = document.getElementById('star4');
	stars[4] = document.getElementById('star5');

	grade[5] = 'Sehr gut';
	grade[4] = 'Gut';
	grade[3] = 'Befriedigend';
	grade[2] = 'Ausreichend';
	grade[1] = 'Mangelhaft';
	grade[0] = 'bitte ausw&auml;hlen';

	var rate_value = document.getElementById('rate_value');
	rate_value.innerHTML = grade[selectedRating];

	for( i = 0; i < 5; i++ )
	{
		if( i < selectedRating )
		{
			stars[i].src = commonStaticHost + '/images/star1.png';
		}
		else
		{
			stars[i].src = commonStaticHost + '/images/star0.png';
		}
	}

	return true;
}

function showRating(b,c,commonStaticHost)
{
	var d=document.getElementById(b).getElementsByTagName('img');
	for(var j=0;j<d.length;j++)
	{
		if(j<=c-1)
		{
			d[j].src = commonStaticHost + '/images/star1.png';
		}
		else
		{
			d[j].src = commonStaticHost + '/images/star0.png';
		}
	}
}
