<?php echo $this->Html->script('site_js/common')?>

<style>
.plus_minus_sign_add {
	display: block;
	float: right;
	color: #888787;
	border: 1px solid #888787;
	padding: 0 5px;
	cursor: pointer;
}

.fixedheight_add{
	height: 100px;
	overflow: hidden;
	text-align: justify;
	margin-right: 12px;
}
</style>
<?php 
	$last_week = date('Y-m-d', strtotime("-1 week")); //1 week ago
	$last_month = date('Y-m-d',strtotime("-1 months")); //1 month ago
	
	$last_3months = date('Y-m-d', strtotime("-3 months")); //3 months ago
	$last_6months = date('Y-m-d', strtotime("-6 months")); //6 months ago
	$year = date('Y-m-d', strtotime("-1 year")); //1 week ago
?>
<?php //pr($blogs);?>
<div class="blocktitle">Blog - Draft</div>
<div class="tar borb">
	<?php echo $this->Html->link('Published',array('controller'=>'contents','action'=>'blog'),array('class'=>'btn btn-mini'))?>
	<?php echo $this->Html->link('Write Blog',array('controller'=>'contents','action'=>'add','Blog'),array('class'=>'btn btn-mini'))?>
	<?php // echo $this->Html->link('Category',array('controller'=>'categories','action'=>'add'),array('class'=>'btn btn-mini'))?>
	<?php echo $this->Html->link('Draft',array('controller'=>'contents','action'=>'draft'),array('class'=>'btn btn-mini'))?>
</div>
<br/>

<div class="row-fluid">

<div class="span9">
		<?php 
	
	$items_per_group = 5;
	$arr_blogList = sizeof($blogs);
	

	$total_groups= ceil($arr_blogList/$items_per_group);


	
	?>
	<script type="text/javascript">
    $(document).ready(function() {
    var path='<?php echo $this->webroot?>';

            var track_load = 0; //total loaded record group(s)
            var loading  = false; //to prevents multipal ajax loads
            var total_groups = '<?php echo $total_groups;?>'; //total record group(s)

            $('#results').load(path+"contents/draftload", {'group_no':track_load,'catId': '<?php echo $catId;?>','date': '<?php echo $date;?>'}, function() {track_load++;}); //load first group

                    $('.partA').bind('scroll', function()
                    {
                    	//alert(track_load);
                        
                      if($(this).scrollTop() + $(this).innerHeight()+150>=$(this)[0].scrollHeight)
                      {
                    	  
                              if(track_load <= total_groups && loading==false) //there's more data to load
                                    {
                                            	
                                            loading = true; //prevent further ajax loading
                                            $('.animation_image').show(); //show loading image

                                            //load data from the server using a HTTP POST request
                                            $.post(path+"contents/draftload",{'group_no': track_load,'catId': '<?php echo $catId;?>','date': '<?php echo $date;?>'}, function(data){

                                                    $("#results").append(data); //append received data into the element

                                                    //hide loading image
                                                    $('.animation_image').hide(); //hide loading image once data is received

                                                    track_load++; //loaded group increment
                                                    loading = false; 

                                            }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

                                                    alert(thrownError); //alert with HTTP error
                                                    $('.animation_image').hide(); //hide loading image
                                                    loading = false;

                                            });

                                    }
                      }
             });
    });
    
</script>
	
<ol id="results">
	</ol>
<div class="animation_image" style="display:none" align="center"><img src="<?php echo $this->webroot;?>img/ajax-loader.gif"></div>
		
</div>

<div class="span3 right-pan">
                <div id='show_all'></div>
                <h4>Recent post</h4>

                <ul class="rpn" >
                        <li><?php echo $this->Html->link('Week'.' ('.$week[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$last_week), array('escape'=>false));?></li>
                        <li><?php echo $this->Html->link('Month'.' ('.$month[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$last_month), array('escape'=>false));?></li>
                        <li><?php echo $this->Html->link('3 Months'.' ('.$_3months[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$last_3months), array('escape'=>false));?></li>
                        <li><?php echo $this->Html->link('6 Months'.' ('.$_6months[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$last_6months), array('escape'=>false));?></li>
                        <li><?php echo $this->Html->link('Year'.' ('.$_1year[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$year), array('escape'=>false));?></li>
                </ul>
                <ul style="border-top: 1px solid #ccc; padding-top: 10px">
                        <?php 
                        $total_post = 0;
                        foreach ($categories as $category): 
                                $total_post += $category[0]['total'];
                        ?>
                        <li><?php 
                        if(!empty($this->params['pass'][0])){
                        	 echo $this->Html->link($category['Category']['name'].' ('.$category[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$category['Category']['id'],$this->params['pass'][0]), array('escape'=>false));
                        }else{
                        	 echo $this->Html->link($category['Category']['name'].' ('.$category[0]['total'].' posts)', array('controller' => 'contents', 'action' => 'draft',$category['Category']['id']), array('escape'=>false));
                        }
                       ?></li>
                        <?php endforeach;?>
                </ul>
        </div>

</div>
<script type="text/javascript">
	$(document).ready(function(){
		$("#show_all").html("<a href='/contents/blog'>Show All (<?php echo $total_post;?> posts)</a>");
	});
</script>

<script type="text/javascript">
     var path='<?php echo $this->webroot;?>';
		function blogpublished(id){

			var con=confirm('Are you sure to publish this blog?');
			if(con==true){
			 $.ajax({
			     	type: 'POST',
			     	url: path +'contents/blogpublished',
			     	data: {id:id},
			     	success: function(data){
			     		location.href="<?php echo $this->here?>";
			     		//$("#rf"+id).attr('onclick',"blogunpublish('"+id+"')").html('Unpublish');
			     		//$("#rf"+id).attr('id','df'+id);
			     	}
		     	});
			}
		}
		function blogunpublish(id){

			var con=confirm('Are you sure to unpublish this blog?');
			if(con==true){
			 $.ajax({
			     	type: 'POST',
			     	url: path +'contents/blogunpublish',
			     	data: {id:id},
			     	success: function(data){
			     			$("#df"+id).attr('onclick',"blogpublished('"+id+"')").html('Publish');
			     			$("#df"+id).attr('id','rf'+id);
			     	}
		     	});
			}
		}
	</script>