/*
 * Search Related Scripts       
 * ======================
 * Scripts that perform property, agent, office and area searches.
 *
 */
//Pushpin Mouseovers

function HasClass(A,B){if(typeof A=="string"){A=document.getElementById(A)}if(A&&A.className){return A.className.match(new RegExp("(\\s|^)"+B+"(\\s|$)"))}return false}function AddClass(A,B){if(typeof A=="string"){A=document.getElementById(A)}if(A){if(!this.HasClass(A,B)){A.className+=" "+B;return true}}return false}function RemoveClass(A,C){if(typeof A=="string"){A=document.getElementById(A)
}if(A){if(HasClass(A,C)){var B=new RegExp("(\\s|^)"+C+"(\\s|$)");A.className=A.className.replace(B," ");return true}}return false}function AddClassToChildTags(B,D,E){if(typeof B=="string"){B=document.getElementById(B)}var C=B.getElementsByTagName(D);if(C&&C.length>0){for(var A=0;A<C.length;A++){AddClass(C[A],E)}}}
function TogglePropertyListViewPreview( e, iTrKey ) {
//alert("Adt Toggle 1");
   if( HasClass( 'property_'+iTrKey+'_previewrow', 'hide' ) ) {
      ShowPropertyListViewPreview( iTrKey );
   } else {
      HidePropertyListViewPreview( iTrKey );
   }
   e.returnValue = false;

   return false;
//alert("Toggle 2");
   //return false;
}

function ShowPropertyListViewPreview( iTrKey ) {
   //Register a unique ID for this, so multiple preview events can happen all at once.
   var pPreviewDiv = document.getElementById('property_'+iTrKey+'_previewrow');
   if( pPreviewDiv ) {
      /*if( !pPreviewDiv.bPreviewFetched ) {
         var strUrl = '/xml-bin/propertydetail?id='+iTrKey;
         g_AjaxQueue.schedule(("PropertyPreview"+iTrKey), strUrl, OnCompleteShowPropertyListViewPreview);
         c21Reporting( 'listing', 'Detail', iTrKey );
      }*/
      RemoveClass( pPreviewDiv, 'hide' );
   }
   return false;
}

function HidePropertyListViewPreview( iTrKey ) {
   AddClass( 'property_'+iTrKey+'_previewrow', 'hide' );
   /*var pPreviewButton = document.getElementById('property_'+iTrKey+'_previewbutton');
   if( pPreviewButton ) {
      pPreviewButton.src = 'listingpreview.gif';
   }*/
}
function changeorderofresults(order)
{ 
   var actual_params = window.location.search;
   var array_cols = order.split(' ');
     var nova_coluna = "col=" + array_cols[0].toString();
   var nova_dir  = "dir=" + array_cols[1].toString();
   var new_params = '';
   if (actual_params.match("col=") || actual_params.match("dir"))
   {
      //coluna de ordenação
      var indexOfParam = actual_params.indexOf("col=");
      var short_param = actual_params.substring(indexOfParam);
      var indexOfFirstDelimiter = short_param.indexOf("&");
      if(indexOfFirstDelimiter <0){
        indexOfFirstDelimiter  = short_param.length;
      }
      var actual_coluna = short_param.substring(0,indexOfFirstDelimiter);
      new_params = actual_params.replace(actual_coluna,nova_coluna);

      //direcção de ordenação
       indexOfParam = actual_params.indexOf("dir=");
       short_param = actual_params.substring(indexOfParam);
       indexOfFirstDelimiter = short_param.indexOf("&");
      if(indexOfFirstDelimiter <0){
        indexOfFirstDelimiter  = short_param.length;
      }
      var actual_dir = short_param.substring(0,indexOfFirstDelimiter);
      new_params = new_params.replace(actual_dir,nova_dir);
   }
   else
   {
      new_params = actual_params + "&" + nova_coluna + "&" + nova_dir;
   }
   
    
  return new_params.replace("?&","?");
}
function changelimitofresults(limite)
{
   var novo_limite = "limit=" + limite;
   var actual_params = window.location.search;
   var new_params = '';
   if (actual_params.match("limit="))
       {
          var indexOfParam = actual_params.indexOf("limit=");
          var short_param = actual_params.substring(indexOfParam);
          var indexOfFirstDelimiter = short_param.indexOf("&");
          if(indexOfFirstDelimiter <0){
            indexOfFirstDelimiter  = short_param.length;
          }
          var actual_limite = short_param.substring(0,indexOfFirstDelimiter);
         new_params = actual_params.replace(actual_limite,novo_limite);
       }
   else
       {
         new_params = actual_params + "&" + novo_limite;
       }
  return new_params;
}

function toggleOverflow(elemId, arrowId){
    var elem = document.getElementById(elemId);
    var arrow = document.getElementById(arrowId);
	if(elem){
		if((elem.style.overflow=="hidden" || elem.style.overflow=="")){
         elem.style.overflow="visible";
         if( arrow ) {
            if( arrow.style.backgroundImage.indexOf("/images/search/topbar_arrow.gif")>=0) {
               arrow.style.backgroundImage = "url(/images/search/topbar_arrow_on.gif)";
            }else{
               arrow.style.backgroundImage = "url(/images/search/h_arrow_up.gif)";
            }
         }
      }else{
         elem.style.overflow="hidden";
         if( arrow ) {
            if( arrow.style.backgroundImage.indexOf("/images/search/topbar_arrow_on.gif")>=0) {
               arrow.style.backgroundImage = "url(/images/search/topbar_arrow.gif)";
            }else{
               arrow.style.backgroundImage = "url(/images/search/h_arrow_down.gif)";
            }
         }
      }
    }
}

function getIndexInArray(array, value){
   for( var i=0; i<array.length; i++ ) {
      if( array[i]==value ) {
         return i;
      }
   }
   return -1;
}
var g_aDropdowns = new Array();
var g_aDropdownActions = new Array();
function requestMenuOverflowOff(elemId, arrowId){
   var index = getIndexInArray(g_aDropdowns, elemId);
   index = (index<0) ? g_aDropdowns.length : index;
   g_aDropdowns[index] = elemId;
   if( !g_aDropdownActions[index] ) {
      g_aDropdownActions[index] = new Array();
   }
   //var now = new Date();
   //g_aDropdownActions[index]['ts'] = now.getUTCMilliseconds();
   //g_aDropdownActions[index]['ts_action'] = now.getUTCMilliseconds()+400;
   g_aDropdownActions[index]['action'] = "close";
   g_aDropdownActions[index]['elem_id'] = elemId;
   g_aDropdownActions[index]['arrow_id'] = arrowId;
   setTimeout( menuOverflowOff, 400 );
}
function menuOverflowOff(){
   //var now = new Date();
   for( var i=0; i<g_aDropdowns.length; i++) {
      var action = g_aDropdownActions[i]['action'];
      var elemId = g_aDropdownActions[i]['elem_id'];

      var arrowId = g_aDropdownActions[i]['arrow_id'];
      //var this_ts = g_aDropdownActions[i]['ts'];
      //var action_ts = g_aDropdownActions[i]['ts_action'];

      var elem = document.getElementById(elemId);
      var arrow = document.getElementById(arrowId);
      if( elem && action=="close") {
         elem.style.overflow="hidden";
         if( arrow ) {
            arrow.style.backgroundImage = "url(/images/search/topbar_arrow.gif)";
         }
      }
   }
}
function menuOverflowOn(elemId, arrowId){
   var now = new Date();
   var index = getIndexInArray(g_aDropdowns, elemId);
   index = (index<0) ? g_aDropdowns.length : index;
   g_aDropdowns[index] = elemId;
   if( !g_aDropdownActions[index] ) {
      g_aDropdownActions[index] = new Array();
   }
   //var now = new Date();
   //g_aDropdownActions[index]['ts'] = now.getUTCMilliseconds();
   //g_aDropdownActions[index]['ts_action'] = now.getUTCMilliseconds()+400;
   g_aDropdownActions[index]['action'] = "open";
   g_aDropdownActions[index]['elem_id'] = elemId;
   g_aDropdownActions[index]['arrow_id'] = arrowId;

   var elem = document.getElementById(elemId);
   var arrow = document.getElementById(arrowId);
   if( elem ) {
         elem.style.overflow="visible";
         /*if( arrow ) {
            if( arrow.style.backgroundImage.indexOf("topbar_arrow.gif")>=0 || arrow.style.backgroundImage.indexOf("topbar_arrow_up.gif")>=0) {
               arrow.style.backgroundImage = "url(/images/search/topbar_arrow_on.gif)";
            }else{
               arrow.style.backgroundImage = "url(/images/search/h_arrow_up.gif)";
            }
         }*/
   }
}
function highlightOption(prefix, type){
   var elem   = document.getElementById(prefix+type);
   var link   = document.getElementById(prefix+type+"_link");
   var icon   = document.getElementById(prefix+type+"_icon");
   var dot    = document.getElementById(prefix+type+"_dot");
   var arrow  = document.getElementById(prefix+type+"_arrow");
	if(elem){
      elem.style.backgroundImage = "url(/images/search/h_yel_bg.gif)";
      elem.style.color="#000000";
      if( link ) {
         link.style.color="#000000";
      }
      if( dot ) {
         dot.style.backgroundImage = "url(/images/search/h_yel_dot.gif)";
      }
      if( arrow ) {
         if( arrow.style.backgroundImage.indexOf("topbar_arrow_on.gif")>=0 ) {
            arrow.style.backgroundImage = "url(/images/search/h_arrow_up.gif)";
         }else{
            arrow.style.backgroundImage = "url(/images/search/h_arrow_down.gif)";
         }
      }
      if( icon ) {
         switch( type ) {
         case "list":
            icon.src="/images/search/h_list_icon.gif";
            break;
         case "map":
            icon.src="/images/search/h_map_icon.gif";
            break;
         case "grid":
            icon.src="/images/search/h_grid_icon.gif";
            break;
         case "photo":
            icon.src="/images/search/h_photo_icon.gif";
            break;
         case "save":
            if( icon.src.indexOf("h_check_icon.gif")>=0 ) {
               icon.src="/images/search/h_check_icon_blk.gif";
            }else{
               icon.src="/images/search/h_save_icon_blk.gif";
            }
            break;
         case "print":
            icon.src="/images/search/h_print_icon_blk.gif";
            break;
         case "email":
            icon.src="/images/search/h_save_icon_blk.gif";
            break;
         case "rss":
            icon.src="/images/search/h_rss_icon_blk.gif";
            break;

         }
      }
   }
}
function endHighlightOption(prefix, type){
   var elem = document.getElementById(prefix+type);
   var link = document.getElementById(prefix+type+"_link");
   var icon = document.getElementById(prefix+type+"_icon");
   var dot  = document.getElementById(prefix+type+"_dot");
   var arrow  = document.getElementById(prefix+type+"_arrow");
	if(elem){
      elem.style.backgroundImage = "url(/images/search/topbar_mid.gif)";
      elem.style.color="#FECE02";
      if( link ) {
         link.style.color="#FECE02";
      }
      if( dot ) {
         dot.style.backgroundImage = "url(/images/search/h_pag_dot.gif)";
      }
      if( arrow ) {
         if( arrow.style.backgroundImage.indexOf("h_arrow_up.gif")>=0 ) {
            arrow.style.backgroundImage = "url(/images/search/topbar_arrow_on.gif)";
         }else{
            arrow.style.backgroundImage = "url(/images/search/topbar_arrow.gif)";
         }
      }
      if( icon ) {
         switch( type ) {
         case "list":
            icon.src="/images/search/list_icon.gif";
            break;
         case "map":
            icon.src="/images/search/map_icon.gif";
            break;
         case "grid":
            icon.src="/images/search/grid_icon.gif";
            break;
         case "photo":
            icon.src="/images/search/photoflow.gif";
            break;
         case "save":
            if( icon.src.indexOf("h_check_icon_blk.gif")>=0 ) {
               icon.src="/images/search/h_check_icon.gif";
            }else{
               icon.src="/images/search/h_save_icon.gif";
            }
            break;
         case "print":
            icon.src="/images/search/h_print_icon.gif";
            break;
         case "email":
            icon.src="/images/search/h_save_icon.gif";
            break;
         case "rss":
            icon.src="/images/search/h_rss_icon.gif";
            break;

         }
      }
   }
}

