<script>
$(document).ready(function(){
  $(document).on('click', ".raw_image", function () {
    if (! $(this).hasClass("w3-green") ){
        pthis = this
      $(".raw_image").removeClass("w3-green");
      $(".raw_image").removeClass("w3-red");
      var set_user_image_url = '/tv/{{ zp_tv_id }}/{{ image_type }}/set/' + $(pthis).attr('image_id');
      $.getJSON( set_user_image_url, function( data ) {
            console.log('first' + data);
        }).done(function(data) {
            console.log('done' + data);
            $(pthis).addClass("w3-green");
        }).fail(function(data) {
            console.log('fail' + data);
            $(pthis).addClass("w3-red");
        }).always(function(data) {
            console.log('always' + data);
        });
    }
  });
  $(document).on('click', ".raw_season_image", function () {
    if (! $(this).hasClass("w3-green") ){
        pthis = this
      $(".raw_season_image").removeClass("w3-green");
      $(".raw_season_image").removeClass("w3-red");
      var set_user_image_url = '/tv/' + $(pthis).attr('zp_tv_id') + '/' + $(pthis).attr('season') + '/' + $(pthis).attr('image_type') + '/set/' + $(pthis).attr('image_id');
      console.log('set_user_image_url: ' + set_user_image_url);
      $.getJSON( set_user_image_url, function( data ) {
            console.log('first' + data);
        }).done(function(data) {
            console.log('done' + data);
            $(pthis).addClass("w3-green");
        }).fail(function(data) {
            console.log('fail' + data);
            $(pthis).addClass("w3-red");
        }).always(function(data) {
            console.log('always' + data);
        });
    }
  });
  $(document).on('click', ".raw_episode_image", function () {
    if (! $(this).hasClass("w3-green") ){
        pthis = this
      $(".raw_episode_image").removeClass("w3-green");
      $(".raw_episode_image").removeClass("w3-red");
      var set_user_image_url = '/tv/' + $(pthis).attr('zp_tv_id') + '/' + $(pthis).attr('season') + '/' + $(pthis).attr('episode') + '/' + $(pthis).attr('image_type') + '/set/' + $(pthis).attr('image_id');
      console.log('set_user_image_url: ' + set_user_image_url);
      $.getJSON( set_user_image_url, function( data ) {
            console.log('first' + data);
        }).done(function(data) {
            console.log('done' + data);
            $(pthis).addClass("w3-green");
        }).fail(function(data) {
            console.log('fail' + data);
            $(pthis).addClass("w3-red");
        }).always(function(data) {
            console.log('always' + data);
        });
    }
  });
    $(function(){
        $('#tv_select').change(function(){
            var new_url = '/tv/' + $(this).val() + '/summary';
            location.href = new_url;
        });
        $("#search_button").click(function(){
            if( $('#search_text').val().length === 0 ) {
                 alert('Name cannot be empty');
            }else{
                  var subJson = { search_text: $('#search_text').val() }
                  console.log(subJson);
                 $.getJSON( "/tv/searchTVShows", subJson, function( data ) {
                 }).done(function(data) {
                    if (data.success == true){
                        $('#tv_select').empty();
                        $.each(data.tvs, function(i, item) {
                            console.log(data.tvs[i].title);
                            console.log(data.tvs[i].id);
                                $('#tv_select').append($('<option>', {
                                    value: item.id,
                                    text : item.title
                                }));
                        });
                    }
                 }).fail(function(data) {
                    console.log('always'  + data);
                 }).always(function(data) {
                    console.log('always'  + data);
                 });
            }
        });
    $("#formsubmit").click(function(){
        var tv_genre_array = new Array();
        $("#tv_genre_select > option").each(function() {
            tv_genre_array.push(this.value)
        });
        $("#tv_genre_list").val(JSON.stringify(tv_genre_array));
        $("#form").submit();
    });
    $("#cformsubmit").click(function(){
        var tv_actor_array = new Array();
        $("#actor_select > option").each(function() {
            tv_actor_array.push(this.value)
        });
        $("#tv_actor_json").val(JSON.stringify(tv_actor_array));
        $("#cform").submit();
    });
    $("#genre_button").click(function(){
        $("#genre_select > option:selected").each(function(){
            $(this).remove().appendTo("#tv_genre_select");
        });
    });
    $("#rating_apply").click(function(){
        $("#rating_form").submit();
    });

    $("#tv_genre_button").click(function(){
        $("#tv_genre_select > option:selected").each(function(){
            $(this).remove().appendTo("#genre_select");
        });
    });
    $("#cformsubmit").click(function(){
        var tv_actor_array = new Array();
        $("#actor_select > option").each(function() {
            tv_actor_array.push(this.value)
        });
        $("#tv_actor_json").val(JSON.stringify(tv_actor_array));
        $("#cform").submit();
    });
    $("#search").click(function(){
        if( $('#name').val().length === 0 ) {
             alert('Name cannot be empty');
        }else{
              var subJson = { name: $('#name').val() }
              console.log(subJson);
             $.getJSON( "/tv/searchActors", subJson, function( data ) {
             }).done(function(data) {
                if (data.success == true){
                    $('#person_select').empty();
                    $.each(data.actors, function(i, item) {
                        console.log(data.actors[i].name);
                        console.log(data.actors[i].id);
                            $('#person_select').append($('<option>', {
                                value: item.id,
                                text : item.name
                            }));
                    });
                }
             }).fail(function(data) {
                console.log('always'  + data);
             }).always(function(data) {
                console.log('always'  + data);
             });
        }
    });

        $("#title_apply").click(function(){
            var title_radio_form_radio_checked = $('input[name=zp_tv_title_id]:checked', '#title_radio_form').val();
            console.log('title_radio_form radio checked value: ' + title_radio_form_radio_checked);
            var div_zp_tv_title_pattern = /^\d+_(.*)/;
            var match = div_zp_tv_title_pattern.exec(title_radio_form_radio_checked);
            console.log(match[0]);
            console.log(match[1]);
            $('#zp_title_title').val($('#' + title_radio_form_radio_checked).text());
            $('#zp_title_ident').val(match[1]);
            $('#title_form').submit();
        });

        $("#overview_apply").click(function(){
            var overview_radio_form_radio_checked = $('input[name=zp_tv_overview_id]:checked', '#overview_radio_form').val();
            console.log('overview_radio_form radio checked value: ' + overview_radio_form_radio_checked);
            var div_zp_tv_overview_pattern = /^\d+_(.*)/;
            var match = div_zp_tv_overview_pattern.exec(overview_radio_form_radio_checked);
            console.log(match[0]);
            console.log(match[1]);
            $('#zp_overview_overview').val($('#' + overview_radio_form_radio_checked).text());
            $('#zp_overview_ident').val(match[1]);
            $('#overview_form').submit();
        });
        $("#episode_title_apply").click(function(){
            var title_radio_form_radio_checked = $('input[name=zp_tv_title_id]:checked', '#title_radio_form').val();
            console.log('title_radio_form radio checked value: ' + title_radio_form_radio_checked);
            var div_zp_tv_title_pattern = /^\d+_(.*)/;
            var match = div_zp_tv_title_pattern.exec(title_radio_form_radio_checked);
            console.log(match[0]);
            console.log(match[1]);
            $('#zp_title_title').val($('#' + title_radio_form_radio_checked).text());
            $('#zp_title_ident').val(match[1]);
            $('#title_form').submit();
        });

        $("#episode_overview_apply").click(function(){
            var overview_radio_form_radio_checked = $('input[name=zp_tv_overview_id]:checked', '#overview_radio_form').val();
            console.log('overview_radio_form radio checked value: ' + overview_radio_form_radio_checked);
            var div_zp_tv_overview_pattern = /^\d+_(.*)/;
            var match = div_zp_tv_overview_pattern.exec(overview_radio_form_radio_checked);
            console.log(match[0]);
            console.log(match[1]);
            $('#zp_overview_overview').val($('#' + overview_radio_form_radio_checked).text());
            $('#zp_overview_ident').val(match[1]);
            $('#overview_form').submit();
        });

        $(".div_title").click(function(){
            var this_id = this.id;
            var div_id_pattern =  /^(\d+)_/;
            var match = div_id_pattern.exec(this_id);
            console.log(match[0]);
            console.log(match[1]);
            $("#" + match[1] + "_radio").prop("checked", true);
        });
        $(".div_overview").click(function(){
            var this_id = this.id;
            var div_id_pattern =  /^(\d+)_/;
            var match = div_id_pattern.exec(this_id);
            console.log(match[0]);
            console.log(match[1]);
            $("#" + match[1] + "_radio").prop("checked", true);
        });

        $(".div_episode_title").click(function(){
            var this_id = this.id;
            var div_id_pattern =  /^(\d+)_/;
            var match = div_id_pattern.exec(this_id);
            console.log(match[0]);
            console.log(match[1]);
            $("#" + match[1] + "_radio").prop("checked", true);
        });
        $(".div_episode_overview").click(function(){
            var this_id = this.id;
            var div_id_pattern =  /^(\d+)_/;
            var match = div_id_pattern.exec(this_id);
            console.log(match[0]);
            console.log(match[1]);
            $("#" + match[1] + "_radio").prop("checked", true);
        });

        $("#actor_button").click(function(){
            $("#actor_select > option:selected").each(function(){
                $(this).remove();
            });
        });

        $("#person_button").click(function(){
            $("#person_select > option:selected").each(function(){
                console.log($(this).val());
                if ($("#actor_select option[value=" + $(this).val() + "]").length > 0){
                    alert('Allready in actors');
                }else{
                   $(this).remove().appendTo("#actor_select");
                }
            });
        });
        $("#running_button_remove").click(function(){
            $("#runnning_tv_select > option:selected").each(function(){
                $(this).remove();
            });
        });
        $("#running_button_add").click(function(){
            $("#running_tv_search_select > option:selected").each(function(){
                console.log($(this).val());
                if ($("#runnning_tv_select option[value=" + $(this).val() + "]").length > 0){
                    alert('Allready in Running');
                }else{
                   $(this).remove().appendTo("#runnning_tv_select");
                }
            });
        });

        $("#running_search_button").click(function(){
            if( $('#search_text').val().length === 0 ) {
                 alert('Name cannot be empty');
            }else{
                  var subJson = { search_text: $('#search_text').val() };
                  console.log(subJson);
                 $.getJSON( "/tv/searchTVShows", subJson, function( data ) {
                 }).done(function(data) {
                    if (data.success == true){
                        $('#running_tv_search_select').empty();
                        $.each(data.tvs, function(i, item) {
                            console.log(data.tvs[i].title);
                            console.log(data.tvs[i].id);
                                $('#running_tv_search_select').append($('<option>', {
                                    value: item.id,
                                    text : item.title
                                }));
                        });
                    }
                 }).fail(function(data) {
                    console.log('always'  + data);
                 }).always(function(data) {
                    console.log('always'  + data);
                 });
            }
        });
        $("#running_submit").click(function(){
            var tv_genre_array = new Array();
            $("#runnning_tv_select > option").each(function() {
                tv_genre_array.push(this.value)
            });
            $("#tv_running_list").val(JSON.stringify(tv_genre_array));
            $("#running_form").submit();
        });

        $('#unidentified_select').change(function(){
        var new_url = '/tv/' + $(this).val() + '/identify';
        location.href = new_url;
    });
    });
});
</script>