$(document).ready(function(){

	menuArea = $('#slideMenu01');

	var group = $(menuArea).find('.group');
	var menu = $(menuArea).find('.menu');
	var contents = $(menuArea).find('.contents');

    $(menuArea)
    	.find('.active')
    	.show(0);	

	var n = $(group).length; // liの数を抽出

	var drawerHeight = $(menuArea).height(); 
	var menuHeight = $(menu).height();
	var contentsHeight; 
	
	var maxHeight = 0;
	var clicktimer = -1;

	for (i=0 ; i<n ; i++){
		$(group).eq(i).css({zIndex:i+2});
		$(contents).eq(i).show(0);
		contentsHeight = $(contents).eq(i).height();
		if (maxHeight < contentsHeight){
			maxHeight = contentsHeight;
		}
	}

	$(contents).css({height:maxHeight});
	drawerHeight = menuHeight*n + maxHeight;

	$(menuArea).height(drawerHeight).css({overflow:"hidden" , zIndex:n+1 , position:"relative"});
	
	changeMenuPosition();

	$(menu).mouseover(
		function (){
			var target = this;
			
			hide = $(menuArea).find('.active');
			show = $(target).parent();
				
			clicktimer = setTimeout(function() {
				$(show).addClass('choose');

				var active = $('.group').index($(hide).get(0));
				var select = $('.menu').index(target);
				
				$(contents).eq(select).show(0);
				$(contents).slice(active+1,select).hide(0);

				if(active<select){
					$(group).slice(active,select+1).css({position:"relative", zIndex:3 , top:menuHeight*active+"px"});
					$($(hide).children('.contents')).slideUp(100,fixMenu());
				}else if (active>select){
					$(target).next().hide(0);
					$(group).slice(select,active+1).css({position:"relative", zIndex:3 , top:menuHeight*select+"px"});
					$($(show).children('.contents')).slideDown(100,fixMenu());
				}
			}, 200);  
	
			$(menu).mouseout( 
				function() {
					clearTimeout(clicktimer);
					return;
				}
			);
		}
	);
	
	function fixMenu(){
		$(group).removeClass('choose active');
		$(show).addClass('active');


		var fixTimer = setTimeout(function() {
			changeMenuPosition();
		},100);
	}
	
	function changeMenuPosition() {	
		for (i=0 ; i<n ; i++){
			$(group).eq(i).css({position:"absolute" , top:menuHeight*i, zIndex:i+2});
	
			if ($(group).eq(i).is('.active')) {
				i++;
				for (; i<n ; i++){
					$(group).eq(i).css({position:"absolute" , top:menuHeight*i+maxHeight, zIndex:i+2});
				}
			}
		}	
	}

});

$(document).ready(function(){

	var switchArea = $('#switch01'); //この関数を適用するエレメント
	var switchObj = $(switchArea).find('.select'); // 選択オブジェクト
	var contentObj = $(switchArea).find('.contents'); // コンテンツオブジェクト

	$(contentObj).hide(0);
    var activeObj = $(switchArea).find(".active").show(0);
	
	var n = $(switchObj).length; // liの数を抽出

	$(switchObj).click(
		function (){
			var target = this;

			hide = $(switchObj).filter('.active');
			show = $(target);
				
			$(show).addClass('choose');
			
			var active = $('.select').index($(hide).get(0)); // hideのエレメントを取得　$(hide).get(0)　してindex番号を調査
			var select = $('.select').index(target);
			
			if (active==select) return;
			
			$(contentObj).eq(active).fadeOut(100); // active番目のcontentObjに対してfadeout処理
			
			var clicktimer = setTimeout(function() {
				$(contentObj).eq(select).fadeIn(200);
			},200);

			$(switchObj).removeClass('active choose');
			$(contentObj).removeClass('active choose');

			$(switchObj).eq(select).addClass('active');
			$(contentObj).eq(select).addClass('active');
		}
	);

});

$(document).ready(function(){
	
	$.get(
		'banner.xml',
		function(bannerXML) {
			var i, j, entry, url, title;
			var xml = $(bannerXML);
			var column = xml.children('data').children('column');
			
			for (i=0 ; i < column.length ; i++){
				var entries = column.eq(i).children('item');
				var n = entries.length;
				rnd = Math.floor(Math.random()*(n));
				var pageTitle = entries.eq(rnd).children('page').text();
				var imgURL = entries.eq(rnd).children('images').text();
				var linkURL = entries.eq(rnd).children('loc').text();
				html = '<a href="' + linkURL + '"><img src="http://www.moto.co.jp' + imgURL + '" alt="' + pageTitle + '" width="198" height="135" border="0"></a>';
				$('#banner > .badge').eq(i).append(html);
			}
			
			
//			html += '<li><a href="' + url + '">' + title + '</a></li>';
//			html += '</ul>';
//			$('#entries_list').html(html);

		},
		'xml'
	);
});
