//
//  Kao Chemical Shared
//
//  History: 2007.12.20 Created


// Initialization
$.shared = {
	init: function() {
		for (module in $.shared) {
			if ($.shared[module].init)
				$.shared[module].init();
		}

		$('a[href]').addFileSize();
		$('ul.menuListA01').each(function(){
			$(this).addClassFL();
		});
		$('div.productsMenuA01 > ul').each(function(){
			$(this).addClassOE();
		});
		$('div.productsMenuA02 > ul').each(function(){
			$(this).addClassFL();
		});
		$('ul.catalogListA01').each(function(){
			$(this).addClassOE();
		});

		$("body").ajaxStop(function(){
			$.shared.msds.deactive();
		});
	}
};

$(document).ready($.shared.init);


// Interwoven original "height" and "margin-top" disable
$.shared.iwInitialize = {
	init: function() {
		$("div.iw_component").css("height", "auto").css("margin-top", 0);
		$("body > div > div:not([id])").css("margin-top", 0);
		$("div#groupCi").parent("div").css("width", "auto").css("display", "block").css("float", "none");
		$("div#groupCi").parent("div").parent("div").css("display", "block").css("float", "none");
		$("div#groupCi").parent("div").parent("div").parent("div").css("display", "block").css("float", "none");
		$("div#groupCi").parent("div").parent("div").parent("div").parent("div").css("width", "auto");
	}
};

// Add "first-child" class to all elements in #content (only IE6)

$.shared.firstChild = {
	init: function() {
		var ua = window.navigator;
		if (ua.appName == "Microsoft Internet Explorer" && ua.appVersion.indexOf("MSIE 7.0") == -1) {/**/
			$("#content *:first-child").addClass("first-child");
			$("td *:first-child").addClass("first-child");
			$("div.localNavA01 *:first-child").addClass("first-child");
			$("div.localNavB01 *:first-child").addClass("first-child");
			$("div.relatedBlockA01 *:first-child").addClass("first-child");
			$("div.relatedInfoBlockA01 *:first-child").addClass("first-child");
		}
	}
};

// Add "firstline" class to productsMenuA01 and productsMenuA02

$.shared.firstLine = {
	init: function() {
		$("div.productsMenuA01 li:lt(2)").addClass("firstline");
		$("div.productsMenuA02 li:lt(3)").addClass("firstline");
	}
};


// Replace "abbr" with "acronym" (only IE6)

$.shared.changeAbbr = {
	init: function() {
		var ua = window.navigator;
		if (ua.appName == "Microsoft Internet Explorer" && ua.appVersion.indexOf("MSIE 7.0") == -1) {/**/
			var abbrs = document.getElementsByTagName('abbr');
			for (var i = 0; i < abbrs.length; i++) {
				var oldAbbr = abbrs.item(i);
				var newAbbr = document.createElement('abbr');
				newAbbr.title = oldAbbr.title;
				oldAbbr.parentNode.insertBefore(newAbbr, oldAbbr);
				while (oldAbbr.nextSibling.nodeName != '/ABBR') {
					newAbbr.appendChild(oldAbbr.nextSibling);
				}
				oldAbbr.parentNode.removeChild(oldAbbr.nextSibling);
				oldAbbr.parentNode.removeChild(oldAbbr);
			}
		}
	}
};


// Button Hover

$.shared.hover = {

	init: function() {
		$('.hover')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.bind('focus', this.enter)
			.bind('blur', this.exit)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	enter: function() {
		if (!this.src.match(/^(.+)_o(\.[a-z]+)$/) && !this.src.match(/^(.+)_a(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		}
	},

	exit: function() {
		if (this.src.match(/^(.+)_o(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
		} else {
			this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1$2");
		}
	}
};

// Button Active

$.shared.active = {

	init: function() {
		$('#searchSubmit')
			.bind('mousedown', this.down)
			.bind('mouseup', this.up)
			.bind('keydown', this.down)
			.bind('keyup', this.up)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_a$2");
	},

	down: function() {
		if (!this.src.match(/^(.+)_a(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1_a$2");
		}
	},

	up: function() {
		this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1_o$2");
	}
};


// Menu List Add Class "first" and "last"

(function($) {
	jQuery.fn.addClassFL = function() {
		return this.children("li").each(function(i){
			if (i % 3 == 0){
				$(this).addClass("first");
			}
			if (i % 3 == 2){
				$(this).addClass("last");
			}
		});
	};
})(jQuery);

// Menu List Add Class "Odd" and "Even"

(function($) {
	jQuery.fn.addClassOE = function() {
		return this.children("li").each(function(i){
			if (i % 2 == 0){
				$(this).addClass("odd");
			}
			if (i % 2 == 1){
				$(this).addClass("even");
			}
		});
	};
})(jQuery);

// PDF Size

(function($) {
	jQuery.fn.addFileSize = function() {
		return this.each(function(){

			var elem = $(this);
			if(!elem.attr('href').match(/\.pdf/)) return;

			$getSize = $.ajax({
				url: elem.attr('href'),
				complete: function(data, status){
					filesize = data.getResponseHeader("Content-Length");
					filesize = Math.round(filesize / 1000);
					elem.after(" <span class='pdfsize'>(PDF:"+filesize+"KB)</span>");
				}
			});
		});
	};
})(jQuery);

// MSDS Download Button

$.shared.msds = {
	init: function() {
		// Input company name button
		$('#companyNameSubmit')
			.bind('click', this.active)
			.bind('keypress', this.active);
		$('input#companyName')
			.bind('keypress', this.checkactive);

	},

	deactive: function() {
		// Change class
		$('.msdsBlockA01').eq(1)
			.removeClass('msdsBlockA01')
			.addClass('msdsBlockA02');

		// Get PDF file path
		i = 0;
		pdfpath = new Array;
		$('div.msdsBlockA02 li.pdf a').each(function(){
			pdfpath[i] = $(this).attr("href");
			i++;
		});

		// Disable PDF link
		i = 0;
		inner = new Array;
		$('div.msdsBlockA02 li.pdf').each(function(){
			a_inner = $(this).children().html();
			inner[i] = $(this).html();
			size_start = inner[i].indexOf("(");
			size = inner[i].substr(size_start);
			$(this).html("<span class='linktitle'>"+a_inner+"</span>"+" <span class='pdfsize'>"+size+"</span>");
			$(this).children("span").children("img").attr("src", "/imgs/icon_pdf_01_disable.gif");
			i++;
		});
	},

	checkactive: function(e) {
		key = (e.keyCode != 0) ? e.keyCode : e.charCode;
		if (key == 13) {
			$.shared.msds.active();
			return false;
		}
	},

	active: function() {
		if ($('input#companyName').attr('value')) {
			$('div.msdsBlockA02')
				.removeClass('msdsBlockA02')
				.addClass('msdsBlockA01');
			i = 0;
			$('div.msdsBlockA01 li.pdf').each(function(){
				$(this).html(inner[i]);
				i++;
			});
			return false;
		} else {
			alert("社名を入力してください");
			return false;
		}
	}
}
