/*
* lm 1.2.2
* Automatic functions for webpages
* Require:
*  jquery.js
*  frame.css
*  content_font.css
*
* Copyright: (c) 2008 ludicmind.net
**************************************/

// Initialization
$.lm = {
	init: function() {
		for (func in $.lm) {
			if ($.lm[func].init)
				$.lm[func].init();
		}
	}
};

$(document).ready(function(){
	$.lm.init();
	$("div.relatedLink ul.linkListA > li:nth-child(odd)").addClass("odd");
	$("div.relatedLink div.linkBlockA > div.item:nth-child(odd)").addClass("odd");
	$("div.fBlockA01 p:eq(0)").css("margin-top", 0);
	$("div.fBlockA02 p:eq(0)").css("margin-top", 0);
	$("div.fBlockA03 p:eq(0)").css("margin-top", 0);
	$("div.fBlockA04 p:eq(0)").css("margin-top", 0);
	$("div.fBlockA05 p:eq(0)").css("margin-top", 0);
	$("div.fBlockA06 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB01 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB02 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB03 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB04 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB05 p:eq(0)").css("margin-top", 0);
	$("div.fBlockB06 p:eq(0)").css("margin-top", 0);
	$("div.itemDetail:eq(0)").addClass("first");
	$("dl.newsDLA01 dt:eq(0)").addClass("first");
	$("dl.newsDLA01 dd:eq(0)").addClass("first");
	$("dl.newsDLA02 dt:eq(0)").addClass("first");
	$("dl.newsDLA02 dd:eq(0)").addClass("first");
	$("div#mensBlock").addDivClassOE();
	$("div#womensBlock").addDivClassOE();
	$("div#kidsBlock").addDivClassOE();
	$("div#specialitiesBlock").addDivClassOE();
	$("div#othersBlock").addDivClassOE();
	$("div#beamsartsBlock").addDivClassOE();
	$("div#servicesBlock").addDivClassOE();
	$("div#cardBlock").addDivClassOE();
	$("div#clubBlock").addDivClassOE();
	$("div#corporateBlock").addDivClassOE();
	$("div.shopBlockC01 div.item:nth-child(odd)").addClass("odd");
	$("div.pBlockC01 > div:lt(4)").css("margin-top", 0);
	$("div.pBlockC02 > div:lt(4)").css("margin-top", 0);
	$("div.pBlockC03 > div:lt(4)").css("margin-top", 0);
	$("div.thumbBlockA01 div:nth-child(even)").addClass("even");
	$("div.thumbBlockB01").addDivClassFL(3);
	$("div.thumbBlockB01 div:lt(3)").addClass("first-line");
	$("ul.linkListA li:lt(2)").css("background-image", "none");
	$("div.linkBlockB > div.item:nth-child(odd)").addClass("odd");
	$("div.linkBlockC div.item:nth-child(even)").addClass("even");
	$("div.linkBlockC div.item:nth-child(odd)").addClass("odd");
	$("div.linkBlockC div.item:eq(1)").css("margin-top", 0);
	$("div.featuredBlockB01").addDivClassFL(3);
	$("div.featuredBlockB01 div.item:lt(3)").css("margin-top", 0);
	$("div.galleryBlockA01 div.gallery:nth-child(even)").addClass("even");
	$("div.galleryBlockA01 div.gallery:lt(2)").addClass("first-line");
	$("div.galleryBlockB01").addDivClassFL(3);
	$("div.galleryBlockB01 div.gallery:lt(3)").addClass("first-line");
	$("div.itemDetailBlockB01 div.itemDetail:nth-child(even)").addClass("even");
	$("div.itemDetailBlockB01 div.itemDetail:lt(2)").addClass("first");
	$("div.andmoreBlockA01 div.itemB div:nth-child(odd)").addClass("odd");
	$("div.andmoreBlockA01 div.itemB div:nth-child(even)").addClass("even");
	$("input#searchWord").bind("click", function(){this.select();});
});

// Global Navigation Stay
$.lm.gnav = {
	rootUrl: "",
	
	init: function() {
		if (document.URL.indexOf("http://pj-beams.ludicmind.net/") != -1) {
			this.rootUrl = "http://pj-beams.ludicmind.net/";
		} else if (document.URL.indexOf("http://pre1.beams.co.jp/") != -1) {
			this.rootUrl = "http://pre1.beams.co.jp/";
		} else {
			this.rootUrl = "http://www.beams.co.jp/";
		}
		var identifier = document.URL.replace(this.rootUrl + "en/", "_");
		var slashIndex = identifier.indexOf("/");
		if(identifier != "_"){
			identifier = identifier.slice(1, slashIndex);
		}
		$("#gNav01 a").each(function(){
			if($(this).attr("href").indexOf(identifier) != -1) {
				var staySrc = $(this).children("img").attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_s$2");
				$(this).children("img")
					.removeClass("hover")
					.attr("src", staySrc);
			}
		});
	}
};


// Related Link Block
$.lm.relatedBlock = {
	init: function() {
		$(".relatedLinkBlockA01").eq(0).addClass("first");
		$(".relatedLinkBlockA01 .relatedLink:first-child").addClass("first");
		$(".relatedLinkBlockA01 .relatedLinkFooter:last-child").addClass("last");
	}
};

// PNG Image
$.lm.pngfilter = {
	init: function() {
		$("img:not(.phover)").css("behavior", "url(/shared/htc/iepngfix.htc)");
	}
};

// Image hover
$.lm.hover = {
	init: function() {
		$('.hover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('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");
		}
	}
};


// PNG Image hover
$.lm.phover = {
	init: function() {
		$('.phover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit)
			.each(this.preload);
	},

	preload: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			this.initial = new Image;
			this.initial.src = this.src;
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
			$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
		} else {
			this.preloaded = new Image;
			this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		}
	},

	enter: function() {
		if($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.preloaded.src;
				$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
			}
		} else {
			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($.browser.msie && $.browser.version <= 6.0){
			if (this.src.match(/(blank)/)){
				this.src = this.initial.src;
				$(this).css("behavior", "url(/shared/htc/iepngfix.htc)");
			}
		} else {
			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");
			}
		}
	}
};

// Opacity Change hover
$.lm.ohover = {
	init: function() {
		$('.ohover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit);
	},

	enter: function() {
		$(this).animate({opacity: 0.6}, 200);
	},

	exit: function() {
		$(this).animate({opacity: 1}, 200);
	}
};

// Link Icon
$.lm.linkIcon = {
	init: function() {
		$("a[href]:has(img)").addClass("noIcon");
		$("a[href]").each(function(){
			if(!$(this).hasClass("noIcon")) {
				if(this.href.indexOf("mailto:") != -1) {
					$(this).css("background", "url(/shared/img/icon_mail_01.gif) no-repeat left 0.4em");
				}
				if(this.href.indexOf(".pdf") != -1) {
					$(this).append(" <img class='pdfIcon' src='/shared/img/icon_pdf_01.gif' alt='PDF' width='11' height='12' />");
				} else if (this.target) {
					$(this).append(" <img class='windowIcon' src='/shared/img/icon_window_01.gif' alt='別ウィンドウで開きます' width='12' height='11' />");
				}
			}
		});
	}
};

// Add "first-child" class to all elements in #content
$.lm.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");
		}
	}
};


// Add "nolink" class to Heading without a[href]
$.lm.noLink = {
	init: function() {
		$("a:not([href])").parent("h2").addClass("nolink");
		$("a:not([href])").parent("h3").addClass("nolink");
		$("a:not([href])").parent("h4").addClass("nolink");
	}
};


// Mouse hover on input[type='image']
$.lm.inputHover = {

	init: function() {
		$('input[type="image"]')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	enter: function() {
		this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
	},

	exit: function() {
		this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1$2");
	}
};


// Navigation Submenu
$.lm.submenu = {
	init: function() {
		var toggle = function(direction, display) {
			return function() {
				var self = this;
				var ul = $(this).children("ul").eq(0);
				if( ul.css("display") == display && !self["block" + direction] ) {
					self["block" + direction] = true;
					ul["slide" + direction]("fast", function() {
						self["block" + direction] = false;
					});
				}
			};
		}
		$("li.hasSubmenu").hover(toggle("Down", "none"), toggle("Up", "block"));
		$("li.hasSubmenu ul").hide();
	}
};


// Inner Link
$.lm.innerLink = {
	init: function() {
		$("a[href]").each(function(){
			if($(this).attr("href").indexOf("#") == 0){
				if($(this).attr("href").indexOf("#top") == 0){
					$(this).click(function(){
						$.scrollTo($(this).attr("href"), {duration: 700, easing: "easeOutExpo"});
						return false;
					});
				} else if($(this).attr("href").indexOf("#h2") == 0) {
					$(this).click(function(){
						$.scrollTo($(this).attr("href"), {duration: 700, easing: "easeOutExpo", offset: {top: -20}});
						return false;
					});
				}
			}
		});
	}
};

// Replace "abbr" with "acronym" (only IE6)
$.lm.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);
			}
		}
	}
};


// Menu List Add Class "first" and "last" - use to ul
(function($) {
	jQuery.fn.addClassFL = function(colNum) {
		return this.children("li").each(function(i){
			if (i % colNum == 0){
				$(this).addClass("first");
			}
			if (i % colNum == colNum - 1){
				$(this).addClass("last");
			}
		});
	};
})(jQuery);


// Menu List Add Class "first" and "last" - use to div
(function($) {
	jQuery.fn.addDivClassFL = function(colNum) {
		return this.children("div").each(function(i){
			if (i % colNum == 0){
				$(this).addClass("first");
			}
			if (i % colNum == colNum - 1){
				$(this).addClass("last");
			}
		});
	};
})(jQuery);


// Menu List Add Class "Odd" and "Even" - use to ul
(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);

// Menu List Add Class "Odd" and "Even" - use to div
(function($) {
	jQuery.fn.addDivClassOE = function() {
		return this.children("div").each(function(i){
			if (i % 2 == 0){
				$(this).addClass("odd");
			}
			if (i % 2 == 1){
				$(this).addClass("even");
			}
		});
	};
})(jQuery);


