var Tipz = new Class({

	getOptions: function(){
		return {
			onShow: function(tip){
				tip.setStyle('visibility', 'visible');
			},
			onHide: function(tip){
				tip.setStyle('visibility', 'hidden');
			},
			maxTitleChars: 30,
			showDelay: 100,
			hideDelay: 100,
			className: 'tool',
			offsets: {'x': 16, 'y': 16},
			fixed: false
		};
	},

	initialize: function(elements, options){
		this.setOptions(this.getOptions(), options);
		this.toolTip = new Element('div').addClass(this.options.className+'-tip').setStyles({
			'position': 'absolute',
			'top': '0',
			'left': '0',
			'visibility': 'hidden'
		}).injectInside(document.body);
		this.wrapper = new Element('div').injectInside(this.toolTip);
		$each(elements, function(el){
			this.build($(el));
		}, this);
		if (this.options.initialize) this.options.initialize.call(this);
	},

	build: function(el){
		el.myTitle = el.href ? ('<img src="http://mozshot.nemui.org/shot/nobg?' + el.href + '" alt="" width="128" height="128" />') : (el.rel || false);
//		el.myTitle = el.href ? ('<span class="ail" style="_filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="http://mozshot.nemui.org/shot?' + el.href + '", sizingMethod=scale); _width:128px; _height:128px;"><img src="http://mozshot.nemui.org/shot?' + el.href + '" " alt="" width="128" height="128" /></span>') : (el.rel || false);
		el.addEvent('mouseover', function(event){
			this.start(el);
			this.locate(event);
		}.bindWithEvent(this));
		if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
		el.addEvent('mouseout', this.end.bindWithEvent(this));
	},

	start: function(el){
		this.wrapper.setHTML('');
		if (el.myTitle){
			new Element('span').injectInside(
				new Element('div').addClass(this.options.className+'-title').injectInside(this.wrapper)
			).setHTML(el.myTitle);
		}
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this);
	},

	end: function(event){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this);
		event.stop();
	},

	locate: function(event){
		var win = {'x': window.getWidth(), 'y': window.getHeight()};
		var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
		var tip = {'x': this.toolTip.offsetWidth, 'y': this.toolTip.offsetHeight};
		var prop = {'x': 'left', 'y': 'top'};
		for (var z in prop){
			var pos = event.page[z] + this.options.offsets[z];
			if ((pos + tip[z] - scroll[z]) > win[z]) pos = event.page[z] - this.options.offsets[z] - tip[z];
			this.toolTip.setStyle(prop[z], pos + 'px');
		};
		event.stop();
	},

	show: function(){
		this.fireEvent('onShow', [this.toolTip]);
	},

	hide: function(){
		this.fireEvent('onHide', [this.toolTip]);
	}

});

Tipz.implement(new Events);
Tipz.implement(new Options);


var Site = {
	start: function() {
		Site.effects();
		Site.externalLinks();
		Site.backToTop();
		Site.myEmail();
		Site.contactText();
		Site.rollover();
	},

	effects: function() {
		var ras = [];
		$$('.storycontent a, .tips').each(function(ra){
			if (ra.getAttribute('title') && !ra.getAttribute('rel')) ras.push(ra);
		});

		new Tipz(ras, {
			maxTitleChars: 22,
			onShow: function(rtip){
				rtip.effects({
					duration: 200,
					transition: Fx.Transitions.quadInOut,
					fps: 60
				}).custom({
					'opacity': [0, 0.85]
				});
			},
			onHide: function(rtip) {
				rtip.effects({
					duration: 100,
					transition: Fx.Transitions.quadInOut,
					fps: 60
				}).custom({
					'opacity': [0.85, 0]
				});
			},
			className: 'round'
		});
	},

	externalLinks: function() {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && (anchor.getAttribute('class')||anchor.getAttribute('className')) == "external")
				anchor.target = "_blank";
		}
	},

	backToTop: function() {
		Site.backtotop = function() {
			var x1 = x2 = x3 = 0;
			var y1 = y2 = y3 = 0;

			if (document.documentElement) {
				x1 = document.documentElement.scrollLeft || 0;
				y1 = document.documentElement.scrollTop || 0;
			}

			if (document.body) {
				x2 = document.body.scrollLeft || 0;
				y2 = document.body.scrollTop || 0;
			}

			x3 = window.scrollX || 0;
			y3 = window.scrollY || 0;

			var x = Math.max(x1, Math.max(x2, x3));
			var y = Math.max(y1, Math.max(y2, y3));

			window.scrollTo(Math.floor(x / 1.5), Math.floor(y / 1.5));

			if (x > 0 || y > 0) {
				window.setTimeout("Site.backtotop()", 25);
			}
		};

		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && (anchor.getAttribute('class')||anchor.getAttribute('className')) == "backToTop") {
				anchor.onclick = function() {
					Site.backtotop();
					return false;
				}
				anchor.onkeypress = function() {
					Site.backtotop();
					return false;
				}
			}
		}
	},

	myEmail: function() {
		var emailriddlerarray=[105,110,102,111,64,109,103,45,99,111,46,110,101,116];
		var encryptedemail_id20=''; //variable to contain encrypted email 
		for (var i=0; i<emailriddlerarray.length; i++)
			encryptedemail_id20+=String.fromCharCode(emailriddlerarray[i]);
		
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute('class')||anchor.getAttribute('className')) {
				if ((anchor.getAttribute('class')||anchor.getAttribute('className')).match('myEmailShow$')) {
					anchor.innerHTML = encryptedemail_id20;
				}
				if ((anchor.getAttribute('class')||anchor.getAttribute('className')).match('^myEmailHref')) {
					anchor.href = "mailto:"+encryptedemail_id20;
				}
			}
		}
	},

	contactText: function() {
		if (!document.getElementsByTagName) return;

		var textarea = document.getElementById('contactText');
		if (textarea) {
			if ("お問い合わせ内容をご記入ください" == textarea.value) {
				textarea.style.color = "#999999";
			}
			textarea.onfocus = function() {
				if ("お問い合わせ内容をご記入ください" == textarea.value) {
					textarea.value = "";
					textarea.style.color = "#000000";
				}
			}
		}
	},

	rollover: function() {
		if (!document.getElementsByTagName) return;
		var images = document.getElementsByTagName("img");
		for (var i=0; i<images.length; i++) {
			var image = images[i];
			if (image.getAttribute("src").match("_off\.")) {
				image.onmouseover = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_off\.", "_on\."));
				}
				image.onmouseout = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_on\.", "_off\."));
				}
			}
		}
	}
}

window.addEvent('domready', Site.start);

