var G_finderMode = 'standard';
var G_finderIsOpen = false;
var G_favoritesIsOpen = false;
var G_snowSpaceIsOpen = false;
var G_firstShow = true;

function Finda(){
	this.fid = '';
	this.flink = '';
}

var fi = new Array();
fi['map'] = new Finda();
fi['country'] = new Finda();
fi['state'] = new Finda();
fi['resort1'] = new Finda();
fi['resort2'] = new Finda();

function escapeQuote(st){ return st.replace(/'/,"\\'") }

function openCountry(link,id,isHot){
	if(G_finderMode == 'compare' && !isHot){
		alert("There are no statistics available for the resorts of this region. Select a highlighted region.");
		return false;
	}
	if(fi.map.fid){jQuery("#world-map").hide()}
	if(fi.map.flink){fi.map.flink.removeClass('selected')}
	if(fi.country.fid){jQuery("#finder-country").hide()}
	if(fi.country.flink){fi.country.flink.removeClass('selected')}
	if(fi.state.fid){jQuery("#finder-state").hide()}
	if(fi.state.flink){fi.state.flink.removeClass('selected')}
	if(fi.resort1.fid){jQuery("#finder-resort1").hide()}
	if(fi.resort1.flink){fi.resort1.flink.removeClass('selected')}
	if(fi.resort2.fid){jQuery("#finder-resort2").hide()}
	if(fi.resort2.flink){fi.resort2.flink.removeClass('selected')}
	createCookie("fp_country",id);
	createCookie("fp_countryLink",link.id);
	eraseCookie("fp_worldMap");
	eraseCookie("fp_state");
	eraseCookie("fp_resort1");
	eraseCookie("fp_resort2");
	activateCountry(link,id);
}

function activateCountry(link,id){
	fi.country.fid = id;
	fi.country.flink = (link) ? jQuery('#'+link.id) : jQuery('#'+readCookie('fp_countryLink'));
	fi.country.flink.addClass('selected');
	jQuery("#finder-country-list").html('');
	jQuery("#finder-country .finder-busy").show();
	var retHtml = '';
	jQuery.getJSON("/finder-json.html?type=country&id="+id, function(json){
		for(var i in json){
                        if (isNaN(i)) continue;
			retHtml += '<li><a id="'+((json[i].hasStates)?'open-state-':'open-resort1-')+json[i].id+'" class="';
			retHtml += (json[i].isHot) ? 'fhot' : 'fcold';
			retHtml += '" href="javascript://" onClick="'+((json[i].hasStates)?'openState':'openResort1')+'(this,'+json[i].id+','+((json[i].isHot)?'true':'false')+');return false;">';
			retHtml += json[i].name+'</a></li>';
		}
		jQuery("#finder-country-list").hide();
		jQuery("#finder-country-list").html(retHtml);
		jQuery("#finder-country .finder-busy").hide();
		jQuery("#finder-country-list").fadeIn(500);

		if(!link){
			if(readCookie("fp_state") && readCookie("fp_stateLink")){
				fi.state.flink = jQuery('#'+readCookie("fp_stateLink"));
				fi.state.flink.addClass('selected');
				activateState('',readCookie("fp_state"));
			}else if(readCookie("fp_resort1") && readCookie("fp_resort1Link")){
				fi.resort1.flink = jQuery('#'+readCookie("fp_resort1Link"));
				fi.resort1.flink.addClass('selected');
				activateResort1('',readCookie("fp_resort1"));
			}
		}
	});
	jQuery("#finder-country").fadeIn('slow');
}

function openState(link,id,isHot){
	if(G_finderMode == 'compare' && !isHot){
		alert("There are no statistics available for the resorts of this country. Please select a highlighted country.");
		return false;
	}
	if(fi.state.fid){jQuery("#finder-state").hide()}
	if(fi.state.flink){fi.state.flink.removeClass('selected')}
	if(fi.resort1.fid){jQuery("#finder-resort1").hide()}
	if(fi.resort1.flink){fi.resort1.flink.removeClass('selected')}
	if(fi.resort2.fid){jQuery("#finder-resort2").hide()}
	if(fi.resort2.flink){fi.resort2.flink.removeClass('selected')}
	createCookie("fp_state",id);
	createCookie("fp_stateLink",link.id);
	eraseCookie("fp_resort1");
	eraseCookie("fp_resort2");
	activateState(link,id);
}

function activateState(link,id){
	fi.state.fid = id;
	if(link){
		fi.state.flink = jQuery('#'+link.id);
		fi.state.flink.addClass('selected');
	}
	jQuery("#finder-state-list").html('');
	jQuery("#finder-state .finder-busy").show();
	jQuery.getJSON("/finder-json.html?type=state&id="+id, function(json){
		var retHtml = '';
		if(json.length > 1){
			retHtml += '<li><a id="open-resort2-all" class="'+((json[0].isHot)?'fhot':'fcold')+'" href="javascript://" onClick="openResort2(this,'+id+','+((json[0].isHot)?'true':'false')+',true);return false;">&lt;All&gt;</a></li>';
		}
		for(var i in json){
                        if (isNaN(i)) continue;
			retHtml += '<li><a id="open-resort2-'+json[i].id+'" class="';
			retHtml += (json[i].isHot) ? 'fhot' : 'fcold';
			retHtml += '" href="javascript://" onClick="openResort2(this,'+json[i].id+','+((json[i].isHot)?'true':'false')+',false);return false;">';
			retHtml += json[i].name+'</a></li>';
		}
		jQuery("#finder-state-list").hide();
		jQuery("#finder-state-list").html(retHtml);
		jQuery("#finder-state .finder-busy").hide();
		jQuery("#finder-state-list").fadeIn(500);

		if(!link && readCookie("fp_resort2") && readCookie("fp_resort2Link")){
			fi.resort2.flink = jQuery('#'+readCookie("fp_resort2Link"));
			fi.resort2.flink.addClass('selected');
			activateResort2('',readCookie("fp_resort2"),(readCookie("fp_resort2All")==1));
		}
	});
	jQuery("#finder-state").fadeIn('slow');
}

function openResort1(link,id,isHot){
	if(G_finderMode == 'compare' && !isHot){
		alert("There are no statistics available for the resorts of this country. Please select a highlighted country.");
		return false;
	}
	if(fi.state.fid){jQuery("#finder-state").hide()}
	if(fi.state.flink){fi.state.flink.removeClass('selected')}
	if(fi.resort1.fid){jQuery("#finder-resort1").hide()}
	if(fi.resort1.flink){fi.resort1.flink.removeClass('selected')}
	if(fi.resort2.fid){jQuery("#finder-resort2").hide()}
	if(fi.resort2.flink){fi.resort2.flink.removeClass('selected')}
	createCookie("fp_resort1",id);
	createCookie("fp_resort1Link",link.id);
	eraseCookie("fp_state");
	eraseCookie("fp_resort2");
	activateResort1(link,id);
}

function activateResort1(link,id){
	fi.resort1.fid = id;
	if(link){
		fi.resort1.flink = jQuery('#'+link.id);
		fi.resort1.flink.addClass('selected');
	}
	jQuery("#finder-resort1-list").html('');
	jQuery("#finder-resort1 .finder-busy").show();
	var retHtml = '';
	jQuery.getJSON("/finder-json.html?type=resortCountry&id="+id, function(json){
		for(var i in json){
                        if (isNaN(i)) continue;
			retHtml += '<li><a id="pick-resort-'+json[i].id+'" class="';
			retHtml += (json[i].isHot) ? 'fhot '+json[i].pack : 'fcold';
			retHtml += '" href="javascript://" onClick="pickResort(this,'+json[i].id+',\''+json[i].alias+'\',\''+escapeQuote(json[i].name)+'\','+((json[i].isHot)?'true':'false')+');return false;">';
			retHtml += json[i].name+'</a></li>';
		}
		jQuery("#finder-resort1-list").hide();
		jQuery("#finder-resort1-list").html(retHtml);
		jQuery("#finder-resort1 .finder-busy").hide();
		jQuery("#finder-resort1-list").fadeIn(500);
	});
	jQuery("#finder-resort1").fadeIn('slow');
}

function openResort2(link,id,isHot,isAll){
	if(G_finderMode == 'compare' && !isHot){
		alert("There are no statistics available for the resorts of this state. Please select a highlighted state.");
		return false;
	}
	if(fi.resort2.fid){jQuery("#finder-resort2").hide()}
	if(fi.resort2.flink){fi.resort2.flink.removeClass('selected')}
	createCookie("fp_resort2",id);
	createCookie("fp_resort2Link",link.id);
	createCookie("fp_resort2All",((isAll)?1:0));
	activateResort2(link,id,isAll);
}

function activateResort2(link,id,isAll){
	fi.resort2.fid = id;
	if(link){
		fi.resort2.flink = jQuery('#'+link.id);
		fi.resort2.flink.addClass('selected');
	}
	jQuery("#finder-resort2-list").html('');
	jQuery("#finder-resort2 .finder-busy").show();
	var retHtml = '';
	var qType = (isAll) ? 'resortCountry' : 'resortState';
	jQuery.getJSON("/finder-json.html?type="+qType+"&id="+id, function(json){
		for(var i in json){
                        if (isNaN(i)) continue;
			retHtml += '<li><a id="pick-resort-'+json[i].id+'" class="';
			retHtml += (json[i].isHot) ? 'fhot '+json[i].pack : 'fcold';
			retHtml += '" href="javascript://" onClick="pickResort(this,'+json[i].id+',\''+json[i].alias+'\',\''+escapeQuote(json[i].name)+'\','+((json[i].isHot)?'true':'false')+');return false;">';
			retHtml += json[i].name+'</a></li>';
		}
		jQuery("#finder-resort2-list").hide();
		jQuery("#finder-resort2-list").html(retHtml);
		jQuery("#finder-resort2 .finder-busy").hide();
		jQuery("#finder-resort2-list").fadeIn(500);
	});
	jQuery("#finder-resort2").fadeIn('slow');
}

function resumeLastPosition(){
	if(readCookie("fp_country") && readCookie("fp_countryLink")){
		activateCountry('',readCookie('fp_country'));
	}else{
// world map disabled until it is developed
//		activateWorldMap(jQuery('#worldMapLink'),'world-map');
	}
}

function addCompare(link,id){
	var re = new RegExp("\\b"+id+"\\b");
	if(G_res.match(re)){
		alert("This resort has already been added.")
	}else{
		G_res += ((G_res)?',':'')+id;
		submitCompare();
	}
}

function removeCompare(id){
	var re = new RegExp("\\b"+id+"\\b");
	G_res = G_res.replace(re,'');
	G_res = G_res.replace(/,,/,',');
	G_res = G_res.replace(/^,/,'');
	G_res = G_res.replace(/,$/,'');
	submitCompare();
}

function removeAllCompares(){
	G_res = '';
	submitCompare();
}

function submitCompare(){
	jQuery('#res').value = G_res;
	createCookie("fp_compare",G_res);
	var form = document.forms.compareForm
	var ts = new Date();
	form.action = location.pathname+'?ts='+ts.getTime();
	form.submit();
}

function toggleFavorites(mode){
	if(G_favoritesIsOpen){
		hideFavorites();
	}else{
		showFavorites();
	}
}

function showFavorites(){
	if(G_favoritesIsOpen){return false}
	if(G_finderIsOpen && G_finderMode == 'standard'){
		jQuery('#finder-box').hide();
		jQuery('#favorites-box').fadeIn("normal");
		// jQuery('#finder-box').fadeOut('normal',function(){jQuery('#favorites-box').fadeIn("normal")});
		G_finderIsOpen = false;
	}else{
		jQuery('#favorites-box').fadeIn("slow");
		jQuery('#finder-standard').slideDown("slow");
	}
	G_favoritesIsOpen = true;
}

function hideFavorites(){
	jQuery('#favorites-box').fadeOut("slow");
	jQuery('#finder-standard').slideUp("normal");
	G_favoritesIsOpen = false;
}

function toggleFinder(mode){
	if(G_finderMode != mode){
//		if(G_finderIsOpen){ hideFinder(G_finderMode) }
		jQuery('#finder-box').hide();
		jQuery('#finder-'+G_finderMode).hide();
		showFinder(mode);
	}else{	
		if(G_finderIsOpen){ hideFinder(mode) }else{ showFinder(mode) }
	}
}

function showFinder(mode){
	if(mode == null || mode == ''){mode = G_finderMode}
	//if(mode == 'standard'){jQuery('#resort-finder-button').attr({class:'open'})}
	var fb = jQuery('#finder-'+mode);
	if(G_finderMode != mode){
		document.getElementById('finder-'+mode).appendChild(document.getElementById('finder-box'))
	}
	if(G_firstShow){
		resumeLastPosition();
		G_firstShow = false;
	}
	if(G_favoritesIsOpen && mode == 'standard'){
		jQuery('#favorites-box').hide();
		jQuery('#finder-box').fadeIn("normal");
		// jQuery('#favorites-box').fadeOut("normal",function(){jQuery('#finder-box').fadeIn("normal")});
		G_favoritesIsOpen = false;
	}else{
		jQuery('#finder-box').fadeIn("slow");
		fb.slideDown("slow");
	}
	G_finderMode = mode;
	G_finderIsOpen = true;
}

function hideFinder(mode){
	//if(mode == 'standard'){jQuery('#resort-finder-button').attr({class:'closed'})}
	var fb = jQuery('#finder-'+mode);
	jQuery('#finder-box').fadeOut("slow");
	fb.slideUp("normal");
	G_finderIsOpen = false;
}

function unloadPage(){
	if(G_finderIsOpen && G_finderMode == 'standard'){ hideFinder(G_finderMode) }
	if(G_favoritesIsOpen){ hideFavorites() }
}

function adminResort(link,id){
	location.search='?resortID='+id;
}

function openWorldMap(link,id){
//	alert("World map search coming soon...");
        if(fi.country.fid){jQuery("#rf_country_"+fi.country.fid).hide()}
        if(fi.country.flink){fi.country.flink.removeClass('selected')}
        if(fi.state.fid){jQuery("#rf_state_"+fi.state.fid).hide()}
        if(fi.state.flink){fi.state.flink.removeClass('selected')}
        if(fi.resort1.fid){jQuery("#rf_resort1_"+fi.resort1.fid).hide()}
        if(fi.resort1.flink){fi.resort1.flink.removeClass('selected')}
        if(fi.resort2.fid){jQuery("#rf_resort2_"+fi.resort2.fid).hide()}
        if(fi.resort2.flink){fi.resort2.flink.removeClass('selected')}
        createCookie("fp_worldMap",id);
        createCookie("fp_worldMap",link.id);
	eraseCookie("fp_country");
	eraseCookie("fp_state");
	eraseCookie("fp_resort1");
	eraseCookie("fp_resort2");
	activateWorldMap(link,id);
}

function activateWorldMap(link,id){
	fi.worldMap.fid = id;
	fi.worldMap.flink = link;
	link.addClass('selected');
	jQuery("#world-map").show('100');
}

function pickResort(link,id,alias,name,isHot){
	if(G_finderMode == 'standard'){
		if(isHot){
			location = '/resort_'+alias+'.html';
			jQuery("#finder-standard").slideUp("normal");
		}else{
			alert("Sorry, this resort doesn't have any information. Please click on the highlighted resorts.");
		}
		return true;

	}else if(G_finderMode == 'compare'){
		if(isHot){ addCompare(link,id) }
		else{ alert("This resort doesn't have statistics. The highlighted resorts do.") }

	}else if(G_finderMode == 'nearby_resorts' || G_finderMode == 'linked_resorts'){
		addResortToList(G_finderMode,alias,name);
		
	}else if(G_finderMode == 'admin'){ adminResort(link,id) }
}

