
Site.polldata = {
	progressbars : {},
	title_progressbars : {},
	value : {},
	vote : 0
};
	Site.polldata.progressbars = {
		1 : 'progressbar_agree',
		2 : 'progressbar_disagree',
		3 : 'progressbar_no_opinion'
	};
	// define titles
	Site.polldata.title_progressbars = {
		1 : 'mee eens',
		2 : 'oneens',
		3 : 'geen mening'
	};
	Site.polldata.value = {
		1 : 0,
		2 : 0,
		3 : 0		
	};
//window['$$'] = console.log;
// ============================[ classe poll functionality ]=======================================
NS.Package('Site.Poll');
(function($){
Site.Poll = function()
{
	this.target = null;
	this.progressClassName = null;
	this.ButtonsClassName = null;
	this.textClassName = null;
	this.title = '';
	this.icon = '';
	this.img_loader = '';
	this.vote = 0;
	this.request_url = '';
	this.request = {};
	this.url = '';
	this.data = '';
};



	// -------------> method 1: create icon <-------------------------------
	Site.Poll.prototype.createIcon = function(){
		var img = $('<img>');
			img.attr({
				id 	: 'quoteTLoader',
				src	: this.icon
			}).addClass('iconPoll');
			if(this.icon == ''){
				img.css({		 	
				 	'display' : 'none'
				});
			}
			return img;
	};

	// -------------> method 2: create the title <-------------------------------
	Site.Poll.prototype.createTitle = function(){
		var div = $('<div>');
		var h3  = $('<h3>');
		var substr = this.title.split(" ");
	    var complete = '';
	    // complete the title
		for(var a = 1; a < substr.length; a++){
		 	complete += substr[a] + ' ';
		}
			// create icon
			this.createIcon().appendTo(div);
		 	// create title
		    h3.html('<span>'+substr[0]+'</span> '+ complete).appendTo(div);
		
			div.attr({
				id : 'quoteTitle'
			}).appendTo(this.target);		
	};
	
	// -------------> method 3: create the quote <-------------------------------
	Site.Poll.prototype.createQuote = function(){
		var _div = $('<div>');
			_div.attr({id : 'quote'}).appendTo(this.target);		
	};

	// -------------> method 4: create poll <-------------------------------
	Site.Poll.prototype.createProgressbars = function(){
		var a = 0;
		var progressbars = Site.polldata.progressbars;
		var titles = Site.polldata.title_progressbars;
	
		var value = Site.polldata.value;
		var isIE = /*@cc_on!@*/false; // check if browser is IE
		
		var span = $('<span>');
			span.attr({
				id : 'progr_wrap'
			}).css({
				'display' : 'none'
			}).appendTo(this.target);	
			
		for(a in progressbars){
			// create progressbars
			var div = $('<div>');
			var h3 = $('<h3>');
			
				h3.html(titles[a] + ': <span id="progrs_'+ a+'">'+ value[a] + ' %'+'</span>');
				div.attr({
					id : progressbars[a]
					
				}).addClass(this.progressClassName);
				// ie fix for progressbar
				if(Site.IE){
					div.css({
					'border' : '1px solid #2660B7'
					});
				}
				h3.appendTo(span);
				div.appendTo(span);					
		}
		//
	};

	// -------------> method 5: create Void bar <-------------------------------
	Site.Poll.prototype.createVoteBar = function(){
		var a = 0;
		var titles = Site.polldata.title_progressbars;
		var voting = this.voting;
		var span = $('<span>');
			span.attr({
				id : 'bar_wrap'
			}).appendTo(this.target)

			
		for(a in titles){
			var button = $('<div>');
			var text_holder = $('<div>');

				// button
				button.attr({
					id : this.ButtonsClassName +'_'+ a
					
				}).addClass(this.ButtonsClassName).click(function (){
					voting(this); // execute
					Site.polldata.vote = 1; // set vote zo dat we weten er is gestemd
				});
				button.appendTo(span);
				// text
				text_holder.addClass(this.textClassName);
				text_holder.text(titles[a]);
				text_holder.appendTo(span);	
		}
		// clear div
		var clear = $('<div>');
			clear.css({
				'clear' : 'both'
			});
		    clear.appendTo(this.target);
	};
	
	// -------------> method 6: create Void bar <-------------------------------
	Site.Poll.prototype.voting = function(object){
		
		var progressbars = Site.polldata.progressbars;
		var titles = Site.polldata.title_progressbars;
		for(a in progressbars){
			
			if(object.id == object.className + '_' + a){
				$('div#'+object.className + '_' + a).css({ 
					'background-position' : '0px -25px'				
				}).attr({
					title : titles[a]
				});
			}else{
				$('div#'+object.className + '_' + a).css({
					'background-position' : '0px 0px'
				}).attr({
					title : ''
				});
			}
		}
	};

	// -------------> method 7: render progressbar poll <-------------------------------	
	Site.Poll.prototype.renderProgressbars = function(input){
		
		var renderpol = function(input){
			var totaal = 0;
			for(var a in Site.polldata.value){		
				totaal += Site.polldata.value[a];
			}
			result = Math.round(((input * 100)/ (totaal)));
			
			if(totaal == 0){
				return 0;
			}else{
				return result;
			}
		}
		var progressbars = Site.polldata.progressbars;
		var value = Site.polldata.value;
	
		
		// Progressbar
		for(var a in progressbars){
	
			$('div#' + progressbars[a]).progressbar({
					value:  renderpol(value[a])
			});
			$('span#progrs_'+ a).html(renderpol(value[a]) + '%');
			$('div#'+ progressbars[a]).progressbar('option', 'value', renderpol(value[a]));
		}		
		// 
		$('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);			
	};


	// -------------> method: 8 quote button and send <----------------=
	Site.Poll.prototype.executeQuote = function(){
		var a = 0;
		var submit = $('<input>');
		var button = this.ButtonsClassName;
		var titles = Site.polldata.title_progressbars;
	
		var determineView = this.determineView;
		var setData = this.setData;
		var propertys = this.propertys();	
		// create button
			submit.attr({
				id : 'sendquote',
				type:'button'			
			})
			.addClass('r-button')
			.val('stem nu')
			.appendTo(this.target).bind('click', function(){
				$(this).unbind('click');
				//console.log(Site.polldata.vote);
				if(Site.polldata.vote == 1) // als er dus is geklikt
					
						window.setTimeout(function(){
							for(var a in titles){
								var element = document.getElementById(button + '_' + a).title;
								if(element != '')					
									if(element == titles[a]){
										propertys.data = element+'='+ 1;
										propertys.render();
										setData(propertys); 	// execute setData
										return;
									}
							}
						}, 550);
			});
	};
	
	
	// -------------> method: 10 determine wen to show and hide <----------------=
	Site.Poll.prototype.determineView = function(bool){
		if(bool){
			$('span#progr_wrap').css({'display' : 'block'});
			$('span#bar_wrap').css({'display' : 'none'});		
			$('div#sendquote').css({'display' : 'none'});
		}
	};
	
	// -------------> method: 11 determine wen to show and hide <----------------=
	Site.Poll.prototype.setData = function(propertys){
		  var _self = this;
		 $.ajax({
		   dataType: 'json',
		   type: 'POST',
		   url:  propertys.request.set,
		   data: propertys.data,
		   beforeSend: function(){
		   	 	propertys.loader().open();
		   },	  		 
		   success: function(msg){
				propertys.loader().close();
					
			    Site.polldata.value = {
			    	1 :  msg[1],
			    	2 :  msg[2],
			    	3 :  msg[3]
			    }
		
			   // check ip adress
			   propertys.determineView(msg.ip);			   
			   propertys.render();
		   },
		    error: function(){
		    	propertys.loader().close();
		    }
		 });	
	};
	
	// -------------> get data <-------------------------------
	Site.Poll.prototype.loadData = function(){	
		
		var _self = this;
		this.loader().open();	
			
		$.post(this.request.get, function(resons){
			_self.loader().close();		
			
			   $('#quote').html(resons.stelling);
			   _self.determineView(resons.ip);
			   if(resons.ip == false){
			   	 $('#bar_wrap, #sendquote').fadeIn(900);
			   }
				Site.polldata.value = {
			    	1 :  resons[1],
			    	2 :  resons[2],
			    	3 :  resons[3]
			    }			   			   
			   _self.renderProgressbars();			
		}, 'json');
	};
		
	// -------------> method: 12 array set for setData <-------------------------------
	Site.Poll.prototype.propertys = function(){
		return {
			determineView	: this.determineView,
			request  		: this.request,
			data			: this.data,
			render 			: this.renderProgressbars,
			img_loader		: this.img_loader,
			icon			: this.icon,
			loader			: this.loader
		 }
	};
	
	Site.Poll.prototype.loader = function(){
		var _img_loader = this.img_loader,
			_icon = this.icon;		
		return {
			open : function(){
		   	   $('.iconPoll').attr({
		   	   		src : _img_loader,
		   	   		id	: 'quoteTLoader'
		   	   });				
			},
			close : function(){
				   $('.iconPoll').attr({
			   		src : _icon,
			   		id	: 'quoteTIcon'
			   });			
			}
		};
	};
	
	// -------------> method: 13 execute <-------------------------------	
	Site.Poll.prototype.execute = function(){
		
			if($('#poll').length > 0){
				this.createTitle();
				this.createQuote();
				this.createProgressbars();
				this.createVoteBar();
				this.renderProgressbars();
				this.executeQuote();
				this.loadData();
			}		
	};
})(jQuery);	
