// Convention: 
// 1) all document text objects are labeled: tx_object


// ==========================
// = Action Function Makers =
// ==========================
function makeScrollnToggleDelay(policy_el){
    return function() {
        var elem_displayed = $(policy_el).css('display');
        if (elem_displayed == "none"){
            $(policy_el).show(); // must show before scrolling
            $('#policy_pane').scrollTo( (policy_el), 200, {offset:-50});
            $(policy_el).hide();
        }
        else {
            $('#policy_pane').scrollTo( (policy_el), 200, {offset:-50});
        }
        setTimeout(function() { 
                $(policy_el).toggle('slow').highlightFade(default_color);
                setTimeout(function() {
                    var displayed = $(policy_el).css('display');
                    if(displayed == 'block'){
                        $(policy_el).css({display: 'inline'});
                    }
                }, 1500);
            }, 300);
    };
}

function makeScrollnHideDelay(policy_el){
    return function() {
        var elem_displayed = $(policy_el).css('display');
        if(elem_displayed != 'none'){
            $('#policy_pane').scrollTo( (policy_el), 200, {offset:-50});
        }
        setTimeout(function() { 
            $(policy_el).highlightFade('black').hide('slow'); 
            setTimeout(function() {
                var displayed = $(policy_el).css('display');
                if(displayed == 'block'){
                    $(policy_el).css({display: 'inline'});
                }
            }, 1500);
        }, 200);
    };
}

function makeScrollnShowDelay(policy_el){
    return function() {
        
        $(policy_el).show(); // must show before scrolling
        $('#policy_pane').scrollTo( (policy_el), 200, {offset:-50});
        $(policy_el).hide();
        
        setTimeout(function() { 
            $(policy_el).highlightFade(default_color).show('slow'); 
            setTimeout(function() {
                var displayed = $(policy_el).css('display');
                if(displayed == 'block'){
                    $(policy_el).css({display: 'inline'});
                }
            }, 1500);
        }, 200);
    };
}


// ===========================
// = Event Callback Handlers =
// ===========================
function makeReplaceTextCallback(item){
    //makes functions that replaces text
    return function(e) {
        txt = $(item['form_el']).attr('value');
        $(item['policy_el']).text(txt).highlightFade(default_color);
    };
}

function makeToggleTextCallback(item){
    //makes functions that toggle text on and off
    return function(e) {
        $(item['policy_el']).each( function(i){
            setTimeout( makeScrollnToggleDelay(this), 1000*i );
        });
    };
}

function makeRadioCallback(item){
    //makes functions that toggle text on and off
    return function(e) {
        var timeout = 200;
        //turn on "on" elements
        for (var i in item['policy_el_on']){
            var el = item['policy_el_on'][i];
            $(el).each( function(j){
                var self;
                self = this;
                setTimeout( makeScrollnShowDelay(self), timeout);
                timeout += 1000;
              
            });
        }
        //turn off "off" elements
        for (var k in item['policy_el_off']){
            var el = item['policy_el_off'][k];
            $(el).each( function(z) {
               var self;
               self = this;
               setTimeout( makeScrollnHideDelay(self), timeout);
               timeout += 1000;
            });
        }


    };
}

// =========
// = Setup =
// =========
function bind_form (bindables, type) {
    // bind input elements to actions corresponding to policy text
    var item;
    
    if (type == 'textbox'){
        for (var i in bindables){
            item = bindables[i];
            $(item['form_el']).keyup( makeReplaceTextCallback(item) );
        }
    } else if ( type == 'checkbox'){
        for (var i in bindables){
            item = bindables[i];
            $(item['form_el']).click( makeToggleTextCallback(item) );
        }
    } else if ( type == 'radio') {
        for (var i in bindables){
            item = bindables[i];
            $(item['form_el']).click( makeRadioCallback(item) );
        }
    }
}


$(document).ready(function() {
	// Setup links between form elements and policy

	var textboxes  =   [{form_el: "#org_institution_name" ,                 policy_el: ".tx_org_institution_name" }, //
                       { form_el: "#org_licensing_office" ,                 policy_el: ".tx_org_licensing_office" }, //
                       { form_el: "#org_sponsored_programs_office" ,        policy_el: ".tx_org_sponsored_programs_office" }, //
                       { form_el: "#disclosure_form" ,                      policy_el: ".tx_disclosure_form" }, //
                       // { form_el: "#disclosure_form_link" ,                 policy_el: ".tx_disclosure_form_link" },
                       { form_el: "#money2inventor" ,                       policy_el: ".tx_money2inventor" },//
                       { form_el: "#money2research" ,                       policy_el: ".tx_money2research" },//
                       { form_el: "#money2university" ,                     policy_el: ".tx_money2university" }],
                       
        checkboxes =   [{ form_el:"#know_faculty" ,                           policy_el: ".tx_know_faculty" },//
                       { form_el: "#know_grad_students" ,                     policy_el: ".tx_know_grad_students" },//
                       { form_el: "#know_undergrad_students" ,                policy_el: ".tx_know_undergrad_students" },//
                       { form_el: "#know_visiting_scholars" ,                 policy_el: ".tx_know_visiting_scholars" },//
                       { form_el: "#know_professional_staff" ,                policy_el: ".tx_know_professional_staff" },//
                       { form_el: "#know_chancellors" ,                       policy_el: ".tx_know_chancellors" },//
                       { form_el: "#know_provost" ,                           policy_el: ".tx_know_provost" },//
                       { form_el: "#know_deans" ,                             policy_el: ".tx_know_deans" },//
                       { form_el: "#know_administrative_staff" ,              policy_el: ".tx_know_administrative_staff" },//
                       { form_el: "#know_all" ,                               policy_el: ".tx_know_all" },//
                       { form_el: "#disclose_patents" ,                       policy_el: ".tx_disclose_patents" },//
                       { form_el: "#disclose_utility_models" ,                policy_el: ".tx_disclose_utility_models" }, //
                       { form_el: "#disclose_industrial_designs" ,            policy_el: ".tx_disclose_industrial_designs" }, //
                       { form_el: "#disclose_copyright_literary" ,            policy_el: ".tx_disclose_copyright_literary" }, //
                       { form_el: "#disclose_trademarks" ,                    policy_el: ".tx_disclose_trademarks" }, //
                       { form_el: "#disclose_newplants" ,                     policy_el: ".tx_disclose_newplants" }, //
                       { form_el: "#disclose_trade_secrets" ,                 policy_el: ".tx_disclose_trade_secrets" }, //
                       { form_el: "#nonsig_minimal_funds" ,                   policy_el: ".tx_nonsig_minimal_funds" },//
                       { form_el: "#nonsig_outside_scope" ,                   policy_el: ".tx_nonsig_outside_scope" },//
                       { form_el: "#nonsig_personal_time" ,                   policy_el: ".tx_nonsig_personal_time" },//
                       { form_el: "#nonsig_minimal_facilities" ,              policy_el: ".tx_nonsig_minimal_facilities" },//
                       { form_el: "#nonsig_space" ,                           policy_el: ".tx_nonsig_space" }, //
                       { form_el: "#nonsig_equipment" ,                       policy_el: ".tx_nonsig_equipment" },//
                       { form_el: "#nonsig_office_equipment" ,                policy_el: ".tx_nonsig_office_equipment" },//
                       { form_el: "#nonsig_comp" ,                            policy_el: ".tx_nonsig_comp" },//
                       { form_el: "#nonsig_secretarial_services" ,            policy_el: ".tx_nonsig_secretarial_services" },//
                       { form_el: "#nonsig_assistants" ,                      policy_el: ".tx_nonsig_assistants" },//
                       { form_el: "#nonsig_supplies" ,                        policy_el: ".tx_nonsig_supplies" },//
                       { form_el: "#nonsig_library" ,                         policy_el: ".tx_nonsig_library" },//
                       // { form_el: "#nonsig_advance_approval" ,                policy_el: ".tx_nonsig_advance_approval" },
                       // { form_el: "#nonsig_compensates" ,                     policy_el: ".tx_nonsig_compensates" },
                       { form_el: "#cons_notify_consultancy" ,                policy_el: ".tx_cons_notify_consultancy" },//
                       { form_el: "#cons_non_significant_consultant" ,        policy_el: ".tx_cons_non_significant_consultant" }, //
                       { form_el: "#cons_obligated_to_inform" ,               policy_el: ".tx_cons_obligated_to_inform" },//
                       { form_el: "#cons_disclose_obligations" ,              policy_el: ".tx_cons_disclose_obligations" },//
                       { form_el: "#licensing_first_rights" ,                 policy_el: ".tx_licensing_first_rights" },//
                       { form_el: "#licensing_non_exclusive" ,                policy_el: ".tx_licensing_non_exclusive" },//
                       { form_el: "#licensing_rescind" ,                      policy_el: ".tx_licensing_rescind" },//
                       { form_el: "#licensing_reimburse" ,                    policy_el: ".tx_licensing_reimburse" },//
                       { form_el: "#licensing_publishing_rights" ,            policy_el: ".tx_licensing_publishing_rights" },//
                       { form_el: "#licensing_sub_license" ,                  policy_el: ".tx_licensing_sub_license" },//
                       { form_el: "#licensing_non_commitment" ,               policy_el: ".tx_licensing_non_commitment" }],//

          radios =     [{ form_el:"#own_sponsored_research1" ,			 policy_el_on: [ ".tx_own_sponsored_research1" ],             policy_el_off: [ ".tx_own_sponsored_research2" ] },//
                       { form_el: "#own_sponsored_research2" ,			 policy_el_on: [ ".tx_own_sponsored_research2" ],             policy_el_off: [ ".tx_own_sponsored_research1" ] },//
                       // { form_el: "#own_sponsored_research3" ,            policy_el_on: [ ".tx_own_sponsored_research3" ],             policy_el_off: [ "" ] },
                       { form_el: "#own_commissioned_work1" ,			 policy_el_on: [ ".tx_own_commissioned_work1" ],              policy_el_off: [ ".tx_own_commissioned_work2" ] },//
                       { form_el: "#own_commissioned_work2" ,			 policy_el_on: [ ".tx_own_commissioned_work2" ],              policy_el_off: [ ".tx_own_commissioned_work1" ] },//
                       // { form_el: "#own_commissioned_work3" ,             policy_el_on: [ ".tx_own_commissioned_work3" ],              policy_el_off: [ "" ] },
                       { form_el: "#own_sig_use1" ,					     policy_el_on: [ ".tx_own_sig_use1" ],                        policy_el_off: [ ".tx_own_sig_use2" ] },//
                       { form_el: "#own_sig_use2" ,					     policy_el_on: [ ".tx_own_sig_use2" ],                        policy_el_off: [ ".tx_own_sig_use1" ] },//
                       // { form_el: "#own_sig_use3" ,                   policy_el_on: [ ".tx_own_sig_use3" ],                        policy_el_off: [ "" ] },
                       { form_el: "#copy_instructional_1" ,			     policy_el_on: [ ".tx_copy_instructional_1" ],                policy_el_off: [ ".tx_copy_instructional_2" ] }, //
                       { form_el: "#copy_instructional_2" ,			     policy_el_on: [ ".tx_copy_instructional_2" ],                policy_el_off: [ ".tx_copy_instructional_1" ] }, //
                       { form_el: "#copy_theses_1" ,					 policy_el_on: [ ".tx_copy_theses_1" ],                       policy_el_off: [ ".tx_copy_theses_2", ".tx_copy_theses_3", ".tx_copy_theses_4" ] },//
                       { form_el: "#copy_theses_2" ,					 policy_el_on: [ ".tx_copy_theses_2" ],                       policy_el_off: [ ".tx_copy_theses_3", ".tx_copy_theses_4", ".tx_copy_theses_1" ] },//
                       { form_el: "#copy_theses_3" ,					 policy_el_on: [ ".tx_copy_theses_3" ],                       policy_el_off: [ ".tx_copy_theses_2", ".tx_copy_theses_4", ".tx_copy_theses_1" ] },//
                       { form_el: "#copy_theses_4" ,					 policy_el_on: [ ".tx_copy_theses_4" ],                       policy_el_off: [ ".tx_copy_theses_3", ".tx_copy_theses_2", ".tx_copy_theses_1" ] },//
                       { form_el: "#copy_publications_1" ,				 policy_el_on: [ ".tx_copy_publications_1" ],                 policy_el_off: [ ".tx_copy_publications_2", ".tx_copy_publications_3" ] },//
                       { form_el: "#copy_publications_2" ,				 policy_el_on: [ ".tx_copy_publications_2" ],                 policy_el_off: [ ".tx_copy_publications_1", ".tx_copy_publications_3" ] },//
                       { form_el: "#copy_publications_3" ,				 policy_el_on: [ ".tx_copy_publications_3" ],                 policy_el_off: [ ".tx_copy_publications_1", ".tx_copy_publications_2" ] },//
                       { form_el: "#copy_commissioned_1" ,				 policy_el_on: [ ".tx_copy_commissioned_1" ],                 policy_el_off: [ ".tx_copy_commissioned_2" ] }, //
                       { form_el: "#copy_commissioned_2" ,				 policy_el_on: [ ".tx_copy_commissioned_2" ],                 policy_el_off: [ ".tx_copy_commissioned_1" ] }, // 
                       { form_el: "#copy_license_to_uni_1" ,			 policy_el_on: [ ".tx_copy_license_to_uni_1" ],               policy_el_off: [ ".tx_copy_license_to_uni_2" ] },//
                       { form_el: "#copy_license_to_uni_2" ,			 policy_el_on: [ ".tx_copy_license_to_uni_2" ],               policy_el_off: [ ".tx_copy_license_to_uni_1" ] },//
                       // { form_el: "#copy_license_to_sponsor_1" ,      policy_el_on: [ ".tx_copy_license_to_sponsor_1" ],           policy_el_off: [ "" ] },
                       // { form_el: "#copy_license_to_sponsor_2" ,      policy_el_on: [ ".tx_copy_license_to_sponsor_2" ],           policy_el_off: [ "" ] },
                       { form_el: "#cons_will_negotiate_consulting1" ,	 policy_el_on: [ ".tx_cons_will_negotiate_consulting1" ],     policy_el_off: [ ".tx_cons_will_negotiate_consulting2" ] },//
                       { form_el: "#cons_will_negotiate_consulting2" ,	 policy_el_on: [ ".tx_cons_will_negotiate_consulting2" ],     policy_el_off: [ ".tx_cons_will_negotiate_consulting1" ] }];//
                    
   	bind_form (textboxes, 'textbox');
   	bind_form (checkboxes, 'checkbox');
   	bind_form (radios, 'radio');
   	
   	//Hide all policy elements we don't want to show up in the initial entry
   	$('.init_hide').hide();
   	
   //Set up navigation-movement
   $('#menu').localScroll({
   		target: '#options_window', //could be a selector or a jQuery object too.
   		axis:'x', //the default is 'y'
   		queue:true,
   		duration:425,
   		hash:false});

});

// =====================
// = Default Variables =
// =====================
var default_color = 'lime';




