sfn.controllers.SurveyController = function(surveyView) {

	var instance = this;

	this.init = function() {
		instance.bindEvents();
		commonController.addEvent(surveyView.submitSurveyButton(), "click",
				instance.submitSurvey);
		surveyView.surveyToolTip().tooltip({
			showURL : false
		});

		commonController.addEvent(surveyView.submitAndEnroll(), "click",
				instance.createFacebookConnectUrlAndSubmitSurvey);

	};

	this.bindEvents = function() {

		surveyView.surveyLinks().each(function(index, eachButton) {
			$(eachButton).bind("click", function() {
				var surveyId = $(eachButton).attr("id").replace("survey", "");
				instance.loadSurvey(surveyId);
			});
		});

	};

	this.loadSurvey = function(id) {
		var callObject = commonController.createCallObject("GET", appRoot
				+ "/pazarArastirmalari/surveyDetails", function(data) {
			surveyView.surveyDetail().html(data);
			commonController.addEvent(surveyView.submitSurveyButton(), "click",
					instance.submitSurvey);

		}, {
			surveyId : id
		});
		ajaxService.execute(callObject);
	};

	this.submitSurvey = function() {
		var $inputs = surveyView.surveyRadioAnswers();

		var values = instance.getSurveyValues();
		
		if(values == "notValid"){
			return;
		}

		var callObject = commonController.createCallObject("POST", appRoot
				+ "/pazarArastirmalari/submitSurvey", instance.onSurveySubmit,
				{
					values : values + surveyView.currentSurvey().val()
				});

		ajaxService.execute(callObject);

	};

	this.getSurveyValues = function() {

		var valid = false;

		var values = "";

		surveyView.surveyQuestions().each(
				function(index, eachQuestion) {
					var questionId = $(eachQuestion).attr("id").replace(
							"question", "");
					var answerType = surveyView.getItemValue("answerType"
							+ questionId);

					if (answerType == "RADIOBOX") {

						var value = surveyView.getQuestionAnswer(
								"radio" + questionId).val();
						if (value == undefined) {
							values= "notValid";
							surveyView.getErrorLabel("error"+questionId).show();
						}
					} else if (answerType == "CHECKBOX") {
						var value = surveyView.getQuestionAnswer(
								"checkbox" + questionId).val();
						if (value == undefined) {
							values= "notValid";
							surveyView.getErrorLabel("error"+questionId).show();
						}
					}

				});

		if(values == "noteValid"){
			return values;	
		}
		
		surveyView.surveyRadioAnswers().each(function() {
			values = values + $(this).val() + "_";
		});

		surveyView.surveyCheckboxAnswers().each(function() {
			values = values + $(this).val() + "_";
		});
		return values;
	};

	this.onSurveySubmit = function(data) {
		if (data) {
			surveyView
					.surveyContent()
					.html(
							"Anket sonuçları 31 Ocak 2012 tarinde Senin Fikrin Ne? platformunda");
			var callObject = commonController.createCallObject("GET", appRoot
					+ "/social/shareOnFacebook", function(data) {
				commonController.showDialog(400, "Tesekkurler", data);

			}, {
				survey : surveyView.currentSurvey().val()
			});

			ajaxService.execute(callObject);

		}
	};
	this.shareOnFacebook = function() {
		var callObject = commonController.createCallObject("POST", appRoot
				+ "/social/postToFacebookWall", function(data) {
		}, {
			survey : surveyView.currentSurvey().val()
		});
		ajaxService.execute(callObject);
	};

	this.createFacebookConnectUrlAndSubmitSurvey = function() {
		var values = instance.getSurveyValues();
		if(values == "notValid"){
			return;
		}
		var callObject = commonController.createCallObject("GET", appRoot
				+ "/social/createFacebookConnectUrl", function(data) {
			// alert(data);
			commonController.redirect(data);
		}, {
			values : values + surveyView.currentSurvey().val()
		});
		ajaxService.execute(callObject);

	};

};

sfn.views.SurveyView = function() {

	this.surveyLinks = function() {
		return $(".surveyLink");
	};

	this.surveyDetail = function() {
		return $("#surveyDetail");
	};

	this.surveyForm = function() {
		return $("#surveyForm");
	};

	this.surveyQuestions = function() {
		return $(".question");
	};

	this.surveyCheckBoxes = function() {
		return $(".checkbox");
	};

	this.surveyRadioAnswers = function() {
		return $("#surveyForm :radio:checked");
	};

	this.surveyCheckboxAnswers = function() {
		return $("#surveyForm :checkbox:checked");
	};

	this.submitSurveyButton = function() {
		return $("#submitSurveyButton");
	};
	this.currentSurvey = function() {
		return $("#currentSurvey");
	};
	this.shareOnFacebook = function() {
		return $("#shareOnFacebook");
	};
	this.surveyToolTip = function() {
		return $("#surveyToolTip *");
	};
	this.surveyContent = function() {
		return $("#surveyContent");
	};
	this.submitAndEnroll = function() {
		return $("#submitAndEnroll");
	};
	this.getItemValue = function(id) {
		return $("#" + id).val();
	};
	this.getQuestionAnswer = function(id) {
		return $("#" + id + ":checked");
	};
	this.getErrorLabel = function(id){
		return $("#"+ id);
	};
};
