$(function () {
        $('ul.drawers').accordion({
            header: 'H4.drawer-handle',
            selectedClass: 'open',
            event: 'click',
            autoHeight : false
             });
             
        
});

$(document).ready(function(){
	if($(".image_paging").length)
		$(".image_paging").pager('fieldset', {
	  	prevText: '',
	  	nextText: '',
	  	navClass: 'nav_link2',
	  	linkText: ['1', '2', '3', '4', '5', '6', '7']
	  	
	});
});

$(document).ready(function(){
	if($(".paging_hr").length)
		$(".paging_hr").pager('span', {
	  	prevText: '',
	  	nextText: '',
	  	navClass: 'nav_link2',
	  	linkText: ['1', '2', '3', '4', '5', '6', '7'],
	  	titleText : "หน้าที่"
	});
});

$(function() {
		
		var name = $("#name"),
			comment_input = $("#comment_input"),
			secret_code = $("#secret_code"),
			allFields = $([]).add(name).add(comment_input).add(secret_code),
			tips = $("#validateTips"),
			error_msg = $(".error_msg"),
			userid = $("input[name=userid]"),
			content_id = $("input[name=content_id]");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("จำนวนตัวอักษรของ " + n + " ต้องอยู่ะหว่าง "+min+" และ "+max+" ตัวอักษร");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
		
			width: 560,
			modal: true,
			buttons: {
				'ส่งความคิดเห็น': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					//bValid = bValid && checkLength(name,"username",1,16);
					bValid = bValid && checkLength(comment_input,"comment_input",1,1000);
					//bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
										
					if (bValid) {
						var option = {comment_input:comment_input.val(),userid:userid.val(),content_id:content_id.val(),secret_code:secret_code.val(),method:"saveComment"};
						$.ajax({
	 						type: "POST",
		 					url: "/SiteManagement/coldboxproxy.cfc",
		 					data: option,
		 					dataType:"json",            
		 					success: function(response){
								if (response) {
									window.location.reload();
								} else {
									
									error_msg.text("คำตอบไม่ถูกต้อง");
								}
							}
						})
					}
				},
				ปิดหน้านี้: function() {
					if ($f()) {
						$f().show().resume();
					}	
					$(this).dialog('close');
					
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		
		
		$('#create-user').click(function() {
			if ($f()) {
				$f().hide().pause();
			}	
			$('#dialog').dialog('open');
			
		});

$.ajaxSetup ({  
         cache: false  
    });


	});

function call_comment (contentId,page,limit){
	var mylimit = parseInt(limit)+1;
	
	var option = {method:"getComment",contentId:contentId,limit:mylimit,offset:page*(limit)};
	var strLI = "";
	var next = parseInt(page)+1;
	var prev = parseInt(page)-1;
	
	$.ajax({
	 	type: "POST",
		 url: "/SiteManagement/coldboxproxy.cfc",
		 data: option,
		 dataType:"json",            
		 success: function(response){
		 		var plusPage = parseInt((response.COUNTALL)%(mylimit-1));
		 		if(plusPage != 0)
		 			plusPage = 1;
		 		else
		 			plusPage = 0;
		 		var lastPage = parseInt((response.COUNTALL)/(mylimit-1));
		 		
		 		lastPage = lastPage + plusPage -1; 
		 		
		 		var loop = response.QUERY.DATA.length -1;
		 		if(page == lastPage)
		 			loop = loop+1;
		 	 	for (var i = 0; i < loop; i++) {
		 	 			 	 	
				var body = response.QUERY.DATA[i][ $.inArray("BODY",response.QUERY.COLUMNS) ];
				
				var date_post = response.QUERY.DATA[i][ $.inArray("CREATE_DATE",response.QUERY.COLUMNS) ];	
				strLI+= '<div class="aComment ui-widget ui-widget-content ui-corner-all">'+
							'<div class="aVatar">'+
								'<div class="th-imgUser">'+
									'<img src="/mcot/images/contentImg/avatar.jpg" />'+
								'</div>'+
							'<div class="avatarPublic"> anonymous </div>'+
						'</div>'+
						'<div class="commentContent"><div class="commentText">'+ body +'</div>'+
						'<div class="commentStatusBar">'+
						'<div class="commentDateTime">' + date_post + '</div>'+
						'<div class="clearbothcomment"/> </div>' +
						'</div>'+
						
						'</div>'; 
				
			}
			
			strLI+= '<div style="clear:left"></div><div class="navig_comment">';
			
			strLI+= 'หน้าที่ ' + parseInt(page+1)+ ' จาก '+ (lastPage+1);
			if(page>0){
			 	strLI+='<a href="javascript:call_comment('+contentId+',0,'+ limit +')"><img src="/mcot/images/contentImg/back3.gif"></a> ';	
			} else {
				strLI+='<img src="/mcot/images/contentImg/back4.gif"> ';
			}
			if(prev >= 0){
				strLI+='<a href="javascript:call_comment('+contentId+','+ prev +','+ limit +')"><img src="/mcot/images/contentImg/back1.gif"></a> ';
			} else {
				strLI+='<img src="/mcot/images/contentImg/back2.gif"> ';
			}
			if(response.QUERY.DATA.length == (parseInt(limit)+1)){
			 	strLI+='<a href="javascript:call_comment('+contentId+','+ next +','+ limit +')"><img src="/mcot/images/contentImg/next1.gif"></a> ';	
			} else {
				strLI+='<img src="/mcot/images/contentImg/next2.gif">';
			}			
			
			if(page < lastPage){
			 	strLI+='<a href="javascript:call_comment('+contentId+','+ lastPage +','+ limit +')"><img src="/mcot/images/contentImg/next3.gif"></a> ';	
			} else {
				strLI+='<img src="/mcot/images/contentImg/next4.gif">';
			}			
			strLI+= '</div>';
			
			$("#create-user").focus();
			
			
			$(".comment_detail").html(strLI);
			/* $("#comment_detail").append(strLI); */
	 	}
	 	
	});		
}

function increaseFont() {
			jQuery('.paging_hr').css('font-size',(parseFloat(jQuery('.paging_hr').css('font-size'))+1)+'px'); 
			jQuery.cookie("ctf", jQuery('.paging_hr').css('font-size'), { path: '/', expires: 10 });
		}
		function decreaseFont() {
			jQuery('.paging_hr').css('font-size',(parseFloat(jQuery('.paging_hr').css('font-size'))-1)+'px'); 	
			jQuery.cookie("ctf", jQuery('.paging_hr').css('font-size'), { path: '/', expires: 10 });
		}
		
$(document).ready(function(){
   $(".mod-head620").hover(function(){
   		
       	$(this).addClass("mod-head620-hover");
       	$(this).removeClass("mod-head620");

   }, function(){
		$(this).addClass("mod-head620");
       	$(this).removeClass("mod-head620-hover");	
       });	
       	
       	

  $("#create-user").hover(function(){
   	 $(this).css("background-position","128px 0px");
	},function(){
 	 $(this).css("background-position","0px 0px");
    }
	);      
	     
 });	
 
 



