/**********************************************************************/
/* Global Variables and Objects                                       */
/**********************************************************************/

//create an instance of the Search object
var localSearch = new GlocalSearch();



/**********************************************************************/
/* New Car Page Fucntions                                             */
/**********************************************************************/
// function used to switch the visibility of the divs on the vehicle view page
// global - the container div holding the divs to be switched
// id - the id of the div that is to be shown
function vehicleDisplaySwitch(global,id,menu_glob,menu_id) {
    //first hide all of the divs
    var a = GetDomElement(global).getElementsByTagName('div');   
    for(var i=0;i<a.length;i++) {
        a[i].className = 'vehicle section none';
    }     
    //now display the one we wanna see!    
    GetDomElement(id).className = 'vehicle section';
    
    //remove the bold from all menu items
    var b = GetDomElement(menu_glob).getElementsByTagName('li');   
    for(var j=0;j<b.length;j++) {
        b[j].className = '';
    }     
    
    //now make the selected menu item bold!
    GetDomElement(menu_id).className = 'selected';
}

//function used to switch the divs for the subaru new cars
function subaruSwitchDisplay(global,id,menu_glob,menu_id) {
    //first hide all of the divs
    var a = GetDomElement(global).getElementsByTagName('div');   
    for(var i=0;i<a.length;i++) {
        if(a[i].className == 'vehicleDetail') {
            a[i].className = 'none';
        }
    }     
    //now display the one we wanna see!    
    GetDomElement(id).className = 'vehicleDetail';
    
    //remove the bold from all menu items
    var b = GetDomElement(menu_glob).getElementsByTagName('li');   
    for(var j=0;j<b.length;j++) {
        b[j].className = '';
    }     
    
    //now make the selected menu item bold!
    GetDomElement(menu_id).className = 'selected';
}

/**********************************************************************/


/**********************************************************************/
/* Your Dealer Page Fucntions                                         */
/**********************************************************************/
//function used to swap the divs that are displayed
function SwitchYourDealerDivs(global, id, pc, menu_glob, menu_id) {
    //first hide all divs in the global container, then display the selected one!
    var a = GetDomElement(global).getElementsByTagName('div');
    for(var i=0;i<a.length;i++) {
       if(a[i].id != 'GoogleMap') {
          a[i].className = "none";
       }
    }
    GetDomElement(id).className = "block";
    
    //now we need to sort out the highlighting for the menu on the left!
    var b = GetDomElement(menu_glob).getElementsByTagName('li');
    for(var j=0;j<b.length;j++) {
       b[j].className = '';
    }
    GetDomElement(menu_id).className = "selected";    
    
    //and finally refresh the GoogleMaps div
    GoogleMapsInitialize(pc,15);
}

//function to make use of the GoogleAjaxSearchAPI to convert a postcode into long lat coords
function GoogleMapsInitialize(postcode,zoom) {
    localSearch.setSearchCompleteCallback(null, 
    function() {
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        SIGoogleMapsLoad(resultLng, resultLat, zoom);
        //callbackFunction(point);
      }else{
        alert("Postcode not found!");
      }
    });  
    
  localSearch.execute(postcode + ", UK"); 
}

//function used to handle the rendering of the google maps image from the long ang lat values for each of the location s
function SIGoogleMapsLoad(lon, lat, zoom) {
    if(document.getElementById("GoogleMap")) {
        if (GBrowserIsCompatible()) {
            //create the map, set the centre, and add the user controls
            var map = new GMap2(document.getElementById("GoogleMap"));
            map.setCenter(new GLatLng(lat, lon), zoom); 
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());

            //now get the bounds for the map in order to add out reference point
            bounds = map.getBounds();
            var southWest = bounds.getSouthWest();
            var northEast = bounds.getNorthEast();
            var lngSpan = northEast.lng() - southWest.lng();
            var latSpan = northEast.lat() - southWest.lat();
            var point = new GLatLng(lat, lon);
            map.addOverlay(new GMarker(point));
        }  
    }    
}

/**********************************************************************/


/**********************************************************************/
/* Finance Page Fucntions                                             */
/**********************************************************************/
//function used to switch the display toggles on the various divs within the finance section
function financeSectionDisplaySwitch(global, id, item_global, item_id, item_menu_id, item_menu_glob) {
    //first hide all LI tags
    var a = GetDomElement(global).getElementsByTagName('li');
    for(var i=0;i<a.length;i++) {
        if(a[i].className.indexOf('section') == -1)
            a[i].className = 'none';
    }
    
    //now display all of the LI's within the selected div
    var b = GetDomElement(id).getElementsByTagName('li'); 
    for(var j=0;j<b.length;j++) {
        if(j==0) {
            b[j].className = 'block selected';
        }
        else {
            b[j].className = 'block';
        }
    }
    
    //now we need to display the first item from the selected section
    financeItemDisplaySwitch(item_global, item_id, item_menu_id, item_menu_glob)
}

//function used to display the correct finance item
function financeItemDisplaySwitch(global, id, menu_id, menu_glob) {
    //first hide all child divs below the global item
    var a = GetDomElement(global).getElementsByTagName('div');
    for(var i=0;i<a.length;i++) {
        a[i].className = 'finance content section none';
    }
    
    //now display the correct div
    GetDomElement(id).className = 'finance content section';
    
    //now we need to alter which divs are selected for the menu
    var b = GetDomElement(menu_glob).getElementsByTagName('li');
    for(var j=0;j<b.length;j++) {
        b[j].className = 'block';
    }

    GetDomElement(menu_id).className = 'block selected';
}

//function used to switch between the finance and insurance pages!
function financeInsuranceSwitch(scope,global,id,finDiv,insDiv,finMenu) {
    //first we will switch the styles to highlight the selected section
    var a = GetDomElement(global).getElementsByTagName('li');
    for(var i=0;i<a.length;i++) {
        a[i].className = 'item';
    }
    GetDomElement(id).className = 'item selected';
    
    //now switch the content divs depending on the scope
    if(scope=='fin') {
        GetDomElement(finDiv).className = 'finance content';
        GetDomElement(insDiv).className = 'finance content none';
        GetDomElement(finMenu).className = 'finance submenu';
    }
    else {
        GetDomElement(finDiv).className = 'finance content none';
        GetDomElement(insDiv).className = 'finance content';
        GetDomElement(finMenu).className = 'finance submenu none';
    }    
}
/**********************************************************************/



/**********************************************************************/
/* Helper Fucntions                                                   */
/**********************************************************************/
//funcion used to shorten any calls for objects from the DOM
function GetDomElement(id) {
    return document.getElementById(id);
}

//function used to clear the default text from any html input element
function ClearDefaultText(id) {
    GetDomElement(id).value = '';
}
/**********************************************************************/



/**********************************************************************/
/* Accessories & Merchandise page functions                           */
/**********************************************************************/
function SwitchAccessories(id,global) {
    //first we will switch the styles to highlight the selected section
    var a = GetDomElement(global).getElementsByTagName('div');
    for(var i=0;i<a.length;i++) {
        if(a[i].className.indexOf('accessory') > -1)
            a[i].className = 'accessory none';
    }
    GetDomElement(id).className = 'accessory';

}
/**********************************************************************/
