(function($){
	
	$.extend({
		CcResearch: function(self,mode,data){
			var vote_url = '/research/';
			var result_url = '/research/result/';
			
			var cookie_key = 'research_posted_status';
			var cookie_param = { expires: 30 };
			var cookie_data = $.cookie(cookie_key);
			if( cookie_data ){
				var posted_id = cookie_data.split(':')[0];
				var posted_status = cookie_data.split(':')[1];
			}
			
			var research_title = self.find('#question');
			var form = self.find('form');
			var button_area = self.find('#btn');
			var vote_item_list = self.find('#answer');
			
			var vote_wrapper_tag = '<li>';
			
			switch(mode){
				default:
				case 'init':
					self.css({
						position: 'relative'
					});
					vote_item_list.empty();
					self.CcResearch('action_vote');
					break;
				
				case 'loading':
					// 特別
//					var oneclickvote_title = $('.oneclickvote .title');
					if(data){
//						oneclickvote_title.text('ワンクリック投票（読み込み中）');
						self.find(':input').attr('disabled','disabled');
					}
					else{
//						oneclickvote_title.text('ワンクリック投票');
					}
					break;
				
				case 'set_form':
					form.attr(data.attributes);
					form.children(':hidden').remove();
					form.prepend(data.hiddens);
					break;
				
				case 'set_vote':
					vote_item_list.animate({height:"hide",opacity:"hide"},function(){
						vote_item_list.empty();
						$.each(data,function(i,val){
							var vote_title = $(vote_wrapper_tag).html(val.vote_html).addClass('item_name');
							vote_item_list.append(vote_title);
						});
						vote_item_list.animate({height:"show",opacity:"show"});
					});
					break;
				
				case 'set_result':
					vote_item_list.animate({height:"hide",opacity:"hide"},'fast',function(){
						vote_item_list.empty();
						$.each(data,function(i,val){
							var vote_title = $(vote_wrapper_tag).html(val.vote_title).addClass('item_name item_'+val.vote_id);
							var vote_ratio = $('<span>').html('('+val.vote_ratio+'%)').addClass('item_ratio');
							vote_title.append(vote_ratio);
							var graph_bar = $('<div>').css('width',val.vote_max_ratio+'%');
							var vote_graph = $('<li>').addClass('item_graph item_'+val.vote_id).append(graph_bar);
							vote_item_list.append(vote_title,vote_graph);
						});
						vote_item_list.animate({height:"show",opacity:"show"},'fast');
					});
					break;
				
				case 'set_button':
					button_area.empty();
					$.each(data, function(i, val){
						var button = $(val);
						switch (i) {
							case 'submit':
								button.click(function(){
									self.CcResearch('action_submit');
									return false;
								});
								break;
							case 'result':
								button.click(function(){
									self.CcResearch('action_result');
									return false;
								});
								break;
							case 'back':
								button.click(function(){
									self.CcResearch('action_vote');
									return false;
								});
								break;
						}
						button_area.append(button);
					});
					break;
				
				case 'set_thaks':
					button_area.empty();
					var button = $('<span>').html('ご投票ありがとうございました');
					button_area.append(button);
					break;
				
				case 'action_vote':
					$.ajax({
						type: 'GET',
						url: vote_url,
						dataType: 'json',
						beforeSend: function(){
							self.CcResearch('loading',true);
							return true;
						},
						complete: function(){
							setTimeout(function(){
								self.CcResearch('loading',false);
							},250)
						},	
						success: function(data,dataType){
							if( posted_id == data.research_id ){
								self.CcResearch('action_result');
							}
							else{
								if(data.research_body.search(/\.(jpg|jpeg|gif|png)$/)!=-1){
									research_title.html($('<img>').attr({
										'src': data.research_body,
										'alt': data.research_title,
										'title': data.research_title
									}));
								}
								else{
									research_title.text(data.research_title);
								}
								self.CcResearch('set_form',data.form);
								self.CcResearch('set_vote',data.vote_item);
								self.CcResearch('set_button',data.button);
							}
						}
					});
					break;
				
				case 'action_submit':
					$.ajax({
						type: 'POST',
						url: vote_url,
						dataType: 'json',
						data: form.find(':input').serialize(),
						beforeSend: function(){
							var values = {}
							$.each(form.find(':input').serializeArray(),function(i,val){values[val.name]=val.value;});
							if(!values.vote){
								alert("項目を選択してください。");
								return false;
							}
							self.CcResearch('loading',true);
							return true;
						},
						complete: function(){
							setTimeout(function(){
								self.CcResearch('loading',false);
							},250)
						},
						success: function(data,dataType){
							$.cookie(cookie_key,data.research_id+':'+data.research_item_id,cookie_param);
							self.CcResearch('action_result');
						}
					});
					break;
				
				case 'action_result':
					$.ajax({
						type: 'GET',
						url: result_url,
						dataType: 'json',
						beforeSend: function(){
							self.CcResearch('loading',true);
							return true;
						},
						complete: function(){
							setTimeout(function(){
								self.CcResearch('loading',false);
							},250)
						},
						success: function(data,dataType){
							if(data.research_body.search(/\.(jpg|jpeg|gif|png)$/)!=-1){
								research_title.html($('<img>').attr({
									'src': data.research_body,
									'alt': data.research_title,
									'title': data.research_title
								}));
							}
							else{
								research_title.text(data.research_title);
							}
							self.CcResearch('set_result',data.vote_item);
							if(posted_id == data.research_id){
								self.CcResearch('set_thaks');
								setTimeout(function(){
									var posted = self.find('.item_'+posted_status).addClass('posted_item');
									posted.animate({opacity: '0.5'},'fast');
									posted.animate({opacity: '1.0'},'fast');
									posted.animate({opacity: '0.5'},'fast');
									posted.animate({opacity: '1.0'},'fast');
									posted.animate({opacity: '0.5'},'fast');
									posted.animate({opacity: '1.0'},'fast');
								},500)
							}
							else{
								self.CcResearch('set_button',data.button);
							}
						}
					});
					break;
			}
		}
	});
	
	$.fn.extend({
		CcResearch: function(mode,data){
			$.each(this,function(i,val){
				$.CcResearch($(val),mode,data);
			});
		}
	});
	
})(jQuery);
