var Poll = (function(){
	function constructor(){
		//this.pollURL = "http://usbdz1web801.baxter.com/galaxy/pollRecorder.jspa";//staging URL
		this.pollURL = "https://webforms.baxterbioscience.com/galaxy/pollRecorder.jspa";

		this.pollName = $('#survey-form input[name=pollName]').val();
		this.answer;
		this.results;
		this.total = 0;
		this.chart = [];
		this.cookie = hasCookie("voted");
		/*if(this.cookie){
			this.getResults();
			$('#survey-form input#'+this.cookie.substring(this.cookie.indexOf('=')+1)+'').attr('checked','checked');
			$('#survey').animate({backgroundPosition:"(0 0)",marginTop:"-195px",height:"269px"},{duration:1})
			//$('#survey form').fadeOut(1,(function(e){return function(){}})(this));
			this.displayActionItems();
		}*/
		$('#survey-form').submit((function(e){return function(){$(this).unbind('click');e.recordVote();return false;}})(this));
	}
	constructor.prototype.displayActionItems = function(){
		$('#survey-form').hide();
		//var label = $('#'+$('#survey-form input[name=answer]:checked').attr('id')+'-label').text();
		//$('#survey-results h1').append(label);
		if('Cufon' in window){
			Cufon.replace('#home-callouts h1',{fontFamily:'tfy2'});
		}
		$('#'+$('#survey-form input[name=answer]:checked').attr('id')+'-msg').show();
		$('#'+$('#survey-form input[name=answer]:checked').attr('id')+'-links').show();
		$('#survey-results').fadeIn(300,function(){
			if(document.getElementById('flash-home') && document.getElementById('flash-home').playAnimation){
				document.getElementById('flash-home').playAnimation();
			}
		});
	}
	
	
	function setCookie(name,value){
		var date = new Date();
		date.setTime(date.getTime()+(90*24*60*60*1000));
		var expires = date.toGMTString();
		document.cookie = name+"="+value+"; expires="+expires+"; path=/;";
	}
	
	function hasCookie(value){
		var cookies = document.cookie.split(';');
		for(var i=0;i < cookies.length;i++){
			var c = cookies[i];
			if(/voted=/.test(c)){
				return c;
			}
		}
		return false;//cookie is not set
	}
	
	constructor.prototype.getResults = function(){
		$(document).ajaxError(function(e, xhr, settings, exception) {
alert('error in: ' + settings.url + ' \\n'+'error:\\n' + exception);
}); 
		$.getJSON(this.pollURL+"?pollname="+this.pollName+"&callback=?",(function(e){return function(data){e.displayResults(data);}})(this));
	}
	
	constructor.prototype.displayResults = function(data){
		//display result data in bar form
		
		this.results = data;
		for(var k=0;k<this.results.length;k++){
			this.total += ~~this.results[k];
		}
		var ranking = this.results.slice(0);
		ranking.sort(function(a,b){return a - b});
		for(var v=0;v<this.results.length;v++){
			this.chart.push(Math.round((this.results[v] / this.total)*100));//percent
			$($('#results-graph li').get(v)).prepend('<div>'+this.chart[v]+'%</div>');
			$($('#results-graph li').get(v)).find('div').css("height",Math.round(20+this.chart[v]*1.4)+"px");
			//$($('#results-graph li').get(v)).find('div').css("margin-top",140-Math.round(this.chart[v]*1.4)+"px");
			/* multi-line poll labels */
			$($('#results-graph li').get(v)).find('div').css("margin-top",120-Math.round(this.chart[v]*1.4)+"px");
			
			$($('#results-graph li').get(v)).find('div').css("background-position",-40*$.inArray(this.results[v],ranking)+"px 20px");
		}
		if('Cufon' in window){
			Cufon.replace('#survey-results #results-graph',{fontFamily:'tfy2'});
			Cufon.replace('#survey-results #results-graph span',{fontFamily:'tfy'});
		}
		
	}
	
	constructor.prototype.recordVote = function(){
		
		//track vote in webtrends
		
		//setCookie("voted",$('#survey-form input[name=answer]:checked').attr('id'));
		
		
		
		this.answer = $('#survey-form input[name=answer]:checked').val();
		if(this.answer != null){//radio button clicked
		
			if(document.getElementById('flash-home') && document.getElementById('flash-home').pauseAnimation){
				document.getElementById('flash-home').pauseAnimation();
			}
			if('dcsMultiTrack' in window){
				dcsMultiTrack('DCS.dcssip',document.location.hostname,'DCS.dcsuri',document.location.pathname,'DCS.dcsqry',"?answer="+this.answer+"&pollname="+this.pollName+"&callback=?",'WT.ti',this.pollName,'WT.dl','26');
			}
			$('#survey').animate({backgroundPosition:"(0 0)",marginTop:"-195px",height:"269px"},{duration:600})
			
			$('#survey form').fadeOut(600,(function(e){return function(){e.displayActionItems();}})(this));
			$.getJSON(this.pollURL+"?answer="+this.answer+"&pollname="+this.pollName+"&callback=?",(function(e){return function(data){e.displayResults(data);}})(this));
			//this.displayResults([86,72,53]); //fake results
		}
	}

	return constructor;
})();





/**
 * @author Alexander Farkas
 * v. 1.21
 */
(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);
