$(function() {
    makeVisualChanges();
});

/* view port resizing */
$(document).ready(function() {
    resize_viewport();

    $('body#LoginBody input').keydown(function(e){
      if (e.keyCode == 13) {
        $(this).parents('form').submit();
        return false;
      }
    });     

		 $('form#new_account input').keydown(function(e){
	      if (e.keyCode == 13) {           
        	$(this).parents('form').submit();		
	        return true;
	      }
	    });          
	
			$('form#new_account').submit(function(e){
				$('div.button_group').block({ message: '<img src="/images/ajax-loader-grey.gif"/>', overlayCSS: { backgroundColor: '#FFF',opacity: 0.9}, border:	'0px'}); 						
		  });

		$("a.form_submit").click(function() {
			$(this).parents('div.button_group').block({ message: '<img src="/images/ajax-loader-grey.gif"/>', overlayCSS: { backgroundColor: '#FFF',opacity: 0.9}, border:	'0px'}); 		
		});

});

function makeVisualChanges() {
    substituteSubmitButtonsForStyledHyperlinks();
    observeTheLinksAndSubmitOnClick();
}

function changeBG(color){
	document.bgColor = color;
}

function substituteSubmitButtonsForStyledHyperlinks() {
	$('input.form_submit_link').each(function() {
		if ($(this).siblings("a.form_submit").size() == 0) {
			$(this).after('<a href="#" class="button form_submit"><span id="' + $(this).attr('id') + '">' + $(this).val() + '</span></a>')
			$(this).hide();
		}
	});
}

function observeTheLinksAndSubmitOnClick() {
    $('a.form_submit').live('click',
    function() {
      $(this).parents('form').trigger("submit");
			return false;
    });
}


$(window).resize(function() {
    $('body').height('');
    resize_viewport();
});

function resize_viewport() {
    if ( $.browser.msie ) { return; }
    var body_height = $('body').height();
    var body_offset_height = body_height + parseInt($('body').css('paddingTop')) + parseInt($('body').css('paddingBottom'));
    var window_height = $(window).height();
    var document_height = $(document).height();
    var offset = body_offset_height - body_height;
    if (document_height > window_height) {
        window_height = document_height;
        offset *= -1;
    }
    if (body_offset_height < window_height) {
        $('body').height(window_height - offset);
    }
}

var Messages = new function() {
  $(function() {
		$('a.add_attachment').click(function(e) {
	      var jq_obj = $(this);
	      var attachments = jq_obj.parents('.attachments').eq(0).find('div.attachment');
				var count = attachments.length;
				if (count > 0) {
					$('a.add_attachment').html('Attach another file');
				}				
							
	      attachments.eq(0).clone(true).each(function(i) {
	       	input = $(this).find('input[type=file]');
	        var id = input.attr('id');
	        var name = input.attr('name');
	        input.attr('id', id.replace(/_0_/, "_" + count + "_"));
	        input.attr('name', name.replace(/[0]/, "[" + count + "]"));
	        input.val('');
	        $(this).insertBefore(jq_obj);
					$(this).show();
	      });

	      return false;
	  });


		$('a.delete_attachment').live('click', function(e){
			var jq_obj = $(this);
	    var attachments = jq_obj.parents('.attachments').eq(0).find('div.attachment');
			var count = attachments.count
			if (count ==1) {
				attachments.find('input[type=file]').val('');
			} else {
				jq_obj.parent('.attachment').remove();								
			}
		});  
	
		$('div.form_group span.selective_notification a').click(function(e) {
	      $(this).parents('.form_group').eq(0).find('ul.recipients').toggle();
				$(this).parents('div.just_some').eq(0).toggle();
	      return false;
	  });

	  $('input.notify_all').change(function(e) {
	      var jq_obj = $(this);
	      if (jq_obj.is(':checked')) {
						$(this).parents('.form_group').eq(0).find('ul.recipients').toggle();
	          $(this).parents('.form_group').eq(0).find('ul.recipients').find('input[type=checkbox]').attr('checked', 'checked');
	      } else {
	          $(this).parents('.form_group').eq(0).find('ul.recipients').find('input[type=checkbox]').attr('checked', '');
	      }
	  });

	  $('ul.recipients input[type=checkbox]').click(function(e) {
	      $(this).parents('ul.recipients').prevAll('input.notify_all').attr('checked', '');
	  });  
	});
};

var PhaseChart = new function() {
  $(function() {
	  $('div.phase_chart ul.data_points li').tooltip({
	      delay: 250,
	      showURL: false,
	      bodyHandler: function() {
	          return "<div class=\"inner\">" + $(this).html() + "</div>";
	      }
	  });
	});
};

var ProjectSwitcher = new function() {
  $(function() {	
		$('div.project_details span.switcher').mouseenter(function() {
	      $('div#ProjectSwitcher').show();
	  });

	  $('div#ProjectSwitcher').mouseout(function(e) {
	      $(this).hide();
	  });

	  $('div#ProjectSwitcher').mouseover(function(e) {
	      $(this).show();
	  });
	});
};

var TimeSpendGraph = new function() {
  $(function() {		
		$('a.time_spend_nav').live('click', function(e) {
	      target = 'div#time_spent';
	      $.get(jQuery(this).attr('href'),
	      function(resp) {
	          time_spent_div = $(target);
	          time_spent_div.empty();
	          time_spent_div.html(resp);
	      });
	      return false;
	  });
	});
};

var DashboardChart = new function() {
	$(function() {
		$('div.quadrant_chart ul li a').tooltip({
	      delay: 250,
	      showURL: false,
	      bodyHandler: function() {
	          return "<div class=\"inner\">" + $(this).parents('li').eq(0).html() + "</div>";
	      }
	  });

	  $('div.quadrant_chart div.chart_group>ul').each(function(i) {
	      var jq_obj = $(this);
	      var padding = (72 - jq_obj[0].offsetHeight) / 2;
	      jq_obj.css({
	          'paddingTop': padding,
	          'paddingBottom': padding
	      });
	  });
	});
};

var GeneralTooltip = new function() {
  $(function() {
		$('span.help').tooltip({
	      delay: 250,
	      showURL: false,
	      bodyHandler: function() {
	          return "<div class=\"inner\">" + this.tooltipText + "</div>";
	      }
	  });
  });
};

var MessagesScreen = new function() {
	$(function() {
		$('select#message_responsibillity_id').change(function() {
			$('div#due_date_fields').find('input').removeAttr('disabled');
			$('div#due_date_fields').show();				
		});
		
		$('select#message_comment_message_attributes_responsibillity_id').change(function() {
			$('div#due_date_fields').find('input').removeAttr('disabled');
			$('div#due_date_fields').show();				
		});
		
		
	});
}; 

var FinancialOverview = new function() {
	$(function() {
		 $('select#weeks_to_show').change(function() {     
				$(this).parent('form').submit();
				//window.location.href = '/dashboard/financial_overview?weeks_to_show=' + $('select#weeks_to_show').val();
			});
	});
};