/**
 * AUDI JavaScript library: Survey related functions
 * 
 * @projectDescription	
 * @namespace			
 *
 * @author 				$Author: hhoettecke $
 * @version				$Revision: 6416 $
 * @copyright			NEUE DIGITALE GmbH, Berlin
 * 
 * @jslint: 
 * 
 * @file:				audi.survey.js
 * $URL: https://svn.pvtool.org/svn/day_audi_ngw/trunk/ngw_base/frontend/js/audi/audi.survey.js $
 */

/* create namespace */
audi_ngw.namespace(audi_ngw, 'survey');
audi_ngw.survey.surveyDomElementSelector = '.audi_survey';



audi_ngw.survey.check = function($scope) {

	if( true === audi_ngw.config.surveyDisable ) {
		return;
	}

	// if the visitor participated in a survey, return
	if( null !== jQuery.cookie(audi_ngw.config.surveySessionCookieName) ) {
		return;
	}

	// if we don't have a scope, we are working with the whole body
	if( !$scope ) {
		$scope = jQuery('body');
	}

	// look for the survey element somewhere in the dom...
	var $survey = jQuery(audi_ngw.survey.surveyDomElementSelector, $scope);

	// if there is no survey-element, return
	if( 0 === $survey.size() ) {
		return;
	}  

	// get the metadata from the element...
	var surveyData = $survey.metadata();


	// if there is no data for a survey, return
	if( !surveyData.survey ) {
		return;
	}

	// iterate through all surveys
	$surveys = jQuery.each(surveyData.survey, function(){
		var survey = this;
		
		// we need at least a name and a url for the survey
		if( !survey.name || !survey.url ) {
			return true;
		}
	
		
		// check if the visitor participated in this special survey in the past year (cookie based)
		if( null !== jQuery.cookie(audi_ngw.config.surveyCookiePrefix + survey.name) ) {
			return;
		}
		
		// if we have start and end date for the survey, we need to check it
		if( survey.start && survey.end ) {
			survey.start = new Date(survey.start);
			survey.end = new Date(survey.end);
			var now = new Date();
			
			// if the start-date is "bigger" than the current date
			// or if the end-date is "smaller" than the current date
			// we skip this survey
			if( survey.start > now || survey.end < now ) {
				return true;
			}
			
		}
		
		// if there is no special frequency, we use the standard one
		if( !survey.frequency ) {
			survey.frequency = audi_ngw.config.surveyFrequency;
		}
		

		var tmpFrequency = Math.random();


		if( tmpFrequency < survey.frequency ) {

			// open the popup with the survey
			audi_ngw.navigation.popup(survey.url, survey.popup);
			
			// set a survey-cookie and a session-cookie
			jQuery.cookie(audi_ngw.config.surveyCookiePrefix + survey.name, 'true', {expires: 365, path: '/'});
			jQuery.cookie(audi_ngw.config.surveySessionCookieName, 'true');			
			
			// return false so we get out of the each and no other survey is called
			return false;			
		}
		
		
	});

	return;

};


audi_ngw.survey.onAjaxModalContentReady = function(event, xhr) {
	
	// get the scope
	var $scope = jQuery('#'+audi_ngw.url.getContentId());

	// call the survey-function with the scope
	audi_ngw.survey.check($scope);
	
	// trigger another event 
	audi_ngw.event.trigger('ajaxModalContentPrepared');
};


audi_ngw.survey.onAjaxSnippetReady = function(event, data) {
	
	// get the scope
	var $scope = jQuery(data.sId);
	
	// call the survey-function with the scope
	audi_ngw.survey.check($scope);
	
	// trigger event 
	audi_ngw.event.trigger('ajaxSnippetPrepared',[event,data]);
};


audi_ngw.survey.initiate = function() {
	
	if( true === audi_ngw.config.surveyDisable ) {
		return;
	}
	
	// bind a function to the event, firing when a modayLayer has been loaded
	audi_ngw.event.bind('ajaxModalContentReady.survey',audi_ngw.survey.onAjaxModalContentReady);
	audi_ngw.event.bind('ajaxSnippetReady',audi_ngw.survey.onAjaxSnippetReady);
	
	
	audi_ngw.survey.check();
		
};
