function Document_Click(e)
{
	if (!e) var obj = window.event.srcElement;
	else var obj = e.target;
	while (obj.nodeType != 1)
	{
		obj = obj.parentNode;
	}
	if (obj.tagName == 'DIV') return;
	
	if(typeof g_oMenu != 'undefined' && g_oMenu)
	{
		g_oMenu.style.display = "none"
		g_oMenu = null;
		document.onclick = null;
	}
	else return;
}

function HideMenu(div)
{
	if(typeof g_oMenu != 'undefined' && g_oMenu && (div.id != g_oMenu.id))
	{
		g_oMenu.style.display = "none"
		g_oMenu = null;
		document.onclick = null;
	}
	else return;
}

function switchdiv(div_1, div_2)
{
	if (document.getElementById)	{
	    if(!document.getElementById(div_1)) return ;
	    if(!(document.getElementById(div_1).style)) return ;
	    if(!(document.getElementById(div_1).style.display)) return ;

		var state_1 = document.getElementById(div_1).style.display;
		if(state_1=="none") {
        		document.getElementById(div_1).style.display="block";
        		document.getElementById(div_2).style.display="none";
	     }
	    if(state_1=="block") {
        		document.getElementById(div_2).style.display="block";
        		document.getElementById(div_1).style.display="none";
	     }
	}
	else if (document.all)	{
	    if(!document.all[div_1]) return ;
	    if(!(document.all[div_1].style)) return ;
	    if(!(document.all[div_1].style.display)) return ;

		var state_1 = document.all[div_1].style.display;
		if(state_1=="none") {
		        document.all[div_1].style.display = "block";
		        document.all[div_2].style.display = "none";
		}
		if(state_1=="block") {
        		document.getElementById(div_1).style.display="none";
        		document.getElementById(div_2).style.display="block";
	     }
    }
}

function showhide()
{
	var nArguments = arguments.length;
	
	function __showhide_one_argument(div)
	{
		HideMenu(document.getElementById(div));
		state = document.getElementById(div).style.display;
		g_oMenu = document.getElementById(div);
		
		if(state == "none")
		{
			g_oMenu.style.display="block";
			g_oMenu.style.visibility = 'visible';
			g_oMenu = document.getElementById(div);
			setTimeout("document.onclick = Document_Click",100);
		}
		else
		{
			g_oMenu.style.display="none";
			g_oMenu.style.visibility = 'visible';
			g_oMenu = null;
		}
		g_oMenu = document.getElementById(div);
		
	}
	
	function __showhide_two_arguments(div,ensurevis)
	{
//		HideMenu(document.getElementById(div));
		state = document.getElementById(div).style.display;
		g_oMenu = document.getElementById(div);
		
		if(state == "none") { g_oMenu.style.display="block";}
		else { g_oMenu.style.display = "none";}
		g_oMenu.style.visibility = 'visible';
	}
		
	if(nArguments == 1) return __showhide_one_argument(arguments[0]);
	else if(nArguments == 2) return __showhide_two_arguments(arguments[0],arguments[1]);
	else return false;
}

// Multiple combo functions =============================

function moveUpList(listField)
{
	if ( listField.length == -1) {  // If the list is empty
	  alert("There are no values which can be moved!");
	} else {
	  var selected = listField.selectedIndex;
	  if (selected == -1) {
	     alert("You must select an entry to be moved!");
	  } else {  // Something is selected
	     if ( listField.length == 0 ) {  // If there's only one in the list
	        alert("There is only one entry!\nThe one entry will remain in place.");
	     } else {  // There's more than one in the list, rearrange the list order
	        if ( selected == 0 ) {
	           alert("The first entry in the list cannot be moved up.");
	        } else {
	           // Get the text/value of the one directly above the hightlighted entry as
	           // well as the highlighted entry; then flip them
	           var moveText1 = listField[selected-1].text;
	           var moveText2 = listField[selected].text;
	           var moveValue1 = listField[selected-1].value;
	           var moveValue2 = listField[selected].value;
	           listField[selected].text = moveText1;
	           listField[selected].value = moveValue1;
	           listField[selected-1].text = moveText2;
	           listField[selected-1].value = moveValue2;
	           listField.selectedIndex = selected-1; // Select the one that was selected before
	        }  // Ends the check for selecting one which can be moved
	     }  // Ends the check for there only being one in the list to begin with
	  }  // Ends the check for there being something selected
	}  // Ends the check for there being none in the list
}

//Hopefully the code makes sense. It is commented pretty well, so that should help. For moving down in the list, the code is quite similar:

function moveDownList(listField)
{
	if ( listField.length == -1) {  // If the list is empty
	  alert("There are no values which can be moved!");
	} else {
	  var selected = listField.selectedIndex;
	  if (selected == -1) {
	     alert("You must select an entry to be moved!");
	  } else {  // Something is selected
	     if ( listField.length == 0 ) {  // If there's only one in the list
	        alert("There is only one entry!\nThe one entry will remain in place.");
	     } else {  // There's more than one in the list, rearrange the list order
	        if ( selected == listField.length-1 ) {
	           alert("The last entry in the list cannot be moved down.");
	        } else {
	           // Get the text/value of the one directly below the hightlighted entry as
	           // well as the highlighted entry; then flip them
	           var moveText1 = listField[selected+1].text;
	           var moveText2 = listField[selected].text;
	           var moveValue1 = listField[selected+1].value;
	           var moveValue2 = listField[selected].value;
	           listField[selected].text = moveText1;
	           listField[selected].value = moveValue1;
	           listField[selected+1].text = moveText2;
	           listField[selected+1].value = moveValue2;
	           listField.selectedIndex = selected+1; // Select the one that was selected before
	        }  // Ends the check for selecting one which can be moved
	     }  // Ends the check for there only being one in the list to begin with
	  }  // Ends the check for there being something selected
	}  // Ends the check for there being none in the list
}

function allSelect(el)
{
	for (i=0;i<el.length;i++) el.options[i].selected = true;
}

// End Multiple combo fncs

function SwichImgs(img,src)
{
	document.getElementById(img).src = src;
}

function AddOnloadFunction(someFunc)
{
    var oldFunc = window.onload;
    if ( typeof window.onload != 'function')
        {
            window.onload = someFunc;
        }
        else
        {
            window.onload = function() {
                if (oldFunc) oldFunc();
                someFunc();
            }
        }
}

function toggleShowHide(elId)
{
	var oElement = document.getElementById(elId);
	if (oElement.style.display == "block")  {  oElement.style.display = "none";  return; }
	oElement.style.display = "block";
}

function Q_search()
{
	 $("#qsearch_res").text("Searching ...");
	 $("#qsearch_res").show();
	 var dataAjax = $("form#shortsearchform").serialize();
	 
	 $.ajaxSetup({ type: "POST",url: reldir+'control/dispatch/search_dispatcher.php',cache: false });
	 $.ajax({
      data: dataAjax,
      success: function(message)
      {
      	 result = parseInt(message);
      	 if (result) {
      	 	window.location = reldir+'viewprop.php?id='+result;
      	 } 
      	 else {
      	 	$("#qsearch_res").text("No such id in our database!");
      	 }
      }
   });	 
   return false;
}

function MainMenuHandler(obj_clickedId, subMenuId)
{
	var obj_clicked = document.getElementById(obj_clickedId);
	var the_menu = document.getElementById(subMenuId);
	obj_clicked.onmouseover = function(sender){
		the_menu.style.display="block";		
	}
	the_menu.onmouseover = function(){ toggleShowHide(subMenuId); }
	the_menu.onmouseout = function(){ toggleShowHide(subMenuId); }
	obj_clicked.onmouseout = function(){
		if (the_menu.style.display=="block") the_menu.style.display="none";
	}
}

function MenuItemsBinder()
{
	MainMenuHandler("avail","menustates");
	MainMenuHandler("residents_click","menuresidents");
	MainMenuHandler("about_click","menures");
	MainMenuHandler("qa","menufaq");
}

AddOnloadFunction(MenuItemsBinder);

function MethodX(newPicUrl, sender, maxValue)
{
	if ( currMainPhotoButtonIndex != undefined )  
	{
		 document.getElementById('featPicButton'+currMainPhotoButtonIndex).className = '';
		 document.getElementById('indexpagingPrev').className = '';
		 document.getElementById('indexpagingNext').className = '';
	}
	currMainPhotoButtonIndex = sender.id.substring(sender.id.length-1);
	document.getElementById('mainpic').src = newPicUrl;	
	sender.className = 'active';
	
	if (currMainPhotoButtonIndex == 0) document.getElementById('indexpagingPrev').className = 'inactive';
	if (currMainPhotoButtonIndex == maxValue-1) document.getElementById('indexpagingNext').className = 'inactive';
}

function MethodX_Prev()
{
	if (currMainPhotoButtonIndex == 0) return;
	var prevIndex = parseInt(currMainPhotoButtonIndex) - 1;
	$('#featPicButton'+prevIndex).click();
}

function MethodX_Next()
{
	if (currMainPhotoButtonIndex >= maxValue-1) return;
	var nextIndex = parseInt(currMainPhotoButtonIndex) + 1;	
	$('#featPicButton'+nextIndex).click();
}

function MethodX_featPrev(total)
{
	var indx = $('#indxCurrShowFeat').val();
	if (indx == 0) 
	{
		var newIndx = parseInt(total) - 1;
	}
	else
	{	
		var newIndx = parseInt(indx) - 1;
	}
	$('#feat'+indx).hide();
	$('#feat' + newIndx).show();
	$('#indxCurrShowFeat').val(newIndx);
}

function MethodX_featNext(total)
{
	var indx = parseInt($('#indxCurrShowFeat').val());	
	if (indx == total - 1)
	{
		var newIndx = 0;
	}
	else
	{	
		var newIndx = parseInt(indx) + 1;
	}	
	$('#feat'+indx).hide();
	$('#feat' + newIndx).show();	
	$('#indxCurrShowFeat').val(newIndx);
}

// wrap selects , for simulaion with divs
function WrapDropDowns(idDropDown, nameField) 
{
		if ($('input[name="'+nameField+'"]').val() != "")
  	{			  		 
  		 $.each($('#'+idDropDown+'.dropdown ul').children(), function(key, value){
  		 		_val = $(value).find("span.value").html();
  		 		if ($('input[name="'+nameField+'"]').val() == _val)
  		 		{
  		 			$("#"+idDropDown+".dropdown dt a span").html($(value).find("a").text());
  		 			return;
  		 		}
  		 });			  		
    }
  	
		$("#"+idDropDown+".dropdown dt a").click(function() {
		    $("#"+idDropDown+".dropdown dd ul").toggle();
		});
		
		$("#"+idDropDown+".dropdown dd ul li a").click(function() {
		    var text = $(this).html();
		    var selVal = $(this).next().html();
		    $("#"+idDropDown+".dropdown dt a span").html(text);
		    $("#"+idDropDown+".dropdown dd ul").hide();
		    $('input[name="'+nameField+'"]').val(selVal);
		});
		
		$(document).bind('click', function(e) {
		    var $clicked = $(e.target);
		    if (! $clicked.parents().hasClass("dropdown"))
		        $("#"+idDropDown+".dropdown dd ul").hide();
		});
 }
 
 function RevolveFeatured()
 {
 	  if (document.getElementById("countShowFeat") == null) return;
 	  var countAll = parseInt(document.getElementById("countShowFeat").value);
 	  if ($.doTimeout != null) {
			  $.doTimeout('loop', 5000, function(){
			  		  MethodX_featNext(countAll);
			  		  return true;
				});
		}
 }
 
 AddOnloadFunction(RevolveFeatured);
