// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(function(){
   $('#link').click(function() {
     $('#link').hide()
   });
   $('#search').click(function() {
     $('#search').val("")
   });
   $("#review_hub").change(function(v){
     if ($("#review_hub:checked").val() == "1") {
       $(".bonus_sites").show();
       $(".hub_add").hide();
     } else {
       $(".bonus_sites").hide();
       $(".hub_add").show();
     }
   });
   $("#gallery_external").change(function(v){
     if ($("#gallery_external:checked").val() == "1") {
       $(".internal input").each(function(intern){
         $(this).attr("disabled", "disabled");
       });
       $(".internal select").each(function(intern){
         $(this).attr("disabled", "disabled");
       });
       $(".internal textarea").each(function(intern){
         $(this).attr("disabled", "disabled");
       });
       $(".external input").each(function(extern){
         $(this).removeAttr("disabled");
       });
       $(".internal").hide();
       $(".external").show();
     } else {
       $(".internal input").each(function(intern){
         $(this).removeAttr("disabled");
       });
       $(".internal select").each(function(intern){
         $(this).removeAttr("disabled");
       });
       $(".internal textarea").each(function(intern){
         $(this).removeAttr("disabled");
       });
       $(".external input").each(function(extern){
         $(this).attr("disabled", "disabled");
       });
       $(".external").hide();
       $(".internal").show();
     }
   });
   $("#babe_review").change(function(v){
     if ($("#babe_review:checked").val() == "1") {
       $("#babe_review_id").attr("disabled", "");
     } else {
       $("#babe_review_id").attr("disabled", "disabled");
     }
   });
   $("#babe_official_website").change(function(v){
     if ($("#babe_official_website:checked").val() == "1") {
       $("#babe_official_website_url").attr("disabled", "");
     } else {
       $("#babe_official_website_url").attr("disabled", "disabled");
     }
   });
   $("#height_conversion").change(function(height){
     $("#babe_height").attr('name', $("#height_conversion").val());
   });
   $("#weight_conversion").change(function(weight){
     $("#babe_weight").attr('name', $("#weight_conversion").val());
   });
   $("#body_measurements_conversion").change(function(height){
     $("#babe_body_measurements").attr('name', $("#body_measurements_conversion").val());
   });
   $(".url").each(function(url) {
     $(this).change(function(){
                 var me = $(this);
                 $.get('/check_url?url='+ $(this).val(), function(data) { 
                          $('#' + me.attr('id') + '_status').html(data);
                       });
          });
   });
   $(".data").hover(
     function() { $(this).addClass("highlight"); },
     function() { $(this).removeClass("highlight"); });
   $(".autocomplete").each( function(autocomplete) {
      $(this).focusout(function() {
        var field = $(this).attr('id').replace(/[a-z]*_/,"");
        $('#' + field + '_suggestion').fadeOut(1000);
      });
      $(this).keyup(function(){
                var me = $(this);
                var field = $(this).attr('id').replace(/[a-z]*_/,"");
                var model = $(this).attr('id').match(/[a-z]*_/,"")[0].replace(/_/,"");
                $('#' + field + '_suggestion').show();
                $.getJSON('/admin/suggest/' + model + 's?' + field + '=' + me.val(), function(data) {
                  if (data.length > 0) {
                    $('#' + field + '_suggestion').html('');
                    for (var item in data) {
                      if (model == 'babe') {
                        $('#' + field + '_suggestion').append('<a href="/babes/' + data[item].babe.id + '/edit">' + data[item].babe.name + ' ' + data[item].babe.surname + '</a><br>');
                      }
                    }
                  }
                });
                
          });
   });
   $(".data td.title").dblclick(function() {
     if (!$(this).hasClass("edit")) {
       text = $.trim($(this).text());
       id = $(this).find("input").val();
       $(this).addClass("edit");
       $(this).html("<input type='text' value='" + text + "' name='gallery["+id+"][title]'> Approved? <input type='checkbox' value='1' name='gallery["+id+"][approved]' checked='checked'><input type='submit' value='Save all'>");
     }
   });
   $('.show-more').each ( function(showmore) {
     $(this).trackStar( {displayID: 'preview_' + $(this).attr("id")} );
   });
   $("#trade_link_type").change(function(t){
     if ($(this).val() == "LinkTradeLink" || $(this).val() == "ModelTradeLink") {
      $("#trade_babe_id").show();
     } else {
      $("#trade_babe_id").hide();
      $("#trade_babe_id").val("");
     }
   });
})
function clk(a) {
    if(document.images){
        (new Image()).src="/click?id="+a;
    }
    return true;
}

