$(function(){

// ===Mainmenu===
	$('#sitemenu li a').each(function(){
		var menuid = 'menu'+($('#sitemenu li a').index(this)+1);
		var alt = $(this).text();
		$(this).empty();
		if($(this).attr('class')=='on'){
			$('<img src="/themes/main/images/sitemenu/'+menuid+'_ro.gif" alt="'+alt+'" />').prependTo(this);
		}else{
			$('<img src="/themes/main/images/sitemenu/'+menuid+'_df.gif" alt="'+alt+'" />').prependTo(this);
		}
		});

	$('#sitemenu li a').hover(function(){
		var imgsrc = $(this).find('img').attr('src').replace(/\_df.(gif|png|jpg|jpeg)$/i,'_ro.$1');
		$(this).find('img').attr('src',imgsrc);
		},function(){
		if($(this).attr('class')=='on'){
		}else{
		var imgsrc = $(this).find('img').attr('src').replace(/_ro\.(gif|png|jpg|jpeg)$/i,'_df.$1');
		$(this).find('img').attr('src',imgsrc);
		}
	});

//=== rolloverimage making ===
	$('a.bt').each(function(){
		$(this).removeClass('bt');
		$(this).wrap('<span class="bt">')
	}
	);
	$('.bt a').hover(function(){
		var imgsrc = $(this).find('img').attr('src').replace(/\.(gif|png|jpg|jpeg)$/i,'_ro.$1');
		$(this).find('img').attr('src',imgsrc);
		},function(){
		var imgparent = $(this).find('img').parent();
		var imgsrc = $(this).find('img').attr('src').replace(/_ro\.(gif|png|jpg|jpeg)$/i,'.$1');
		$(this).find('img').attr('src',imgsrc);
	});

//=== imgcaption & img lightbox ===
// ===captionblock===
	$('img.fcr').each(function(){
		var captxt = $(this).attr('alt').replace('[/url]','</a>').replace(']','">').replace('[url=','<a class="noicon" href="');
		var capwidth = $(this).attr('width');
		capwidth=capwidth+10;
		$(this).wrap('<span style="width:'+capwidth+'px;font-size:90%;float:right;margin:0 0 10px 10px;text-align:center;dispay:block;">');
		$(this).attr('alt','').after('<br />'+captxt);
	});

	$('img.fcl').each(function(){
		var captxt = $(this).attr('alt').replace('[/url]','</a>').replace(']','">').replace('[url=','<a class="noicon" href="');
		var capwidth = $(this).attr('width');
		capwidth=capwidth+10;
		$(this).wrap('<span style="width:'+capwidth+'px;font-size:90%;float:left;margin:0 10px 10px 0;text-align:center;dispay:block;">');
		$(this).attr('alt','').after('<br />'+captxt);
	});


	$('img.fcc').each(function(){
		var captxt = $(this).attr('alt').replace('[/url]','</a>').replace(']','">').replace('[url=','<a class="noicon" href="');
		$(this).wrap('<span style="text-align:center;font-size:90%;margin:0 10px 10px 0;clear:both;dispay:block;">');
		$(this).attr('alt','').after('<br />'+captxt);
	});

//=== lightbox block ===
	$('.lightbox a:has(img)').each(function(){
		var altx = $(this).find('img').attr('alt');
		$(this).attr('title',cap);
		$(this).attr('rel','lightbox[image1]');
		$(this).append('<br />'+cap);
		var altx = '画像拡大表示' + altx;
		$(this).find('img').attr('alt',altx);
		$(this).lightBox();
	});

//=== lightbox block ===
	$('.lb a:has(img)').each(function(){
		var altx = $(this).find('img').attr('alt');
		$(this).attr('title',altx);
		$(this).attr('rel','lightbox[image1]');
		var altx = '画像拡大表示' + altx;
		$(this).find('img').attr('alt',altx);
		$(this).lightBox();
	});

	//==floatimage margin for FCKeditor==

	$('#CenterWrapper img').each(function(){
	if($(this).attr('align')=='left'){
		$(this).css({'margin':'0 10px 10px 0','float':'left'});
		$(this).removeAttr('align');
	}else if($(this).attr('align')=='right'){
		$(this).css({'margin':'0 0 10px 10px','float':'right'});
		$(this).removeAttr('align');
	}else if($(this).attr('align')=='center'){
		$(this).css({'margin':'0 10px 10px 10px'});
	}
	});


	//==== IE6 png ====
	//http://docs.jquery.com/Tutorials:PNG_Opacity_Fix_for_IE6
	var blank = new Image();
	blank.src = '/images/blank.gif';

	$(document).ready(function() {
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		// get all pngs on page
		// $('img[src$=.png]').each(function() {
		// get all pngs which has a class 'fixpng'.
		// because this function doen't work for a few small size png.
		$('img.fixpng').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
	});

	function fixPng(png) {
		var src = png.src;
		// set width and height
		if (!png.style.width) { png.style.width = $(png).width(); }
		if (!png.style.height) { png.style.height = $(png).height(); }
		// replace by blank image
		png.onload = function() { };
		png.src = blank.src;
		// set filter (display original image)
		png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}



});

