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 | if (navigator.userAgent.indexOf('AppleWebKit/') == -1 ) rateButton.value = ""; //hide the text IE/Opera - breaks safari |
---|
27 | with (rateButton.style) |
---|
28 | { |
---|
29 | textIndent = "-50px"; //hide the text FF |
---|
30 | marginLeft = marginRight = 0; |
---|
31 | } |
---|
32 | |
---|
33 | if (i!=gRatingButtons.length-1 && rateButton.nextSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
34 | rateButton.parentNode.removeChild(rateButton.nextSibling); |
---|
35 | if (i>0 && rateButton.previousSibling.nodeType == 3 /*TEXT_NODE*/) |
---|
36 | rateButton.parentNode.removeChild(rateButton.previousSibling); |
---|
37 | |
---|
38 | if(window.addEventListener){ // Mozilla, Netscape, Firefox |
---|
39 | rateButton.addEventListener("click", updateRating, false ); |
---|
40 | rateButton.addEventListener("mouseout", resetRatingStarDisplay, false ); |
---|
41 | rateButton.addEventListener("mouseover", updateRatingStarDisplayEvt, false ); |
---|
42 | } |
---|
43 | else if(window.attachEvent) { // IE |
---|
44 | rateButton.attachEvent("onclick", updateRating); |
---|
45 | rateButton.attachEvent("onmouseout", resetRatingStarDisplay); |
---|
46 | rateButton.attachEvent("onmouseover", updateRatingStarDisplayEvt); |
---|
47 | } |
---|
48 | } |
---|
49 | resetRatingStarDisplay(); |
---|
50 | } |
---|
51 | |
---|
52 | function resetRatingStarDisplay() |
---|
53 | { |
---|
54 | updateRatingStarDisplay( gUserRating ); |
---|
55 | } |
---|
56 | |
---|
57 | function updateRatingStarDisplay(userRating) |
---|
58 | { |
---|
59 | for (var i=0; i<gRatingButtons.length; i++) |
---|
60 | gRatingButtons[i].className = (userRating!=="" && userRating>=gRatingButtons[i].initialRateValue ) ? "rateButtonStarFull" : "rateButtonStarEmpty"; |
---|
61 | } |
---|
62 | |
---|
63 | function updateRatingStarDisplayEvt(e) |
---|
64 | { |
---|
65 | updateRatingStarDisplay( |
---|
66 | e.target ? e.target.initialRateValue : e.srcElement.initialRateValue); |
---|
67 | } |
---|
68 | |
---|
69 | function updateRating(e) |
---|
70 | { |
---|
71 | var rateButton = e.target || e.srcElement; |
---|
72 | if (rateButton.initialRateValue == gUserRating) |
---|
73 | return false; //nothing to do |
---|
74 | |
---|
75 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=true; |
---|
76 | var y = new PwgWS(gRatingOptions.rootUrl); |
---|
77 | y.callService( |
---|
78 | "pwg.images.rate", {image_id: gRatingOptions.image_id, rate: rateButton.initialRateValue } , |
---|
79 | { |
---|
80 | onFailure: function(num, text) { |
---|
81 | alert(num + " " + text); |
---|
82 | document.location = rateButton.form.action + "&rate="+rateButton.initialRateValue; |
---|
83 | }, |
---|
84 | onSuccess: function(result) { |
---|
85 | gUserRating = rateButton.initialRateValue; |
---|
86 | for (var i=0; i<gRatingButtons.length; i++) gRatingButtons[i].disabled=false; |
---|
87 | if (gRatingOptions.updateRateElement) gRatingOptions.updateRateElement.innerHTML = gRatingOptions.updateRateText; |
---|
88 | if (gRatingOptions.ratingSummaryElement) |
---|
89 | { |
---|
90 | var t = gRatingOptions.ratingSummaryText; |
---|
91 | var args =[result.average, result.count, result.stdev], idx = 0, rexp = new RegExp( /%\.?\d*[sdf]/ ); |
---|
92 | _xxx = t.match( rexp ); |
---|
93 | while (idx<args.length) t=t.replace(rexp, args[idx++]); |
---|
94 | gRatingOptions.ratingSummaryElement.innerHTML = t; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | ); |
---|
99 | return false; |
---|
100 | } |
---|