// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

//var $j = jQuery.noConflict();

//$j(document).ready( function() {
//  enableTabs('fund-tabs');
  
//});

//function enableTabs(tabs_container_id) {
//  if( $j('#' + tabs_container_id).length > 0 ) {
//    $j('#' + tabs_container_id).tabs();
//  }
//}


$(document).ready( function() {
  enableTabs('about-us-tabs');
  enableTabs('analysts-tabs');
  enableTabs('documents-tabs');
  enableYearDropDown();
  //enable_library_document_type_on_change(); //not needed in this project
  init_sucker();
});

var jSucker = function(css_id, css_class){
		return {
			
			css_class: "",
			css_id: "",
			
			init: function( css_id, css_class ){
				// if this is ie6
				if( document.all && document.getElementById){
					this.css_id = css_id;
					this.css_class = css_class;
					var lis = css_id;
					jQuery(lis).mouseover( this.over );
					jQuery(lis).mouseout( this.out );
				}
			},
			
			over: function(){
				jQuery(this).addClass(jSucker.css_class);
			},
			
			out: function(){
				jQuery(this).removeClass(jSucker.css_class);
			}
			
		};
}();

var init_sucker = function(){
	jSucker.init(".dropdown", "over");
}

function enableTabs(tabs_container_id) {
  if( $('#' + tabs_container_id).length > 0 ) {
    $('#' + tabs_container_id).tabs();
  }
}

var enableYearDropDown = function() {
  if( $('.show_year_form').length > 0 ) {
    $('.show_year_form select').change( submit_year_form );
  }  
}

var submit_year_form = function() {
  $(this).parent('form').submit();
}

var enable_library_document_type_on_change = function() {
  if( $('#new_library_document').length > 0 ) {
    $('#new_library_document #library_document_library_document_type_id').change( get_remote_fields_for_document_type );
  }
} 

var get_remote_fields_for_document_type = function() {
  $('#doc_type_fields').load("/library_documents/fields_for_doc_type", 
                             "library_document_type_id=" + $('#library_document_library_document_type_id').attr("value"),
                             function(){ $('#new_library_document').change( check_if_exists ); } );
}

var check_if_exists = function() {
  $.get("check_if_exists", { library_document_type_id: $('#library_document_library_document_type_id').attr("value"),
                             strategy_id:              $('#library_document_strategy_id').attr("value"),
                             fund_id:                  $('#library_document_fund_id').attr("value"),
                             quarter:                  $('#library_document_quarter').attr("value"),
                             year:                     $('#date_year').attr("value"),
                             month:                    $('#date_month').attr("value") },
                           function(data){ $('#suggest1').html(data); } );
}

var setNewWindow = function(){
	var links = $("a[rel=external]").bind("click", function(){
		var location = $(this).attr("href");
		window.open(location,"New Window");
		return false;
	})
}
$(document).ready(setNewWindow)

