/*
 * Print Customization
 * Allow people to add/suppress certain page elements from printing. All options
 * are added via the reportmenu module, except for Perspective Reports which
 * are added via this file.
 *
 * TODO: most of the form is defined in reportmenu.module, with the exception of
 * 		perspective reports which are only defined in this file. we may want to
 *		consider moving all print customization to this file.
 *
 * Property
 *    - Personal Info (logo / contact info)
 *    - Managed By
 *    - Leasing Contact (building)
 *    - Leasing Contact (suite)
 *    - Building Description (paragraph)
 *    - Additional Rent (block)
 *    - Info Date (suites table column)
 *    - Google Map
 *		- Suite Comments
 *
 * Stacked Availability (TBD)
 *    - Table blocks of rows
 *    - Page breaks after stacked graphs
 *
 * Stacked Availability (May 2010)
 *   - Building Images
 *   - Building Details
 *   - Occupancy Costs
 *   - Available Area
 *   - Vacant Area
 *
 * Stacked Availability
 *   - Add ability to add comments underneath each building
 *   
 * Reports (Perspective)
 * if a report has more than one table on the page, allow people the ability to
 * supress any of the tables
 */

$(document).ready(function(){

	// if a floorplan displays a generic image hide it from print
	if ($('#floorplan').length) {
		$('#floorplan img[src*="/_generic/"]').parent().addClass('no-print');
	}
	// if the floorplan is being hidden from print disable it as a print option
	if ($('#floorplan').hasClass('no-print')) {
		$('#print-options input#print-floorplan').attr('disabled', 'disabled');
	}

	/*
	 * Print Buildings
	 * for building teasers, if a building has a page break in the middle of the
	 * summary, the layout gets afffected because the right content doesn't
	 * maintain it's floated position on the next page (if the left doesn't
	 * extend to this page too)
	 * 
	 * each page can print about 2 summaries per page, so add classes and markup
	 * for this.
	 */
	$('.building.teaser:nth-child(odd)').addClass('even');
	$('.building.teaser:nth-child(even)').addClass('odd');
	
	/* ideally building pages will fit on one page, but when there is too much
	 * content for one page, keep the page column alignment on additional pages
	 * by setting the left column height the same as the right.
	 * 
	 * TODO / NOTE: this works for keeping the print layout BUT, there's an issue
	 *   when the page has columns that wrap (takes the height before the wrap
	 *   fix has been applied
	 */
	/*if ($('.building').length && $.browser.msie) {
	 $('.building .left:first').height($('.building .right:first').height());
	}*/
	
	/*
	 * Print Options
	 */
	
  /* hide the print options box */
  var print_options = $('#print-options');
  print_options.hide();
  
  /* hide elements to start with */
  $('.hide').hide();
  
  /*
   * Link
   * expand / collapse print options box
   */
  $('ul.menu a.print').click(function() {
    /* open the print suppress options box, unless there are no suppressable
     * elements, then we'll just show the print dialogue */
    if ($('#print-options input[type="checkbox"]').size()) {
      print_options.toggle();
			// add/remove class to print options link
			link = $('#menu-report a.print');
			if (link.hasClass('expanded')) {
				link.removeClass('expanded');
				link.parent().removeClass('expanded');
				//link.text(link.text().replace('‹ ', ''));
				
				// remove style attr
				link.parent().next().removeAttr('style');
			} else {
				link.addClass('expanded');
				link.parent().addClass('expanded');
				//link.prepend('&lsaquo; ');
				
				// link is being abs positioned, so move the next element over
				/*var margin_left = link.outerWidth(true) + 13; //(13 = left margin + border)
				link.parent().next().css('margin-left', margin_left);*/
			}
    } else {
      window.print();
    }
    
    // return false to prevent the browser going to the href
    return false;
  });
  
  /*
   * Submit (Print)
   * popup the browser print dialogue
   */
  $('#print-options input[type="submit"]').click(function() {
    window.print();
    
    // return false to prevent the form from being submitted
    return false;
  });
  
  /*
   * Check for Elements
   * there can be form options that don't exist on a page. if they don't exist,
   * remove them.
   *
   * target element can be an id or class
   */
  $('#print-options input[type="checkbox"]').each(function() {
    var elem = $(this).attr('value');
    
    if (!$('#' + elem).length && !$('.' + elem).length) {
      $(this).parent().remove(); 
    }
  });
  
  /*
   * Builing Comments (Stacked Availability)
   * add maxlength to textareas
   */
  var max_length = 144;
  $('.print-comments textarea').after('<div class="descr">Maximum Characters: ' + max_length + '</div>');
  
  $('.print-comments textarea').keypress(function(event) {
    var max_length = 144;
    var key = event.which;
    // all keys including return.
	  if (key >= 33 || key == 13) {
      if (this.value.length > max_length) {
        event.preventDefault();
      }
    }
  });
	
	/*
	 * Reports (Perspective)
	 * if a report has more than one table on the page, allow people the ability
   * to supress any of the tables
   * 
   * NOTE: options are added by the reportmenu module, EXCEPT in this *special* case
	 */
	if ($('table.report').size() > 1) {
		//print_options.toggle();
		$('table.report').each(function(i, val) {
			// add the table id
			// we need to also hide the tabs that appear just above the table, so
		  // instead of applying an id we'll apply the same class to both
			var tid = 'report-' + (i+1);
			$(this).addClass(tid);
			$(this).prev('.tabs').addClass(tid);
			
			// add this table to the print options
			option = '<label class="option">';
			option += '<input size="5" name="' + tid + '" id="print-' + tid + '" class="form-checkbox" value="' + tid + '" checked="checked" type="checkbox">';
			option += '<span>' + $(this).find('.caption th').html() + '</span>';
			option += '</label>';
		  $('#print-options form .form-item').append(option);
		});
	}
	
  /*
   * Print Customize
   * the value of the checkbox contains the id/class of the element to hide
   *       
   * BUG (IE): IE doesn't trigger a change event immediately as a box is
   *           checked / unchecked. Appears that you have to check the box AND
   *           then click somewhere else on the page for the change event to trigger.
   *           
   *           So we have to use the 'click' event for IE. The problem is that a
   *           click event can trigger when a checkbox is clicked but nothing's
   *           changed. In this instance I don't think it's a problem. Unsure
   *           if there are any accessibility issues with using 'click' event.    
   */
  //$('#print-options input[type="checkbox"]').change(function() {
	//$('#print-options input[type="checkbox"]').click(function() {
	$('#print-options input[type="checkbox"]').bind(($.browser.msie ? 'click' : 'change'), function () {
    var elem = $(this).attr('value');
    
    // is this an id/class
    if ($('#' + elem).length) {
      elem = $('#' + elem);
    } else {
      elem = $('.' + elem);
    }
    
    // show/hide the element
    if (!elem.hasClass('hide')) {
      elem.addClass('hide');
      elem.hide();
    } else {
      elem.removeClass('hide');
      elem.show();
    }
		
		// if this is a perspective report page, don't let people suppress all
		// tables. if only one table is visible, disable the checkbox.
		if ($('table.report').size()) {
			if ($('table.report.hide').size() + 1 == $('table.report').size()) {
				$('#print-options input[id^="print-report-"]').each(function() {
					if ($(this).is(':checked')) {
						$(this).attr('disabled', 'disabled');
					}
				});
			} else {
				$('#print-options input[id^="print-report-"]').each(function() {
					$(this).removeAttr('disabled');
				});
			}
		}
		
  });
	
	/*
	 * Building / Suite View Toggle
	 * on a building details page you can toggle between viewing all suites in a
	 * building, and an individual suite.
	 * 
	 * TODO: since this toggle is done via ajax, any print options that are selected
	 * are not reflected in the newly loaded content. we need the newly loaded
	 * content to reflect any print options that have already been set.
	 * 
	 * UPDATE: This may not be necessary anymore as the building / suite links
	 *         are about to change from ajax loads to page loads.
	 */

});