var globals = new Object();
globals.timerId;//httpRequest Timer Id
globals.runningRequest;//now running Request
globals.runningMethod;//now running Method - network error >> retry 
globals.isNetwork;
globals.isRunning = false;

// ex) getXMLHTTPRequest("http://127.0.0.1",'sayHello'), >> startRefresh : ON 
//     getXMLHTTPRequest("http://127.0.0.1",'sayHello','OFF') 
function getXMLHTTPRequest(url,callback,startRefresh)
{
	if(startRefresh != 'OFF'){
		//showRefresh();
	}
	globals.runningRequest = new cRequest(url, callback, startRefresh);
}

var divInitMethod = {main:'mainLoad',gallery:'galleryLoad',weather:'weatherLoad',health:'healthLoad',visas:'visasLoad',when2go:'when2goLoad',pois:'poisLoad',places:'placesLoad',nearby:'nearbyLoad',see:'seeLoad',shop:'shopLoad',sleep:'sleepLoad',eat:'eatLoad',night:'nightLoad',map:'mapLoad'};

function mainLoad(type) {}
function weatherLoad(type) {}
function healthLoad(type) {}
function visasLoad(type) {}
function when2goLoad(type) {}
function poisLoad(type) {}
function placesLoad(type) {}
function galleryLoad(type) {
	if (!loaded_gallery) {
		url = "/?go=ajax&ret=gallery&ref="+ref+"&d="+dnid;
		getXMLHTTPRequest(url,galleryUpdate);
	}
}
function galleryUpdate() {
	if (result.status == "ok") {
		var rhtml = result.resText;
		if (rhtml.length > 0) {
			loaded_gallery = true;
			document.getElementById("gallery").innerHTML = rhtml;
		}
	}
}
function nearbyLoad(type) {
	if (!loaded_nearby) {
		url = "/?go=ajax&ret=nearby&ref="+ref+"&d="+dnid+"&g="+geo_id+"&poi="+poi_id+"&a="+act+"&l="+lvl_next+"&dest_title="+dest_title;
		getXMLHTTPRequest(url,nearbyUpdate);
	}
}
function nearbyUpdate(result) {
	if (result.status == "ok") {
		var rhtml = result.resText;
		if (rhtml.length > 0) {
			loaded_nearby = true;
			document.getElementById("nearby").innerHTML = rhtml;
		}
	}
}

function tabsOff()
{
	elems = getElementsByClass("tab");
	for(i=0; i<elems.length; i++) {
		elems[i].className="tab";
	}
}

function mapLoad(type) {
	document.getElementById("footprints").style.display="none";
	document.getElementById("content").style.display="none";
	document.getElementById("bottnav").style.display="none";
	document.getElementById("footer").style.display="none";
	document.getElementById("google_map").style.display="block";
}

function mapUnLoad() {
	document.getElementById("footprints").style.display="block";
	document.getElementById("content").style.display="block";
	document.getElementById("bottnav").style.display="block";
	document.getElementById("footer").style.display="block";
	document.getElementById("google_map").style.display="none";
}

var search_timer;
var search_timer2;
var layerId = 'main';

addEventListener("load", function()
{
	c_wrapper = document.getElementById("iphone_body_id");
	c_wrapper.addEventListener("touchstart", scroll_controller, false);
	document.getElementById("search_input").onblur = search_delayed_blur;
	document.getElementById("search_input").onkeyup = search_keyup;
	//navi.init('main');//default div id
}, false);

function search_submit()
{
	if (document.getElementById("search_input").value == "") {
		alert("Please enter keyword to search");
	} else {
		document.getElementById("search_form").submit();
	}
}

function search_focus()
{
	document.getElementById("search_suggestions").style.display = 'block';
	window.setTimeout("window.scrollBy(0,40)", 500);
}

function search_delayed_blur()
{
	search_timer = window.setTimeout(suggestions_clear, 1500);
}

function search_keyup()
{
	if (search_timer2) {
		window.clearTimeout(search_timer2);
		search_timer2 = null;
	}
	document.getElementById("search_suggestions").innerHTML = "";
	document.getElementById("search_loading").style.display = 'block';
	search_timer2 = window.setTimeout("search_ajax()", 250);
}

function search_ajax()
{
	var val = document.getElementById("search_input").value;
	if (val != "") {
		url = "/?go=ajax&ret=geotax&ref="+ref+"&q="+escape(val);
		getXMLHTTPRequest(url,suggestions_update);
	}
}

function suggestions_update(result)
{
	if (result.status == "ok") {
		var rhtml = result.resText;
		if (rhtml.length > 0) {
			document.getElementById("search_suggestions").innerHTML = rhtml;
		}
		document.getElementById("search_loading").style.display = 'none';
	}
}

function suggestions_clear()
{
	document.getElementById("search_suggestions").style.display = 'none';
	window.setTimeout('window.scrollTo(0,0)', 500);
}


function pop_location_and_focus()
{
	document.getElementById("change-loc").style.display = 'block';
	document.getElementById("pu-search-input").focus();
	setTimeout("window.scrollTo(0,100)", 500);
}

/*
 * Scroll controller
 */

// create the scroll controller
var scroll_controller = {
	current_position : 0
}

// updates the rotation of the ring to the specified radians angle
scroll_controller.set_scroll = function (scroll) {
	this.current_position = scroll;
	//nav.style.top = scroll;
}

// handle events
scroll_controller.handleEvent = function (event)
{
	this.lastScroll = 0;
	// dispatch the event to the right method based on the type
	switch (event.type)
	{
		case 'touchstart':
		this.interactionStart(event);
		break;
		case 'touchmove':
		this.interactionMove(event);
		break;
		case 'touchend':
		this.interactionEnd(event);
		break;
	}
}

// called when a touchstart event is received on an item
scroll_controller.interactionStart = function (event)
{
	window.addEventListener('touchmove', this, true);
	window.addEventListener('touchend', this, true);
}

// called when a touchmove event is received
scroll_controller.interactionMove = function (event)
{
	//setTimeout("nav.style.top = (window.pageYOffset)+350 + 'px'", 500);
	//setTimeout("nav.style.top = (window.pageYOffset)+240 + 'px'", 500);
}

// called when a touchend event is received
scroll_controller.interactionEnd = function (event)
{
	// stop events, we're done
	window.removeEventListener('touchmove', this, true);
	window.removeEventListener('touchend', this, true);
}