[2435] | 1 | var gRatingOptions, gRatingButtons, gUserRating; |
---|
[1814] | 2 | |
---|
[2435] | 3 | function makeNiceRatingForm(options) |
---|
[1814] | 4 | { |
---|
[7957] | 5 | gRatingOptions = options; |
---|
[2435] | 6 | var form = document.getElementById('rateForm'); |
---|
| 7 | if (!form) return; //? template changed |
---|
[1814] | 8 | |
---|
[2435] | 9 | gRatingButtons = form.getElementsByTagName('input'); |
---|
| 10 | gUserRating = ""; |
---|
| 11 | for (var i=0; i<gRatingButtons.length; i++) |
---|
| 12 | { |
---|
| 13 | if ( gRatingButtons[i].type=="button" ) |
---|
| 14 | { |
---|
| 15 | gUserRating = gRatingButtons[i].value; |
---|
| 16 | break; |
---|
| 17 | } |
---|
| 18 | } |
---|
[1814] | 19 | |
---|
[2435] | 20 | for (var i=0; i<gRatingButtons.length; i++) |
---|
| 21 | { |
---|
| 22 | var rateButton = gRatingButtons[i]; |
---|
| 23 | rateButton.initialRateValue = rateButton.value; // save it as a property |
---|
| 24 | try { rateButton.type = "button"; } catch (e){}// avoid normal submit (use ajax); not working in IE6 |
---|
[1814] | 25 | |
---|
[12639] | 26 | rateButton.value = " "; //hide the text (Apple + IE would show text above the stars) |
---|
[2435] | 27 | with (rateButton.style) |
---|
| 28 | { |
---|
| 29 | marginLeft = marginRight = 0; |
---|
| 30 | } |
---|
[1814] | 31 | |
---|
[2435] | 32 | if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
| 33 | rateButton.parentNode.removeChild(rateButton.nextSibling); |
---|
| 34 | if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
| 35 | rateButton.parentNode.removeChild(rateButton.previousSibling); |
---|
[1814] | 36 | |
---|
[6577] | 37 | pwgAddEventListener(rateButton, "click", updateRating); |
---|
[7957] | 38 | pwgAddEventListener(rateButton, "mouseout", function() {updateRatingStarDisplay( gUserRating );}); |
---|
| 39 | pwgAddEventListener(rateButton, "mouseover", function(e) { |
---|
| 40 | updateRatingStarDisplay( e.target ? e.target.initialRateValue : e.srcElement.initialRateValue); |
---|
| 41 | }); |
---|
[2435] | 42 | } |
---|
| 43 | updateRatingStarDisplay( gUserRating ); |
---|
[1814] | 44 | } |
---|
| 45 | |
---|
| 46 | function updateRatingStarDisplay(userRating) |
---|
| 47 | { |
---|
[2435] | 48 | for (var i=0; i<gRatingButtons.length; i++) |
---|
| 49 | gRatingButtons[i].className = (userRating!=="" && userRating>=gRatingButtons[i].initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty"; |
---|
[1814] | 50 | } |
---|
| 51 | |
---|
| 52 | function updateRating(e) |
---|
| 53 | { |
---|
[2435] | 54 | var rateButton = e.target || e.srcElement; |
---|
| 55 | if (rateButton.initialRateValue == gUserRating) |
---|
| 56 | return false; //nothing to do |
---|
| 57 | |
---|
| 58 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=true; |
---|
| 59 | var y = new PwgWS(gRatingOptions.rootUrl); |
---|
| 60 | y.callService( |
---|
| 61 | "pwg.images.rate", {image_id: gRatingOptions.image_id, rate: rateButton.initialRateValue } , |
---|
| 62 | { |
---|
[11834] | 63 | method: "POST", |
---|
[2435] | 64 | onFailure: function(num, text) { |
---|
| 65 | alert(num + " " + text); |
---|
| 66 | document.location = rateButton.form.action + "&rate="+rateButton.initialRateValue; |
---|
| 67 | }, |
---|
| 68 | onSuccess: function(result) { |
---|
| 69 | gUserRating = rateButton.initialRateValue; |
---|
| 70 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=false; |
---|
[11839] | 71 | if (gRatingOptions.onSuccess) gRatingOptions.onSuccess(result); |
---|
[2435] | 72 | if (gRatingOptions.updateRateElement) gRatingOptions.updateRateElement.innerHTML = gRatingOptions.updateRateText; |
---|
| 73 | if (gRatingOptions.ratingSummaryElement) |
---|
| 74 | { |
---|
| 75 | var t = gRatingOptions.ratingSummaryText; |
---|
[11827] | 76 | var args =[result.score, result.count, result.average], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ ); |
---|
[2435] | 77 | while (idx<args.length) t=t.replace(rexp, args[idx++]); |
---|
| 78 | gRatingOptions.ratingSummaryElement.innerHTML = t; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | ); |
---|
| 83 | return false; |
---|
[7957] | 84 | } |
---|
| 85 | |
---|
| 86 | (function() { |
---|
| 87 | if (typeof _pwgRatingAutoQueue!="undefined" && _pwgRatingAutoQueue.length) |
---|
| 88 | { |
---|
| 89 | for (var i=0; i<_pwgRatingAutoQueue.length; i++) |
---|
| 90 | makeNiceRatingForm(_pwgRatingAutoQueue[i]); |
---|
| 91 | } |
---|
| 92 | _pwgRatingAutoQueue = { |
---|
| 93 | push: function(opts) { |
---|
| 94 | makeNiceRatingForm(opts); |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | })(); |
---|