(function() {

var isIe = Browser.Engine.trident;
var isIe6 = Browser.Engine.version <= 4;
var topLinks = $$('.top-nav a');
var fx = {},
	pos = {
		'introduction': [275, 475],
		/*'news': [350, 550],*/
		'products': [410, 210],
		'tripkid': [375, 75],
		'mashimaro': [435, 135],
		'recruitment': [660, 360],
		'tkcf': [720, 420]
	};
var currentId = '<?php echo $this->params->controller; ?>',
	currentShowId = null,
	hideTT = null, zIndex = 10, init = {}, mousein = {};

if (isIe) {
	topLinks.each(function(link) {
		new Element('span', { 'text': link.get('text'), 'class': 'shadow' }).setStyle('width', link.getWidth()).injectAfter(link);
	});
}

function showSub(id, event) {
	var subId = 'sub_' + id, navId = 'nav_' + id;
	var sub = document.id(subId), nav = document.id(navId);
	if (!sub || !pos[id]) return;
	if (!fx[id])
		fx[id] = new Fx.Morph(sub, {duration: 800, transition: 'back:out'});
	if (!init[id]) {
		init[id] = true;
		mousein[id] = false;
		sub.addEvents({
			mouseleave: function() {
				var id = this.get('id');
				var subId = id.replace('sub_', '');
				if (subId != currentId)
					hideSub(subId);
				mousein[id] = false;
			},
			mouseenter: function() {
				mousein[id] = true;
			}
		});
	}
	var f = fx[id];
	if (currentShowId) {
		hideSub(currentShowId);
	}
	sub.setStyles({ 'display': '', 'left': pos[id][1], 'z-index': zIndex++ });
	f.pause();
	f.clearChain();
	f.start({
		left: pos[id][0],
		opacity: 1
	}).chain(function() {
		currentShowId = id;
	});
	for (var p in pos) {
		if (p != id)
			hideSub(p);
	}
}

function hideSub(id, event) {
	var subId = 'sub_' + id;
	var sub = document.id(subId);
	if (!sub || !pos[id]) return;
	if (!fx[id])
		fx[id] = new Fx.Morph(sub, {duration: 800, transition: 'back:out'});
	if (!init[id]) {
		init[id] = true;
		mousein[id] = false;
		sub.addEvents({
			mouseleave: function() {
				var id = this.get('id');
				var subId = id.replace('sub_', '');
				if (subId != currentId)
					hideSub(subId);
				mousein[id] = false;
			},
			mouseenter: function() {
				mousein[id] = true;
			}
		});
	}
	if (event) {
		if (mousein[id])
			return;
		//console.log(cx, cy, p.top, p.bottom, p.left, p.right);
	}
	var f = fx[id];
	f.pause();
	f.clearChain();
	f.start({
		left: pos[id][1],
		opacity: 0
	}).chain(function() {
		sub.setStyle('display', 'none');
	});

}

showSub(currentId);

$$('.top-nav a').addEvents({
	mouseenter: function(event) {
		if (isIe) {
			var parent = this.getParent('li');
			parent.addClass('top-nav-curr');
		}
		var id = this.get('id');
		var channelId = id.replace('nav_', '');
		showSub(channelId, event);
	},
	mouseleave: function(event) {
		if (isIe) {
			var parent = this.getParent('li');
			parent.removeClass('top-nav-curr');
		}
		var id = this.get('id');
		var channelId = id.replace('nav_', '');
		if (hideTT != null) clearTimeout(hideTT);
		hideTT = setTimeout(function() {
			hideSub(channelId, event);
		}, 600);
	}
})



var _body = document.getElement('div.body'),
	bodyHeight = _body.getHeight().toInt(),
	winHeight = $(window).getHeight().toInt();


if (bodyHeight + (124 + 56) < winHeight)
	document.getElement('div.body-in').setStyle('height', winHeight - (124 + 56));
else if (isIe6) {
	//document.getElement('div.body-in').setStyle('height', $(window).getHeight().toInt() - 175);
}

var pagerLinks = document.getElements('.pager .p-link');
if (pagerLinks.length > 0) {
	pagerLinks[1].setStyle('border', '0');
}

$$('div.side-shadow').setStyle('height', _body.getHeight() * 0.85);

var elSide = document.getElement('div.side');

function loadBrandIcon() {
	var mmIconLength = 8, pick = $random(1, mmIconLength), strPick = String(pick).pad(2, '0', 'left');
	var mmIconBaseUrl = '/images/skin01/brand_icon/mm' + strPick + '.gif';

	var id = 'brand_icon_' + $time();

	new Asset.image(mmIconBaseUrl, {
		onload: function() {
			var width = this.width, height = this.height;
   			var box = new Element('div', { 'id': id });
   			box.setStyles({
   				'width': width,
	  			'height': height,
				'position': 'absolute',
				'top': document.getElement('div.body-in').getHeight() - height - 5,
				'left': width > 250 ? (width / 7) : (width / 4),
				'z-index': 2
   			});
   			this.inject(box);
   			box.inject(document.getElement('div.body-in'));
		}
	});
}

loadBrandIcon();

//new Asset.image()

}) ();
