/* [www.realcom.co.jp]
 * Summary: common script
 * LastModified: 2010-04-08 19:44:51.
 * This script requires [jquery.js & jquery.cookie.js]
 */

/*----------------------------------------------------------------------------
 Global Object
----------------------------------------------------------------------------*/
var realcom;
if (!realcom) { realcom = {}; }

/*----------------------------------------------------------------------------
 check UserAgent
----------------------------------------------------------------------------*/
realcom.osClass = function() {
	if (navigator.userAgent.indexOf("Mac") != -1) {
		$("body").addClass("os_mac");
	} else if (navigator.userAgent.indexOf("Windows") != -1) {
		$("body").addClass("os_win");
	} else {
		return false;
	}
};

/*----------------------------------------------------------------------------
change font size
----------------------------------------------------------------------------*/
realcom.fontSize = {
	conf: {
		containerClassName: 'switchFontsize',
		cookieName: 'fontSize'
	},
	initClassName: '',
	setStyle: function(fValue) {
		var value = fValue;
		var $btn = $('dl.' + this.conf.containerClassName + ' a');
		$btn.removeClass('cr');
		if (fValue === 'L') {
			value = 'L';
			$btn[1].className = 'cr';
		} else {
			value = 'M';
			$btn[0].className = 'cr';
		}
		$.cookie(this.conf.cookieName, value, { expires: 7, path: "/", domain: location.hostname, secure: false });
		document.body.className = this.initClassName;
		$(document.body).addClass('fontSize' + value);
	},
	init: function() {
		var s = '<dl class="';
		s += this.conf.containerClassName;
		s += '">';
		s += '<dt>文字サイズの変更</dt>';
		s += '<dd class="m"><a href="#" onclick="realcom.fontSize.setStyle(\'M\');return false;">標準</a></dd>';
		s += '<dd class="l"><a href="#" onclick="realcom.fontSize.setStyle(\'L\');return false;">拡大</a></dd>';
		s += '</dl>';
		$('#header').append(s);
		this.initClassName = document.body.className;
		this.setStyle($.cookie(this.conf.cookieName));
	}
};

/*----------------------------------------------------------------------------
dropdown menu
----------------------------------------------------------------------------*/

realcom.dropDownNav = function (globalNavEle, dropDownNavEle) {
	var self = this;
	self.timer;

	$(globalNavEle).find('a').each(function() {
		// id付与
		var ele = self.setIdFromHref(this);
		// ドロップダウンコンテンツのレイアウトの調整
		$('#nav_' + ele.id).each(function() {
			var n = ( $(this).width() / 2 ) - ( $(ele).width() / 2 );
			n = Math.floor(n);
			$(this).css({
				marginLeft: -n + 'px' 
			});
		});
	}).hover(function() {
		self.show('#nav_' + this.id, dropDownNavEle);
	}, function() {
		self.hide('#nav_' + this.id);
	});
	$(dropDownNavEle).hover(function() {
		clearTimeout(self.timer);
	}, function() {
		self.hide(dropDownNavEle);
	});

	$(dropDownNavEle).hide();
};
realcom.dropDownNav.prototype = {
	show: function(ele, nav) {
//        $(nav).fadeOut('fast');
		$(nav).hide();
		$(ele).queue(function() {
			$(this).fadeIn().dequeue();
		});
		clearTimeout(this.timer);
	},
	hide: function(ele) {
		this.timer = setTimeout(function() {
//            $(ele).fadeOut('fast');
			$(ele).hide();
		}, 100);
	},
	setIdFromHref: function(ele) {
		var href = $(ele).attr('href');
		if (href) {
			var lastI1 = href.lastIndexOf('/');
			href = href.slice(0, lastI1);
			var lastI2 = href.lastIndexOf('/');
			ele.id = href.slice(lastI2 + 1, href.length);
		}
		return ele;
	}
};

/*----------------------------------------------------------------------------
 onload event function
----------------------------------------------------------------------------*/
if (jQuery) {
jQuery(document).ready(function() {
	// set favicon.ico to document
	document.createElement('abbr');
	var fcLink = document.createElement('link');
	fcLink.setAttribute('rel', 'shortcut icon');
	fcLink.setAttribute('href', '/favicon.ico');
	document.getElementsByTagName('head')[0].appendChild(fcLink);
	
	// initialize
	realcom.osClass();
	if ( document.getElementById('header') ) {
		realcom.fontSize.init();
	}
	new realcom.dropDownNav('dl.nav_global', 'div.nav_dropdown');
})
}


