/*-- functions --*/
function mainDisplay() {
	var imgListPath = 'http://'+location.hostname+'/common/xml/imgList.xml';
	//var imgListPath = 'http://'+location.hostname+'/test/common/xml/imgList.xml';
	var count = new Number();
	var imgArr = new Array();
	var imgKey = new Array();
	var waitTime = 5000; //msec;
	var step = new Number(0);

	keyShuffle = function() {
		var i = imgKey.length;

		while (--i) {
			var j = Math.floor(Math.random() * (i + 1));
			if (i == j) continue;
			var k = imgKey[i];
			imgKey[i] = imgKey[j];
			imgKey[j] = k;
		}
		return true;
	},

	next = function(){
		var k = imgKey[step];
		var tag = '<img src="'+imgArr[k].src+'" alt="'+imgArr[k].alt+'" />';
		$("#main-image").wait(waitTime).fadeOut("slow",function(){
			$("#main-image").html(tag).ready(function(){
				$("#main-image").fadeIn('slow',function(){
					step++;
					if(step==count){step=0;} //ポインタ末端でループ再実行
					next();
				});
			});
		});
	},

	randomDisplay = function() {
		if(count>1){
			if(keyShuffle() !== false){
				next();
			}
		}
	},

	getArraies = function(XML){
		$(XML).find('Img').each(function(){
			var arr = new Array(3);
			arr['id'] = $(this).find('ID').text();
			arr['src'] = $(this).find('Src').text();
			arr['alt'] = $(this).find('Alt').text();
			imgArr[count] = arr;
			imgKey.push(count);
			count++;
		});
		randomDisplay();
	}

	$.get(imgListPath,{},getArraies);

}
function smartRollover() {
	if(document.getElementsByTagName) {
		var images = document.getElementsByTagName("img");

		for(var i=0; i < images.length; i++) {
			if(images[i].getAttribute("src").match("_off."))
			{
				images[i].onmouseover = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
				}
				images[i].onmouseout = function() {
					this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
				}
			}
		}
	}
}

function globalNavi(){
	var thisDir = location.pathname;
	var rfReg = new RegExp('reforming','gi');
	if(thisDir.match(rfReg)){
		$(".sub-categories").css({display:'block'});
	}else{
		
		$(".sub-categories > li").click(function(){
			location.href = $(this).children('a').attr('href');
			return false;
		});
	
		$("#global-menu > li").dblclick(function(){
			location.href =  $(this).children('a').attr('href');
			return false;
		});
	
		$("#global-menu > li:has(ul)").toggle(
			function(){
				$(this).children('ul').wait(500).slideDown('fast');
			},
			function(){
				$(this).children('ul').slideUp('fast');
			}
		);
	}
}

function externalLink(){
	$("a[rel='external']").click(function(){
		window.open($(this).attr("href"));
		return false;
	});
}


$(function(){
	smartRollover();
	mainDisplay();
	globalNavi();
	externalLink();
});

/*-- plugins --*/

// Delay Plugin for jQuery
// - http://www.evanbot.com
// - copyright 2008 Evan Byrne
/*
 * Jonathan Howard
 * jQuery Pause
 * version 0.2
 * Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
 * Feel free to do whatever you'd like with this, just please give credit where
 * credit is do.
 * pause() will hold everything in the queue for a given number of milliseconds,
 * or 1000 milliseconds if none is given.
 */
// Wait Plugin for jQuery
// http://www.inet411.com
// based on the Delay and Pause Plugin
 (function($) {
    $.fn.wait = function(option, options) {
        milli = 1000; 
        if (option && (typeof option == 'function' || isNaN(option)) ) { 
            options = option;
        } else if (option) { 
            milli = option;
        }
        // set defaults
        var defaults = {
            msec: milli,
            onEnd: options
        },
        settings = $.extend({},defaults, options);

        if(typeof settings.onEnd == 'function') {
            this.each(function() {
                setTimeout(settings.onEnd, settings.msec);
            });
            return this;
        } else {
            return this.queue('fx',
            function() {
                var self = this;
                setTimeout(function() { $.dequeue(self); },settings.msec);
            });
        }

    }
})(jQuery);

