/**
* create a query string by object
*/
function ykk_qs(params) {
	var i = 0;
	var qs = '';
	for(var p in params) {
		if(i > 0) qs += '&';
		// -----

		qs += p + '=' + encodeURIComponent(params[p]);
		// -----

		i++;
		// -----
	}
	
	return qs;
	// -----
}/**
* asset javascript
*/
function ykk_script(id, url, params) {
	try {
		var script = document.createElement('script');
		script.setAttribute('type', 'text/javascript');
		script.setAttribute('src', url + '?' + ykk_qs(params));
		script.setAttribute('id', id);
		// -----

		var head = document.getElementsByTagName('head').item(0);
		head.appendChild(script);
		// -----

	} catch(e) { /*alert('exception alert: ' + e)*/ }
	// -----
}

/**
* clear asset javascript by id
*/
function ykk_script_clear(id) {
	try {
		if(id != undefined) {
			var script = document.getElementsByTagName('script');
			// -----

			for(var i in script) {
				if(script[i].id == id) {
					var head = document.getElementsByTagName('head').item(0);
					head.removeChild(script[i]);
					// -----
				}
			}
		}
	} catch(e) { /*alert('exception alert: ' + e)*/ }
	// -----
}

/**
* asset css
*/
function ykk_css(source) {
	try {
		var css = document.createElement('link');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('media', 'screen');
		css.setAttribute('type', 'text/css');
		css.setAttribute('href', source);
		// -----

		var head = document.getElementsByTagName('head').item(0);
		head.appendChild(css);
		// -----
	} catch(e) { /*alert('exception alert: ' + e)*/ }
	// -----
}

/**
* YouKuki Service Class
*
* author: Francesco Mantello
*
*/
function YouKuki(ykk_url, ykk_uid, ykk_format, ykk_template, ykk_ip, r) {
	this.uid = ykk_uid;
	this.format = ykk_format;
	this.template = ykk_template;
	// -----
	
	this.rnd = r;
	// -----
	
	// this.tu = null;
	this.ref = window.location;
	// -----
		
	this.json = null;
	// -----

	// url service
	this.url = {
		survey: ykk_url + '/service/survey.php',
		response: ykk_url + '/service/response.php',
		view: ykk_url + '/service/view.php'
	};

	var params = {
		fase: 'view__initialize',
		uid: this.uid, // token survey
		format: this.format, // format survey
		template: this.template, // format template
		ref: this.ref, // window.location
		rnd: this.rnd
	};

	ykk_css('http://multimedia.youkuki.com/css/youkuki.php');
	ykk_script('initialize' + this.rnd, this.url.view, params);
	// -----
};

YouKuki.prototype = {};
// -----

YouKuki.prototype.survey = function() {
	var id = 'youkuki' + this.uid;
	// -----

	if(document && document.getElementById(id)) {
		var div = document.getElementById(id);
		// -----

		div.innerHTML = this.json.html;
		// -----
	}
};

YouKuki.prototype.results = function() {
	var id = 'youkuki' + this.uid;
	// -----

	if(document && document.getElementById(id)) {
		var div = document.getElementById(id);
		// -----

		div.innerHTML = this.json.html;
		// -----
	}
};

YouKuki.prototype.vota = function(id, type) {
	var response = '';
	// -----

	switch(type) {
		case 1:
		case 2: {
			for (i=0;i<document.forms['question' + id + this.rnd].elements['response'].length;i++){
				if (document.forms['question' + id + this.rnd].elements['response'][i].checked==true) {
					response += document.forms['question' + id + this.rnd].elements['response'][i].value + ';';
					// -----
				}
			}
		}break;

		case 3: {
			response = document.getElementById('rispostaAperta').value;
			// -----
		}break;
	}

	if(response == '') {
		alert('Devi inserire correttamente una risposta!');
		// -----

		return false;
		// -----
	}

	var params = {
		fase: 'response__vota',
		format: this.format,
		template: this.template,
		s: this.json.survey.id,
		h: this.json.site.id,
		c: this.json.cookie.id,
		q: id,
		t: type,
		r: response,
		rnd: this.rnd
	};

	ykk_script_clear('vota' + this.rnd);
	ykk_script('vota' + this.rnd, this.url.response, params);
	// -----
};

YouKuki.prototype.render = function(view) {
	var params = {
		fase: 'view__render',
		format: this.format,
		template: this.template,
		survey: this.json.survey.id,
		question: this.json.selected_question,
		view: view,
		tag: this.tag_filter,
		rnd: this.rnd
	};

	ykk_script_clear('view' + this.rnd);
	ykk_script('view' + this.rnd, this.url.view, params);
	// -----
};

YouKuki.prototype.navigate = function(selected_question) {
	this.tag_filter = '';
	// -----

	var params = {
		fase: 'view__navigate',
		format: this.format,
		template: this.template,
		survey: this.json.survey.id,
		question: selected_question,
		rnd: this.rnd
	};

	ykk_script_clear('view' + this.rnd);
	ykk_script('view' + this.rnd, this.url.view, params);
	// -----
};

YouKuki.prototype.cloud = function(tag) {
	this.tag_filter = tag;
	// -----

	this.render('open');
	// -----
};

// todo: non so cosa, ma qualcosa sicuro...
YouKuki.prototype.csv = function(survey) {
	var p = '?fase=survey__csv';
	p += '&id=' + survey;
	// -----

	var w = window.open(this.url.survey + p, 'youkuki__' + survey + '__csv', 'width=100,height=100,top=0,left=0,toolbar=0,location=0,directories=0,status=0');
	w.focus();
	// -----
};

var ykk_url_4b711aecd5e1d = 'http://service.youkuki.com';
var ykk_uid_4b711aecd5e1d = '4a072a3a6c5a041f30ed69ee199b14d6';
var ykk_format_4b711aecd5e1d = 1;
var ykk_template_4b711aecd5e1d = 1;
var ykk_ip_4b711aecd5e1d = '38.107.191.84';
// -----

var ykk_4b711aecd5e1d = new YouKuki(ykk_url_4b711aecd5e1d, ykk_uid_4b711aecd5e1d, ykk_format_4b711aecd5e1d, ykk_template_4b711aecd5e1d, ykk_ip_4b711aecd5e1d, '4b711aecd5e1d');
// -----

