window.onload=init;
 
function init(){
	  document.getElementById("new-search").className="";
}

function toggleDash() {
	var classname = document.getElementById('dashboard').className;
	if (classname == 'hidden') {
		showDash();
	} else {
		hideDash();
	}
}

var sw;
function hideDash(){
	sw = document.getElementById("switch");
	document.getElementById("dashboard").style.display="none";
	var newL = document.createElement("a");
	newL.setAttribute("href","javascript:toggleDash();");
	newL.setAttribute("class","openMedia");
	newL.setAttribute("className","openMedia");
	newL.appendChild(document.createTextNode("show media"));
	sw.innerHTML="";
	//helpDash();
	sw.appendChild(newL);
	document.getElementById('dashboard').className = 'hidden';
	setCookie('hideDash',true,null,'/');
}

function showDash(){
	sw = document.getElementById("switch");
	document.getElementById("dashboard").style.display="block";
	var newL = document.createElement("a");
	newL.setAttribute("href","javascript:toggleDash();");
	newL.setAttribute("class","closeMedia");
	newL.setAttribute("className","closeMedia");
	newL.appendChild(document.createTextNode("close media"));
	sw.innerHTML="";
	//helpDash();
	sw.appendChild(newL);
	document.getElementById('dashboard').className = '';
	deleteCookie('hideDash');
}

function helpDash(){
	sw = document.getElementById("switch");
	var newL = document.createElement("a");
	newL.setAttribute("href","#");
	newL.setAttribute("class","help");
	newL.setAttribute("className","help");
	newL.appendChild(document.createTextNode("media help"));
	sw.appendChild(newL);
}


function setSelectedTab(parentElt,tabElt) {
	menuElts = document.getElementById(parentElt).getElementsByTagName("a");
	//Set all the tabs to have no styles.
	for(i=0;i<menuElts.length;i++){
		menuElts[i].className="";
	}
	
	// Set the selected tab
	document.getElementById(tabElt).className = 'selected';
}

function showQuickSearchResults() {
	document.getElementById('search-popup-container').style.display='block';
}

function hideQuickSearchResults() {
	document.getElementById('search-popup-container').style.display='none';
}

function callHotelSearchUpdate(params) { 
	new Ajax.Updater('hotel-search-form-results','/ajaxSearch/city_hotel_search_results?'+params, 
		{asynchronous:true, 
		 evalScripts:true,
		 parameters:Form.serialize('hotel-search-form'),
		 requestHeaders:['X-Update', 'hotel-search-form-results']
		 })
}

function updateFilterCount() {
	new Ajax.Updater('counter','/hotels/ajax_search_fields',
		 {onLoading:function(request){document.getElementById('counter').innerHTML='???';},
		 asynchronous:true,
		 evalScripts:true,
		 parameters:Form.serialize("hotel-search-form"),
		 requestHeaders:['X-Update', 'counter']})
}

  function closeSearchRes(){
            document.getElementById("search-popup-container").style.display="none";
}

var hotelFilterCount_timeout;
 
 function hotelFilterCountDelay() {
        
	clearTimeout(hotelFilterCount_timeout);
	hotelFilterCount_timeout = setTimeout("updateFilterCount();", 350);
}

var quickAjaxSearchTimer;

function submitQuickSearch(searchType) {
	entry = document.getElementById('search-term');
	if (entry.value != '') {
		quickAjaxSearch(searchType);
	} else {
		hideQuickSearchResults();
	}
}

function quickAjaxSearch(searchType) {
	clearTimeout(quickAjaxSearchTimer);
	quickAjaxSearchTimer = setTimeout("_quickAjaxSearch('"+searchType+"')", 350)
}


function _quickAjaxSearch(searchType) {
	new Ajax.Updater('search-popup-container','/ajax_search/'+searchType,
		{onLoading:showQuickSearchSearching(),
		asynchronous:true,
		evalScripts:true,
		parameters:Form.serialize("quick-search-form"),
		requestHeaders:['X-Update', 'search-popup-container']})
}

function clearField(field) {
	if(document.getElementById(field)) document.getElementById(field).value='';
}

function showQuickSearchResults() {
	document.getElementById('search-popup-searching').style.display='none';
	document.getElementById('search-popup-container').style.display='block';
}

function hideQuickSearchResults() {
	document.getElementById('search-popup-searching').style.display='none';
	document.getElementById('search-popup-container').style.display='none';
}

function showQuickSearchSearching() {
	if (document.getElementById('search-popup-container').style.display == 'block'
		&& document.getElementById('quick-search-results-heading')) {
		document.getElementById('quick-search-results-heading').innerHTML =
			'Searching';
	} else {
		document.getElementById('search-popup-searching').style.display='block';
	}
}

/**
 * Loads the selected page from the value of the passed optionBox.
 * @param {Object} entered - optionBox object
 */
function LoadPage(entered)
{
  with (entered)
  {
    window.location=options[selectedIndex].value;
  }
}


function show_this(val1){
		
	if ( document.getElementById("choose-content-item").value == 0 ){
	  return false;	  
	}	else {	
		if(val1 == "video"){
			new Ajax.Updater('video','/virtual_movies/ajax_show_vid', {onLoading:function(request){document.getElementById('dashboard-chooser-form').disabled=true;}, onComplete:function(request){document.getElementById('dashboard-chooser-form').disabled=false;}, asynchronous:true, evalScripts:true, parameters:Form.serialize('dashboard-chooser-form'), requestHeaders:['X-Update', 'video']});
		} else {
			new Ajax.Updater('video','/virtual_tours/ajax_show_vid', {onLoading:function(request){document.getElementById('dashboard-chooser-form').disabled=true;}, onComplete:function(request){document.getElementById('dashboard-chooser-form').disabled=false;}, asynchronous:true, evalScripts:true, parameters:Form.serialize('dashboard-chooser-form'), requestHeaders:['X-Update', 'video']});
		}	
	}
}

function replaceInnerHTML(elementName,htmlFrag) {
	document.getElementById(elementName).innerHTML= htmlFrag;
}

function updateDashboardControls() {
	new Ajax.Updater('dashboard-controls',
		'/dashboard/showControls',
		{onLoading:function(request){document.getElementById('dashboard-form').disabled=true},
		onComplete:function(request){document.getElementById('dashboard-form').disabled=false},
		asynchronous:true, 
		evalScripts:true, 
		parameters:Form.serialize('dashboard-form'),
		requestHeaders:['X-Update', 'dashboard-viewer']});	
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
