/*
* 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();
	$(".sideBarBlockA01 dl").each(function(){
		$(this).find("dt:eq(0)").css("border-top", "1px solid #9c9c9c");
	});
	$(".sideBarBlockA02 ul").each(function(){
		$(this).find("li:eq(0)").css("border-top", "none");
	});
	$.ajax({
		type: "GET",
		url: "/pressroom/xml/pressroom.xml",
		dataType: "xml",
		success: function(xml) {
			$("#newsRelease").empty();
			var nTitle;
			var nUrl;
			var nDate;
			var entryId;
				for(i=0; i<5; i++){
				entryId = "entry:eq(" + i + ")";
				nTitle = $(xml).find(entryId).find("title").text();
				nUrl = $(xml).find(entryId).find("link").text();
				nDate = $(xml).find(entryId).find("modified").text();
				$("#newsRelease").append('<dt><a href="' + nUrl + '">' + nDate + '</a></dt>');
				$("#newsRelease").append('<dd><a href="' + nUrl + '">' + nTitle + '</a></dd>');
			}
		} 
	});
	if(typeof(cmFlv) != "undefined"){
		var flashvars = {};
		flashvars.ratio = ratio;
		flashvars.movurl = "/top-elements/flv/" + cmFlv;
		var params = {};
		params.allowScriptAccess = "sameDomain";
		params.allowFullScreen = false;
		params.scale = "noscale";
		params.quality = "low";
		params.salign = "lt";
		params.wmode = "window";
		params.menu = false;
		params.loop = false;
		params.bgcolor = "#d9d9d9";
		var attributes = {};
		swfobject.embedSWF("/top-elements/swf/moviePlayer.swf", "flashAlt", "257", "149", "9.0.0", "/top-elements/swf/expressInstall.swf", flashvars, params, attributes);
		$(".cmBlock .cm").after("<p class='cmTitle'>" + cmTitle + "</p>");
	}
});


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_image_01_s.gif" width="11" height="11" alt="1" />');
		} else {
			$("#mainImageIcon").append('<img src="img/icon_image_01.gif" width="11" height="11" 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(/top-elements/htc/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(/top-elements/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(/top-elements/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(/top-elements/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");
			}
		}
	}
};


// 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");
		}
	}
};


// 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;
					});
				}
			}
		});
	}
};


// 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");
	}
};


// 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);


// Subpage Link
$.lm.subPage = {
	init: function() {
		$("a.subPage")
			.live("click", this.openWindow)
			.live("keypress", this.openWindow);
	},
	
	openWindow: function() {
		window.open($(this).attr("href"), "subPage", "width=1024, menubar=no, toolbar=no, scrollbars=yes");
		return false;
	}
};

