/*
* lm 1.6
*
**************************************/

// Initialization
$.lm = {
	init: function() {
		for (func in $.lm) {
			if ($.lm[func].init)
				$.lm[func].init();
		}
	}
};

$(document).ready(function(){
	$.lm.init();
	var mainImage = new MainImage();
});


function MainImage(){
	var imgNum = $("#mainImage div.image").length;
	var current = 0;
	var timer = setInterval(rotateImg, 6000);
	
	for(i=0; i<imgNum; i++){
		if(i==0){
			$("#mainImageIcon").append('<img src="img/icon_mainimage_01_s.gif" width="7" height="7" alt="1" />');
		} else {
			$("#mainImageIcon").append('<img src="img/icon_mainimage_01.gif" width="7" height="7" alt="' + (i + 1) + '" />');
		}
	}
	
	$("#mainImageIcon img").click(function(){
		$("#mainImageIcon img").each(function(){
			$(this).attr("src", $(this).attr("src").replace(/^(.+)_s(\.[a-z]+)$/, "$1$2"));
		});
		$(this).attr("src", $(this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_s$2"));
		var id = $(this).index("#mainImageIcon img");
		var old = current;
		current = id;
		$("#mainImage div.image").css("z-index", 1);
		$("#mainImage div.image:eq(" + current + ")").css("z-index", 2).fadeIn("normal", function(){
			$("#mainImage div.image:eq(" + old + ")").hide();
		});
		clearInterval(timer);
		timer = setInterval(rotateImg, 6000);
	});
	
	function rotateImg(){
		var old = current;
		current++;
		if(current >= imgNum) current = 0;
		
		var id = "#mainImage div.image:eq(" + current + ")";
		var oldId = "#mainImage div.image:eq(" + old + ")";
		$("#mainImage div.image").css("z-index", 1);
		$(id).css("z-index", 2).fadeIn("normal", function(){
			$(oldId).hide();
		});
		
		$("#mainImageIcon img").each(function(){
			$(this).attr("src", $(this).attr("src").replace(/^(.+)_s(\.[a-z]+)$/, "$1$2"));
		});
		var buttonId = "#mainImageIcon img:eq(" + current + ")";
		$(buttonId).attr("src", $(buttonId).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_s$2"));
	}
}

// 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");
		}
	}
};


// Image hover and active
$.lm.hover2 = {
	init: function() {
		$('.hover2')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit)
			.live('mousedown', this.down)
			.live('mouseup', this.up)
			.each(this.preload);
	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_o$2");
		this.preloaded2 = new Image;
		this.preloaded2.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_a$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");
		}
	},

	down: function() {
		if (this.src.match(/^(.+)_o(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_o(\.[a-z]+)$/, "$1_a$2");
		}
	},

	up: function() {
		if (this.src.match(/^(.+)_a(\.[a-z]+)$/)){
			this.src = this.src.replace(/^(.+)_a(\.[a-z]+)$/, "$1_o$2");
		}
	}
};

// Filter hover
$.lm.fHover = {
	init: function() {
		$('.fHover').hover(function(){
			$(this)
				.css("display", "block")
				.css("position", "relative")
				.append("<div id='hoverFilter' style='position: absolute; margin: 0; top: 0; left: 0; width: " + $(this).find("img").attr("width") + "px; height: " + $(this).find("img").attr("height") + "px; background-color: #ffffff; opacity: 0.45; filter: alpha(opacity=45)'></div>");
			$("div#hoverFilter").delay(100).fadeOut("normal", function(){
				$("div#hoverFilter").remove();
			});
		},
		function(){
			$("div#hoverFilter").remove();
		});
	}
};


// 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.7}, 200);
	},

	exit: function() {
		$(this).animate({opacity: 1}, 200);
	}
};

// Notice hover
$.lm.nHover = {
	init: function() {
		$('img.nHover')
			.live('mouseover', this.enter)
			.live('mouseout', this.exit)
			.live('focus', this.enter)
			.live('blur', this.exit);
		$('a.nHover')
			.live('mouseover', this.aenter)
			.live('mouseout', this.aexit)
			.live('focus', this.aenter)
			.live('blur', this.aexit);
	},

	enter: function() {
		$(this).animate({opacity: 0.5}, 0).animate({opacity: 1}, 500);
	},

	exit: function() {
	},

	aenter: function() {
		$(this).find("img").animate({opacity: 0.5}, 0).animate({opacity: 1}, 500);
	},

	aexit: function() {
	}
};

// PNG Image
$.lm.pngfilter = {
	init: function() {
		$("img:not(.phover)").css("behavior", "url(js/iepngfix.htc)");
	}
};

// 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(js/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(js/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(js/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");
			}
		}
	}
};


// Add "first-child" class to all elements in #content
$.lm.firstChild = {
	init: function() {
		var ua = window.navigator;
		if (ua.appName == "Microsoft Internet Explorer") {
			$("#gNav *:first-child").addClass("first-child");
			$("#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");
	}
};


