// JavaScript Document
//var open_desc;
/*
function hoverEntryDescription(elm){
	//alert("sohw");
	
	if(open_desc){
		open_desc.hide('fast');
	}
	
	open_desc = typeof(elm)=="string" ? $('.'+elm).children('.desc') : $(this).children('.desc');
	//alert($(this).offset().top + 280 > $('body').height + " / "+)
	var ot =  $(this).offset().top;
	var ol =  $(this).offset().left;
	//alert($('html,body').width())
	open_desc.css({
		top: ot,
		left: ol
	});
	
	
	$.ajax({
		url: $(this).children('a').attr('href'),
		type: 'GET',
		dataType: "html",
		success:function(html){
			var htdoc = $(html);
			open_desc.html(htdoc);
			
			open_desc.animate({
				top: open_desc.css('top'),
				left: open_desc.css('left'),
				opacity: "show"
			});
		},
		error:function(err){
			open_desc.append("読み込めませんでした。");
		}
	})
	
	return false;
}
*/
function getTooltipPos(elm){
	var ot =  elm.offset().top + 280 > $('html,body').height() ? $('html,body').height() - (elm.offset().top + 280) : -15;
	var ol =  elm.offset().left + 450 > $('html,body').width() ? - 450 : 60;
	
	return [ot, ol];
}

$(function(){
	
	//左メニュー
	
	var path = location.href.replace(/^http\:\/\/[a-z0-9\.\-]+\//, '').split('/');
	var file = path[path.length - 1].replace(/\.html$/, '');
	if(file == "index"){
		file = path[path.length - 2];
	}
	
	$('#menu_'+file).addClass("selected");
	
	/*
	$('body').click(function(){
		open_desc.hide('fast');
	})
	*/
	//var wrp = $('body').append($('<div id="modal_wrp">'));
	/*
	$('li.item').hover(hoverEntryDescription,function(){
		$(this).children('.desc').hide('fast');
	});
	*/
	//各詳細ページ
	
	$('li.item').tooltip({ 
	    delay: 0, 
	    showURL: false, 
	    bodyHandler: function(e) {
	    	
	      var open_desc = $('<div id="tooltip_'+Math.floor(Math.random()*100)+'">');
	      var elm = $(this);
	    	$.ajax({
					url: elm.find('a > img').attr('longdesc'),
					type: 'GET',
					dataType: "html",
					success:function(html){
						var htdoc = $(html);
						open_desc.html(htdoc);
						
					},
					error:function(err){
						open_desc.append("読み込めませんでした。");
					}
				});
				
	      return open_desc; 
	    }
	});
	
	$('li.item > a[href=="#"]').click(function(){
		return false;
	});
	
	//indexページ
	
	$('.index').find('.brand > h3').hover(
		function(){
			$(this).find('.main_image').css('opacity', 0.5);
		},
		function(){
			$(this).find('.main_image').css('opacity', 1);
		}
	);
	
	$('.brand:odd').each(function(){
		
		var th = $(this).children('p').height();
		var ph = $(this).prev().children('p').height();
		
		if(th > ph){
			$(this).prev().children('p').height(th);
			$(this).prev().height($(this).height());
		}else{
			$(this).children('p').height(ph);
			$(this).height($(this).prev().height());
		}
		
	});
	
	$('.brand:even').each(function(){
		$(this).css({clear:'left'})
	});
	
	
	$('.first ul').show();
	$('.first h3 span').addClass('open')
	
	//ブランド一覧
	$('#main_visual > a > img').hover(
		function(){
			$(this).css('opacity', 0.5);
		},
		function(){
			$(this).css('opacity', 1);
		}
	)
	
	$('#main_visual > a > img').click(function(){
		var href = $(this).parent().attr('href');
		$(href).next().next().show('fast');
	});
	
	
	$('#entry_wrp > .series > h3').click(function(){
		//$.dump(this)
		if($(this).next().css('display') == 'none'){
			$(this).next().show('fast');
			$(this).children().addClass('open');
		}else{
			$(this).next().hide('fast');
			$(this).children().removeClass('open');
			$('#tooltip > .body').children().remove();
		}
	});
	
	$('#tooltip > h3').remove();
	
	var hash = location.hash;
	//alert(hash)
	if($(hash).length){
		$(hash).parent().show();
		try {
			$(hash).mouseover();
		}catch(e){
		
		}
		$('#tooltip').css({
			top: $(hash).offset().top + 280 > $('body').height() ? $(hash).offset().top - 280 + 10 : $(hash).offset().top + 50,
			left: $(hash).offset().left + 450 > $('body').width() ? $(hash).offset().left - 450 + 10 : $(hash).offset().left + 50
		})
	}
})
