//pagenation
var base_url	= location.protocol+"//"+location.hostname+"/";

(function($) {
    $.fn.pagenation = function(url, defer) {
    	/*var _settings = {
       		url:'',
       		content_id:0,
       		post_type:'',
       		limit:8,
       		order:'unique_id DESC',
       		publish_type:'publish'
       	};
       	for (var i in settings) {
       		_settings[i] = settings[i];
       	}
       	var geturl = url+"/"+content_id+"/"+post_type+"/"+limit+"/"+offset+"/"+order+"/"+publish_type*/
       	
    	return this.each(function() {
    		var $$		= $(this);
    		
    		function pageNext(offset) {
    			$.get(url+"/"+offset,
    				function(res){
    					var obj = $.evalJSON(res);
    					$$.children(".pagenation_list").each(function() {
    						$(this).html(obj["list"]);
    					});
    					$$.children(".pagenation").each(function() {
    						$(this).html(obj["pagenation"]);
    					});
    					$$.find(".page_num_box").click(function() {
    	    				var offset = parseInt($(this).attr('name'));
    	    				pageNext(offset);
    	    			});
    				}
    			);
			}
    		function init() {
    			$$.find(".page_num_box").click(function() {
    				var offset = parseInt($(this).attr('name'));
    				pageNext(offset);
    			});
    			if(defer) {
    				$.get(url+"/0",
    					function(res){
        					var obj = $.evalJSON(res);
        					$$.children(".pagenation_list").each(function() {
        						$(this).html(obj["list"]);
        					});
        					$$.children(".pagenation").each(function() {
        						$(this).html(obj["pagenation"]);
        					});
        				}
    				);
    			}
    		}
    		init();
    	});
    };
}
)(jQuery);

(function($) {
    $.fn.poll = function() {
       	
    	return this.each(function() {
    		var $$		= $(this);
    		
    		function doPoll(unique_id) {
    			$.get("/internal/poll/"+unique_id,
    				function(res){
    					var obj = $.evalJSON(res);
    					//$("#poll").find(".poll_msg").text(obj["msg"]);
    					if(obj["pollability"]) {
    						var point = parseInt($("#poll_status").find(".poll_point").text());
    						$("#poll_status").find(".poll_point").text(point+1);
    						$$.html("<img src='/img/buttons/poll3.png' width='180' height='60' alt='投票しました' title='投票しました' />");
    					} else {
    						$$.html(obj["msg"]);
    					}
    				}
    			);
    		}
    		function init() {
    			$$.html("<img id='poll_button' src='/img/buttons/poll1.png' width='180' height='60' alt='投票する' title='投票する' />");
    			$("#poll_button").mouseover(function() {
    				$("#poll_button").attr("src", "/img/buttons/poll2.png");
    			});
    			$("#poll_button").mouseout(function() {
    				$("#poll_button").attr("src", "/img/buttons/poll1.png");
    			});
    			$("#poll_button").click(function() {
    				doPoll(unique_id);
    			});
    		}
    		init();
    	});
    };
}
)(jQuery);

(function($) {
    $.fn.memberPoll = function() {
       	
    	return this.each(function() {
    		var $$		= $(this);
    		
    		function doPoll(unique_id) {
    			$.get("/internal/member_poll/"+unique_id,
    				function(res){
    					var obj = $.evalJSON(res);
    					if(obj["pollability"]) {
    						var point = parseInt($("#poll_status").find(".poll_point").text());
    						$("#poll_status").find(".poll_point").text(point+1);
    						$$.html("<img src='/img/buttons/poll3.png' width='180' height='60' alt='投票しました' title='投票しました' />");
    					} else {
    						$$.html(obj["msg"]);
    					}
    				}
    			);
    		}
    		function init() {
    			$$.html("<img id='poll_button' src='/img/buttons/poll1.png' width='180' height='60' alt='投票する' title='投票する' />");
    			$("#poll_button").mouseover(function() {
    				$("#poll_button").attr("src", "/img/buttons/poll2.png");
    			});
    			$("#poll_button").mouseout(function() {
    				$("#poll_button").attr("src", "/img/buttons/poll1.png");
    			});
    			$("#poll_button").click(function() {
    				doPoll(unique_id);
    			});
    		}
    		init();
    	});
    };
}
)(jQuery);

(function($) {
    $.fn.comment = function() {
       	
    	return this.each(function() {
    		var $$		= $(this);
    		
    		function commentRequest() {
    			var name	= $("#comment :input[name='name']").val();
    			var comment	= $("#comment :input[name='comment']").val();
    			if(name == "" || comment == "") {
    				alert("なまえとコメントは必須です。");
    				return false;
    			}
    		}
    		function commentResponse(res) {
    			var obj = $.evalJSON(res);
    			if(obj["is_error"]) {
    				alert("コメント投稿でエラーが発生しました。");
    			} else {
    				$$.children(".pagenation_list").each(function() {
    					$(this).append(obj["html"]);
    				});
    				$("#comment_form :input[name='comment']").val('');
    			}
    		}
    		function init() {
    			var comment_options = {
    				beforeSubmit:	commentRequest,
    				success:		commentResponse,
    				semantic:		true
    			};
    			$('#comment_form').ajaxForm(comment_options);
    		}
    		init();
    	});
    };
}
)(jQuery);

/*function getScrollPosition() {
	return (document.documentElement.scrollTop || document.body.scrollTop);   
}*/
