// define RemoteVote object, if not already defined
var loadingText = 'Loading...';
var RemoteVote = window.RemoteVote || {
	// url to call when poll is initially loaded
	pollUrl: 'http://www.yopoll.com/display/showpoll.aspx?pollid=',
	
	// url to call when answer is clicked
	voteUrl: 'http://www.yopoll.com/display/processpoll.aspx?answerId=',

	cookie_name: 'RemoteVoteGUID',
	
	// timestamp parameter to add to each request (to avoid caching)
	timestamp: '&time=' + new Date().getTime(),

	polls: {},
	questions: {},
	load_texts: {},

	// gets a javascript url in a script tag
	get: function(url) {
		var script = document.createElement('script');
		
		// add GUID parameter, if there is a cookie
		var guid_cookie = this.check_guid();
			
		if (guid_cookie)
			url += '&guid=' + guid_cookie;

		script.src = url + this.timestamp;
		document.body.appendChild(script);
	},

	// adds poll with a certain id
	add: function(poll) {
		this.polls[poll] = document.createElement('div');
		document.body.appendChild(this.polls[poll]);
		this.get(this.pollUrl + poll);
	},
	
	// changes html for poll
	show: function(q, html) {
		if (this.questions[q]){
			this.loading(q, loadingText);
			this.questions[q].innerHTML = (html + "<br />" || '');
		}
	},
	
	// adds poll and attaches click-to-vote
	poll: function(poll, q, html, strGuid) {
		if (!this.questions[q]) {
			//CJG - commented out to make poll appear where we want it to
			this.questions[q] = document.createElement('div');
			//CJG -make poll appear where we want it to
			//this.questions[q] = document.getElementById('yopoll' + q);
			
			//CJG - commented out to make poll appear where we want it to
			//this.polls[poll].appendChild(this.questions[q]);
			document.getElementById('yopoll').appendChild(this.questions[q]);
			
		}
		this.show(q, html);
		//CJG
		this.guid(strGuid);
		
		var answers = this.questions[q].getElementsByTagName('input');
		for (var i=0;i < answers.length;i++) {
			answers[i].onclick = function() {
				RemoteVote.vote(q, this.value);
			};
		}
		
	},
	
	// sets loading text, between voting and result
	loading: function(q, text) {
		this.load_texts[q] = text;
	},

	// sends vote for answer
	vote: function (q, a) {
		this.show(q, this.load_texts[q]);
		this.get(this.voteUrl + a);
	},

	guid: function (id) {
		// set cookie
		var date = new Date();
		date.setTime(date.getTime()+(10*365*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
    	document.cookie = this.cookie_name+"="+id+expires+"; path=/";
    },
    
    check_guid: function () {
    	var nameEQ = this.cookie_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 false;
    }
};

// add poll to page if pollId is defined
if (window.pollId) RemoteVote.add(pollId);
