/**
 * CoffeeCup Software: Support Javascript Library
 *
 * This file contains javascript library for the support section of www.coffeecup.com
 *
 * @author     Jeff Welch <jw@coffeecup.com>
 * @category   Empire
 * @package    Public
 * @copyright  Copyright (c) 2006-2007 CoffeeCup Software, Inc. (http://www.coffeecup.com/)
 * @version    $Id$
 */

jQuery.fn.extend({
  // Hide extras list items in the sidebar
  ccHideExtras: function(number_to_show) {
      return this.each(function() {
         $(this).find('#' + $(this).attr('id') + '_hide_again').remove();
         if($(this).find('li:gt(' + --number_to_show + ')').slideUp('fast').length > 0)
         {
            $(this).append('<li class="view_all" id="' + $(this).attr('id') + '_view_all' + '">View All</li>');
            $(this).find('#' + $(this).attr('id') + '_view_all').click(function() {
               $(this).parent().ccShowExtras(number_to_show);
            });
         }
      });
  },
  // Show extra list items in the sidebar
  ccShowExtras: function(number_to_show) {
     return this.each(function() {
        $(this).find('#' + $(this).attr('id') + '_view_all').remove();
        $(this).find('li').slideDown('fast');
        $(this).append('<li class="hide_again" id="' + $(this).attr('id') + '_hide_again' + '">Hide Again</li>');
        $(this).find('#' + $(this).attr('id') + '_hide_again').click(function() {
           $(this).parent().ccHideExtras(++number_to_show);
        });
     })
  }
});

$(document).ready(function() {
      
   $('.topictable tr.topicrow').hover(
      function() {$(this).addClass('hover');}, 
      function() {$(this).removeClass('hover');
   })

   // Only show 3 list items by default
   $('#resolved_topics_list').ccHideExtras(3);

   if($('.support_select').length > 0)
   {
      $('#software_select option:first').text('-- Choose One --');
      $('.support_select').change(function(){
         if($('.support_select:checked').val() == 'firefactor')
         {
            $('#website_url').show();          
            $('#hosting_provider').hide();
            $('#browser_problem').hide();
            $('#software_select_label').hide();
         }         
         else if($('.support_select:checked').val() != 'software')
         {
            $('#website_url').hide();
            $('#hosting_provider').hide();
            $('#browser_problem').hide();
            $('#software_select_label').hide();
         }
         else
         {
            $('#website_url').show();
            $('#software_select_label').show();
            $('#browser_problem').show();
            $('#hosting_provider').show();            
         }
      }).change();
   }

   $('#topic_screen').val(screen.width+"x"+screen.height);

   // If the tooltip function exists, add tooltips appropriately
   if($.fn.tooltip)
   {
      $('.quick_overview_link').tooltip({
         track: true,
         delay: 0,
         showURL: false,
         opacity: 1,
         fixPNG: true,
         bodyHandler: function() {
            return $(this).prev().html();
         },
         top: -15,
         left: 5
      });
   }

   // Setup the checkboxes to highlight when things are checked
   $('td.check input[type=checkbox]').change(function(){
      this.checked ? $(this).parent().parent().addClass('selected') : $(this).parent().parent().removeClass('selected');
   }).change();

   // Setup the global checkbox
   $('#global_check').change(function(){
      var global_checked = this.checked;
      $('td.check input[type=checkbox]').each(function(){
         this.checked = global_checked;
         $(this).change();
      });
   });

   // If a quick search is on the page
   if($('#quick_search').length > 0)
   {
      // Get all the post contents
      var all_contents = $('.posttext').text();
      var hidden = 0;

      // Hide the tags that aren't in the text
      $('#quick_search_tags li').each(function(){
         tag_search = new RegExp($(this).text(), "i");
         if(!tag_search.test(all_contents))
         {
            $(this).hide();
            hidden++;
         }
      })

      // If all tags are hidden, hide the title too
      if($('#quick_search_tags li').length == hidden)
      {
         $('#quick_search_tags').hide();
      }

      // Setup the search button
      $('#search_term').change(function(){
        $('#quick_search a').attr('href', $('#quick_search a').attr('href').replace(/^(\/help\/topics\/quick_search\?locale\=[^&]*&search_term\=).*?$/, '$1' + encodeURIComponent($(this).val())));
        $('#quick_search a').attr('title', $(this).val());
      }).change();

      // Setup enter/return capturing for the search button
      $('#search_term').keypress(function(event) {
		   if(event.keyCode == 13)
		   {
            $(this).change();
            $('#quick_search a').click();
		      return false;
		   }
		});
		
		$('#quick_search a').click(function(event){
         event.preventDefault();
         $.fn.colorbox({href: 'https://' + document.domain + $('#quick_search a').attr('href'), title: $('#search_term').val(), transition:"elastic", width:"75%", height:"75%", iframe:true, open:true});
		});
   }

   var hide_optional_fields = true;

   $('.optional_fields input, .optional_fields select').each(function(){
      if($(this).val() != '')
      {
         hide_optional_fields = false;
      }
   });

   if(hide_optional_fields)
   {
      $('.optional_fields').hide();
      $('.optional_fields').parent().append('<span class="show_optional_fields">Show Optional Fields</span>');
   }
   else
   {
      $('.optional_fields').parent().append('<span class="show_optional_fields">Hide Optional Fields</span>');
   }

   $('.show_optional_fields').click(function(){
      $('.optional_fields').toggle();
      $('.show_optional_fields').text( $('.show_optional_fields').text() == 'Show Optional Fields' ?  'Hide Optional Fields' : 'Show Optional Fields'  );
   });
   
   $('.bundle .name').addClass('expanded').click(function(){
      if($(this).hasClass('collapsed'))
      {
         $(this).removeClass('collapsed');
         $(this).addClass('expanded');
         $('.' + $(this).attr('id')).show();
      }
      else
      {
         $(this).removeClass('expanded');
         $(this).addClass('collapsed');
         $('.' + $(this).attr('id')).hide();
      }
   });
   
});