/*
File: internship/scripts/intern-browse-script.js

MAKE SURE YOU KEEP TRACK OF EVERYTHING YOU DO!

Author: Dwight VanTuyl
DD-MMM-YYYY: 14-Mar-2008
Purpose: Javascript fun for the internship browse page.
+++++++++++++
Modifications
+++++++++++++
Editor:		
DD-MMM-YYYY:
Watcha did:	
~~~~~~~~~~~~~
*/


Ext.namespace('Intern.browse');

Intern.browse.global = {

  init: function() {
    var form = $("form[name='browseForm']");
  
    //Format keywords text field to an ext text field
    var keywordsField = new Ext.form.TextField({
      name: 'keywords',
      emptyText: 'Search short and full description',
      width:310
    });
    keywordsField.applyTo('extKeywords');
  
    //Click on any browse selection to filter results
    $("select.selectBox").click(function(){
      
      //This is only for the ling field select box
      //Since the value is a number, we have to hold onto the classlabel to show in the "remove" link
      var displayValue = $(this).children(":selected").attr('display');
      if($(this).attr('name') == 'lingField'){
        $("input[name='lingFieldDisplay']").val(displayValue);
      }
      form.submit();
    });
    
    
    //If any browse selection has been made then a link to remove the selection is made availible
    //Click on the remove selection to remove it
    $("span#removeHasLocation").click(function(){
      $("input[name='hasLocation']").val('');
      form.submit();
    });

    $("span#removeComp").click(function(){
      $("input[name='comp']").val('');
      form.submit();
    });
    
    $("span#removeCountry").click(function(){
      $("input[name='country']").val('');
      form.submit();
    });
    
    $("span#removeRegion").click(function(){
      $("input[name='region']").val('');
      form.submit();
    });
    
	$("span#removeCity").click(function(){
      $("input[name='city']").val('');
      form.submit();
    });
	
    $("span#removeType").click(function(){
      $("input[name='type']").val('');
      form.submit();
    });
    
    $("span#removeLangName").click(function(){
      $("input[name='langName']").val('');
      form.submit();
    });
    
    $("span#removeLangFamily").click(function(){
      $("input[name='langFamily']").val('');
      form.submit();
    });
    
    $("span#removeLingField").click(function(){
      $("input[name='lingField']").val('');
      form.submit();
    });
    
    
    //click on a announcement row to open the announcement
    $("tr.announceRow").click(function(){
      document.location.href="/internship/browse/intern-browse-announceProcess.cfm?id=" + 
      $(this).attr('id');
    });
    
    
    //ADVANCED SEARCH TOGGLE  
    //Init advanced search on page load
    if($("input[name='showAdvanced']").val() == 'true'){
      $("div#advancedBrowseForm").show();
    }else{
      $("div#advancedBrowseForm").hide();    
    }
    
    //Open Advanced search instantly on click
    $("span#openAdv").click(function(){
      $("div#advancedBrowseForm").show();
      $("span#openAdv").hide();
      $("span#closeAdv").show();
      $("input[name='showAdvanced']").val('true');      
    });
    
    //Close Advanced search instantly on click
    $("span#closeAdv").click(function(){
      $("div#advancedBrowseForm").hide();
      $("span#openAdv").show();
      $("span#closeAdv").hide();
      $("input[name='showAdvanced']").val('false');      
    });
    
    
    //GOOGLE MAP TOGGLE  
    //Init advanced search on page load
    if($("input[name='showMap']").val() == 'true'){
      $("div#map_canvas").show();
    }else{
      $("div#map_canvas").hide();    
    }
    
    //Open Google map
    $("span#showMap").click(function(){
      $("input[name='showMap']").val('true');
      form.submit();      
    });
    
    //Close Google map
    $("span#hideMap").click(function(){
      $("input[name='showMap']").val('false');
      form.submit();
    });
    
    //HEADER ORDERING FUNCTIONS
    $("span.header").click(function(){
      var field = $(this).attr('field')
      
      //if the same column header is being clicked again then change the direction of order
      if(field == $("input[name='orderBy']").val()){
        if($("input[name='orderDirection']").val() == 'ASC'){
          $("input[name='orderDirection']").val('DESC');
        }else{
          $("input[name='orderDirection']").val('ASC');
        }
      //else order by the header being clicked
      }else{
        $("input[name='orderBy']").val(field);
      }
      form.submit();
    });
    
    
    //PAGE NAV FUNCTIONS
    $("span.pageNum").click(function(){
      $("input[name='currentPage']").val($(this).text());
      form.submit();
    });
    
    $("span#prevPage").click(function(){
      $("input[name='currentPage']").val($(this).attr('page'));
      form.submit();    
    });
    
    $("span#nextPage").click(function(){
      $("input[name='currentPage']").val($(this).attr('page'));
      form.submit();    
    });
  },
  
  //Resets all search filters to blank
  reset: function(){
    $("input[name='keywords']").val('');
    $("input[name='hasLocation']").val('');
    $("input[name='comp']").val('');
    $("input[name='country']").val('');
    $("input[name='region']").val('');
	$("input[name='city']").val('');
    $("input[name='type']").val('');
    $("input[name='langFamily']").val('');
    $("input[name='langName']").val('');
    $("input[name='lingField']").val('');
    $("input[name='orderBy']").val('DATESTAMP');
    $("input[name='orderDirection']").val('DESC');
    $("input[name='currentPage']").val('1');
    $("form[name='browseForm']").submit();
  },
  
  //Listen to the search button and just submit the form when clicked
  search: function(){
    $("form[name='browseForm']").submit();
  }
}

Ext.onReady(Intern.browse.global.init, Intern.browse.global);