/*! $Id: experienceCommentHandler.min.js 8482 2010-02-16 14:27:10Z kschaefer $ */
/*
 * com.spark-launch-ms.experienceCommentHandler
 * Copyright © 2010	Ignition Groupe	GmbH. All rights reserved.
 * http://www.ignitiongroupe.com/
 *
 */

/*global window:false, $:false, jQuery:false, com:true, commentsBox: false, Recaptcha: false */
//noinspection BadExpressionStatementJS
"use strict";

// Introduce namespace
if ("undefined" === typeof(com)) {
	var com;
}
com = com || {};
com.sparklaunchms = com.sparklaunchms || {};

// Prototype definition
/**
 * This	prototype provides functionality to validate and send comments
 * on the Spark Experience page.
 *
 * @author	BHillebrand <Bernd.Hillebrand@de.ignitiongroupe.com>
 * @author	K. Schaefer <karsten.schaefer@de.ignitiongroupe.com>
 * @version	1.0.0
 */
com.sparklaunchms.experienceCommentHandler = com.sparklaunchms.experienceCommentHandler || (function () {
	// var experienceCommentHandler;
	//==========================================================================
	// Private static fields

	//==========================================================================
	// Private static methods

	//==========================================================================
	// Instance	constructor
	/**
	 * experienceCommentHandler prototype definition and constructor for creating data	objects
	 * that	describe a hot spot
	 *
	 * @constructor
	 * @return {experienceCommentHandler}
	 */
	//noinspection NestedFunctionJS,FunctionNamingConventionJS
	function experienceCommentHandler() {
		return this;
	}

	//==========================================================================
	// Static fields

	//==========================================================================
	// Static methods

	//==========================================================================
	// Instance	fields

	/**
	 * The array that stores all hot spot data
	 * that	has	been added via the static method ModelGalleryColorizer.add
	 *
	 * @field {Boolean}
	 */
	//experienceCommentHandler.prototype.isLoggedIn = null;

	//==========================================================================
	// Instance	methods
	/**
	 * Provide more	info about the class then just [object Object]
	 * but actually	tell the user what prototype this object is	made of
	 *
	 * @return {String}	The	identifier
	 */
	//noinspection AnonymousFunctionJS,NestedFunctionJS
	experienceCommentHandler.prototype.toString = function	() {
		return "[object	com.sparklaunchms.experienceCommentHandler]";
	};

	/**
	 * "Media-Box"
	 * @author	K. Schaefer <karsten.schaefer@de.ignitiongroupe.com>
	 * @prototype
	 * @init
	 * @return jQuery
	 */
	experienceCommentHandler.prototype.commentsBox = {
		htmlElement: undefined,
		commentsSource: undefined,
		init: function () {
			var copyCommentElement, commentElement, to = this;
			jQuery.ajax({
				url: this.commentsSource,
				type: "GET",
				dataType: "json",
				success: function (result) {
					jQuery(to.htmlElement).addClass("activity_status_loading");
					commentElement = jQuery(".commentList .commentItem", to.htmlElement);
					jQuery(to.htmlElement).find(".commentList").find("div.commentItem").remove();
					jQuery.each(result.comments, function (i, item) {
						var linkElement, loopAuthorWebAddress = item.authorWebAddress || '';
						loopAuthorWebAddress = (0 === loopAuthorWebAddress.indexOf('http://')) ? loopAuthorWebAddress.slice(7) : loopAuthorWebAddress;
						copyCommentElement = jQuery(commentElement).clone();
						if (0 === i % 2) {
							jQuery(copyCommentElement).addClass("oddJS");
						} else {
							jQuery(copyCommentElement).addClass("evenJS");
						}
						jQuery(".commentCopy", copyCommentElement).append(item.commentText);
						jQuery(".commentName", copyCommentElement).append(item.authorName);
						jQuery(".commentDate", copyCommentElement).append(new Date(item.commentDate).toLocaleDateString() + "<br />" + new Date(item.commentDate).toLocaleTimeString());
						if ('' !== loopAuthorWebAddress) {
							linkElement = jQuery("<a href='http://" + loopAuthorWebAddress + "'/>");
							jQuery(".commentLink", copyCommentElement).append(linkElement);
							jQuery(".commentLink a", copyCommentElement).append(loopAuthorWebAddress);
						}
						jQuery(to.htmlElement).find(".commentList").append(copyCommentElement);
					});
					jQuery(".commentList", to.htmlElement).css("display", "block");
					jQuery(".commentsHeadline h3 .number", to.htmlElement).text("" + jQuery(result.comments).length);
					jQuery(to.htmlElement).removeClass("activity_status_loading");
				},
				error: function () {
					$(".commentErrorBox .errorJson").addClass("formError");
					jQuery(to.htmlElement).removeClass("activity_status_loading");
				}
			});
			jQuery(document).ready(function () {
				jQuery(to.htmlElement).removeClass("activity_status_loading");
			});
			return this;
		}
	};

	/**
	 * validates Fields
	 * @prototype
	 */
	//noinspection AnonymousFunctionJS,NestedFunctionJS
	experienceCommentHandler.prototype.validateFields = function (name) {
		var isValid = true,
			authorName = $("form input[name='authorName']").val().trim() || "",
			authorEmail = $("form input[name='authorEmail']").val().trim() || "",
			authorWeb = $("form input[name='authorWeb']").val().trim() || "",
			commentText = $("form textarea[name='commentText']").val().trim() || "",
			emailFilter = /^[a-zA-Z0-9!#$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?\^_`{|}~\-]+)*@(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?\.)+([a-zA-Z0-9]{2,})+$/,
			minLengthField = 4,	//const
			minLengthAuthorWebField = 12; //const

		//init
		$("form .formError").removeClass("formError");
		$(".commentErrorBox *").removeClass("formError");

		//trim
		$("form input[name='authorName']").val(authorName);
		$("form input[name='authorEmail']").val(authorEmail);
		$("form textarea[name='commentText']").val(commentText);

		if (minLengthField > authorName.length) {
			isValid = false;
			$("form input[name='authorName']").parent().addClass("formError");
			$(".commentErrorBox .errorAuthorName").addClass("formError");
		}

		if (minLengthField > authorEmail.length || !emailFilter.test(authorEmail)) {
			isValid = false;
			$("form input[name='authorEmail']").parent().addClass("formError");
			$(".commentErrorBox .errorAuthorEmail").addClass("formError");
		}

		if (minLengthField > commentText.length) {
			isValid = false;
			$("form textarea[name='commentText']").parent().addClass("formError");
			$(".commentErrorBox .errorCommentText").addClass("formError");
		}
		if ("http://" === authorWeb) {
			authorWeb = "";
		}
		$("form input[name='authorWeb']").val(authorWeb);

		if (0 !== authorWeb.length  && (minLengthAuthorWebField > authorWeb.length || 0 !== authorWeb.indexOf('http://'))) {
			isValid = false;
			$("form input[name='authorWeb']").parent().addClass("formError");
			$(".commentErrorBox .errorAuthorWeb").addClass("formError");
		}

		return isValid;
	};

	/**
	 * validates Captcha
	 * @prototype
	 */
	//noinspection AnonymousFunctionJS,NestedFunctionJS
	experienceCommentHandler.prototype.validateCaptcha = function () {
		var recaptcha_challenge_field = $("form input#recaptcha_challenge_field").val() || "",
				recaptcha_response_field = $("form input#recaptcha_response_field").val() || "",
				fdata,
				//url = "http://prodapp4.community.chevroleteurope.com/delivery-service/";
				url = "http://community.chevroleteurope.com/delivery-service/";

		fdata = "type=captchaValidation&recaptcha_response_field=" + recaptcha_response_field +
			"&recaptcha_challenge_field=" + recaptcha_challenge_field + "&jsoncallback=?";

		$.getJSON(url, fdata, function (json) {
			if ("success" === json.result) {
				$("form").eq(0).submit();
			} else {
				$(".recaptcha_input_area").addClass("formError");
				$(".commentErrorBox .errorRecaptcha").addClass("formError");
				Recaptcha.reload();
			}
		});
	};

	/**
	 * send sendFormmData
	 * @prototype
	 */
	//noinspection AnonymousFunctionJS,NestedFunctionJS
	experienceCommentHandler.prototype.sendFormData = function () {
		if (experienceCommentHandler.prototype.validateFields()) {
			experienceCommentHandler.prototype.validateCaptcha();
		}
	};

	
	/**
	 * init
	 * @prototype
	 * @init
	 * @return jQuery
	 */
	//noinspection AnonymousFunctionJS,NestedFunctionJS
	experienceCommentHandler.prototype.init = function () {
		//alert('init');
		var url = window.location.protocol + "//" + window.location.host + window.location.pathname,
			result = document.location.search.match(/(result)=([\w]*)&?/i); // string[]

		$("form input[name='successUrl']").val(url + "?result=success#addComment");
		$("form input[name='failUrl']").val(url + "?result=error#addComment");

		if (result && "error" === result[2]) {
			$(".commentErrorBox .errorJson").addClass("formError");
		} else if (result && "success" === result[2]) {
			$(".commentSuccessBox .errorJson").addClass("formError");
		}

		experienceCommentHandler.prototype.commentsBox.init();
		return this;
	};

	//==========================================================================
	// Return prototype
	return experienceCommentHandler;
}());

//==============================================================================
// Usage
