function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

$(function(){
	/**
	 *	滑鼠動作
	 */
	$(".JsMouseAction").mouseover(function(){
		$(this).removeClass('mouseout').addClass('mouseover');
	});
	$(".JsMouseAction").mouseout(function(){
		$(this).removeClass('mouseover').addClass('mouseout');
	});
	
	/**
	 *	主功能標示
	 */
    var pathname=location.pathname.split('/');
	pathname=pathname[pathname.length-1];
	if (pathname=='') {
		pathname='index.html';
	}
	$("#menus li").each(function(){
		var _href=$(this).find("a").attr("href").split('?');
		_href=_href[0];
		if (_href==pathname) {
			$(this).find("a").addClass("liched");
		}
		$(this).find("li").each(function(){
			var _href=$(this).find("a").attr("href").split('?');
			_href=_href[0];
			if (_href==pathname) {
				$(this).parent().parent().find("a").addClass("liched");
			}
		});
	});

	/**
	 *	自動把字串多出來的字元變成... 用法:class="truncate" length="10"
	 */
	$(".truncate").each(function(){
		var _len=$(this).attr("length");
		var _txt=$(this).text();
		var _txt2=_txt.substring(0,_len);
		if (_txt!=_txt2)
			$(this).text(_txt2+'...');
	});
	
	/**
	 *	點圖放大 用法: 在外層加上 class="chpic" param="target:'product_picture'"
	 */
	$(".chpic a").click(function(){
		var param;
		eval("param={"+$(".chpic").attr("param")+'}');
		var src=$(this).attr("href");
		$("#"+param.target).attr({src:src});
		return false;
	});
	
	$('.f,.navigation img').ifixpng(); 
	
	if ($("a[rel='example4']").size()>0){
		$("a[rel='example1']").colorbox();
		$("a[rel='example2']").colorbox({transition:"fade"});
		$("a[rel='example3']").colorbox({transition:"none", width:"75%", height:"75%"});
		$("a[rel='example4']").colorbox({slideshow:true});
		$(".example5").colorbox();
		$(".example6").colorbox({iframe:true, innerWidth:425, innerHeight:344});
		$(".example7").colorbox({width:"80%", height:"80%", iframe:true});
		$(".example8").colorbox({width:"50%", inline:true, href:"#inline_example1"});
		$(".example9").colorbox({
			onOpen:function(){ alert('onOpen: colorbox is about to open'); },
			onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
			onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
			onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
			onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
		});
		
		//Example of preserving a JavaScript event for inline calls.
		$("#click").click(function(){ 
			$('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
			return false;
		});
	}
	
	$('.slideshow').cycle({
		fx: 'fade'
	});
	
	$('ul.sf-menu').superfish();

	if ($("#country").size()>0) {
		var v=$("#country").attr('valc').split(';');
		
		for(var k in v){
			
			$("#country").append("<option value='"+v[k]+"'>"+v[k]+"</option>");
		}
	}
});

$(window).load(function(){
	if ($.browser.msie) {
		document.execCommand("BackgroundImageCache",false,true);
	}
	
	/**
	 *	自動縮小圖片到一定尺寸內 用法: class="AutoImgSize" autosize="width:80,height:50"
	 */
	
	$(".AutoImgSize,.post_content img").each(function(){
		var _width=$(this).css("width").replace("px","");
		var _height=$(this).css("height").replace("px","");
		var autosize;
		if ($(this).attr("autosize"))
			eval("autosize={"+$(this).attr("autosize")+'}');
		else
			eval("autosize={"+$(".post_content").attr("autosize")+'}');
		var x = autosize.width/_width*100;
		var y = autosize.height/_height*100;
		if (y>x) y=x;
		if (x>100 && y>100) y=100;
		$(this).css("width",Math.floor(_width*y/100));
		$(this).css("height",Math.floor(_height*y/100));
	});
	
	if ($(".search_f").size()>0) {
		var shl=$(".inside_box:eq(1)").height();
		var shr=$(".search_f").height();
		if (shl>shr) {
			$(".search_f").css("height",shl-50)
		}
	}
	
});
