/**
 * Dynamic Rating stars
 * @copyright 2006 Beau D. Scott http://beauscott.com
 * @
 */

var Stars = Class.create();
Stars.prototype = {
	/**
	 * Mouse X position
	 * @var {Number} options
	 */
	_x: 0,
	/**
	 * Mouse X position
	 * @var {Number} options
	 */
	_y: 0,
	/**
	 * Constructor
	 * @param {Object} options
	 */
	initialize: function(options)
	{

		this.info = false;
		/**
		 * Initialized?
		 * @var (Boolean)
		 */
		this._initialized = false;
		//this.info = false;
		/**
		 * Base option values
		 * @var (Object)
		 */
		this.options = {
			bindField: null,			// Form Field to bind the value to
			maxRating: 5,				// Maximum rating, determines number of stars
			container: null,			// Container of stars
			imagePath: '../Static/image/rating/',                   // Path to star images
			callback: null,				// Callback function, fires when the stars are clicked
			actionURL: null,			// URL to call when clicked. The rating will be appended to the end of the URL (eg: /rate.php?id=5&rating=)
			value: 0,					// Initial Value
			locked: false,
			imageType: 9,				// Initial imageType
			count: 0,
			staticUrl: ""
			
		};
		Object.extend(this.options, options);
		this.locked = this.options.locked ? true : false;
		
		/**
		 * Image sources for hover and user-set state ratings
		 */
		switch (this.options.imageType)
		 {
			 default:
				this.infoTitle = new Array("mierny","obleci","daje radę","wypas","wyziewowy");
			 break;
			 case 1:
				this.infoTitle = new Array("ubrany","obnażone uczucia","naga prawda","topless","totalnie rozebrany");
			 break;
			 case 2:
				this.infoTitle = new Array("turystyczna","tyrolska","luncheon","gulasz angielski","wiedeńska");
			 break;
			 case 3:
				//this.infoTitle = new Array("potknięcie","skucha","wtopa","faux pas","mistrz świata");
				this.infoTitle = new Array("lewus","amator","sezonowy zawodnik","zawodowiec","mistrz świata");
			 break;
			 case 4:
				this.infoTitle = new Array("mireny","obleci","daje radę","wypas","wyziewowy");
			 break;
			 case 5:
				this.infoTitle = new Array("chwilowy","słaby","mizerny","groźny","śmiertelny");
			 break;
			 case 6:
				this.infoTitle = new Array("potańcówka w remizie","festyn","chlanie w akademiku","impreza w klubie","bal na salonach");
			 break;
			 case 7:
				this.infoTitle = new Array("skisły","mdły","zjadliwy","pyszny","wyborny");
			 break;
			 case 8:
				this.infoTitle = new Array("mireny","obleci","daje radę","wypas","wyziewowy");
			 break;
			 case 9:
				this.infoTitle = new Array("decha z dziurą","toj toj","szalet miejski","domowy kibelek","hotelowa toaleta");
			 break;
		 }
		 
		 switch (this.options.imageType)
		 {
			 default:
				var prefix = "toiletPaper";
			 break;
			 case 1:
				 var prefix = "naked";
			 break;
			 case 2:
				 var prefix = "konserwa";
			 break;
			 case 3:
				 var prefix = "aktywny";
			 break;
			 case 4:
				 var prefix = "krzywonoga";
			 break;
			 case 5:
				 var prefix = "dziury";
			 break;
			 case 6:
				 var prefix = "konserwa";
			 break;
			 case 7:
				 var prefix = "garnek";
			 break;
			 case 8:
				 var prefix = "krzywonoga";
			 break;
			 case 9:
				 var prefix = "toiletPaper";
			 break;
		 }

		this._starSrc = {
			empty: this.options.imagePath + prefix +"-empty.gif",
			full: this.options.imagePath + prefix + ".gif",
			half: this.options.imagePath + prefix + "-half.gif"
		};
		/**
		 * Preload images
		 */
		for(var x in this._starSrc)
		{
			var y = new Image();
			y.src = this._starSrc[x];
		}

		//document.getElem

		/**
		 * Images to show for pre-set values, changes when hovered, if not locked.
		 */
		this._setStarSrc = {
			empty: this.options.imagePath + prefix + "-empty.gif",
			full: this.options.imagePath + prefix + ".gif",
			half: this.options.imagePath + prefix + "-half.gif"
		};

		/**
		 * Preload images
		 */
		for(var x in this._setStarSrc)
		{
			var y = new Image();
			y.src = this._setStarSrc[x];
		}

		this.value = -1;
		this.stars = [];
		this._clicked = false;


		if(this.options.container)
		{
			this._container = $(this.options.container);
			this.id = this._container.id;
		}
		else
		{


			this.id = 'starsContainer.' + Math.random(0, 100000);
			

			if(this.infoTitle[Math.round(this.options.value-1)])
				document.write('<div class="ratingDescription" id="infoTitle">' + this.infoTitle[Math.round(this.options.value-1)]+ '</div>' );
			else
				document.write('<div class="ratingDescription" id="infoTitle">brak ocen</div>' );

			document.write('<div style="float:left;clear:both;" id="' + this.id + '"></div>' );

			this._container = $(this.id);
			this._display();
			this.setValue(this.options.value);
			this._initialized = true;

			if(this.options.count != 0)
			{
				ratingCountDiv = document.createElement("div");
				ratingCountDiv.setAttribute("id", "ratingCountDiv");
				ratingCountDiv.setAttribute("style", "margin-top:2px;float:right;");
				ratingCountDiv.innerHTML = ' x ';
				this._container.appendChild(ratingCountDiv);

				ratingCount = document.createElement("span");
				ratingCount.setAttribute("id", "ratingCount");
				ratingCount.setAttribute("style", "margin-top:2px;");
				ratingCount.innerHTML = this.options.count + " ";

				ratingCountDiv.appendChild(ratingCount);

				ratingCountImg = document.createElement("img");
				ratingCountImg.setAttribute("src", this.options.staticUrl + "/image/Strona/iconPreserveLogin.gif");
				ratingCountImg.setAttribute("title", "pasztet");
				ratingCountImg.setAttribute("alt", this.options.count);

				ratingCountDiv.appendChild(ratingCountImg);

			//	document.write('<div id="ratingCountDiv" style="margin-top:2px;"> x <span id="ratingCount">' + this.options.count + ' </span> <img src="' + this.options.staticUrl + '/image/Strona/iconPreserveLogin.gif" title="pasztet" alt="zdaniem ' + this.options.count + '"/> </div>' );
			}
			else
			{
				ratingCountDiv = document.createElement("div");
				ratingCountDiv.setAttribute("id", "ratingCountDiv");
				ratingCountDiv.setAttribute("style", "margin-top:2px;float:left;");
				this._container.appendChild(ratingCountDiv);

//				document.write('<div id="ratingCountDiv" style="margin-top:2px;"></div>' );
			}
		}
	},
	_display: function()
	{




		if(this.locked != true)
		{
			infoElementRate = document.createElement("div");
			infoElementRate.setAttribute("id", "ocenInfo");
			infoElementRate.setAttribute("class", "infoRating");
			
			infoElementRate.innerHTML = "oceń: ";
			this._container.appendChild(infoElementRate);
		}

		for(var i = 0; i < this.options.maxRating; i++)
		{
                   
			var star = new Image();
			star.src = this.locked ? this._starSrc.empty : this._setStarSrc.empty;
			if(this.locked != true)
                star.style.cursor = 'pointer';
						
            if(this.locked != true)
				star.title = this.infoTitle[i];

			star.className = "imageRating";
			
			!this.locked && Event.observe(star, 'mouseover', this._starHover.bind(this));
			!this.locked && Event.observe(star, 'click', this._starClick.bind(this));
			!this.locked && Event.observe(star, 'mouseout', this._starClear.bind(this));
			this.stars.push(star);
			this._container.appendChild(star);

			
		}
		

	},
	_starHover: function(e)
	{
		if(this.locked) return;
		if(!e) e = window.event;
		var star = Event.element(e);

		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			this.stars[i].src = greater ? this._starSrc.empty : this._starSrc.full;
			if(this.stars[i] == star) greater = true;
		}
	},
	_starClick: function(e)
	{
		if(this.locked) return;
		if(!e) e = window.event;
		var star = Event.element(e);
		this._clicked = true;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(this.stars[i] == star)
			{
				this.setValue(i+1);
				break;
			}
		}
	},
	_starClear: function(e)
	{
		if(this.locked && this._initialized) return;
		var greater = false;
		for(var i = 0; i < this.stars.length; i++)
		{
			if(i > this.value) 
                                greater = true;

			if((this._initialized && this._clicked) || this.value == -1)
				this.stars[i].src = greater ? (this.value + .5 >= i ) ? this._starSrc.half : this._starSrc.empty : this._starSrc.full;
			else
				this.stars[i].src = greater ? (this.value + .5 >= i) ? this._setStarSrc.half : this._setStarSrc.empty : this._setStarSrc.full;
		}
	},

        
	/**
	 * Sets the value of the star object, redraws the UI
	 * @param {Number} value to set
	 * @param {Boolean} optional, do the callback function, default true
	 */
	setValue: function(val)
	{
		
		var doCallBack = arguments.length > 1 ? !!arguments[1] : true;
		if(this.locked && this._initialized) return;
		this.value = val-1; //0-based
		if(this.options.bindField)
			$(this.options.bindField).value = val;
		if(this._initialized && doCallBack)
		{
			
			if(this.options.actionURL)
				new Ajax.Request(this.options.actionURL, {onComplete: this.options['callback'],parameters: "val=" + val, method: 'post'});
			else
				if(this.options.callback)
					this.options['callback'](val);
		}
		this._starClear();
	}
};

function setRating(json)
{
	
	var rating = json.responseText.evalJSON();
	
	s1.setValue(rating["rating"],false);

	//aktalizacja info title

	infoTitleDiv = document.getElementById("infoTitle");
	

    s1.locked = true;
	s1.info = true;

	
	if(s1.options.count != 0)
	{
		ratingCount = document.getElementById("ratingCount");
		var count = parseInt(ratingCount.innerHTML);
		
		count++;

		ratingCount.innerHTML = count + " ";
		
	//	ratingCountDiv = document.getElementById("ratingCountDiv");
	//	ratingCountDiv.innerHTML += '<img src="../Static/image/Strona/iconPreserveLogin.gif" title="zdaniem ' + count + '" alt="zdaniem ' + count + '"/>';
	}
	else
	{
		
		ratingCountDiv = document.getElementById("ratingCountDiv");
		ratingCountDiv.innerHTML = ' x <span id="ratingCount">1 </span> <img src="' + s1.options.staticUrl+ '/image/Strona/iconPreserveLogin.gif" title="zdaniem 1" alt="zdaniem 1"/>';
	 
	}

	infoTitleDiv.innerHTML =  s1.infoTitle[Math.round(rating["rating"]-1)] ;

	spanRateInfo = document.getElementById("ocenInfo");

	//spanRateInfo.style.display = "none";
	//infoElementAfterRate = document.createElement("span");
	//infoElementAfterRate.innerHTML = "oceniłeś";
	spanRateInfo.className = "infoRatingLong";
	spanRateInfo.innerHTML = "oceniłeś";
	s1._container.appendChild(infoElementAfterRate);


}
