jQuery().ready(function(){	

	// theme switcher
	if (jQuery('#switcher').length)
	{
	  jQuery('#switcher').themeswitcher();
	}

  // all hover and click logic for buttons
  jQuery(".fg-button:not(.ui-state-disabled)")
  .hover(
      function(){
          jQuery(this).addClass("ui-state-hover");
      },
      function(){
          jQuery(this).removeClass("ui-state-hover");
      }
  )
  .mousedown(function(){
          jQuery(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
          if( jQuery(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){jQuery(this).removeClass("ui-state-active");}
          else {jQuery(this).addClass("ui-state-active");}
  })
  .mouseup(function(){
      if(! jQuery(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
          jQuery(this).removeClass("ui-state-active");
      }
  });

	/*
		Javascript for list view only
	*/
	if (jQuery('.sf_admin_list').length)
	{
		// actions menu on list view
		jQuery('#sf_admin_actions_button').menu({ 
			content: jQuery('#sf_admin_actions_menu').html(),
			showSpeed: 300
		});
		
		// filter button to show the modal window of available filters
		jQuery('#sf_admin_filter_button')
			.addClass('sf_button-toggleable')
			.click(function(e) {
			  e.preventDefault();
				jQuery('.sf_admin_filter').dialog('isOpen') ? jQuery('.sf_admin_filter').dialog('close') : jQuery('.sf_admin_filter').dialog('open');
			});

		// modal window for filters
		jQuery('.sf_admin_filter').dialog({
			autoOpen: false,
			width: 600,
			close: function(evt, ui){
				jQuery('#sf_admin_filter_button').removeClass('ui-state-active');
			},
			open: function(evt,ui){
				jQuery('#sf_admin_filter_reset, #sf_admin_filter_submit').hide();
				jQuery('#sf_admin_filter_button').addClass('ui-state-active');
			},
			buttons: {
				"Filter": function() { 
					jQuery(this).dialog("close"); 
					jQuery('#sf_admin_filter_submit').parents('form').submit();
				}, 
				"Reset": function() { 
					jQuery(this).dialog("close");
					location.href = jQuery('#sf_admin_filter_reset').attr('href');
				} 
			}
		});

		// toggle table visibility on caption title
		jQuery('.sf_admin_list caption h1').click(function(){
		  jQuery('.sf_admin_list table tbody, .sf_admin_list table thead, .sf_admin_list table tfoot').toggle();
		});

		// mouseover and click on table row
		jQuery('.sf_admin_list table tbody tr')
		.hover(
			function() {
			  jQuery(this).addClass('ui-state-hover');
			},
			function() {
			  jQuery(this).removeClass('ui-state-hover');
			}
		)
		.click(function(e) {
			// change row color
			jQuery(this).toggleClass('ui-state-highlight');
			// change checkbox status
			var chx = jQuery(this).find('input:checkbox');
			if (jQuery(this).hasClass('ui-state-highlight')) jQuery(chx).attr('checked','checked');
			else jQuery(chx).removeAttr('checked');
		});

    // batch actions
    jQuery('.sf_admin_batch_actions_choice select').selectmenu({
      style: 'dropdown',
      width: 200
    });
	}

	/*
		Javascript for form view only
	*/
	if (jQuery('.sf_admin_form').length)
	{
    // tabs for form edition
    jQuery('#sf_admin_form_tab_menu').tabs().addClass('ui-tabs-vertical ui-helper-clearfix');
    jQuery('#sf_admin_form_tab_menu li').removeClass('ui-corner-top').addClass('ui-corner-all');

    // default size for input
    jQuery('input[type="text"], input[type="password"]').each(function(){
      if (jQuery(this).attr('size') == 0 || jQuery(this).attr('size') == 20) {
        jQuery(this).attr({ size: 60 });
      }
    });

    // default size for textarea
    jQuery('textarea').each(function(){
      if (jQuery(this).attr('rows') == 4 && jQuery(this).attr('cols') == 30) {
        jQuery(this).attr({ cols: 58, rows: 5 });
      }
    });

    // added focused state to inputs
    jQuery(':input')
    .focus(function() {
			//jQuery(this).closest('.sf_admin_form_row').addClass('focused ui-state-active ui-corner-all');
			//jQuery(this).closest('.sf_admin_form_row').addClass('focused ui-state-default ui-corner-all');
			jQuery(this).closest('.sf_admin_form_row:not(.ui-state-error)').addClass('focused ui-state-highlight ui-corner-all');
		})
		.blur(function() {
			//jQuery(this).closest('.sf_admin_form_row').removeClass('focused ui-state-active ui-corner-all');
			//jQuery(this).closest('.sf_admin_form_row').removeClass('focused ui-state-default ui-corner-all');
			jQuery(this).closest('.sf_admin_form_row:not(.ui-state-error)').removeClass('focused ui-state-highlight ui-corner-all');
		});

		// check error messages in all tabs
		jQuery('.ui-tabs-panel:has(span.ui-icon-alert)').each(function(i) {
		  jQuery('.ui-tabs-nav a[href=#' + this.id + ']').prepend('<span class="ui-state-error-text"><span class="ui-icon ui-icon-alert floatright"></span></span>');
		});
	}

});

