(function($){
	$.fn.shuffle = function() {
		return this.each(function(){
			var items = $(this).children();
			return (items.length)
				? $(this).html($.shuffle(items))
				: this;
		});
	}

	$.shuffle = function(arr) {
		for(
			var j, x, i = arr.length; i;
			j = parseInt(Math.random() * i),
			x = arr[--i], arr[i] = arr[j], arr[j] = x
		);
		return arr;
	}

	$(function(){
		$('a[href*=#]').click(function() {
			if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
				var $target = $(this.hash);
				$target = $target.length && $target
				|| $('[name=' + this.hash.slice(1) +']');
				if ($target.length) {
					var targetOffset = $target.offset().top;
					$('html,body')
					.animate({scrollTop: targetOffset}, 'swing');
					return false;
				}
			}
		});
		
		$.get("/common/xml/news.xml", function(data){
			var xml = $(data);
			var cruise = xml.find("cruise");
			if(cruise.size() != 0){
				$.shuffle(cruise);
				var container = $(".indexHead .leftBlock").empty().css({position:"relative"});
				cruise.each(function(){
					$('<a href="' + $(this).find("link").text() + '"><img src="' + $(this).find("img").text() + '" alt="' + $(this).find("title").text() + '" width="490" height="245" /></a>')
						.appendTo(container)
						.mouseover(function(){
							var src = $(this).find("img").attr("src");
							var ftype = src.substring(src.lastIndexOf('.'), src.length);
							$(this).find("img").attr("src", src.replace(ftype, '_on'+ftype));
						})
						.mouseout(function(){
							var src = $(this).find("img").attr("src");
							var ftype = src.substring(src.lastIndexOf('.'), src.length);
							$(this).find("img").attr("src", src.replace('_on'+ftype, ftype));
						})
						.css({position:"absolute", top:0, left:0})
						.addClass("SHOW_IMG")
							.not(":first-child")
							.removeClass("SHOW_IMG")
							.hide()
							.end();
				});
				
				startSlideshow(container);
			}
		});

		$.get("/common/xml/new_news.xml", function(data){
			var xml = $(data);
			var height = 0, maxheight = 0;
			$(".headLine dt").append('<br /><a href="http://www.nipponmaru.jp/news/"><img src="/common/img/btn_headline.gif" alt="一覧で見る" width="97" height="20" onmouseover="this.src=\'/common/img/btn_headline_on.gif\'" onmouseout="this.src=\'/common/img/btn_headline.gif\'" /></a>');
			$(".headLine dd").empty().css({position:"relative", display:"block"});
			if(xml.find("item").size() != 0){
				$(data).find("item").each(function(){
					height = $('<a href="' + $(this).find("link").text() + '">' + '<img src="/news/assets/img/icon_new.gif" alt="NEW" width="29" height="11" /> ' + $(this).find("date").text() + ' ' + $(this).find("title").text() + '</a>')
						.appendTo(".headLine dd")
						.css({position:"relative", display:"block"})
						.addClass("SHOW_NEWS")
						.not(":first-child")
							.removeClass("SHOW_NEWS")
							.hide()
							.end()
						.height();
					maxheight = height < maxheight ? maxheight : height;
				});
				var css = {minHeight: maxheight + "px"};
				if(navigator.userAgent.indexOf("MSIE 6") != -1){
					css.height = maxheight + "px";
				}
				$(".headLine dd a").css(css);
				startSlidenews($(".headLine dd"));
			}
		});
		
		function startSlideshow(container){
			setTimeout(function(){slideshow(container)}, 5000);
		}
		function slideshow(container){
			var next = container.find(".SHOW_IMG").removeClass("SHOW_IMG").fadeOut(1000).next();
			if(next.size() == 0) next = container.find("a").eq(0);
			next.addClass("SHOW_IMG").fadeIn(1000, function(){startSlideshow(container);});
		}
		function startSlidenews(container){
			setTimeout(function(){slidenews(container)}, 10000);
		}
		function slidenews(container){

			var next = container.find(".SHOW_NEWS").next();
			if(next.size() == 0) next = container.find("a").eq(0);
			container.find(".SHOW_NEWS").removeClass("SHOW_NEWS").fadeOut(1000, function(){
				next.addClass("SHOW_NEWS").fadeIn(1000, function(){startSlidenews(container);});
			});
		}
	});
})(jQuery);

function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'over') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_on'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function initPage(){
	initRollovers();
}

;(function($){
	$(document).ready(function(){
// popup
// class="popup400x600" -> window.open(this.href,"popup","width=400,height=600,...)
$("a[class^='popup']").click(function(){
	if($.browser.safari ){
		window.open(this.href,"_blank");
		return false;
	}
	var className = $(this).attr("class").match(/^popup([0-9]{1,})x([0-9]{1,})/) ;	
	var width = RegExp.$1;
	var height = RegExp.$2;
	var state = "";
	var notHasSize = "yes"
	if(width!=null && height !=null){
		state += "width="+width+",height="+height+",";
		notHasSize = "no"
	}
	state += "location="+notHasSize+",toolbar="+notHasSize+",directories="+notHasSize+",";
	state += "status=yes,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes";
	window.name = document.domain + "root";
	window.open(this.href,"popup"+(new Date()).getTime().toString(),state);
	return false;
});
// open in popup parent window.
// add class="openParentWin" on a-tag in a popup window.
$("a.openParentWin").click(function(){
	window.open(this.href,document.domain + "root");
	return false;
});

// target _blank auto add
var domains = [document.domain,"groupsitedomains"];
var domain_selector = ""
var left_str= ":not([href^=http://";
var left_str_https= ":not([href^=https://";
var right_str = "])";
domain_selector = left_str+domains.join(right_str+left_str)+right_str;
domain_selector+= left_str_https+domains.join(right_str+left_str_https)+right_str;
$("a[href^=http]:not([class^=popup])"+domain_selector+", area[href^=http]:not([class^=popup])"+domain_selector)
.addClass("external");

//if has class .extenal -> open _blank window
function windowOpen(){
	window.open(this.href,"_blank");
	return false;
}

		$("a.external").bind("click",windowOpen);
		
	});
})(jQuery);

if (window.addEventListener) window.addEventListener("load",initPage,false);
if (window.attachEvent) window.attachEvent("onload",initPage);
