/**
 * File with classes of boxes which could appear on any page.
 * This file is alredy included on every page, so classes for
 * handling boxes specific for just some pages should have it's own
 * js file which should be included from php-side like $page->add_js('name.js')
 *
 * @author XShady
 * @project itzone
 */

/**
 * Class for creating animated votes panel
 *
 */
pollBox = {
	//
 	// XS: TODO: If somebody will add more poll-boxes on panel, i'ts defenetly
	// gonna be fucked! In that case it needs to make unique ID for each,
	// for example by using ID of panel (right, left, etc. - see class for
	// managing panels) + index of box on the panel.
	//
	// For short of time, I'm gonna fix it just in case some bampot will
	// one rainy day need to use multiple pollboxes on one page.
    //
	initialize:
	    function(id, delay, options, maxLen)
	    {
	        this.id = (id != '') ? id : 'pollbox';
	        this.delay = (delay != '') ? delay : 0.1;
	        this.element = document.getElementById(this.id);

	        if (!this.element)
	        {
	            if (this.debug)
	                window.alert('pollBox.initialize() error: Element "' + this.id + '" not found !');
	            return false;
			}

			this.optionsCount = (options != '') ? options : 10;
			this.maxLength = (maxLen != '') ? maxLen : 100;
			this.maxOptionLength = 0;
			this.optionsSizes = [];
			this.optionsValues = [];
			this.effectCounter = 0;
			
			this.getValues();

			return true;
		},

	getValues:
	    function()
	    {
	        var option = this.element;

	        for (var i = 0; i < this.optionsCount; i++)
	        {
	            option = document.getElementById(this.id + '_' + i);

	            if (!option)
		        {
		            if (this.debug)
		                window.alert('pollBox.getValues() error: Element "' + option + '" not found !');
		            return false;
				}


	            this.optionsSizes[i] = parseInt(document.getElementById(this.id + '_' + i + '_width_value').innerHTML);
	            this.optionsValues[i] = document.getElementById(this.id + '_' + i + '_percent_value').innerHTML;

	            // Calculate max size to don't do effect for full possible length
				this.maxOptionLength = (this.maxOptionLength < this.optionsSizes[i]) ? this.optionsSizes[i] : this.maxOptionLength;
			}

			// Do effect
			this.timer = setInterval('pollBox.doEffect()', this.delay * 1000);

			return true;
		},

	doEffect:
	    function()
	    {
	        var percent = 0;

	        if (this.effectCounter > this.maxOptionLength)
	        {
	            clearInterval(this.timer);
	            return false;
			}

			for (var i = 0; i < this.optionsCount; i++)
			{
			    // If bar should be longer, make it longer and update percent value
			    if (this.effectCounter < this.optionsSizes[i])
			    {
			        percent = round((this.maxLength / 100) * this.effectCounter);
					document.getElementById(this.id + '_' + i + '_bar').style.width = this.effectCounter + 'px';
					document.getElementById(this.id + '_' + i + '_percent').innerHTML = percent + '%';
				}

				// If bar has original size, make sure everithing has proper value
				if (this.effectCounter == this.optionsSizes[i])
				{
				    document.getElementById(this.id + '_' + i + '_bar').style.width = this.optionsSizes[i] + 'px';
				    document.getElementById(this.id + '_' + i + '_percent').innerHTML = this.optionsValues[i];
				}
			}

            this.effectCounter++;
			return true;
		}
}