/**
*   poll functionality
*
*/

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}


$(document).ready(function() {
	//$('div:regex(id, p[0-9]{17})').each(function() {
		$('div:regex(id, p[0-9]{16})').each(function() {
		var pollId = String($(this).attr('id')).substring(1);		
		var cookie = $.cookie("dawn_frontpoll_v2");

		if (!cookie) {
			$('#p' + pollId).show();
		}
		
		else {
	
			jpoll_vote(pollId, "");
		}
	});
});


function poll_setvalue (pollId, pollOptionId) {
    var e = $('#v' + pollId);

    /* Set the value if we can find the element */
    if (e) {    
        e.val(pollOptionId);
    }
}

 
function jpoll_vote(pollId, pollOption) {
 
    var polls = 'utils/polls_vote.asp';
    var loc = location.href;
    
        
                
    var dev = String(loc).match(/^.*?\/WORKAREA\/.*?\//);
    var stag = String(loc).match(/^.*?\/STAGING\//);
    var live_study = String(loc).match(/^.*?www.dawnstudy.com\//);
    var live_mind = String(loc).match(/^.*?www.dawnmind.com\//);
    var live_youth = String(loc).match(/^.*?www.dawnyouth.com\//);
    
    var live_study_2 = String(loc).match(/^.*?dawnstudy.com\//);
    var live_mind_2 = String(loc).match(/^.*?dawnmind.com\//);
    var live_youth_2 = String(loc).match(/^.*?dawnyouth.com\//);
    
    /*var test = String(loc).match(/^.*?\/test.dawnstudy.com\//);*/
    
    /*var live_study = String(loc).match(/^(http:\/\/){0,1}(www\.){0,1}dawnstudy\.com(\/|$)/);
    var live_mind = String(loc).match(/^(http:\/\/){0,1}(www\.){0,1}dawnmind\.com(\/|$)/);
    var live_youth = String(loc).match(/^(http:\/\/){0,1}(www\.){0,1}dawnyouth\.com(\/|$)/);*/
    
    var test = String(loc).match(/^.*?test.dawnstudy.com\//);
    
    /* Figure out what system we're on */
    if (dev) {
        loc = dev + polls;
      
    }
    else if (live_study) {
        loc = live_study + polls;
       
    }        
    else if (live_mind) {
        loc = live_mind + polls;
       
    }        
    else if (live_youth) {
        loc = live_youth + polls;
       
    }        
    
    
    else if (live_study_2) {
        loc = live_study_2 + polls;
       
    }        
    else if (live_mind_2) {
        loc = live_mind_2 + polls;
       
    }        
    else if (live_youth_2) {
        loc = live_youth_2 + polls;
       
    }        
    
    
    
    else if(test) {
        loc = test + polls;
       
    }        
    else if(stag) {
        loc = stag + polls;
      
    }   
    else {
    loc = test + polls;
    }     
    

    $.post(loc, {'vote_poll_id': pollId, 'vote_option_id' : pollOption}, function(data) {     
		$.each(data.Records, function(i, item) {
			var optionId = item.OptionId;
			var pct = item.pct;
			
			var rightColumnWithInPixels = 110;
			var pixelsWide = Math.round((rightColumnWithInPixels * pct) / 100);

			$('#porp' + optionId).html(pct + '%');
			$('#pori' + optionId).css({'width' : pixelsWide, 'display': 'block', 'background-color': 'rgb(0, 148, 234)'});				
		});
		
		$('#p'+ pollId).css({'display': 'none'});
		$('#pr'+ pollId).css({'display': 'block'});
        //$('#p' + pollId).hide();			
        //$('#pr' + pollId).show();
        
		$.cookie("dawn_frontpoll_v2", pollId, { path: '/', expires: 365});
    }, "json");               
}

function poll_vote(pollId) {
    var e = $('#v' + pollId);
    var pollOption = e.val();  
                   
    if (pollOption != '') {
		jpoll_vote(pollId, pollOption);		
    }
}
