/*
**************************************************************************
 Dies ist UNVEROEFFENTLICHTER URHEBERRECHTLICH GESCHUETZTER QUELLCODE der
 eWerk IT GmbH, der Inhalt dieser Datei darf ohne die ausdrueckliche
 Erlaubnis der eWerk IT GmbH nicht an Dritte weitergegeben, kopiert oder
 in sonstiger Form, im Ganzen oder in Teilen weitergegeben werden.

 Eine Weitergabe an Dritte, oder Veraenderungen am Programm duerfen nur
 mit ausdruecklicher Genehmigung der eWerk IT GmbH erfolgen.

 Copyright 2007-2008, eWerk IT GmbH,
 Alle Rechte vorbehalten

 This is UNPUBLISHED PROPRIETARY SOURCE CODE of eWerk IT GmbH, the
 contents of this file may not be disclosed to third parties, copied or
 duplicated in any form, in whole or in part, without the prior written
 permission of eWerk IT GmbH.

 Distribution to a third party or changes at the data file are only
 allowed with the explicit permission of eWerk IT GmbH.

 Copyright 2007-2008, eWerk IT GmbH,
 All rights reserved
***************************************************************************/

/**
 * 
 * Leipzig Liest Webseite: Bookfair Rating - AJAX Client
 * 
 * This JavaScript file holds the client side logic of rating bookfair events
 * 
 * @package    module:product
 * @version    $Id: bookfair_rating.js,v 1.1.2.4 2007/11/30 15:29:02 m.werner Exp $
 * @author     Michael Werner <m.werner@ewerk.com>
 * @copyright  2007-2008 eWerk IT GmbH
 */
	
	/*
	* - could be used for debugging an object
	*
	* @params	object 	$obj
	*/
	function vardump(obj) 
	{
	    var a= new Array();
	    for (var p in obj) a[a.length]= p;
	    	a.sort();
	    var w= window.open ('','vardumper');
	    w.document.write('<dl><dt>'+obj+'</dt>');
	    for (var i=0; i<a.length; i++) w.document.write('<dd>'+a[i]+'<i>= '+obj[a[i]]+'</i></dd>');
	    w.document.write('</dl>');
	    w.document.onclick= function () {w.close();};
	}
	
	
	/********************************************************************/
	/*																    */
	/*			  	Step: load and update user voting				    */
	/*			  	@see: template bookfair_showDetail				    */
	/*			  	-> set variable: server_path_event_showdetail       */
	/*																    */
	/********************************************************************/
	
	
    /*
	* AJAX-REQUEST
	* - get rating for first load
	*
	* @author 	Michael Werner <m.werner@ewerk.com>
	* @uses		showRatingResult (RESPONSE)
	*
	* @params   string 	$prefix
	* @params   int		$bookfair_id
	*/
	function getUserRating(prefix, bookfair_id) 
	{
		var params  = prefix+"[action]=load_user_rate";
			params += "&"+prefix+"[bookfair_id]="+bookfair_id;
			params  = params.replace(/\[/g,"%5B");
			params  = params.replace(/\]/g,"%5D");
					
		new Ajax.Request(server_path_event_showdetail, 
			   {method: "post", 
				parameters: params,
				onSuccess: showRatingResult});
	}
	
	/*
	* AJAX-REQUEST
	* - insert/update user rating
	* 
	* @author 	Michael Werner <m.werner@ewerk.com>
	* @uses     showRatingResult (RESPONSE)
	*
	* @params   string 	$prefix
	* @params   int		$bookfair_id
	* @params   string 	$rate
	*/
	function setUserRating(prefix, bookfair_id, rate) 
	{
		var params  = prefix+"[action]=set_user_rate";
			params += "&"+prefix+"[bookfair_id]="+bookfair_id;
			params += "&"+prefix+"[rate]="+rate;
			params  = params.replace(/\[/g,"%5B");
			params  = params.replace(/\]/g,"%5D");

		//action: insert/update user voting
		new Ajax.Request(server_path_event_showdetail, 
						   {method: "post", 
							parameters: params,
							onSuccess: showRatingResult});
	}
	
	/*
	* AJAX-RESPONSE
	* - set rendered tpl-code into div 'showRatingResult'
	* - see 'bookfair_showDetail_rate_ajax'
	*
	* @author 	Michael Werner <m.werner@ewerk.com>
	* @see 		getUserRating
	* @see 		setUserRating
	*/
	function showRatingResult(request)
	{
		var response = request.responseXML;
		
		//use it for mozilla debugging
		//console.log(response);	
		
		//get first level of main variables
		for (var i = 0; i < response.childNodes[0].childNodes.length; i++)
		{
			switch (response.childNodes[0].childNodes[i].tagName)
			{
				case 'html':
					var html = response.childNodes[0].childNodes[i].textContent;
					if (html == undefined) html = response.childNodes[0].childNodes[i].text;
					$('showRatingResult').innerHTML = html;
				break;
			}
		}
		
		return true;
	}
