1 | var gRatingOptions, gRatingButtons, gUserRating; |
---|
2 | |
---|
3 | function makeNiceRatingForm(options) |
---|
4 | { |
---|
5 | gRatingOptions = options; |
---|
6 | var form = document.getElementById('rateForm'); |
---|
7 | if (!form) return; //? template changed |
---|
8 | |
---|
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 | } |
---|
19 | |
---|
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 |
---|
25 | |
---|
26 | rateButton.value = " "; //hide the text (Apple + IE would show text above the stars) |
---|
27 | with (rateButton.style) |
---|
28 | { |
---|
29 | marginLeft = marginRight = 0; |
---|
30 | } |
---|
31 | |
---|
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); |
---|
36 | |
---|
37 | pwgAddEventListener(rateButton, "click", updateRating); |
---|
38 | pwgAddEventListener(rateButton, "mouseout", function() {updateRatingStarDisplay( gUserRating );}); |
---|
39 | pwgAddEventListener(rateButton, "mouseover", function(e) { |
---|
40 | updateRatingStarDisplay( e.target ? e.target.initialRateValue : e.srcElement.initialRateValue); |
---|
41 | }); |
---|
42 | } |
---|
43 | updateRatingStarDisplay( gUserRating ); |
---|
44 | } |
---|
45 | |
---|
46 | function updateRatingStarDisplay(userRating) |
---|
47 | { |
---|
48 | for (var i=0; i<gRatingButtons.length; i++) |
---|
49 | gRatingButtons[i].className = (userRating!=="" && userRating>=gRatingButtons[i].initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty"; |
---|
50 | } |
---|
51 | |
---|
52 | function updateRating(e) |
---|
53 | { |
---|
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 | { |
---|
63 | method: "POST", |
---|
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; |
---|
71 | if (gRatingOptions.onSuccess) gRatingOptions.onSuccess(result); |
---|
72 | if (gRatingOptions.updateRateElement) gRatingOptions.updateRateElement.innerHTML = gRatingOptions.updateRateText; |
---|
73 | if (gRatingOptions.ratingSummaryElement) |
---|
74 | { |
---|
75 | var t = gRatingOptions.ratingSummaryText; |
---|
76 | var args =[result.score, result.count, result.average], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ ); |
---|
77 | while (idx<args.length) t=t.replace(rexp, args[idx++]); |
---|
78 | gRatingOptions.ratingSummaryElement.innerHTML = t; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | ); |
---|
83 | return false; |
---|
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 | })(); |
---|