﻿ rangeForm = "closed";
var rangeHeight
var filterHeight
var rangeHeight
var btnArray
var data
var maxCompare = 4;
var totalCompare = 0;
var cboArray
var currentFormName = "fmFilter";

function pageInit(){


    // Hide the loading bar
    if($('loadBar')){
        $('loadBar').hide();
        $('loadBar').removeClassName('hidden');
    }

    // SUBMIT BUTTONS
    // ------------------------------
    if ($('searchBar')){
        btnArray = $('searchBar').getElementsByTagName("input");
        
        for (var i=0; i < btnArray.length; i++) {
            if (btnArray[i].type == "submit"){
                btnArray[i].onclick = function(){
                var currentForm = this.id
                    currentFormArray = currentForm.split("_")
                    currentFormName = currentFormArray[0]

                    //performSearch();
                   // return(false);
                }
            }
        }
    }
    
    // 'Filter the range' and 'Range finder' links
    // ------------------------------
    // Opens and closes the 'filter range/filter' forms
    if ($('fmFilterLink') && $('fmFinderLink')){
		
        
        // Work out which search type to show on page load
        if($('filterRange').value=="true"){
            $('fmFilterLink').className = "closedPanel"
            $('fmFinderLink').className = "openedPanel"
            filterAnim('filter');
            currentFormName = "fmFilter";
        }else{
            $('fmFinderLink').className = "closedPanel"
            $('fmFilterLink').className = "openedPanel"
            filterAnim('range');
            currentFormName = "fmFinder";
        }

        filterHeight = $('fmFilterLI').offsetHeight;
        rangeHeight = $('fmFinderLI').offsetHeight;
		rangeHeight = rangeHeight-5;
        
        $('fmFilterLink').onclick = function(){
            filterAnim('filter');
            return(false);
        }
        $('fmFinderLink').onclick = function(){
            filterAnim('range');
            return(false);
        }        
    }
//    performSearch();
}

function filterAnim(activeForm){

    // If the range form is closed - open it and close the filter form
    if (activeForm == "range"){
        $('filterRange').value="false"
        $('fmFinderLink').className = "closedPanel"
        $('fmFilterLink').className = "openedPanel"
		
        new Effect.Morph('fmFilterLI', {
          style: 'height:50px;', // CSS Properties
          duration: 0.3
        });
    
        new Effect.Morph('fmFinderLI', {
          style: 'height:'+rangeHeight+'px;', // CSS Properties
          duration: 0.3
        });
        
        new Effect.Morph('recordListDiv', {
          style: 'height:0px;padding-top:0px', // CSS Properties
          duration: 0.3
        });

        new Effect.Morph('navTrack1', {
          style: 'height:0px;', // CSS Properties
          duration: 0.3
        });
        
        new Effect.Morph('controller1', {
          style: 'height:0px;', // CSS Properties
          duration: 0.3
        });
/*        
        new Effect.Morph('recordList', {
          style: 'height:0px;', // CSS Properties
          duration: 0.3
        });
*/

//resetCheckboxes('fmFinder');
       
    }else{
    // Open the Filter form and close the Range form
        $('filterRange').value="true"
        $('fmFilterLink').className = "closedPanel"
        $('fmFinderLink').className = "openedPanel"

        new Effect.Morph('fmFilterLI', {
          style: 'height:'+filterHeight+'px;', // CSS Properties
          style: 'height:225px;', // CSS Properties
          duration: 0.3
        });
   
        new Effect.Morph('fmFinderLI', {
          style: 'height:55px;', // CSS Properties
          duration: 0.3
        });

        new Effect.Morph('recordListDiv', {
          style: 'height:283px;padding-top:17px; ', // CSS Properties
          duration: 0.3
        });

        new Effect.Morph('navTrack1', {
          style: 'height:300px;', // CSS Properties
          duration: 0.3
        });
        
        new Effect.Morph('controller1', {
          style: 'height:20px;', // CSS Properties
          duration: 0.3
        });
/*
        
        new Effect.Morph('recordList', {
          style: 'height:0px;', // CSS Properties
          duration: 0.3
        });
*/
        //resetCheckboxes('fmFilter');
    }
}

function resetCheckboxes(formID){
    //Get all the checkboxes from the form and set them to unchecked
    var cboList = $(formID).getElementsByTagName("input");

     for (var i=0; i < cboList.length; i++) {
            if (cboList[i].type == "checkbox"){ 
                cboList[i].checked = false;
            }
        }

}

function performSearch(){
   
    // Firstly, reset the checkboxes in the other form
     if($('fmFilter')){
        if(currentFormName == "fmFilter"){
    //        resetCheckboxes('fmFinder');
        }else{
        
       
            resetCheckboxes('fmFilter');
            
        }
}
    //data = $(currentFormName).serialize(); // gets all forms data

   // new Effect.Appear($('loadBar'), {duration:0.1, from:0.1, to:1}); // Show loading bar
    new Effect.Fade($('searchResults'), {duration:0.1, from:1, to:0.1});
    
}
// Shows the 'Sort By' drop down
//  Event.observe(window,"load",function() { pageInit();});
document.observe("dom:loaded",function() {pageInit();});
