// Use jQuery via jQuery(...)
jQuery(document).ready( function($) {
   /*
	*
	* expand/collapse asset details
	*
	*/
   $('#asset_click_me').toggle(
	   function() {
		   $('#asset_breakdown').slideDown();
		   $(this).text('less details');
	   },
	   function() {
		   $('#asset_breakdown').slideUp();
		   $(this).text('more details');
	   }
   );
   /*
	*
	* expand/collapse loan details
	*
	*/
   $('#loan_click_me').toggle(
	   function() {
		   $('#loan_breakdown').slideDown();
		   $(this).text('less details');
	   },
	   function() {
		   $('#loan_breakdown').slideUp();
		   $(this).text('more details');
	   }
   );
   /*
	*
	* jquery suggest (autocomplete)
	*
    * caching is on by default
    *
	*/
   /* $("#suggest").suggest("jquery_search.php",{ onSelect: function() { alert("You selected: " + this.value ) } } ); */

   //$("#suggest").suggest("jquery_search.php", { minchars:5 } ); // the syntax specifying the option is
                                                                // javascript object notation
                                                                // (see http://www.dyve.net/jquery/autocomplete.txt)
                                                                //
                                                                // see the bottom of the suggest source code
                                                                // for other options
   /*
	*
	* clue tips for glossary terms
	*
	*/
   $("#asset, #lnlsnet, #lnre, #lnag, #lnci, #lncon, #lnatres, #chbal, #sc, #frepo, #trade, #bkprem, #ore, #intan, #idoa, #liab, #netinc").cluetip({
		 local:true // get contents locally (ie, from same page and not via Ajax)
		,cluetipClass:'jtip' // jtip theme styling (see the jquery.cluetip.css file)
		,hideLocal:true // hide the local contents
		,sticky:true // the tip does not go away after click
		,closePosition:'bottom' // the location of the 'close' button
		,arrows:true // display the 'arrow' for the cluetip
		,activation:'click' // click on the element to show the cluetip (not mouseover)
		,showTitle:false // don't show the title bar in the cluetip
		,closeText: '[X] Close'
		,positionBy:'mouse' // cluetip position is determined by location of mouse
		//,topOffset: -5
		//,fx: {
		//      open:       'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
		//      openSpeed:  '0'
		//     }
   });
   /*
	*
	* Validate the search form before submission
	*
	*/
   $("#bank_search_form").submit( function() {
	   var error_arr = new Array();
	   var error_msg = '';
	   ret_val = true;
	   var val = $('#suggest').val();
	   var trim_val = val.replace(/^\s+|\s+$/g, '');
	   var trim_val_lc = trim_val.toLowerCase()
	   if( !trim_val ) {
		   error_arr.push('Search term cannot be empty');
	   }
	   else {
		   if( trim_val.length < 3 ) {
			   error_arr.push('Search term must be at least 3 characters');
		   }
		   if( trim_val_lc == 'bank'
			   || trim_val_lc == 'the'
			   || trim_val_lc == 'com'
			   || trim_val_lc == 'comm'
			 ) {
			   error_arr.push('Search term cannot be "' + trim_val + '"');
		   }
	   }
	   if( error_arr.length > 0 ) {
		   ret_val = false;
		   error_msg += "Please fix the following search error";
		   if( error_arr.length > 1) { error_msg += "s"; }
		   error_msg += ":<br />\n<ol>\n";
		   for( i in error_arr ) {
			   error_msg += "    <li>" + error_arr[i] + "</li>\n";
		   }
		   error_msg += "</ol>\n";
		   $("#error_msg").html(error_msg).show();
		   $("#search_results").hide();
		   //$("#suggest").focus().css({border: '2px solid #993333'});
	   }
	   return ret_val;
   });
   /*
    *
	* display graph data via ClueTip plugin
	*
	*/
   $('#asset_graph, #liab_graph, #netinc_graph').cluetip({
		 cluetipClass:'jtip' // jtip theme styling (see the jquery.cluetip.css file)
		,sticky:true // the tip does not go away after click
		,closePosition:'title' // the location of the 'close' button
		,arrows:true // display the 'arrow' for the cluetip
		,activation:'click' // click on the element to show the cluetip (not mouseover)
		,closeText: '[X] Close'
		,positionBy:'mouse' // cluetip position is determined by location of mouse
		,width: 200
   });
   /* ratio table needs bigger width */
   $('#ratio_graph').cluetip({
		 cluetipClass:'jtip' // jtip theme styling (see the jquery.cluetip.css file)
		,sticky:true // the tip does not go away after click
		,closePosition:'title' // the location of the 'close' button
		,arrows:true // display the 'arrow' for the cluetip
		,activation:'click' // click on the element to show the cluetip (not mouseover)
		,closeText: '[X] Close'
		,positionBy:'mouse' // cluetip position is determined by location of mouse
		,width: 350
   });
   /*
    *
    * set focus on seach box
	*
	*/
   $('#suggest').focus();
   /*
    *
    * cluetip for Performance Ratings key
	*
	*/
	$("#performance_ratings").cluetip({
		 local:true // get contents locally (ie, from same page and not via Ajax)
		,cluetipClass:'jtip' // jtip theme styling (see the jquery.cluetip.css file)
		,hideLocal:true // hide the local contents
		,sticky:false // the tip does not go away after click
		,closePosition:'bottom' // the location of the 'close' button
		,arrows:true // display the 'arrow' for the cluetip
		,activation:'hover' // click on the element to show the cluetip (not mouseover)
		,showTitle:false // don't show the title bar in the cluetip
		//,closeText: '[X] Close'
		,positionBy:'mouse' // cluetip position is determined by location of mouse
		,width: 200
		//,topOffset: 50
		//,fx: {
		//      open:       'fadeIn', // can be 'show' or 'slideDown' or 'fadeIn'
		//      openSpeed:  '0'
		//     }
   });
}); /* end DOM ready method */
