﻿/*

	Title: Almega Arbetsgivaruiden custom Survey promo
	Author: Daytona

	Depends: jquery 1.2.6

  @name ArbSurvey
  @function
  @description Creates, injects survey widget with cookie handling

  @param {Object} Options
  		@param {array} [settings.urls='array']
 				An array of URLs that the survey should point to. Required.
			@param {int} [settings.delay=1500]
 				The delay before we show the survey.
			@param {boolean} [settings.animate=true]
 				Wether or not to animate the survey when it shows.
			@param {int} [settings.animSpeed=800]
 				The animation speed in milliseconds.
			@param {string} [settings.posX=50px]
 				The position of the survey promo relative to the left of the window.
			@param {object} [settings.cookie.name=arb_survey_rejected]
 				Name of the cookie that stores the user choice (of hiding the promo)
			@param {object} [settings.cookie.expires=100]
 				Expiration in days for the cookie.


	@return {dataType}
 				An Survey object with exposed methods.


*/
(function($){

	if( typeof jQuery === 'function' ) {

		var ArbSurvey = function(){

			var survey;
			this.element = null;

			// Configurable options
			ArbSurvey.prototype.options = {
				urls: new Array(),
				delay: 1500,
				animSpeed: 800,
				animate: true,
				posX: '50px',
				message: null,
				title: null,
				cookie: {
					name: 'survey_rejected',
					expires: 100
				}
			}

			ArbSurvey.prototype.init = function(options){

					// safe global scope
					survey = this;

					// use options from init
					survey.options = $.extend(survey.options, options);

					// halt if there's no survey urls loaded OR the survey is rejected
					if( !survey.options.urls.length || survey.readCookie( survey.options.cookie.name ) ) return;

					survey.element = new survey.widget(options.message, options.title);
					survey.inject();

					// close button and answer "No thanks" hides and sets cookie
					survey.element.find('.survey_close, .choice-no').click(function(event){
						event.stopPropagation();
						survey.hide(true);
						survey.createCookie( survey.options.cookie.name, true, survey.options.cookie.expires);
						return false;
					});
					survey.element.find('.choice-yes').click(function(event){
						event.stopPropagation();
						survey.hide(true);
						survey.createCookie( survey.options.cookie.name, true, survey.options.cookie.expires);
					});
			}

			ArbSurvey.prototype.widget = function(message, title){

				// create survey widget element
				var $survey_widget = $('<div />')
										.attr({
											'class': 'arb_survey',
											id: 'arb_survey-' + Math.random().toString(16).slice(2, 10)
										}),
					$survey_content_wrapper = $('<div class="arb_survey_content"></div>'),
					$closeBtn = $('<a href="#" class="survey_close" title="Nej tack"></a>'),
					$survey_content;

				if (message && title) {
					$survey_content = '<div class="survey_head">' + title + '</div>';
					$survey_content += message;

				} else {
					$survey_content = '<div class="survey_head">Hej!</div>'
										+ '<p>Vill du tjuvtitta på nya almega.se och svara på några frågor om designen? Det tar ca fyra minuter.</p>'
										+ '<p>Delar av undersökningen är på engelska.</p>';
				}
				$survey_content += '<p class="choices"><a href="' + Survey.getSurveyURL() + '" class="choice-yes">Ja, jag hjälper gärna till</a> <a href="#" class="choice-no">Nej, tack</a></p>';

				// set horisontal position
				$survey_widget.css({
					left: survey.options.posX
				});

				// build widget
				$survey_content_wrapper
					.append($survey_content);
				$survey_widget
					.append($survey_content_wrapper)
					.append($closeBtn);

				return $survey_widget;
			}

			ArbSurvey.prototype.inject = function(){

				if( !this.element ) return;

				$(survey.element)
					.hide()
					.appendTo('body');

				survey.show();
			}

			ArbSurvey.prototype.getSurveyURL = function(){

				var url;

				// If we should pick a random survey url
				if( survey.options.urls.length > 1 ) {

						var survey_count = survey.options.urls.length;
						var randomNum = Math.round(Math.random() * (survey_count - 1));

						return survey.options.urls[randomNum];
				}
				else {
					url = survey.options.urls[0];
				}

				return url;
			}

			ArbSurvey.prototype.getExternalOptions = function(){


			}

			/* Show / Hide  */

			ArbSurvey.prototype.hide = function(destroy){

					if( survey.options.animate )
						$(survey.element).slideUp( survey.options.animSpeed , function(){ if( destroy ) $(survey.element).remove(); });
					else
						$(survey.element).hide(0, function(){ if( destroy ) $(survey.element).remove(); });



			}
			ArbSurvey.prototype.show = function(){

				setTimeout(function(){

						if( survey.options.animate )
							$(survey.element).slideDown( survey.options.animSpeed );
						else
							$(survey.element).show(0);

				}, survey.options.delay);

			}


			/* Cookie handling */
			/* thanks PPK */
			ArbSurvey.prototype.createCookie = function(name,value,days) {
				if (days) {
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				}
				else var expires = "";
				document.cookie = name+"="+value+expires+"; path=/";
			}
			ArbSurvey.prototype.readCookie = function(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
				}
				return null;
			}
			ArbSurvey.prototype.eraseCookie = function(name) {
				createCookie(name,"",-1);
			}


		};
		var Survey = new ArbSurvey();

		$(document).ready(function () {

			var thisURL = document.location.href;
			var config = {
				delay: 2000,
				animate: true,
				title: "Hej!",
				message: "<p>När du är klar med ditt ärende vore vi glada om du ville hjälpa oss svara på ett några frågor om vår webbplats. <br><br>Det tar bara ett par minuter.</p>",
				posX: $('.c-MainArea').offset().left + ($('.c-MainArea').width() - 310),
				cookie: {
					name: 'attitude_survey_rejected'
				},
				urls: [
					// temp URLs to test random
					thisURL + '?survey=1',
					thisURL + '?survey=2',
					thisURL + '?survey=3',
					thisURL + '?survey=4',
					thisURL + '?survey=5',
					thisURL + '?survey=6',
					thisURL + '?survey=7'
				]
			};

			// Get URL options from data-attribute if present
			var $scriptTag = $('script[src*=survey.js]');
			var urls = $scriptTag.is('[data-urls]') ? {urls: $scriptTag.attr('data-urls').split(',')} : {};

			// Init Survey app
			Survey.init($.extend(config, urls));
		});
	}

})(jQuery);
