jQuery.noConflict()(function($){

var pic_dir='/common_2009/pic/';
var preLoadImg = new Object();

// メインメニュー
	$('body.home #mainhomeb').attr('src',pic_dir+'home-b_f3.jpg');
	$('body.news #mainnewsb').attr('src',pic_dir+'news-b_f3.jpg');
	$('body.newcar #mainnewcarb').attr('src',pic_dir+'newcar-b_f3.jpg');
	$('body.usedcar #mainusedcarb').attr('src',pic_dir+'usedcar-b_f3.jpg');
	$('body.service #mainserviceb').attr('src',pic_dir+'service-b_f3.jpg');
	$('body.showroom #mainshowroomb').attr('src',pic_dir+'showroom-b_f3.jpg');
	$('body.inquiry #maininquiryb').attr('src',pic_dir+'inquiry-b_f3.jpg');
	$('#mainmenu > ul > li > ul').css('visibility','hidden').hide();
	$("#mainmenu > ul > li").each(function(){
		var swapimg = $(this).children('a').find('img');
		var imgSrc = swapimg.attr('src');
		var separater = imgSrc.lastIndexOf('.');
		var separaterprev = separater;
		var chkstr = imgSrc.substr(separater - 3, 3);
		if(chkstr == '_f3') {separaterprev = separaterprev - 3}
		var onSrc = imgSrc.substr(0, separaterprev) + '_f2' + imgSrc.substr(separater, 4);
		preLoadImg[imgSrc] = new Image();
		preLoadImg[imgSrc].src = onSrc;
		$(this).hover(
			function() {
				$(this).children('ul').css('visibility','visible').slideDown('250');
				swapimg.css({opacity: "0"}).attr('src',onSrc).animate({ 
    				opacity: 1}, 250 );
			},
			function() {
				$(this).children('ul').css('visibility','hidden').hide();
				swapimg.attr('src',imgSrc);
			}
		);
	});
	$("#main-news").load("/news/common/new.inc");
	
// サイドメニュー
	$('.sidemenu.closed > ul > li > ul').css('display','none');							
	$('.sidemenu.closed > ul > li:has(ul)').append('<div class="open" title="クリックでオープン">&nbsp;</div><div class="close" title="クリックでクローズ">&nbsp;</div>');
	$('.sidemenu.closed > ul > li div.close').css('display','none');							
	$('.sidemenu.closed > ul > li > div.open').click(
		function(){
			$(this).parent().children('ul').slideDown(250).end()
			.children('div.open').css('display','none').end()
			.children('div.close').css('display','block');							
		}
	);
	$('.sidemenu.closed > ul > li > div.close').click(
		function(){
			$(this).parent().children('ul').slideUp(250).end()
			.children('div.close').css('display','none').end()
			.children('div.open').css('display','block');
		}
	);

// 新車
	$('#newcarlist').after('<p id="message"></p>');
	$('.newcar #newcarselect select').find('option:first').attr('selected','selected').end()
	.find('option:gt(1)').attr('selected','');
	$('.newcar #newcarselect select').change(
		function(){
			var dispflug = 0;
			var selecttype = $('.newcar select[name^="type"] option:selected').attr('value');
			var selectusage = $('.newcar select[name^="usage"] option:selected').attr('value');
			var selecteco = $('.newcar select[name^="eco"] option:selected').attr('value');
			var selectprice = $('.newcar select[name^="price"] option:selected').attr('value');
			$('#newcarlist > li').each(
			function(){
				$(this).css('display','none');
				var delay;
				var hit = 1;
				var classstr = $(this).attr('class');
				if(classstr.lastIndexOf(selecttype) < 0 && selecttype!='all') {
					hit=0;
				}
				if(classstr.lastIndexOf(selectusage) < 0 && selectusage!='all') {
					hit=0;
				}
				if(classstr.lastIndexOf(selecteco) < 0 && selecteco!='all') {
					hit=0;
				}
				if(classstr.lastIndexOf(selectprice) < 0 && selectprice!='all') {
					hit=0;
				}
				if(hit) {
					$(this).fadeIn(750);
					dispflug = 1;
				}
			});
			if(!dispflug) {
				$('#message').text('当てはまる車種はありませんでした。');
			} else {
				$('#message').text('');
			}
		});
	$('#newcarlist > li > a').each(function(){
		var self = $(this);
		var alttext = self.parent().find('img').attr('alt')
		var carlead = self.parent().find('span.lead').html();
		var carspec = self.parent().find('span.spec').html();
		self.hover(function(e){
			$(this).parent().find('img').attr('alt','');
			$("body").append('<div id="tooltip"><div class="tooltiptop">&nbsp;</div><div class="tooltipbody"><strong>'+carlead+'</strong><br />'+carspec+'</div><div class="tooltipbottom">&nbsp;</div></div>');
        	$("#tooltip").css({
				top: e.pageY+16,
				left: e.pageX+4
				}).fadeIn(250);
			},function(){
				$(this).parent().find('img').attr('alt',alttext);
				$("#tooltip").hide().remove();
		});
    	self.mousemove(function(e){
			$("#tooltip").css({
				top: e.pageY+16,
				left: e.pageX+4
			});
		});
	});


// 店舗一覧
	$('#mapbox > ul > li').css('cursor','pointer').each(
		function(){
			var imgSrc = $(this).children('img.icon');
			preLoadImg[imgSrc] = new Image();
			preLoadImg[imgSrc].src = pic_dir+'showroom-icon.png';
			$(this).hover(
			function(){
				$(this).css('background','#e5f1ff');
				$(this).find('img.icon').attr('src',pic_dir+'showroom-icon.png');
			},
			function(){
				$(this).css('background','#fff');
				$(this).find('img.icon').attr('src',pic_dir+'spacer.gif');
			}).click(
			function(){
				var showroomlink=$(this).find('a').attr('href');
				location.href=showroomlink;
			});
		});
	$('#mapbox > ul > li > a > img.icon').each(function(){
		var self = $(this);
		var shopname = self.parent().find('span.shopname').text();
		var address = self.parent().parent().find('span.address').text();
		var tel = self.parent().parent().find('span.tel').text();
		self.hover(function(e){
			$("body").append('<div id="tooltip"><div class="tooltiptop">&nbsp;</div><div class="tooltipbody"><strong>'+shopname+'</strong><br />'+address+'<br />'+tel+'</div><div class="tooltipbottom">&nbsp;</div></div>');
        	$("#tooltip").css({
				top: e.pageY+16,
				left: e.pageX+4
				}).fadeIn(250);
			},function(){
				$("#tooltip").hide().remove();
		});
    	self.mousemove(function(e){
			$("#tooltip").css({
				top: e.pageY+16,
				left: e.pageX+4
			});
		});
	});

// トップページ ニュース
	var lead_height = $('#newsbox').height() - 130;
	$('#newsbox > ul > li span.lead').height(lead_height);
	$('#newsbox > ul > li').find('span.lead').css({'display':'none'}).end()
	.find('img').css({'display':'none'});
	$('#newsbox > ul > li:first').find('span.lead').css({'display':'block'}).end()
	.find('img').css({'display':'inline'});
	$('#newsbox > ul > li').css('cursor','pointer').bind('mouseenter',
		function(){
			$('#newsbox > ul > li').removeClass('clicked');
			$(this).addClass('clicked').children('span.lead').fadeIn(500).end()
			.find('img').fadeIn(500);
			$('#newsbox > ul > li:not([class*="clicked"])').find('span.lead').hide().end()
			.find('img').hide();
		});
	$('#newsbox > ul > li').click(
		function(){
			var newslink=$(this).children('a').attr('href');
			var openwindow=$(this).children('a').attr('target');
			if(target == '_blank') {
				window.open(newslink, null);
			} else {
				location.href=newslink;
			}
		});

// トップページ 新年イメージ
	var body_class = $('body').attr('class');
	var getdate = new Date();
    var month = getdate.getMonth() + 1;
    var day = getdate.getMonth();
	if(body_class == 'toppage home' && month == 1 && day <= 11){ // トップページの時のみ実行
		var newyeartimer = setTimeout(
		function(){
			startnewyear();
		}, 5000);
		function startnewyear(){
			$('#newyearimage').fadeOut(1000);
		}
		$('#newyearb').css('cursor','pointer').css('opacity','0.75').hover(
		function(){
			$(this).css('opacity','1')
		},function(){
			$(this).css('opacity','0.75')
		}).click(
		function(){
			$('#newyearimage').fadeIn(1000);
			$('#newyearcloseb').fadeIn(1000);
		});
		$('#newyearcloseb').css('cursor','pointer').css('opacity','0.75').hover(
		function(){
			$(this).css('opacity','1')
		},function(){
			$(this).css('opacity','0.75')
		}).click(
		function(){
			$('#newyearimage').fadeOut(1000);
			$(this).fadeOut(1000);
		});
	}
// トップページ 店舗一覧
	$('#showroommapbox').css({'display':'none'});
	$('#showroominfobox span.address').css({'display':'none'});
	$('#showroominfobox').hover(
		function(){
			$('#showroommapbox').slideDown(250);
		},function(){
			$('#showroommapbox').slideUp(250);
		});
	$('#showroominfobox div.sidemenu > ul > li').each(function(){
		var imgSrc = $(this).children('img');
		preLoadImg[imgSrc] = new Image();
		preLoadImg[imgSrc].src = pic_dir+'showroom-icon.png';
		 var classname = $(this).attr('class');
		 $(this).css('cursor','pointer').hover(
			function() {
				$(this).css({'background-color':'#E7FBFF'})
				.children('span.address').css({'display':'block'});
				$('#showroommapbox > a > img.'+classname).attr('src',pic_dir+'showroom-icon.png');
			},function() {
				$(this).css({'background-color':'transparent'})
				.children('span.address').css({'display':'none'});
				$('#showroommapbox > a > img.'+classname).attr('src',pic_dir+'spacer.gif');
			}).click(
			function(){
				var showroomlink=$(this).children('a').attr('href');
				location.href=showroomlink;
			});
		});
	$('#showroommapbox > a > img').each(function(){
		var classname = $(this).attr('class');
		var imgSrc = $(this);
		preLoadImg[imgSrc] = new Image();
		preLoadImg[imgSrc].src = pic_dir+'showroom-icon.png';
		$(this).hover(
		function(){
			$(this).attr('src',pic_dir+'showroom-icon.png');
			$('#showroominfobox div.sidemenu > ul > li.'+classname).css({'background-color':'#E7FBFF'})
			.children('span.address').css({'display':'block'});
		},function(){
			$(this).attr('src',pic_dir+'spacer.gif');
			$('#showroominfobox div.sidemenu > ul > li.'+classname).css({'background-color':'transparent'})
			.children('span.address').css({'display':'none'});
			});
		});
	
// フッター
	$("#footer-news").load("/news/common/new.inc");

// ロールオーバー
	var preLoadImg = new Object();
	$("a.rollover img").each(function(){
		var imgSrc = this.src;
		var sep = imgSrc.lastIndexOf('.');
		var onSrc = imgSrc.substr(0, sep) + '_f2' + imgSrc.substr(sep, 4);
		preLoadImg[imgSrc] = new Image();
		preLoadImg[imgSrc].src = onSrc;
		$(this).parent("a.rollover").hover(
			function() {
				$(this).children("img").css({opacity: "0"}).attr('src',onSrc).animate({ 
    				opacity: 1}, 250 );
			},
			function() {
				$(this).children("img").attr('src',imgSrc);
			}
		);
	});

});

function openSidemenu(selector){
	jQuery.noConflict()(function($){
		$(selector).children('ul').slideDown(250).end()
		.children('div.open').css('display','none').end()
		.children('div.close').css('display','block');
  		return $(selector);
	});
}


window.addEvent('domready', function(){
	//call multiBox
	var initMultiBox = new multiBox({
		mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
		container: $(document.body),//where to inject multiBox
		descClassName: 'mbDesc',//the class name of the description divs
		path: './Files/',//path to mp3 and flv players
		useOverlay: true,//use a semi-transparent background. default: false;
		maxSize: {w:900, h:1000},//max dimensions (width,height) - set to null to disable resizing
		addDownload: false,//do you want the files to be downloadable?
		pathToDownloadScript: './Scripts/ForceDownload.asp',//if above is true, specify path to download script (classicASP and ASP.NET versions included)
		addRollover: false,//add rollover fade to each multibox link
		addOverlayIcon: true,//adds overlay icons to images within multibox links
		addChain: false,//cycle through all images fading them out then in
		recalcTop: true,//subtract the height of controls panel from top position
		addTips: true//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
	});
});

function ImageOpen(theURL) {
	ImageOpenWindow(theURL);
}

function ImageOpenWindow(theURL,winName,features) {
if(!winName){winName = "photo";}
if(!features){features = "scrollbars=1,resizable=1,width=720,height=600";}

      imgWindow = window.open("",winName,features);
      imgWindow.document.close();
      imgWindow.document.open;
      imgWindow.document.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n");
      imgWindow.document.write("       \"http://www.w3.org/TR/html4/loose.dtd\">\n");
      imgWindow.document.write("<html lang=\"ja\"\n");
      imgWindow.document.write("<head>\n");
      imgWindow.document.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\">\n");
      imgWindow.document.write("<meta http-equiv=\"Content-Language\" Content=\"ja\">\n");
      imgWindow.document.write("<title>Photo</title>\n");
      imgWindow.document.write("<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\">\n");
      imgWindow.document.write("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n");
      imgWindow.document.write("<link rel=\"stylesheet\" media=\"screen,print\" href=\"/common/dummy.css\" type=\"text/css\">");
      imgWindow.document.write("<script type=\"text/javascript\">");
      imgWindow.document.write("<!--\n");
      imgWindow.document.write("self.window.focus()\;\n");
      imgWindow.document.write("//-->\n");
	  imgWindow.document.write("</script>\n");
      imgWindow.document.write("</head>\n");
      imgWindow.document.write("<body class=\"smallwindow\">\n");
      imgWindow.document.write("<div id=\"photo\">\n");
      imgWindow.document.write("<img src=\"" + theURL + "\"></div>\n");
      imgWindow.document.write("<div id=\"closewindow\"><a href=\"javascript:window.close();\">Close</a></div>\n");
      imgWindow.document.write("</body>\n");
      imgWindow.document.write("</html>");
      imgWindow.document.close();
}
