// {{MadCap}} //////////////////////////////////////////////////////////////////
// Copyright: MadCap Software, Inc - www.madcapsoftware.com ////////////////////
////////////////////////////////////////////////////////////////////////////////
// <version>5.0.1.0</version>
////////////////////////////////////////////////////////////////////////////////

//
//    Class CMCRatingDialog
//

function CMCRatingDialog( winNode, initialRating )
{
	CMCDialog.call( this, winNode );

	// Public properties

	this.mCurrentRating	= initialRating;

	// Overridden properties
	
	this.StyleClass			= "Rating";
	this.FontFamily			= "Arial";
	this.FontSize			= "12px";
	this.FontWeight			= "normal";
	this.FontStyle			= "normal";
	this.Color				= "#000000";
	this.TitleLabel			= "Topic Rating:";
	this.TitleFontFamily	= "Arial";
	this.TitleFontSize		= "14px";
	this.TitleFontWeight	= "bold";
	this.TitleFontStyle		= "normal";
	this.TitleFontVariant	= "small-caps";
	this.TitleColor			= "#000000";
	this.BackgroundColor	= "#ffffcc";
	this.SubmitButtonLabel	= "Submit";
	this.CancelButtonLabel	= "Cancel";
	this.ShadowColor		= "#000000";
	this.ShadowDistance		= 5;
	this.ShadowOpacity		= 100;
	this.BorderLeft			= "solid 2px #000000";
	this.BorderRight		= "solid 2px #000000";
	this.BorderTop			= "solid 2px #000000";
	this.BorderBottom		= "solid 2px #000000";
	
	this.AverageRatingLabel		= "Average Rating:";
	this.YourRatingLabel		= "Your Rating:";
	this.CommentLabel			= "Feedback for the author (optional):";
	this.AverageRatingTooltip	= "Average topic rating";
	this.YourRatingTooltip		= "Your rating";
	
	this.CommentLengthExceeded = "The maximum comment length was exceeded by {n} characters.";
}

CMCRatingDialog.prototype = new CMCDialog();
CMCRatingDialog.prototype.constructor = CMCRatingDialog;
CMCRatingDialog.prototype.base = CMCDialog.prototype;

CMCRatingDialog.prototype.InnerHtml =	"" +
										"<table class='MCDialogOuterTable'>" +
											"<col style='width: 100px;' />" +
											"<col style='width: auto;' />" +
											"<tr>" +
												"<td id='MCAverageRatingLabel' class='Label'>" +
													"Average Rating:" +
												"</td>" +
												"<td>" +
													"<span id='MCAverageRatingIcons' title='Average topic rating' style='font-size: 1px;'></span>" +
													"<span id='MCRatingCount'></span>" +
												"</td>" +
											"</tr>" +
											"" +
											"<tr>" +
												"<td id='MCYourRatingLabel' class='Label'>" +
													"Your Rating:" +
												"</td>" +
												"<td>" +
													"<span id='MCUserRatingIcons' title='Your rating' style='font-size: 1px;' />" +
												"</td>" +
											"</tr>" +
											"" +
											"<tr>" +
												"<td style='font-size: 1px; height: 10px;' colspan='2'>&#160;</td>" +
											"</tr>" +
											"" +
											"<tr>" +
												"<td id='MCCommentLabel' class='Label' colspan='2'>" +
													"Feedback for the author (optional):" +
												"</td>" +
											"</tr>" +
											"" +
											"<tr>" +
												"<td style='padding-right: 10px;' colspan='2'>" +
													"<textarea id='MCRatingComment' cols='35' rows='8' style='width: 100%;'></textarea>" +
												"</td>" +
											"</tr>" +
											"" +
											"<tr>" +
												"<td style='text-align: right;' colspan='2'>" +
													"<br />" +
													"<input id='MCDialogSubmit' type='button' value='Submit rating' />" +
													"<input id='MCDialogCancel' type='button' value='Cancel' />" +
												"</td>" +
											"</tr>" +
										"</table>";

CMCRatingDialog.prototype.OnInitializing	= function()
{
	this.base.OnInitializing.call( this );

	//

	var topicID	= FMCGetMCAttribute( document.documentElement, "MadCap:liveHelp" );

	gServiceClient.GetAverageRating( topicID, this.GetRatingOnComplete, null );

	//

	var avgRatingIcons	= this.mWindow.document.getElementById( "MCAverageRatingIcons" );
	var userRatingIcons	= this.mWindow.document.getElementById( "MCUserRatingIcons" );
	var img				= this.mWindow.document.createElement( "img" );

	CMCFlareStylesheet.SetImageFromStylesheet( img, "ToolbarItem", "TopicRatings", "EmptyIcon", MCGlobals.RootFolder + MCGlobals.SkinTemplateFolder + "Images/Rating0.gif", 16, 16 );
	
	avgRatingIcons.appendChild( img );
	avgRatingIcons.appendChild( img.cloneNode( true ) );
	avgRatingIcons.appendChild( img.cloneNode( true ) );
	avgRatingIcons.appendChild( img.cloneNode( true ) );
	avgRatingIcons.appendChild( img.cloneNode( true ) );
	
	userRatingIcons.appendChild( img.cloneNode( true ) );
	userRatingIcons.appendChild( img.cloneNode( true ) );
	userRatingIcons.appendChild( img.cloneNode( true ) );
	userRatingIcons.appendChild( img.cloneNode( true ) );
	userRatingIcons.appendChild( img.cloneNode( true ) );
	userRatingIcons.onclick = this.UserRatingIconsOnclick;
	userRatingIcons.onmousemove = this.UserRatingIconsOnmousemove;
	userRatingIcons.onmouseout = this.UserClearRatingIcons;
	
	FMCDrawRatingIcons( this.mCurrentRating, userRatingIcons );
	
	this.mWindow.document.getElementById( "MCRatingComment" ).focus();
};

CMCRatingDialog.prototype.GetRatingOnComplete	= function( averageRating, ratingCount, onCompleteArgs )
{
	var iconContainer	= document.getElementById( "MCAverageRatingIcons" );

	FMCDrawRatingIcons( averageRating, iconContainer );
};

CMCRatingDialog.prototype.LoadStyles	= function()
{
	this.base.LoadStyles.call( this );
	
	var averageRatingLabel		= CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "AverageRatingLabel", this.AverageRatingLabel );
	var yourRatingLabel			= CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "YourRatingLabel", this.YourRatingLabel );
	var commentLabel			= CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "CommentLabel", this.CommentLabel );
	var averageRatingTooltip	= CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "AverageRatingTooltip", this.AverageRatingTooltip );
	var yourRatingTooltip		= CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "YourRatingTooltip", this.YourRatingTooltip );
	
	document.getElementById( "MCAverageRatingLabel" ).firstChild.nodeValue = averageRatingLabel;
	document.getElementById( "MCYourRatingLabel" ).firstChild.nodeValue = yourRatingLabel;
	document.getElementById( "MCCommentLabel" ).firstChild.nodeValue = commentLabel;
	document.getElementById( "MCAverageRatingIcons" ).title = averageRatingTooltip;
	document.getElementById( "MCUserRatingIcons" ).title = yourRatingTooltip;
	
	this.CommentLengthExceeded = CMCFlareStylesheet.LookupValue( "Dialog", this.StyleClass, "CommentLengthExceeded", this.CommentLengthExceeded );
};

CMCRatingDialog.prototype.UserRatingIconsOnclick	= function( e )
{
	if ( !e ) { e = window.event; }
	
	var dlg	= CMCDialog.GetDialog( this );
	
	var x			= FMCGetMouseXRelativeTo( window, e, this );
	var numImgNodes	= this.getElementsByTagName( "img" ).length;
	var iconWidth	= 16;
	var numIcons	= Math.ceil( x / iconWidth );
	var rating		= numIcons * 100 / numImgNodes;
	
	rating = Math.min( rating, 100 );
	rating = Math.max( rating, 0 );

	dlg.mCurrentRating = rating;
}

CMCRatingDialog.prototype.UserRatingIconsOnmousemove	= function( e )
{
	if ( !e ) { e = window.event; }
	
	FMCRatingIconsOnmousemove( e, this );
}

CMCRatingDialog.prototype.UserClearRatingIcons	= function( e )
{
	if ( !e ) { e = window.event; }
	
	var dlg	= CMCDialog.GetDialog( this );
	
	FMCClearRatingIcons( dlg.mCurrentRating, this );
}

CMCRatingDialog.prototype.OK	= function()
{
	if ( this.mCurrentRating < 0 || this.mCurrentRating > 100 )
	{
		alert( "Please specify your rating." );
		
		return false;
	}
	
	var topicID	= FMCGetAttribute( this.mWindow.document.documentElement, "MadCap:liveHelp" );
	var comment	= this.mWindow.document.getElementById( "MCRatingComment" ).value;
	
	try
	{
		gServiceClient.SubmitRating( topicID, this.mCurrentRating, comment );
	}
	catch ( ex )
	{
		var message = this.CommentLengthExceeded.replace( /{n}/g, ex.Data.ExceedAmount );
		alert( message );
		
		return false;
	}
	
	return true;
};

CMCRatingDialog.prototype.Cancel	= function()
{
};

//
//    End class CMCRatingDialog
//
