$(document).ready(function() {

  if (window.location.href.indexOf('?tab=news') > -1) {
    $('ul.quicktabs_tabs li').removeClass('active');
    $('ul.quicktabs_tabs li.submit_content').addClass('active');
  }

  $('.proconnect .node-form textarea').each(function(i){ 
    $(this).parent().find('label').addClass('noalign');
  });  

  $('#tango-proconnect-finder-form #edit-zip').click(function () {
    $(this).val('');
  });

  $('#tango-proconnect-finder-form #edit-zip').blur(function () {
    if ($(this).val() == '') {
      $(this).val('Enter ZIP code');
    }
  });

  $('#edit-GotoPage').parent().css({'display': 'inline'});

  $('.proconnect #payment-pane .option:first').addClass('paypal-label');

  if ($(".proconnect #uc-cart-checkout-form .form-radio:checked").val() == 'credit') {
    get_payment_details('cart/checkout/payment_details/' + 'credit');
  }

  $(".proconnect #amazon-import-result-form .form-checkbox").click(function () {
    if ($(this).parent().parent().parent().hasClass('alt')) {
      $(this).parent().parent().parent().toggleClass('checkedalt');
    } else {
      $(this).parent().parent().parent().toggleClass('checked');
    }
  });

  $(".proconnect .shop-wrapper tr:even").addClass("alt");
  $(".proconnect #amazon-import-result-form tr:even").addClass("alt");
  $(".proconnect #amazon-import-result-form tr:last-child").removeClass("alt");

  $('#node-form a.add-case-study').click(
   function() {
     $('#client-type-form').toggle();
     this.blur();
     return false;
   });

  $('#insert-pro-blog').toggle(function(){ 
     $('#pro-blog').slideDown();
     this.blur();
     return false;
  },function(){ 
     $('#pro-blog').slideUp();
     this.blur();
     return false;
  });

  $('#insert-ptp-blog').toggle(function(){ 
     $('#ptp-blog').slideDown();
     this.blur();
     return false;
  },function(){ 
     $('#ptp-blog').slideUp();
     this.blur();
     return false;
  });

  $('#insert-pro-book').toggle(function(){ 
     $('#pro-amazon').slideDown();
     this.blur();
     return false;
  },function(){ 
     $('#pro-amazon').slideUp();
     this.blur();
     return false;
  });

  $('a.qs_check').click(function() {
     $('#edit-field-specialties-in-qs-0-value').val('');
     var x= 0;
     var arr = new Array();
     if ($(this).attr('checked') == 'checked') {
       $(this).removeAttr("checked");
       $(this).text('My Specialty');
     } else {
       $(this).attr('checked', 'checked');
       $(this).text('Main Specialty');
     }

     $('a.qs_check').each(function(i){ 
       if($(this).attr('checked') == 'checked'){
         arr[x] = $(this).attr('parent');
         x++;
       }
     });
     $('#edit-field-specialties-in-qs-0-value').val(arr.toString());     

     this.blur();
   });

  $('.specialties .form-checkboxes input.form-checkbox').click(
   function() {
     if($(this).attr('checked')){
        $('#qs_' + $(this).attr('id')).show();
     }
     else{
        $('#qs_' + $(this).attr('id')).hide();
     }

     this.blur();
   }); 

  $('.shop-wrapper .delete').click(function(e){
    seq = new Date().getTime();
    url_called = '/ajax/book/delete/' +$(this).attr("node")+'/' +$(this).attr("uid")+'/' + seq;
    node = $(this).attr("node");

    $.getJSON(url_called, function(obj_back_from_php) {       
      what = '#book-' + node;
      if(obj_back_from_php == 'ok'){
        $(what).slideUp();
      }
      else{
        alert("You are not authorized to delete this book!");
      }
    });
    $(this).blur();
    return false;
  });

  str = $('#edit-field-specialties-in-qs-0-value').val();
  if (str) {
    var str_arr = str.split(",");
    $('a.qs_check').each(function(i) { 
     for (x=0; x < str_arr.length; x++) {
        if ($(this).attr('parent') == str_arr[x]) {
          $(this).show();
          $(this).attr('checked', 'checked');
          $(this).text('Main Specialty');
        }
     }
    });
  }
  
 $('#tango-proconnect-submitquestion-form #edit-question').click(
   function() {
     if($(this).val() == 'Enter your question here. Keep it short and sweet, only the first 700 characters will be sent to your Pro!'){  
       $(this).val('');
     }
   });  
   
  $('#email_me_private').toggle(function(){ 
     $('#proconnect_private_message').slideDown();
     this.blur();
     return false;
  },function(){ 
     $('#proconnect_private_message').slideUp();
     this.blur();
     return false;
  });  
  
  $('#proconnect_private_message #edit-submit').click(function() {
    var seq = new Date().getTime();
    var url;
    url = '/ajax/pro-privatemsg/' + seq;
   
    var to = $('#proconnect_private_message #edit-to').val();
    var name = $('#proconnect_private_message #edit-sender-name').val();
    var username = $('#proconnect_private_message #edit-sender-username').val();    
    var subject = $('#proconnect_private_message #edit-subject').val();
    var message = $('#proconnect_private_message #edit-message').val();
    var send_inbox = $('#proconnect_private_message input#edit-send-to-yt-inbox-1:checked').val();    

    $.post(url, { to: to, name: name, username: username, subject: subject, message: message, send_inbox: send_inbox },
      function(data){
       $('#proconnect_private_message').slideUp();
       $('#proconnect_private_message_response').html(data);
      }, "html");

      return false;
    });  
    
  $('#pc-tc').click(function () {
    window.open(this.href,'','height=400,width=600,scrollbars=yes');
    return false;
  });         

});
