/*****************************************************************************************************/
/*                                                                                                   */
/*                                        'POLLS PANEL' CLASS                                        */          
/*                                                                                                   */
/*****************************************************************************************************/

function POLLS(){
	
	var JSObject = this;
	
	this.type = "polls"; 
	this.arr_inputs = [];
						
	this.form;
	this.DOMDoc;
	
	this.send_btn;
	this.results_btn;
	
	this.sendingValues = false;

	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION INIT                                                */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	
	this.init = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                          INFORMATION                                              */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		
		this.send_btn = 	$('#'+this.type+'_send_btn',this.DOMDoc).get(0);
		this.results_btn = 	$('#'+this.type+'_results_btn',this.DOMDoc).get(0);
		
		this.form = this.DOMDoc.getElementById(this.type+'_id').form;
		$("#"+this.form.id,this.DOMDoc).bind("submit",function(){return false;});

		this.initCreate();
		this.enableButton(this.send_btn);
		this.enableButton(this.results_btn);

	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                  FUNCTION CREATE PANEL                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.initCreate = function(){
		
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                     INPUT RADIO 'ANSWERS' ACTIONS                                  */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var $Answers = $('input[name="'+this.type+'_answer"]',this.DOMDoc);
		this.arr_inputs.push($Answers);
		$Answers.inputvalidate({
				required: 			"yes",
				valid:				false,
				errorParams:		{
										container: $("#"+JSObject.type+"_answer_container",JSObject.DOMDoc).get(0),
										textClass: 'error_text_poll'
									},
				errors:  			["Please select an answer."],
				extraParams:		{
										value: null
									},
				DOMDoc: 			JSObject.DOMDoc
		});
		
		for (var i=0; i<$Answers.get().length; i++){
			
			$($Answers.get(i)).click(function(){
				$Answers.HideError();
				$Answers.SetOptions({ valid: true, extraParams: {value : this.value} });
			});
			
			if ($Answers.get(i).checked){
				$Answers.SetOptions({ valid: true, extraParams: {value : $Answers.get(i).value} });
			}
		}
		
				
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    INPUT BUTTON 'SEND' ACTIONS                                    */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		var send_mouseDown = false;
		$(this.send_btn).mousedown(function(){
										
										if (JSObject.sendValues == true) return;
										
										send_mouseDown = true;
										$(JSObject.DOMDoc.body).mouseup(function(){
																		 
											 if (JSObject.sendValues == true) return;
											 
											 if (send_mouseDown == true){
												send_mouseDown = false;
												$(JSObject.send_btn).unbind("mouseleave");
												$(JSObject.DOMDoc.body).unbind("mouseup");
												JSObject.sendValues = true;
												JSObject.validate();
											 }
										})
									});
		
		$(this.send_btn).mouseup(function(){
										if (JSObject.sendValues == true) return;
										
										if (send_mouseDown == true){
											send_mouseDown = false;
											$(this).unbind("mouseleave");
											$(JSObject.DOMDoc.body).unbind("mouseup");
											JSObject.sendValues = true;
											JSObject.validate();
										}
									});
									
									
		/*****************************************************************************************************/
		/*                                                                                                   */
		/*                                    IMG BUTTON 'CLOSE' ACTIONS                                     */          
		/*                                                                                                   */
		/*****************************************************************************************************/
		$(this.results_btn).bind("click", function(){
													document.location.href = JSInterface.localpath + 'polls/poll/results';
												});									
		
		
		
	}
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION ENABLE BUTTON                                            */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.enableButton = function(btn){
		JSObject.sendValues = false;
		
		$(btn).css('cursor','pointer');
		$(btn).animate({opacity:1},100);
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION DISABLE BUTTON                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.disableButton = function(btn){
		JSObject.sendValues = true;
		
		$(btn).animate({opacity:0.4},100);
		$(btn).css('cursor','default');
	}

	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                 FUNCTION VALIDATE INFORMATION                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.validate = function(){
		
		var countErrors = 0;
				
		// y coordinates of error inputs
		var arr_errorsYCoord = [];
		
		// find how many errors are in the form
		for (var i=0; i<this.arr_inputs.length; i++){
			
			var $input = this.arr_inputs[i];
			var options = $input.GetOptions();
			
			// requirement field error
			if (($input.val().length == 0 || (options.extraParams.value == null && typeof options.extraParams.value == 'object')) && options.required == "yes"){
				$input.ShowError(0);
				countErrors++;
			}
			// error which is already displayed on screen
			else if (options.valid == false){ 
				countErrors++;
			}
			
			// save y coordinate of the error input
			if ($input.GetOptions().valid == false){
				arr_errorsYCoord.push($input.offset().top);	
			}
		}
		
		// if there are no errors from syntax point of view, then send data
		if (countErrors == 0){
			JSObject.sendData();
		}
		//move container(div) scroll to the first error
		else{		
			JSObject.sendValues = false;
		}
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SUBMIT FORM                                         */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.submitForm = function(){
		
		return JSInterface.AjaxUpload.dosubmit(JSObject.form, {'onStart' : JSObject.startUploadingData, 'onComplete' : JSObject.completeUploadingData});
		
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                      FUNCTION SEND DATA                                           */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.sendData = function(){
		
		$("#"+this.form.id,this.DOMDoc).unbind("submit");
		$("#"+this.form.id,this.DOMDoc).bind("submit",function(){JSObject.submitForm();});
		$("#"+this.form.id,this.DOMDoc).submit();
		
		JSObject.disableButton(JSObject.send_btn);
		JSObject.disableButton(JSObject.results_btn);
	}
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                FUNCTION START UPLOADING DATA                                      */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.startUploadingData = function(){
		// make something useful before submit (onStart)
		
		//add loader
		/*var msg = 'Sending data...';
		JSInterface.Preloader.start(msg,450,120);*/
		
		//disable form elements
		setTimeout(function(){
						var aElems = JSObject.form.elements;
						nElems = aElems.length;
						
						for (j=0; j<nElems; j++) {
							aElems[j].disabled = true;
						}
					},300);
		
		$('#'+JSObject.type+'_container',JSObject.DOMDoc).animate({opacity:0.4},300);
		
		return true;
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                              FUNCTION COMPLETE UPLOADING DATA                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.completeUploadingData = function(responseJSON){
		
		if ((typeof(responseJSON) != "object" && typeof(responseJSON) != "string") || responseJSON == "") return;
							
		 var JSON = eval ("("+responseJSON+")");
		
		/* $(JSObject.comment_form_container).css("display","none");
		 $(JSObject.comment_form_container).html(unescape(JSON.html));*/
							 
		// alert(JSON);
		
		$("#"+JSObject.form.id,JSObject.DOMDoc).unbind("submit");
		$("#"+JSObject.form.id,JSObject.DOMDoc).bind("submit",function(){return false;});
		
		// remove preloader
		// JSInterface.Preloader.remove(100);
		
		response = Boolean(Number(String(JSON.valid)));
		
		if (response == true){
			
			// show message
			var message = 'Thank you for voting.';
			JSInterface.Loader.display({message: message});
			
			// write html in container
			$('#'+JSObject.type+'_container',JSObject.DOMDoc).html(unescape(JSON.html));
		}
		else{
			
			// show message
			var message = 'There was an error. Please try again in few seconds.';
			JSInterface.Loader.display({message: message});
			
			//enable form elements
			setTimeout(function(){
							var aElems = JSObject.form.elements;
							nElems = aElems.length;
							for (j=0; j<nElems; j++) {
								aElems[j].disabled = false;
							}
						},300);
			
			//enable buttons
			JSObject.enableButton(JSObject.send_btn);
			JSObject.enableButton(JSObject.results_btn);
		}
		
		$('#'+JSObject.type+'_container',JSObject.DOMDoc).animate({opacity:1},300);
	}

}
