$(document).ready(function()
{		
    
	/* Browsers */
	/*--------------------------------------*/
	$iphone = (navigator.userAgent.indexOf('iPhone') != -1);
	$itouch = (navigator.userAgent.indexOf('iPod') != -1);
	$msie = $.browser.msie;
	$safari = $.browser.safari;
	
	/* Form Tabs */
	/*--------------------------------------*/
	$("#greenroom-content_main .tabs").tabs(
	{
		show: 	function(event, ui)
				{
					$("#greenroom-content_main .tabs").show();
					
					if(ui.index == $(this).tabs('length')-1)
					{
						$('[name=next_tab]').hide();
					}
					else
					{
						$('[name=next_tab]').show().attr('rel', ui.index+1);
					}
					
					$('#'+ui.panel.id+' :input:first').focus();
				}
	});
	
	$('[name=next_tab]').click(function()
	{
		$("#greenroom-content_main .tabs").tabs('select', parseInt($(this).attr('rel')));
	});
	
	/* Module Select */
	/*--------------------------------------*/
	var $nav_module_current = $('#greenroom-nav_module_select').val();
	
	
	function doNothing(args)
	{
    // This method doesn't do anything but without it the menu system has issues!
	}
	
	
	if(!$itouch && !$iphone)
	{
		$('#greenroom-nav_module_select').selectbox(
		  { onChangeCallback: doNothing } 
    );
		
		$('#greenroom-nav_module_select_container li').click(function()
		{
			change_module($nav_module_current);			
		});
	}
	else
	{
		$("#greenroom-nav_module_select").show();
		
		$('#greenroom-nav_module_select').change(function()
		{
			change_module($nav_module_current);
		});
	}
	
	/* Anchors */
	/*--------------------------------------*/
	$('a[rel^='+$module+'][rel$='+$func+']').addClass('active');
	
	/* Login */
	/*--------------------------------------*/
	$('.login #username').focus();
	
	/* Functions */
	/*--------------------------------------*/
	$('#greenroom-nav_functions li').click(function()
	{
		$(this).siblings().children('a')
		.filter('.active')
		.addClass('disabled');

		$(this).children('a')
		.addClass('active');
	});
	
	$('.tabs a').keypress(function(e)
	{
		if(e.charCode == 32)
			$(this).click();
	});
	
	$('.submit a').keypress(function(e)
	{
		if(e.charCode == 32)
			window.location = $(this).attr('href');
	});
	
	/* Checkboxes */
	$('table.checkbox tr')
	.filter(':has(:checkbox:checked)')
	.addClass('selected')
	.end()
		.live('click', function(event)
		{
			$(this).toggleClass("selected");
			
			if (event.target.type !== "checkbox") 
			{
				checkbox = $(":checkbox", this);
				checkbox.attr("checked", checkbox.is(':not(:checked)'));
			}
		});
		
		
	/* Wysiwyg */
	$('.richtext').wysiwyg(
	{
		controls:
		{
			insertImage:  { visible: false },
		
			separator06:  { visible: false },
			
			h1mozilla: { visible: false },
			h2mozilla: { visible: false },
			h3mozilla: { visible: false },
			
			h1: { visible: false },
			h2: { visible: false },
			h3: { visible: false },
		
			separator08: { visible: false },
			
			increaseFontSize: { visible: false },
			decreaseFontSize: { visible: false }
			
		}
	});
	
	$('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
	
	
	
	/* Automatic Contact Details Completer!*/
  /*--------------------------------------*/

  var contacts;

  /* If we are on either the create or edit pages for projects */
  if($module == 'projects' && $('#Contact-Information').length > 0)
  {
    $('#Contact-Information').prepend('<div id="existing_contact_label" class="label"><span id="pid" class="label">Existing Contact?</span><input id="existing_contact" type="checkbox" name="existing_contact" value="yes" /></div>');
  }

  $('#existing_contact').change(function()
  { 
    checked_value = $(this).attr('checked');
    use_existing_contact(checked_value);
  });


  function use_existing_contact(status)
  {
    if(status == true)
    {
      /* Set our input fields to read only so they won't be modified by the user */
      $('#Contact-Information').children().children('input:gt(0)').attr('readonly', 'true').addClass('readonly');
      /* Add a dropdown to allow the user to select an existing contact*/
      $('#existing_contact_label').append('<div id="select_existing_contact" class="label"><span id="pid" class="label">Existing Contact</span><select id="existing_contact_select"></select></div>');
      fill_existing_contact();
    }
    else
    {
      $('#Contact-Information').children().children('input:gt(0)').attr('readonly', '').removeClass('readonly');
      $('#select_existing_contact').remove();
    }
  }


  function fill_existing_contact()
  {

    $.ajax(
      {
        url: '/admin/projects/json_contacts',
        dataType: 'json',
        success: function(response)
        {
          $('#existing_contact_select').html('');
          for(i in response)
          {
            $('#existing_contact_select').append('<option value="' + response[i].id + '">' + response[i].name + '</option>');
          }
          
          update_contact_input_fields();
          
        }
    });
  }

  $('#existing_contact_select').live('change', function()
  {
    update_contact_input_fields();
  });
	  
	  
	function update_contact_input_fields()
	{
	  $.ajax(
      {
        url: '/admin/projects/json_contacts/' + $('#existing_contact_select').val(),
        dataType: 'json',
        cache: 'false',
        success: function(response)
        {
          $('#form-name').val(response[0].name);
          $('#form-email').val(response[0].email);
          $('#form-phone').val(response[0].phone);
          $('#form-mobile').val(response[0].mobile);
          $('#form-address').val(response[0].address);
        }
    });
	}

});
	
/* Module Select */
/*--------------------------------------*/
function change_module($nav_module_current)
{
	var $nav_module_updated = $('#greenroom-nav_module_select').val();
	
	if($nav_module_updated != $nav_module_current)
	{
		window.location = $base_url+$nav_module_updated;
	}
}

    

