/*
	Scripts for Barometer Plugin
	[ Requires jQuery ]

*/

jQuery(document).ready(function(){
	
	jQuery("#randLink").fadeIn();

	var pid = jQuery("#absPID").val();
	var cookID = "barometerID-" + pid;
	var barAS = jQuery.cookie(cookID);
	
	if(barAS && barAS == "1") {
	
		jQuery('#asBarSelect').remove();
		jQuery('#asBarVoted').fadeIn();	
	
	} else {
	
		jQuery('.asBarBlock').css('cursor', 'pointer');
	
		jQuery('select#absMood').change( function() {
		
			// Store variabels of post id and which selection they chose
		    var pid = jQuery("#absPID").val();
			var val = jQuery(this).val();
			
			jQuery.cookie("barometerID-" + pid, "1", { expires: 30 });
			
			// Set Opacity for effect
			jQuery('.asBarBlock').css('opacity', '0.4');
			jQuery('.asBarBlock').css('filter', 'alpha(opacity = 40)');
			
			// Run it all through Ajax
			jQuery.ajax({
				type: 'POST',
				url: templateDir + '/wp-content/plugins/Barometer/includes/rBarPost.php',
				data: 'val=' + val + '&pid=' + pid,
				
				success: function(ret) {
		            if(ret == 'true'){
		            
		            	jQuery('#asBarSelect').remove();
		            	jQuery('#asBarVote').fadeIn();
		            	
		            	
	    				setTimeout('window.location.reload()', 3000 );
		            	
		            } else {
		            	// Could do something here... but not gonna
		            }
				}
			})
			return false;
		});
		
		
		
		jQuery('div.asBarBlock').click( function() {
		
			// Store variabels of post id and which selection they chose
		    var pid = jQuery("#absPID").val();
			var val = jQuery(this).attr('id');
			
			jQuery.cookie("barometerID-" + pid, "1", { expires: 30 });
			
			// Set Opacity for effect
			jQuery('.asBarBlock').css('opacity', '0.4');
			jQuery('.asBarBlock').css('filter', 'alpha(opacity = 40)');
			
			// Run it all through Ajax
			jQuery.ajax({
				type: 'POST',
				url: templateDir + '/wp-content/plugins/Barometer/includes/rBarPost.php',
				data: 'val=' + val + '&pid=' + pid,
				
				success: function(ret) {
		            if(ret == 'true'){
		            
		            	jQuery('#asBarSelect').remove();
		            	jQuery('#asBarVote').fadeIn();
		            	
		            	
	    				setTimeout('window.location.reload()', 3000 );
		            	
		            } else {
		            	// Could do something here... but not gonna
		            }
				}
			})
			return false;
		});			
		
			
	
	}	
	
});


jQuery.cookie=function(key,value,options){if(arguments.length>1&&(value===null||typeof value!=="object")){options=jQuery.extend({},options);if(value===null){options.expires=-1;}
if(typeof options.expires==='number'){var days=options.expires,t=options.expires=new Date();t.setDate(t.getDate()+days);}
return(document.cookie=[encodeURIComponent(key),'=',options.raw?String(value):encodeURIComponent(String(value)),options.expires?'; expires='+options.expires.toUTCString():'',options.path?'; path='+options.path:'',options.domain?'; domain='+options.domain:'',options.secure?'; secure':''].join(''));}
options=value||{};var result,decode=options.raw?function(s){return s;}:decodeURIComponent;return(result=new RegExp('(?:^|; )'+encodeURIComponent(key)+'=([^;]*)').exec(document.cookie))?decode(result[1]):null;};
