/*
Page:           rating.js
Created:        Aug 2006
Last Mod:       Mar 11 2007
Handles actions and requests for rating bars.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See readme.txt for full credit details.
--------------------------------------------------------- */

var xmlhttp
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	  try {
	  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	   xmlhttp=false
	  }
	 }
	@else
	 xmlhttp=false
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 try {
	  xmlhttp = new XMLHttpRequest();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function sndReq(vote,id_num,ip_num,units) {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', '../starrater/jsVote.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);	
}

function handleResponse() {
  if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}




/***
	 * JSDoc zur Funktion:
	 * askForFeedback
	 *
	 *
	 * Gibt modalen Feedback Dialog aus, wenn 1 und 2 Sterne Bewertung abgegeben wurde.
	 *
	 ***/	
	function askForFeedback() {
	   $('a[rel="modal"]:first').qtip(
	   {
	      content: {
	         title: {
	            text: 'Feedback',
	            button: 'schließen'
	         },
	         text:                              
		    '<p><strong>Vielen Dank für Ihre Bewertung.</strong></p>' +
		    '<p>Wir sind bemüht das Frauenhandbuch ständig zu verbessern, deswegen interessiert es uns warum Ihnen dieser Abschnitt des '+
		    'Frauenhandbuchs <em>nicht gefallen</em> hat.</p>' +
 		    '<p>Bitte helfen Sie uns das Projekt zu verbessern und schreiben Sie uns doch Ihre Meinung.</p>'+
 		    '<p><a href="http://www.frauenhandbuch.com/contact">Kontakt Formular</a>'+
 		    '<p><strong>Vielen Dank!</strong></p>'

	      },
	      position: {
	         target: $(document.body), // Position it via the document body...
	         corner: 'center' // ...at the center of the viewport
	      },
	      show: {
	         ready: true
	      },
	      hide: false,
	      style: {
	         width: { max: 450 },
	         padding: '14px',
	         border: {
	            width: 9,
	            radius: 9,
	            color: '#666666'
	         },
	         name: 'light'
	      },
	      api: {
	         beforeShow: function()
	         {
	            // Fade in the modal "blanket" using the defined show speed
	            $('#qtip-blanket').fadeIn(this.options.show.effect.length);
	         },
	         beforeHide: function()
	         {
	            // Fade out the modal "blanket" using the defined hide speed
	            $('#qtip-blanket').fadeOut(this.options.hide.effect.length);
	         }
	      }
	   });
	
	   // Create the modal backdrop on document load so all modal tooltips can use it
	   $('<div id="qtip-blanket">')
	      .css({
	         position: 'absolute',
	         top: $(document).scrollTop(), // Use document scrollTop so it's on-screen even if the window is scrolled
	         left: 0,
	         height: $(document).height(), // Span the full document height...
	         width: '100%', // ...and full width
	
	         opacity: 0.7, // Make it slightly transparent
	         backgroundColor: 'black',
	         zIndex: 5000  // Make sure the zIndex is below 6000 to keep it below tooltips!
	      })
	      .appendTo(document.body) // Append to the document body
	      //.hide(); // Hide it initially

	}	


/* =============================================================== */
var ratingAction = {
		'a.rater' : function(element){
			element.onclick = function(){

				var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
				var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
				var parameterList = new Array();
	
				for (j = 0; j < parameterTokens.length; j++) {
					var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
					var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
					parameterList[parameterName] = parameterValue;
				}
				var theratingID = parameterList['q'];
				var theVote = parameterList['j'];
				var theuserIP = parameterList['t'];
				var theunits = parameterList['c'];
				
				// Wenn der Benutzer sehr schlechte Bewertung abgeben hat, frage nach Feedback
				if (theVote == 1 || theVote == 2) {
					askForFeedback();
				}
				//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
				sndReq(theVote,theratingID,theuserIP,theunits); return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);

