var DOMAIN;
var PATH = '/';
var cookVote = 'nikeos.userVote', cookPoll = 'nikeos.pollId'; 
var uniqueID = Math.round(Math.random()*(360*60));
var pollID = 0;
var date = new Date();
date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));

var addedHeight = (JQ.browser.msie && JQ.browser.version < 7) ? 0 : 8;

function checkPollCookies(){
	if (JQ.cookie(cookPoll) == pollID && JQ.cookie(cookVote) > 0) return false;
	return true;
}

function processResponse(response) {
	var self = this;
	var index = 0;
	var $pollInfo = JQ('.poll-text');
	this.setPollCookie = function(response) {
		try {
			tracking.click(track_site + '>blog>poll-vote');
	        JQ.cookie(cookVote, uniqueID, { path: PATH, domain: DOMAIN, expires: date });
	        JQ.cookie(cookPoll, pollID, { path: PATH, domain: DOMAIN, expires: date });
			self.fadeOutPoll();
			//self.showResults();
		} catch (e) {
	 		console.log('type error: ', e);
	 		alert("Something went wrong and there is no actual error.");
		}
	};
	this.fadeOutPoll = function() {
		JQ('#latest-poll').find('span.radio').css('visibility','hidden');
		JQ('#latest-poll').fadeOut('slow', self.showResults);
//		JQ('#latest-poll').fadeOut('fast', self.grow);
	};
	this.showResults = function() {
		JQ('#latest-poll-results').css({
			'display' : 'block'
		});
		var newHeight = (JQ('#latest-poll-results').position().top - JQ('#right_polls_top').position().top) + JQ('#latest-poll-results').height() + addedHeight;
		JQ('#latest-poll-results').parent().css({
			'height' : newHeight.toCSS()
		});
	    JQ('#latest-poll-results').fadeIn('fast',self.grow);

//	    JQ('#latest-poll-results').fadeIn('fast',self.fadeOutPoll);
	};
	self.grow = function() {
		var choice = JQ('input.poll-option:checked').attr('value').toInt();
		var total = JQ($pollInfo[0]).attr('class').match(/total-(\S*)/)[1].toInt() + 1;
		if (index == $pollInfo.length) {
			return false;
		} else {
			var pollBar = '<div class="poll-bar" style="width: 0;"></div>';
			var $update = JQ('#latest-poll-result-' + index);
			$update.find('div.poll-bar-wrap').append(pollBar);
		    var count = $update.find('div.poll-text').attr('class').match(/count-(\S*)/)[1].toInt();
		    if (choice == index) count++;
			perc = (count/total).toPercent();

NIKEOS.log('count: %i, index: %i, perc: ', count,index,perc);
			$update.find('span.poll-percentage').html(perc + '%');
			$update.find('div.poll-bar').animate({
				width: perc + '%'
			}, 500, 'easeOutQuad',self.grow);
			index++;
		};
	};

	self.setPollCookie();	
}

JQ(document).ready(function() {
	var clicker = new clickTrack();
	clicker.popup(['.pop_footer']);
	clicker.track(['.footer_link']);

	if (JQ('#sort_by_author').size() > 0) {
		var query = location.search.substring(1);
		var pairs = query.split('&');
		var togglePopTagsLinks = new toggleTags();
		togglePopTagsLinks.toggler();
/*
		if (location.hash) {
			var hashy = location.hash;
			if (hashy == JQ('#by_author')) {
				togglePopTagsLinks.toggleClass('sort_' + hashy);
			}
		}
*/
		if (query.search(/correspondents/) > -1) {
			console.log("yes it is")
		}
	}
	
	pollID = JQ('#poll-poll-id').attr('value');
/*	
	JQ('.more-link').click(function() {
		tracking.click(track_site + '>blog>post>' + JQ(this).attr('title').replace(/^\s*(.*?)\s*$/, '$1'));
	});
	JQ('#featured_article h2 a, .recent-post h2 a').click(function() {
		tracking.click(track_site + '>blog>post>' + JQ(this).text().replace(/^\s*(.*?)\s*$/, '$1'));
	});
	JQ('#popular-tags-inner-wrap li a').click(function() {
		tracking.click(track_site + '>blog>tag>' + JQ(this).text().replace(/^\s*(.*?)\s*$/, '$1'));
	});
	JQ('#recent-posts-inner-wrap li a').click(function() {
		tracking.click(track_site + '>blog>recent-posts>' + JQ(this).text().replace(/^\s*(.*?)\s*$/, '$1'));
	});
*/
	JQ('a.more').add('a.less').click(function(){
		var $this = JQ(this);
		var $div = JQ('#lesstags');
		$div.toggle();
		
		if ($div.is(':visible')) $this.attr('title', text_less).attr('class', 'less').html(text_less + '&nbsp;<img src="' + img_less + '" />');
		else $this.attr('title', text_more).attr('class', 'more').html(text_more + '&nbsp;<img src="' + img_more + '" />');
			
		return false;
	});
	if(checkPollCookies()) {
		var clicked = false;
		JQ('div.poll-bar-wrap').empty();
		JQ('span.poll-percentage').empty();
		JQ('#button-vote').click(function() {
			NIKEOS.log('%a is the value of clicked', clicked);
			if (clicked) return false;
			if (JQ('input.poll-option:checked').length > 0) {
				clicked = true;
				var soapBody = new SOAPObject("addAnonymousVote");
				soapBody.ns = "http://jivesoftware.com/webservices";
	
				soapBody.appendChild(new SOAPObject('pollID')).val(pollID);
				soapBody.appendChild(new SOAPObject('index')).val(JQ('input.poll-option:checked').attr('value'));
				soapBody.appendChild(new SOAPObject('uniqueID')).val(uniqueID);
	
				var sr = new SOAPRequest('/rpc/soap/PollService', soapBody);

				SOAPClient.SOAPServer = '/rpc/soap/PollService';
//				JQ('#button-vote').fadeOut('fast');
				JQ('input.poll-option').disabled = true;
				SOAPClient.SendRequest(sr, processResponse); //Send request to server and assign callback function
				return false;
			} else {
				console.log('Please select an option before you vote');
				alert(poll_error);
				return false;
			}
		});
	} else {
        JQ('#latest-poll').css('display','none');
		JQ('#latest-poll-results').css({
			'display' : 'block'
		});
		var newHeight = (JQ('#latest-poll-results').position().top - JQ('#right_polls_top').position().top) + JQ('#latest-poll-results').height() + addedHeight;
		JQ('#latest-poll-results').parent().css({
			'height' : newHeight.toCSS()
		});
		NIKEOS.log('%a is the new height', newHeight);
	}
	
	
	
});

