
(function($) {
  $.fn.image_qtip = function() {
    $(this).qtip({
      content: $('#comic .addthis').html(),
      style: {
        name: 'light',
        width: 105,
        tip: true,
        border: {
          width: 3,
          radius: 3
        }
      },
      show: {
        ready: true,
        when: false
      },
      hide: false,
      position: {
        corner: {
          target: 'rightTop',
          tooltip: 'leftMiddle'
        },
        adjust: {
          y: 52
        }
      }
    });
  };
})(jQuery);


$(function(){

  //error tooltip
  $('span.error_list').each(function() {
    //remove help text
    $(this).parent().next().children('span.help').remove();

    //tooltip
    $(this).hide().parent().next().children('input:visible, textarea:visible').qtip({
      content: $(this).html(),
      style: {
        name: 'red',
        tip: true,
        'background-color': '#FCC',
        color: '#333',
        border: {
          color: '#C5161D',
          radius: 3
        }
      },
      position: {
        corner: {
          target: 'rightMiddle',
          tooltip: 'leftMiddle'
        }
      },
      show: {
        ready: true
      /*when: {
          event: 'focus'
        }*/
      },
      hide: {
        when: {
          event: null
        }
      }
    });
  });

  //help tooltip
  $('span.help').each(function(){
    $(this).hide().siblings('input, textarea').qtip({
      content: $(this).html(),
      style: {
        name: 'blue',
        tip: true,
        'background-color': '#70c1ef',
        color: '#333',
        border: {
          color: '#016ca9',
          radius: 3
        }
      },
      position: {
        corner: {
          target: 'rightMiddle',
          tooltip: 'leftMiddle'
        }
      },
      show: {
        when: {
          event: 'focus'
        }
      },
      hide: {
        when: {
          event: 'blur'
        }
      }
    });
  });

  //comic tooltip
  $('#comic img[title]').qtip({
    style: {
      name: 'dark',
      width: {
        max: 400
      },
      border: {
        width: 3,
        radius: 3
      }
    },
    position: {
      target: 'mouse'
    }
  });

  //preload images and slide in / show social media tooltip
  $('img.comic').hide();
  $('ul.comic-nav').hide();
  if ($('img.comic').length>0)
    if ($('img.comic')[0].complete == true) {
      $('img.comic, #comic .addthis, #comic .fb-like, #comic .plusone').show('fade', {}, 'fast', function(){

        //$(this).image_qtip();
        /*$('ul.comic-nav').show('slide', {
          direction: 'up'
        }, 'fast');*/
      });
      $('ul.comic-nav').fadeIn('fast').show();
    }
    else {
      $('img.loader').show();
      $('img.comic').load(function(){
        $('img.loader').hide('fade', {}, 'slow', function(){
          $('img.comic, #comic .addthis, #comic .fb-like, #comic .plusone').show('fade', {}, 'fast', function(){
            //$(this).image_qtip();
          });
          $('ul.comic-nav').fadeIn('fast').show();
        });
      });
    }

  //archive tooltip
  $('#archive a[title]').qtip({
    style: {
      name: 'dark',
      width: {
        max: 500
      },
      border: {
        width: 3,
        radius: 3
      },
      tip: true
    },
    position: {
      corner: {
        target: 'rightMiddle',
        tooltip: 'leftMiddle'
      },
      adjust: {
        x: 5
      }
    }
  });

  //admin tooltip
  $('#admin-bar a[title]').qtip({
    style: {
      name: 'dark',
      textAlign: 'center',
      width: {
        max: 300
      },
      border: {
        width: 3,
        radius: 3
      },
      tip: true
    },
    position: {
      corner: {
        target: 'bottomMiddle',
        tooltip: 'topMiddle'
      },
      adjust: {
        y: 5
      }
    }
  });

  //main nav tooltip
  $('#main-nav a[title]').qtip({
    style: {
      name: 'dark',
      textAlign: 'center',
      width: {
        max: 300
      },
      border: {
        width: 3,
        radius: 3
      },
      tip: true
    },
    position: {
      corner: {
        target: 'topMiddle',
        tooltip: 'bottomMiddle'
      }
    }
  });

  //powered by zenstruck
  $('#zenstruck a').hover(function(){
    $(this).pulse({
      marginLeft: ['5px', '-5px'],
      marginRight: ['-5px', '5px']
    }, {
      duration: 100,
      times: 4
    })
    .animate({
      marginLeft: '0px',
      marginRight: '0px'
    }, 'fast');
  });

  //datepicker
  $("#ia_comic_created_at").datepicker({
    dateFormat: 'yy-M-dd'
  });

  //set focus to first input
  $("form:not(.filter) :input[type=text]:visible:enabled:first").focus();
});





