/*
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 http_request = false;
   function makeRequest(url) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('No se pudo crear la instancia XMLHTTP');
         return false;
      }
	  http_request.onreadystatechange = alertContents;
      http_request.open('GET', url, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
			
            result = http_request.responseText;
			
			var update = new Array();			
			if(result.indexOf('|') != -1) {
				update = result.split('|');
				changeText(update[0], update[1],update[2]);
			}       
         } else {
            alert('Hubo un problema en ejecutar la petición.');
         }
      }
   }


function sndReq(vote,id_num,units,op) {
	var theUL = document.getElementById('unit_ul'+id_num); // the UL
	// switch UL with a loading div
	theUL.innerHTML = '<div class="loading"></div>';
    makeRequest('index.php?module=umFotos&type=ajax&func=califica_ajax&op='+op+'&j='+vote+'&q='+id_num+'&c='+units);
}


function changeText( div2show, text, puntos ) {
    // Detect Browser
	id = parseInt(div2show);
    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('unit_long'+id);
        viewer.innerHTML = text;
		var puntuacion = document.getElementById('votacion'+id);
        puntuacion.innerHTML = puntos;
    }  else if(IE) {
        document.all['unit_long'+id].innerHTML = text;
		document.all['votacion'+id].innerHTML = puntos;
    }
}

/* =============================================================== */
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 theunits = parameterList['c'];
			var opcion = parameterList['op'];
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq(theVote,theratingID,theunits,opcion); return false;		
			}
		}
		
	};
Behaviour.register(ratingAction);
