var dispatcher = new Dispatcher();
var as = new AddressSearcher('entry', 'result', 0.2, {
    found_help: '&nbsp;<span id="step1help" style="color: #ff0000">←をクリックしてStep 2.へ</span>'
});
var mm = new MapManager('map', 36.254683, 138.119466, 5, {
    height_excludes: ['top', 'input_zone', 'bottom']
	,height_excludes_margin: 15
});

var Operator = Class.create();
Operator.prototype = {
    link_name_: null,
    title_name_: null,
    org_href_: null,
    lat_: null,
    lng_: null,
    zoom_: null,

    initialize: function(link_name, title_name, org_href) {
        this.link_name_ = link_name;
        this.title_name_ = title_name;
        this.org_href_ = org_href;
    },

    on_locate: function(lat, lng, label) {
        var elm = $(this.title_name_);
        if (elm.setText) {
            elm.setText(label);
        } else {
            elm.value = label;
        }
        this.lat_ = lat;
        this.lng_ = lng;
        this.make_href_();
		$('make_button').disabled = false;
		$('step1help').hide();
		$('disabled_second_block').hide();
		$('second_block').show();
    },
	
	on_location_click: function() {
		$('title').focus();
	},

    make_href_: function() {
        var elm = $(this.link_name_);
        var title_elm = $(this.title_name_);
		elm.lat.value = this.lat_;
		elm.lng.value = this.lng_;
//        var href = this.org_href_ + '?lat=' + this.lat_ + '&lng=' + this.lng_ + '&label=' + encodeURIComponent(title_elm.value);
        if (this.zoom_) {
			elm.zoom.value = this.zoom_;
		}
    },

    on_center_changed: function(lat, lng) {
        this.lat_ = lat;
        this.lng_ = lng;
        this.make_href_();
    },

    title_changed: function() {
        this.make_href_();
    },

    on_zoom_end: function(newLevel, oldLevel) {
        this.zoom_ = newLevel;
        this.make_href_();
    }
}
var op = new Operator('input_label', 'title', '/pin/create');

dispatcher.register(as);
dispatcher.register(mm);
dispatcher.register(op);

var FocusBlurAction = Class.create();
FocusBlurAction.prototype = {
	element_name_: null,
	focus_action_: null,
	blur_action_: null,
	opts_: null,
	initialize: function(element_name, focus_action, blur_action) {
		this.element_name_ = element_name;
		this.focus_action_ = focus_action;
		this.blur_action_ = blur_action;
		this.opts_ = new Object();
		Event.observe(element_name, 'focus', this.on_focus_action_.bind(this));
		Event.observe(element_name, 'blur', this.on_blur_action_.bind(this));
	},
	on_focus_action_: function() {
		this.focus_action_($(this.element_name_), this.opts_);
	},
	on_blur_action_: function() {
		this.blur_action_($(this.element_name_), this.opts_);
	}
}

function on_load_actions() {
    mm.start();
    Event.observe('title', 'change', op.title_changed.bind(op)); 
	new FocusBlurAction('entry', active_to, deactive_to);
	new FocusBlurAction('title', active_to2, deactive_to2);
    new HelpOnInput('entry', 'ここに住所や駅名などを入力してください。', {
		clear_on_init: true,
		help_color: '#f00000'
	});
    new HelpOnInput('title', 'タイトルや一言メッセージをどうぞ。', {
        clear_on_init: true,
		help_color: '#808080'
    });
}

function active_to(elm, opts) {
	if (elm.style.borderColor) {
		opts.original = elm.style.borderColor;
	} else {
		opts.original = '#ffffff';
	}
	elm.style.borderColor = '#ff0000';
}

function active_to2(elm, opts) {
	active_to(elm, opts);
	$('disp_help').innerHTML = '↓下にタイトルやひと言メッセージをどうぞ。次に地図上の赤マークをつかんで正しい位置に動かします。最後に「作る!」をクリックします。';
	var mb = $('make_button');
	mb.src = '/images/btn_make2.png';
	mb.disabled = false;
}

function deactive_to(elm, opts) {
	elm.style.borderColor = opts.original;
}

function deactive_to2(elm, opts) {
	deactive_to(elm, opts);
	$('disp_help').innerHTML = '';
}

function on_resize_actions() {
    mm.setMapSize();
}

Event.observe(window, 'load', on_load_actions, false);
Event.observe(window, 'unload', GUnload, false);
Event.observe(window, 'resize', on_resize_actions, false);

function set_place() {
	if (as.execute_click()) {
		$('title').focus();
	}
}

function set_label() {
//	var elm = $('title');
//	if (elm.getText) {
//		elm.value = elm.getText();
//	} else {
//	}
//	op.title_changed();
}
