/*
 * Main Scripts File — http://frantsev.ru/frantsev.ru.js
 * Copyright © 2008–2009 Nikolay Frantsev. All rights reserved.
 */

// kill frames
if (parent.frames.length != 0)
	parent.location.href = document.location.href;

// check domain
if (['frantsev.ru', 'frantsev'].indexOf(parent.location.host) == -1)
	parent.location.href = 'http://frantsev.ru/';

var frantsev = {

	// constants

	INTERVAL : 20,
	SCROLL_STEP : 100,
	SCROLL_ACCELERATION : 25,
	FOOTER_STEP : 5,
	FOOTER_ACCELERATION : 3,

	// variables

	pages : {},
	timer : null,

	// main

	resize : function ()
	{
		if (this.window_width != window.innerWidth)
			this.main();
	},

	main : function ()
	{
		// define basic variables
		this.scroll_top = document.getElementById('scroll').offsetTop;
		this.main_height = document.getElementById('main').offsetHeight;
		this.window_width = window.innerWidth;

		// go home by default
		var hash = window.location.hash.replace(/^#/, '');
		hash = (hash && (document.getElementById(hash) || document.getElementById(hash + '-old'))) ? hash : 'home';
		window.location.replace('#home');

		// process navigation
		var pages = document.getElementById('scroll').childNodes, top = 0;
		for (var i = 0, l = pages.length - 1; i < l; i++)
			if (pages[i].nodeName && pages[i].nodeName.toLowerCase() == 'div')
			{
				var height = pages[i].offsetHeight;
				var id = pages[i].id.replace(/\-old$/, '');
				this.pages[id] = {'top' : top, 'height' : height};
				top -= height;
				pages[i].id = id + '-old';
			}

		this.page(hash, true);
	},

	page : function (id, quick)
	{
		window.location.replace('#' + id);
		clearTimeout(this.timer);
		this.move(this.pages[id]['top'], this.scroll_top > this.pages[id]['top'] ? -this.SCROLL_STEP : this.SCROLL_STEP, this.pages[id]['height'] - 2060 + 140,
			this.main_height > this.pages[id]['height'] - 2060 + 140 ? -this.FOOTER_STEP : this.FOOTER_STEP, quick);
	},

	move : function (top, top_step, height, height_step, quick)
	{
		if (this.scroll_top == top && this.main_height == height)
			return;

		// quick jump without any scroll
		if (quick)
		{
			this.scroll_top = top;
			document.getElementById('scroll').style.top = this.scroll_top + 'px';
			this.main_height = height;
			document.getElementById('main').style.height = this.main_height + 'px';
			return;
		}

		// move main
		if (this.scroll_top != top)
		{
			this.scroll_top += top_step;
			if (top_step > 0)
				if (this.scroll_top > top)
					this.scroll_top = top;
				else
					top_step += this.SCROLL_ACCELERATION;
			else
				if (this.scroll_top < top)
					this.scroll_top = top;
				else
					top_step -= this.SCROLL_ACCELERATION;
			document.getElementById('scroll').style.top = this.scroll_top + 'px';
		}

		// move footer
		if (this.main_height != height)
		{
			this.main_height += height_step;
			if (height_step > 0)
				if (this.main_height > height)
					this.main_height = height;
				else
					height_step += this.FOOTER_ACCELERATION;
			else
				if (this.main_height < height)
					this.main_height = height;
				else
					height_step -= this.FOOTER_ACCELERATION;
			document.getElementById('main').style.height = this.main_height + 'px';
		}

		// repeat it
		this.timer = setTimeout(function () {
			if (frantsev.scroll_top != top || frantsev.main_height != height)
				frantsev.move(top, top_step, height, height_step, quick);
		}, this.INTERVAL);
	}

};

// document ready
$(function () {

	$(document).pngFix();
	$('.pngfix').pngFix();

	frantsev.main();

	// fix position when window has resized
	$(window).resize(function () {
		frantsev.resize();
	});

	// hide noscript data
	$('#noscript').remove();

	// make contact data. do not ask me how it works :)
	$('#jid-mozilla,#mail-mozilla').text('@'.replace(/$/, 'mozilla-russia').replace(/^/, 'shutnik').replace(/$/, '.org'));
	$('#jid-nikolay,#mail-nikolay').text('@'.replace(/$/, 'frantsev').replace(/^/, 'nikolay').replace(/$/, '.ru'));
	$('#jid-mozilla,#jid-nikolay').each(function () {
		$(this).attr('href', $(this).text().replace(/^/, 'xmpp:').replace(/$/, '?roster;name=Shutnik'));
	});
	$('#mail-mozilla,#mail-nikolay').each(function () {
		$(this).attr('href', $(this).text().replace(/^/, 'mailto:'));
	});

	// make link handlers
	$('a.local').each(function () {
		if (!(id = $(this).get(0).hash))
			return;
		$(this).attr('onclick', 'frantsev.page("' + id.replace(/^#/, '') + '");return false');
	});

	// make cool tooltips for abbr
	$('abbr').each(function (index) {
		if (!(title = $(this).attr('title')))
			return;
		$(document.body).append('<div id="tooltip' + index + '" class="tooltip">' + title + '</div>');
		$(this).removeAttr('title').mouseover(function () {
			$('#tooltip' + index).stop().show().fadeTo(300, 0.9);
		}).mouseout(function () {
			$('#tooltip' + index).stop().fadeTo(300, 0, function () {
				$(this).hide();
			});
		}).mousemove(function (e) {
			$('#tooltip' + index).css({left:e.pageX + 15, top:e.pageY + 15});
		});
	});

});
