var RMT = {
	replaceAnchor: function() {
		var elements = ['a', 'area'];
		for (var i in elements) {
			$(elements[i]).each(function(j) {
				if ($(this).attr('href') && $(this).hasClass('external')) {
					$(this).attr({ target: '_blank' });
				}
			});
		}
	},

	initListItem: function() {
		if(document.getElementById('globalNav')) {
			var item = document.getElementById('globalNav').getElementsByTagName('li');
	
			for(var i = 0, l = item.length; i < l; i++) {
				if(item[i].getElementsByTagName('ul')[0]) {
					item[i].onmouseover = function() {
						RMT.switchMenu(this.getElementsByTagName('ul')[0], 'block');
					};
					item[i].onmouseout = function() {
						RMT.switchMenu(this.getElementsByTagName('ul')[0], 'none');
					};
				}
			}
		}
	},

	switchMenu: function(obj, value) {
		if ($('#gNavIndicate').hasClass('true')) {
			obj.style.display = value;
		}
	},

	createSwMenu: function() {
		if ($('#globalNav')) {
			$('#globalNav').append(
				$('<li>')
					.attr({
						'id': 'gNavIndicate',
						'class': 'true'
					})
					.html('')
					.click(function() {
						var d;
						if ($(this).hasClass('true')) {
							$(this)
								.text('全メニュー非表示')
								.css({'background': 'none'});
							d = 'block';
						}
						else {
							$(this)
								.text('全メニュー表示')
								.css({'background': 'none'});
							d = 'none';
						}
						$(this).toggleClass('true');
						$(this).toggleClass('false');

						$('#globalNav > li').each(function(j) {
							if ($('ul', this)) {
								$('ul', this).css({display: d});
							}
						});
					})
			);
		}
	},

	createRollOver: function(e) {
		var images = document.getElementsByTagName(e);

		for(var i = 0, l = images.length; i < l; i++) {
			(function(image, flag, result) {
				if(!image.src.match(/site_fontsize/)) {
					if(flag) {
						var onover = function() {
							image.src = result[1] + '_on' + result[2];
						}
						var onout = function() {
							image.src = result[1] + '_off' + result[2];
						}
	
						try {
							image.addEventListener('mouseover', onover, false);
							image.addEventListener('mouseout', onout, false);
						}
						catch(e) {
							image.attachEvent('onmouseover', onover);
							image.attachEvent('onmouseout', onout);
						}
	
						var unload = function() {
							try {
								image.removeEventListener('mouseover', onover, false);
								image.removeEventListener('mouseout', onout, false);
							}
							catch(e) {
								image.detachEvent('onmouseover', onover);
								image.detachEvent('onmouseout', onout);
							}
							try {
								window.removeEventListener('unload', unload, false);
							}
							catch(e) {
								window.detachEvent('onunload', unload);
							}
						};
						try {
							window.addEventListener('unload', unload, false);
						} catch(e) {
							window.attachEvent('onunload', unload);
						}
					}
				}
			})(images[i], /^(.+)_off(\..+)$/.test(images[i].src), images[i].src.match(/^(.+)_off(\..+)$/));
		}
	},

	changeHeight: function(selector, n) {
		$(function() {
			if (typeof n != 'number') {
				$(selector).flatHeights();
			}
			else {
				var sets = [], temp = [];
				$(selector).each(function(i) {
					temp.push(this);
					if (i % n + 1 == n) {
						sets.push(temp);
						temp = [];
					}
				});
				if (temp.length) sets.push(temp);

				$.each(sets, function() {
					$(this).flatHeights();
				});
			}
		});
	},

	plugin: false,

	isFlashPlugin: function() {
		var requestedVer = new deconcept.PlayerVersion([8,0,0]);
		var installedVer = deconcept.SWFObjectUtil.getPlayerVersion();
		if(installedVer.versionIsValid(requestedVer)) {
			this.plugin = true;
		}
		else {
			this.plugin = false;
		}

		return this.plugin;
	},

	childWindow: null,

	openPopup: function(name, target, w) {
		if (name == 'yuga') {
			if(target == 'self') {
				window.open('/products/bathroom/br_tv/yuga/yuga_point', target);
				w.close();
			}
			else {
				this.childWindow = window.open('/products/bathroom/br_tv/yuga/yuga_flash', target, 'width=800,height=656,titlebar=no,menubar=no,location=no,toolbar=no,scrollbars=no,status=no,directories=no');
				this.childWindow.focus();
			}
		}
		else if (name == 'eco') {
			if(target == 'self') {
				window.open('/products/waterheater/about_eco/', target);
				w.close();
			}
			else {
				this.childWindow = window.open('/products/waterheater/about_eco/rvd-e/index.html', target, 'width=760,height=570,titlebar=no,menubar=no,location=no,toolbar=no,scrollbars=no,status=no,directories=no');
				this.childWindow.focus();
			}
		}
		else {
			return false;
		}
	},

	initLightbox: function() {
		$('a.lightbox').lightBox({fixedNavigation:false});
		$('span.lightbox a').lightBox({fixedNavigation:false});
	},

	hoverGlass: function() {
		var onPass = "/shared/img/icon/loupe_on.gif";
		var offPass = "/shared/img/icon/loupe_off.gif";
		var onLPass = "/shared/img/icon/loupe_l_on.gif";
		var offLPass = "/shared/img/icon/loupe_l_off.gif";

		$("a.loupe").append('<img src="' + offPass +'" alt="" class="icon" />');
		$("a.loupe").hover(function(){
			$("img.icon", this).attr("src",onPass);
		},function(){
			$("img.icon", this).attr("src",offPass);
		});

		$("a.loupeL").append('<img src="' + offLPass +'" alt="" class="icon" />');
		$("a.loupeL").hover(function(){
			$("img.icon", this).attr("src",onLPass);
		},function(){
			$("img.icon", this).attr("src",offLPass);
		});
	},

	addExternal: function() {
		$("a[class='external icon']:not(a[href*='.pdf'])").append('<img src="/shared/img/icon/external.gif" class="exicon" alt="" />');
		$("a[class='external icon'][href*='.pdf']").append('<img src="/shared/img/icon/pdf.gif" class="pdficon" alt="PDFファイル" />');
	},

	start: function() {
		this.replaceAnchor();
		this.createSwMenu();
		this.initListItem();
		this.initLightbox();
		this.createRollOver('img');
		this.createRollOver('input');
		this.hoverGlass();
		this.addExternal();
	}
};

$(function(){
	RMT.start();
});
