/* iidawave.js - utf-8 */

iidawave_menu = null;

function init_iidawave() {
	// iidawave
	$('<div id="iidawave">')
		.appendTo($('#menubar'))
		.addClass('ui-iidawave')
		.hover(function(){ $(this).addClass('ui-iidawave-hot'); }, function(){ $(this).removeClass('ui-iidawave-hot'); })
		.click(function(ev){
			var rc = getElementPosition( ev.currentTarget || window.event.srcElement );
			iidawave_menu = new uiMenu();
			iidawave_menu.append('iidawave(0)', null, 'IIDA WAVE  -  暮らして楽しい街、飯田');
			iidawave_menu.separator();
			iidawave_menu.append('iidawave(1)', null, 'Music  -  ミュージック', true);
			iidawave_menu.append('iidawave(2)', null, 'Cinema  -  シネマ', true);
			iidawave_menu.append('iidawave(3)', null, 'Runners  -  ランナーズ', true);
			iidawave_menu.append('iidawave(4)', null, 'Walking  -  ウォーキング', true);
			iidawave_menu.append('iidawave(5)', null, 'Yasai  -  やさい', true);
			iidawave_menu.append('iidawave(6)', null, 'Cycling  -  サイクリング', true);
			iidawave_menu.track(rc.left, rc.bottom);
			ev.preventDefault();
			ev.returnValue = false;
		});
	$('body').mousedown(function(ev){
		if (iidawave_menu != null) {
			var rc = getElementPosition( document.getElementById(iidawave_menu.id) );
			if (ev.clientX < rc.left || ev.clientX > rc.right || ev.clientY < rc.top || ev.clientY > rc.bottom) {
				closeMenu();
				ev.preventDefault();
				ev.returnValue = false;
			}
		}
	});
}
function iidawave(index) {
	closeMenu();
	var url = null;
	switch (index) {
	case 0: url='http://www.iidawave.com/'; break;
	case 1: url='http://music.iidawave.com/'; break;
	case 2: url='http://cinema.iidawave.org/'; break;
	case 3: url='http://runnerswave.com/weblog/'; break;
	case 4: url='http://walking.iidawave.org/'; break;
	case 5: url='http://831wave.com/'; break;
	case 6: url='http://cycling.iidawave.org/'; break;
	}
	if (url != null)
		window.location = url;
}
function closeMenu() {
	if (iidawave_menu != null) {
		iidawave_menu.close();
		iidawave_menu = null;
	}
}
function isEmpty(elem) { return (elem == null || elem == undefined || elem.length == 0); }

function getElementPosition(elem) {
	var pos = elem.getBoundingClientRect();
	var scale = 1;
	if ($.browser.safari) {
		var z = $('<div>').css({position:'absolute',left:'0',top:'-100px',width:'10px',height:'10px'}).appendTo($(document.body));
		var q = z.get(0).getBoundingClientRect();
		z.remove();
		scale = (q.bottom - q.top) / 10;
	}
	var left = (document.body.scrollLeft || document.documentElement.scrollLeft) - document.documentElement.clientLeft;
	var top = (document.body.scrollTop  || document.documentElement.scrollTop) - document.documentElement.clientTop;
	return {
		left:   Math.floor(pos.left / scale) + left,
		top:    Math.floor(pos.top / scale) + top,
		right:  Math.floor(pos.right / scale) + left,
		bottom: Math.floor(pos.bottom / scale) + top
	}
}

function docWidth() { return (document.documentElement.clientWidth || document.body.clientWidth); }
function docHeight() { return (document.documentElement.clientHeight || document.body.clientHeight); }

function uiMenu() {
	this.id = 'popup-menu';
	this.contents = "";
	this.isnull = function() { return (this.menu==null); }
	this.autoclose = true;
	this.menu = null;
	this.cssshadow = ($.browser.safari || ($.browser.mozilla && $.browser.version >= '1.9.1'));
	this.cssMenuItem = 'menuitem';
	this.cssMenuItemHover = 'ui-menuitem-hover';
	this.cssMenuSeparator = 'menuseparator';
	if ($.browser.msie && $.browser.version < '7') {
		this.cssMenuItem += '-ie6';
		this.cssMenuItemHover += '-ie6';
		this.cssMenuSeparator += '-ie6';
	}
	this.track = function(x, y) {
		if (!isEmpty(this.contents)) {
			$(':input').each(function(){$(this)[0].blur();});

			this.menu = $('<div id="'+this.id+'" class="menu-frame">').appendTo('body').hide().mousedown(function(ev){ ev.preventDefault(); });
			var inner = $('<div class="menu-frame-inner">').appendTo(this.menu);

			if (this.cssshadow)
				this.menu.addClass($.browser.safari ? 'menu-webkit' : 'menu-gecko');
			var pos = {
				x: x!=null ? x : 0,
				y: y!=null ? y : 0
			};

			inner.html(this.contents);
			this.menu.css({top:pos.y+'px',left:pos.x+'px',width:'auto'}).fadeIn(300);
			if (parseInt(this.menu.css('left')) + this.menu.width() > docWidth()) {
				var newLeft = Math.max(0, docWidth() - this.menu.width());
				this.menu.css({left:newLeft});
			}
			var me = this;
			$('.' + this.cssMenuItem).hover(
					function(){$(this).addClass(me.cssMenuItemHover)},
					function(){$(this).removeClass(me.cssMenuItemHover)}
				);
		}
	}
	this.close = function() {
		if (this.menu != null) {
			this.menu.fadeOut(100, function(){ $(this).remove(); });
			this.menu = null;
		}

		this.contents = "";
	}
	this.append = function(clickFunction, cssStyle, displayText, checked) {
		var str = (!isEmpty(displayText) ? displayText : '') + '  ';
		str.replace(/ /g, '&nbsp');

		var innercls = "menuitem-inner";
		if (checked != null && checked == true)
			innercls += ' ui-useicon';

		this.contents
				+= '<div class="' + this.cssMenuItem + '"'
				+ (!isEmpty(cssStyle) ? (' style="' + cssStyle + '"') : "")
				+ (!isEmpty(clickFunction) ? (' onclick="' + clickFunction + ';return false;"') : "")
				+ '><div class="'+innercls+'">'
				+ str
				+ '</div></div>';
	}
	this.separator = function() {
		this.contents += '<div class="' + this.cssMenuSeparator + '"></div>';
	}
	this.maxheight = function(height) {
		if (height > 0)
			this.contents = '<div style="max-height:'+height+'px;overflow-x:hidden;overflow-y:auto;">' + this.contents + '</div>';
	}
}

