<div class="row">
    <div class="col-md-12">
        <?php echo $this->session->flashdata('msg');?>
        <div class="box">
            <div class="box-title">
                <h3><i class="fa fa-bars"></i> <?php echo $title;?></h3>
                <div class="box-tool">
                    <a href="#" data-action="collapse"><i class="fa fa-chevron-up"></i></a>

                </div>
            </div>
            <div class="box-content">

                <form class="form-horizontal" id="addcategory" action="<?php echo site_url('admin/content/addnews');?>" method="post">
                    <input type="hidden" name="action" value="<?php echo isset($action)?$action:'';?>">
                    <input type="hidden" name="id" value="<?php echo isset($id)?$id:'';?>">

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('source');?>:</label>

                        <div class="col-sm-9 col-md-10 controls">
                            <select class="form-control input-sm" name="source_id" id="source_id">
                                <option value="0"><?php echo lang_key('no_source');?></option>
                                <?php
                                $v = isset($post->source_id)?$post->source_id:'';
                                $v = (set_value('source_id')!='')?set_value('source_id'):$v;
                                ?>
                                <?php
                                $CI = get_instance();
                                $CI->load->model('admin/content_model');
                                $sources = $CI->content_model->get_all_sources();
                                foreach ($sources->result() as $cat) {
                                    $sel = ($v==$cat->id)?'selected="selected"':'';
                                    ?>
                                    <option value="<?php echo $cat->id;?>" <?php echo $sel;?>><?php echo $cat->source_name;?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <?php echo form_error('source'); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('category');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <select class="form-control input-sm" name="category" id="category">
                                <option value="0"><?php echo lang_key('no_category');?></option>
                                <?php
                                $v = isset($post->category)?$post->category:'';
                                $v = (set_value('category')!='')?set_value('category'):$v;
                                ?>
                                <?php
                                $CI = get_instance();
                                $CI->load->model('admin/category_model');
                                $categories = $CI->category_model->get_all_parent_categories_by_range();
                                foreach ($categories->result() as $cat) {
                                    $sel = ($v==$cat->id)?'selected="selected"':'';
                                    ?>
                                    <option value="<?php echo $cat->id;?>" <?php echo $sel;?>><?php echo $cat->title;?></option>
                                <?php
                                }
                                ?>
                            </select>
                            <?php echo form_error('category'); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('sub_category');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <select class="form-control input-sm" name="sub_category" id="sub_category">
                                <option value=""><?php echo lang_key('select_one');?></option>

                            </select>
                            <?php echo form_error('sub_category'); ?>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('title');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php
                            $v = isset($post->title)?$post->title:'';
                            $v = (set_value('title')!='')?set_value('title'):$v;
                            ?>
                            <input type="text" name="title" value="<?php echo $v;?>" placeholder="<?php echo lang_key('type_something');?>" class="form-control input-sm" >
                            <?php echo form_error('title'); ?>
                        </div>
                    </div>

                    <div class="form-group icon-class-holder">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('description');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php
                            $v = isset($post->description)?$post->description:'';
                            $v = (set_value('description')!='')?set_value('description'):$v;
                            ?>

                            <textarea name="description" class="form-control rich"><?php echo $v;?></textarea>
                            <?php echo form_error('description'); ?>
                        </div>
                    </div>



                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('link');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php
                            $v = isset($post->link)?$post->link:'';
                            $v = (set_value('link')!='')?set_value('link'):$v;
                            ?>
                            <input type="text" name="link" value="<?php echo $v;?>" placeholder="<?php echo lang_key('type_something');?>" class="form-control input-sm" >
                            <?php echo form_error('link'); ?>
                        </div>
                    </div>

                     <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('tags');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php
                            $v = isset($post->tags)?$post->tags:'';
                            $v = (set_value('tags')!='')?set_value('tags'):$v;
                            ?>
                            <textarea name="tags" value="<?php echo $v;?>" class="form-control input-sm"><?php echo $v;?></textarea>
                            <span><?php echo lang_key('put_as_comma_separated');?></span>
                            <?php echo form_error('tags'); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label">&nbsp;</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <img class="thumbnail" id="featured_photo" src="<?php echo get_featured_photo_by_id('');?>" style="width:256px;">
                        </div>
                        <div class="clearfix"></div>
                        <span id="featured-photo-error"></span>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label"><?php echo lang_key('media');?>:</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php
                            $v = isset($post->media)?$post->media:'';
                            $v = (set_value('media')!='')?set_value('media'):$v;
                            ?>
                            <input type="text" name="media" id="media" value="<?php echo $v;?>" placeholder="<?php echo lang_key('type_something');?>" class="form-control input-sm" >
                            <?php echo form_error('media'); ?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-labell">&nbsp;</label>
                        <div class="col-sm-9 col-md-10 controls"><?php echo lang_key('or');?></div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-labell">&nbsp;</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <?php $featured_img = (isset($post->media))?$post->media:'';?>
                            <?php $v = (set_value('media')!='')?set_value('media'):$featured_img;?>
                            <input type="hidden" name="media_photo" id="featured_photo_input" value="<?php echo $v;?>">
                            <iframe src="<?php echo site_url('admin/content/featuredimguploader');?>" style="border:0;margin:0;padding:0;height:130px;"></iframe>
                            <span class="help-inline">&nbsp;</span>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 col-md-2 control-label">&nbsp;</label>
                        <div class="col-sm-9 col-md-10 controls">
                            <button class="btn btn-primary" type="submit"><i class="fa fa-check"></i> <?php echo lang_key('save');?></button>
                        </div>
                    </div>


                </form>

            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="<?php echo base_url('assets/tinymce/tinymce.min.js');?>"></script>

<script type="text/javascript">
    var base_url = '<?php echo base_url();?>';
    jQuery(document).ready(function(){

        jQuery('#category').change(function(e){
            var val = jQuery(this).val();
            if(val!='')
            {
                var loadUrl = site_url+'/show/load_sub_categories/'+val;
                jQuery.post(
                    loadUrl,
                    {},
                    function(responseText){
                        jQuery('#sub_category').html(responseText);

                        <?php 
                        $v = (isset($post->category))?$post->category:'-1';
                        $v = (set_value('parent_category')!='')?set_value('parent_category'):$v;
                        ?>  
                        var parent_category = <?php echo $v;?>;

                        <?php 
                        $v = (isset($post->sub_category))?$post->sub_category:'-1';
                        $v = (set_value('sub_category')!='')?set_value('sub_category'):$v;
                        ?>  
                        var sub_category = <?php echo $v;?>;

                        if(parent_category==val)
                        {
                            if(sub_category!=0)
                            jQuery('#sub_category').val(sub_category);
                            else
                            jQuery('#sub_category').val("");                            
                        }
                        else
                        {
                            jQuery('#sub_category').val("");                            
                        }
                    }
                );                
            }
        }).change();

        
    });

    var manual_creation = '<?php echo (isset($post->manual_creation))?$post->manual_creation:1;?>';
    jQuery('#featured_photo_input').change(function(){
        var val = jQuery(this).val();
        if(val!='')
        {
            //if(manual_creation==1)
            var src = base_url+'uploads/images/'+val;
            // else
            // {

            //     var src = val;                
            // }
        }
        else
        {
            var src = base_url+'assets/admin/img/preview.jpg'
        }
        jQuery('#featured_photo').attr('src',src);
        jQuery('#media').attr('value',src);
    });

    function IsValidImageUrl(url) {
        $("<img>", {
            src: url,
            error: function() { 
                var src = base_url+'assets/admin/img/preview.jpg'
            },
            load: function() {

                jQuery('#featured_photo').attr('src',url);
            }
        });
    }

    jQuery('#media').change(function(){
        var src = jQuery(this).val();
        IsValidImageUrl(src);
    }).change();


    tinymce.init({
        convert_urls : 0,
        selector: ".rich",

        plugins: [

            "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",

            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",

            "save code table contextmenu directionality emoticons template paste textcolor"

        ]

    });
</script>