/* *************************************************************************
Title : Common Javascript Functions
Author : Designkitchen, Inc.
Description : Common functions throughout the site
Created : May 28 2008
Modified : 


JS TOC
=ADD LOAD EVENT
=HOME FOOTER
=INIT SWAP IMAGE
=SUBMITS GLOBAL SEARCH
=SWAP FOCUS/SWAP BLUR
=MM_preloadImages
=GOGGLE AUTOFILL COLOR CORRECT -  IE ONLY
=BOOKMARK SCRIPT 
************************************************************************* */



/* *************************************************************************
=ADD LOAD EVENT
Allows for multiple 'onload' functions
************************************************************************* */

function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
		} else{
			window.onload = function(){
				oldonload();
				func();
				}
			}
	
	}

 /* *************************************************************************
=HOME FOOTER
The footer is contained in the master page, this function moves the footer's
DOM location on the home page (necessary because of the flash)
************************************************************************* */

function homeFooter(){
     
    if($('FlashHolder')){
        var footer = $$('.footerContent')[0];
        $('homeModule').appendChild(footer);
        footer.style.display = "block";
        
    }
}
addLoadEvent(homeFooter);




/* *************************************************************************
=INIT SWAP IMAGE
Image src swap for images/image buttons on hover
************************************************************************* */

function initSwapImage(){
   
    var allImgs = $$('.swapImg')
    if(allImgs.length > 0){
        for(var i=0; i<allImgs.length; i++){
            var strLength = allImgs[i].src.length;
            var strNum = strLength - 4;
            var strExt = allImgs[i].src.substring(strNum, strLength)
            var strPath = allImgs[i].src.substring(0, strNum);
            var strOver = strPath+"-o"+strExt;
            allImgs[i].rel = strOver;
            allImgs[i].onmouseover = function(){
                swapImgHover(this)
            }
            allImgs[i].onmouseout = function(){
                swapImgHover(this)
            }
            
        }
    }
}

function swapImgHover(thisImg){
  

   var oldSrc = thisImg.src;
   var oldRel = thisImg.rel;
   
   thisImg.src = oldRel;
   thisImg.rel = oldSrc;
   
}


addLoadEvent(initSwapImage);



/* *************************************************************************
=SUBMITS GLOBAL SEARCH
Onkeypress listener, submits the search and not the form on enter
also inits the focus and blur functions of the search text field
************************************************************************* */
function initSearchField(){
    var thisForm = document.getElementsByTagName("form")[0];
    var searchField = $('txtSearch');
    var submitBtn = $('btnSearch');

    if (searchField == null)
        return; 
     
    searchField.onfocus = function(){
     
       swapFocus(this, "Search"); 
        }
    searchField.onblur = function(){
       
        swapBlur(this, "Search"); 
        }
        
        
    searchField.onkeypress = function(e){
         // Check for enter key being pressed and filter any non-numeric keypresses
         var key;

         if(window.event)
             key = window.event.keyCode;    //IE
         else
             key = e.which;                 //firefox

         if((key == 13)) {
            // The enter key was pressed
               
               if((searchField.value != "Search")&&(searchField.value != "")){
                
                if (searchField.className.indexOf('static') != -1){
                    window.location = "http://www.wheels.com/Search.aspx?searchText=" + searchField.value;
                  }
                else{
                    window.location = "/public/Search.aspx?searchText=" + searchField.value;
                  }
               }
               return false;
            }  
        
     }
    
   
   submitBtn.onclick = function(){
           if(searchField.value != "Search"){
               window.location = "/public/Search.aspx?searchText=" + searchField.value;
               return false;
            }
            else{return false;}
   }

   
}
   
/* *************************************************************************
=SWAP FOCUS/SWAP BLUR
resets input text when users clicks in/out of input field
************************************************************************* */
function swapFocus(input, value){
 
    if(input.value == value){
        input.value = "";
    }
}
function swapBlur(input, value){
    if(input.value == ""){
        input.value = value;
    }
}

addLoadEvent(initSearchField);



/* *************************************************************************
=MM_preloadImages
MarcroMedia Preload function 
************************************************************************* */

function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//Preloads png graphics for a:hover (this is specific to the ie6 css)
MM_preloadImages('../images/nav.about-o.png','../images/nav.services-o.png','../images/nav.results-o.png','../images/nav.reference-o.png','../images/nav.login.drivers-o.png','../images/nav.login.clent-o.png')



/* *************************************************************************
=GOGGLE AUTOFILL COLOR CORRECT -  IE ONLY
Blocks the google autofill color 
************************************************************************* */
  if(window.attachEvent)
    window.attachEvent("onload",setListeners);

  function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      if(inputList[i].type != "checkbox"){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "#fff";
      }
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
    }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "#fff")
      event.srcElement.style.backgroundColor = "#fff";
  }
  
  function rolloverSubmit(divID, image){
    //var el=window.event? event.srcElement: e.target
    
    var arrBtn = divID.getElementsByTagName("input");
    
    var btnID = arrBtn[3];
    alert(btnID);
    btnID.style.backgroundImage="url"+"('" + image+"')";
    
    //if (el.tagName=="INPUT"&&el.type=="button")
    //el.style.backgroundImage="url"+"('"+image+"')"
 }
 





/***********************************************
=BOOKMARK SCRIPT
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}


/******************
For breadcrumbs redirect
********************/
function goTo(link)
{
	window.location=link;
}

