/**
 *
 * @param  maptabid to disable maptab, set true.
 * @return
 *:*/
function window_controller(mainwindow, drawer, lat, lon, maptabid, bodychild)
{
	this.mainwindow = mainwindow;
	this.drawer = drawer;

	this.ie_sucks = true;

	this.place_marker = null;

	this.outermost = bodychild || "cont"; 
	this.maptab = maptabid || "maptab";
	this.detailsmap = window.map_elementid;
	//this.topmost_visibility = topmost_visibility;
	this.visibility = [];
	if (this.mainwindow) {
		this.visibility[this.mainwindow] = is_visible(this.mainwindow);
	}
	if (this.drawer) {
		this.visibility[this.drawer ] = is_visible(this.drawer);
	}

	this.set_default_latlng(lat, lon);
}

window_controller.prototype.set_default_latlng = function (lat, lon)
{
	this.default_latlng = new GLatLng(lat, lon);
}

window_controller.prototype.switch_and_recenter = function ()
{
	this.toggle_and_recenter(this.mainwindow);
	this.toggle(this.drawer);
}

window_controller.prototype.ie_display_hack = function ()
{
	// avoid IE bug that 'container's table borders are shown even 'window' is hidden.
	if ( this.ie_sucks ) {
		var container = $('container');
		if ( container ) {
			Element.show( container );
			this.ie_sucks = false;
		}
	}
}

window_controller.prototype.toggle_and_recenter = function (subjectid, lat, lng)
{
	this.toggle( subjectid );

	this.ie_display_hack();
	
	var p;
	if (lng == null) {
		p = this.default_latlng;
		if ( p == null || p.is_valid() == false) {
			if ( map == null )
				return null;
			p = map.getCenter();
			if ( p == null || p.is_valid() == false) {
				p = new GLatLng(lat, lng);
			}
		}
	} else {
		p = new GLatLng(lat, lng);
	}
		
	return this.make_point_visible(p);
}

window_controller.prototype.toggle = function (subjectid, afterFinish)
{
try{
	var subject = $(subjectid);
	var visible = is_visible( subject );
	var next_state = !visible;


	var layer_width = subject.offsetWidth;
	if ( next_state ) {
		// make visible.
		subject.style.left = (0 - layer_width) + "px";
		toggle_visibility(subject);
		subject.style.visibility = "";
		this.move(subject, layer_width, afterFinish);

	} else {
		// hide.
		this.move(subject, 0 - layer_width, function () {
			if (afterFinish)
				afterFinish.apply(this);
			toggle_visibility(subject);
		} );
	}
	
	// hide placemarker when drawer is visible.
	if ( subjectid == this.drawer && this.place_marker != null ) {
		if (next_state)
			map.removeOverlay(this.place_marker);
		else
			map.addOverlay(this.place_marker);
		
	}
	
	// show maptab if mainwindow is nidden and drawer is not exist.
	if ( subjectid == this.mainwindow && ! this.is_layer_visible( this.drawer ) ) {
		// and move maptab.
		var maptabid = this.maptab;
		var tab = $(maptabid);
		if ( maptabid != true && tab) {
			var x = tab.offsetWidth;
			var move_by_x =  (next_state) ? 0 - x : x;
			this.move(maptabid, move_by_x, function() {
				toggle_visibility(tab);
			});
		} else {
			trace("tab not found.");
		}
	}

	this.visibility[subjectid] = next_state;
	
}catch(e){
	trace(e);
}
}

window_controller.prototype.move = function (subject, x, afterFinish)
{
	new Effect.MoveBy(subject, 0, x, {
			afterFinish: afterFinish,
			duration: 0.15,
			transition: function (pos) {
				return Math.pow(pos, 3);
			}
		} );
}

window_controller.prototype.make_point_visible = function (latlng, dont_pan)
{
	if (latlng == null) {
		trace("use previous center");
		latlng = this.visible_center;
	} else {
		this.visible_center = latlng;
	}

	//var coverX = cover.offsetWidth;
	var pixel = map.fromLatLngToDivPixel(latlng);


	//var pixel = map.spec.getBitmapCoordinate(lat, lon, zoom);
	//var x = pixel.x - (map.viewSize.width / 2) + coverX + (map.viewSize.width - coverX)
	//var r = map.spec.getLatLng(pixel.x - (coverX / 2), pixel.y , zoom);
	
	// 地図と上のレイヤとが重なってる量を出す
	var coverX = 0;
	if ( this.mainwindow ) {
		if ( this.visibility[this.mainwindow] ) {
			coverX = $(this.mainwindow).offsetWidth;
		} else {
			coverX = 0;
		}
	} else {
		if (this.drawer && this.visibility[this.drawer]) {
			
		} else {
			
		}
		coverX = 0;
	}

//	var intersect = $(this.outermost).parentNode.offsetWidth - coverX;
	var container = map.getContainer();
	var intersect = container.offsetWidth - coverX;

	pixel.x -= (container.offsetWidth - intersect ) / 2 
//	pixel.x -= (container.offsetWidth - intersect) / 2;
//	pixel.y -= mogue_footerY;

	var r = map.fromDivPixelToLatLng(pixel);
	
	if (dont_pan)
		map.setCenter(r);
	else
		map.panTo(r);
	return r;
}

window_controller.prototype.open_drawer = function (lat, lon)
{
	this.resize_map_window(70);
	this.toggle_and_recenter(this.drawer, lat, lon);
}
/*
window_controller.prototype.close_drawer = function ()
{
	map.closeInfoWindow();
	
	this.toggle( this.drawer, function () {
		this.resize_map_window(100);
	} );
	this.toggle_and_recenter(this.mainwindow);
}
*/

/**
 * close drawer and resize map div to 100%.
 * @param
 * @return
 **/
window_controller.prototype.close_drawer = function ()
{
	this.remove_overlays();
	this.resize_map_window(100);
	
	if (map)
		map.closeInfoWindow();
	
	if (this.drawer)
		this.toggle( this.drawer );
	if (this.mainwindow) {
		var p = map.getCenter();
		var lat = p.lat();
		var lng = p.lng();
		this.set_default_latlng(lat, lng);
		this.toggle_and_recenter(this.mainwindow);
	}
}

window_controller.prototype.remove_overlays = function ()
{
	if (window.mv) {
		var e = new each_periodic( 50, window.mv.points, function (k) {
				map.removeOverlay(k.marker);
			} );
	}

}

window_controller.prototype.set_dnd_handlers = function (marker)
{
		GEvent.addListener(marker, 'dragstart', function () {
			map.closeInfoWindow();
		}.bind(this) );
		GEvent.addListener(marker, 'dragend', function () {
			var p = marker.getPoint();
			window.lat = p.lat();
			window.lon = p.lng();
			this.set_default_latlng(window.lat, window.lon);

			if ( this.visibility[this.mainwindow] ) {
				this.make_point_visible(p);
			} else {
				GEvent.trigger(marker, 'click');
				//this.toggle_mainwindow(p);
			}
		}.bind(this) );
	
}
/**
 * add a marker which indicates items place.
 * this marker is hide when drawer is shown.
 * @param
 * @return
 **/
window_controller.prototype.add_place_marker = function (lat, lon, index, click_handler, is_draggable)
{
	if ( this.place_marker != null ) {
		map.removeOverlay(this.place_marker);
	}
	var marker = create_marker(new GPoint(lon, lat), null, null, index, is_draggable);
	if ( is_draggable ) {
		this.set_dnd_handlers(marker);
	}

	if (click_handler == null) {
		click_handler = function() {
			this.toggle_mainwindow();
		};
	}

	//GEvent.addListener(marker, 'click', click_handler.bind(marker) );
	GEvent.addListener(marker, 'click', click_handler.bind(marker) );
	map.addOverlay(marker);

	this.place_marker = marker;
	return marker;
}

window_controller.prototype.resize_map_window = function (v)
{
	var m = $(this.detailsmap);
	if (m == null) {
		trace( "detailsmap" + " is null.");
	} else {
		m.style.width = v + "%";
	}
	if (map)
		map.checkResize();
}

/**
 * toggle mainwindow and hide drawer if it exists.
 * @param
 * @return
 **/
window_controller.prototype.toggle_mainwindow = function (lat, lon)
{
	if ( typeof map == 'object' ) {
		map.closeInfoWindow();
	} else {
		// if the map is not defined yet, prevent toggling.
		return;
	}

	if ( this.drawer && this.visibility[ this.drawer ] ) {
		this.remove_overlays();
		this.toggle(this.drawer);
		this.resize_map_window(100);
	}
	this.toggle_and_recenter( this.mainwindow, lat, lon );
}

window_controller.prototype.is_layer_visible = function (id)
{
	var b = ( id && this.visibility[id] );
	return b;
}

/**
 * show mapview.
 * close main window, open drawer and resize map to 70%.
 * @param
 * @return
 **/
window_controller.prototype.show_map_navigation = function (favtype, maptitle, load_callback)
{
try{
	$('mapview_title').innerHTML = maptitle;

	var panes = [
			'searchuievnew',
			'searchuievtag', 
			'searchuievarea', 
			'searchuievdate'
	];

	window.mv = new mapview(panes, 'elm_status');
	window.mv.toggle_pane("searchuievnew"); // make return to initial state.

	panes.each ( function (p, q) {
		$(p).innerHTML = "";
	} );
	Element.hide( $("search_result") );

	if ( favtype == "event")
		Element.show("tab_searchuievdate");
	else
		Element.hide("tab_searchuievdate");

	this.resize_map_window(70);
	if (map)
		map.panTo( this.default_latlng );

	this.toggle( this.mainwindow, load_callback );
	this.toggle( this.drawer );

}catch(e) {
	trace(e);
}
}