2008-07-15 01:29:23 +00:00
var gRatingOptions , gRatingButtons , gUserRating ;
2007-02-14 00:36:34 +00:00
2008-07-15 01:29:23 +00:00
function makeNiceRatingForm ( options )
2007-02-14 00:36:34 +00:00
{
2008-07-15 01:29:23 +00:00
gRatingOptions = options || { } ;
var form = document . getElementById ( 'rateForm' ) ;
if ( ! form ) return ; //? template changed
2007-02-14 00:36:34 +00:00
2008-07-15 01:29:23 +00:00
gRatingButtons = form . getElementsByTagName ( 'input' ) ;
gUserRating = "" ;
for ( var i = 0 ; i < gRatingButtons . length ; i ++ )
{
if ( gRatingButtons [ i ] . type == "button" )
{
gUserRating = gRatingButtons [ i ] . value ;
break ;
}
}
2007-02-14 00:36:34 +00:00
2008-07-15 01:29:23 +00:00
for ( var i = 0 ; i < gRatingButtons . length ; i ++ )
{
var rateButton = gRatingButtons [ i ] ;
rateButton . initialRateValue = rateButton . value ; // save it as a property
try { rateButton . type = "button" ; } catch ( e ) { } // avoid normal submit (use ajax); not working in IE6
2007-02-14 00:36:34 +00:00
2009-07-07 04:36:33 +00:00
if ( navigator . userAgent . indexOf ( 'AppleWebKit/' ) == - 1 && navigator . userAgent . indexOf ( 'MSIE 8' ) == - 1 ) rateButton . value = "" ; //hide the text IE<8/Opera - breaks safari
2008-07-15 01:29:23 +00:00
with ( rateButton . style )
{
textIndent = "-50px" ; //hide the text FF
marginLeft = marginRight = 0 ;
}
2007-02-14 00:36:34 +00:00
2008-07-15 01:29:23 +00:00
if ( i != gRatingButtons . length - 1 && rateButton . nextSibling . nodeType == 3 /*TEXT_NODE*/ )
rateButton . parentNode . removeChild ( rateButton . nextSibling ) ;
if ( i > 0 && rateButton . previousSibling . nodeType == 3 /*TEXT_NODE*/ )
rateButton . parentNode . removeChild ( rateButton . previousSibling ) ;
2007-02-14 00:36:34 +00:00
2008-07-15 01:29:23 +00:00
if ( window . addEventListener ) { // Mozilla, Netscape, Firefox
rateButton . addEventListener ( "click" , updateRating , false ) ;
rateButton . addEventListener ( "mouseout" , resetRatingStarDisplay , false ) ;
rateButton . addEventListener ( "mouseover" , updateRatingStarDisplayEvt , false ) ;
}
else if ( window . attachEvent ) { // IE
rateButton . attachEvent ( "onclick" , updateRating ) ;
rateButton . attachEvent ( "onmouseout" , resetRatingStarDisplay ) ;
rateButton . attachEvent ( "onmouseover" , updateRatingStarDisplayEvt ) ;
}
}
resetRatingStarDisplay ( ) ;
2007-02-14 00:36:34 +00:00
}
function resetRatingStarDisplay ( )
{
2008-07-15 01:29:23 +00:00
updateRatingStarDisplay ( gUserRating ) ;
2007-02-14 00:36:34 +00:00
}
function updateRatingStarDisplay ( userRating )
{
2008-07-15 01:29:23 +00:00
for ( var i = 0 ; i < gRatingButtons . length ; i ++ )
gRatingButtons [ i ] . className = ( userRating !== "" && userRating >= gRatingButtons [ i ] . initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty" ;
2007-02-14 00:36:34 +00:00
}
function updateRatingStarDisplayEvt ( e )
{
2008-07-15 01:29:23 +00:00
updateRatingStarDisplay (
e . target ? e . target . initialRateValue : e . srcElement . initialRateValue ) ;
2007-02-14 00:36:34 +00:00
}
function updateRating ( e )
{
2008-07-15 01:29:23 +00:00
var rateButton = e . target || e . srcElement ;
if ( rateButton . initialRateValue == gUserRating )
return false ; //nothing to do
for ( var i = 0 ; i < gRatingButtons . length ; i ++ ) gRatingButtons [ i ] . disabled = true ;
var y = new PwgWS ( gRatingOptions . rootUrl ) ;
y . callService (
"pwg.images.rate" , { image _id : gRatingOptions . image _id , rate : rateButton . initialRateValue } ,
{
onFailure : function ( num , text ) {
alert ( num + " " + text ) ;
document . location = rateButton . form . action + "&rate=" + rateButton . initialRateValue ;
} ,
onSuccess : function ( result ) {
gUserRating = rateButton . initialRateValue ;
for ( var i = 0 ; i < gRatingButtons . length ; i ++ ) gRatingButtons [ i ] . disabled = false ;
if ( gRatingOptions . updateRateElement ) gRatingOptions . updateRateElement . innerHTML = gRatingOptions . updateRateText ;
if ( gRatingOptions . ratingSummaryElement )
{
var t = gRatingOptions . ratingSummaryText ;
var args = [ result . average , result . count , result . stdev ] , idx = 0 , rexp = new RegExp ( /%\.?\d*[sdf]/ ) ;
_xxx = t . match ( rexp ) ;
while ( idx < args . length ) t = t . replace ( rexp , args [ idx ++ ] ) ;
gRatingOptions . ratingSummaryElement . innerHTML = t ;
}
}
}
) ;
return false ;
2006-12-15 04:57:45 +00:00
}