/* last edit:2010.10.28 v.0323d*/

/*in-field labels -------------*/
function OnEnter( field ) { if( field.value == 'Our products' ) { field.value = "";field.style.color = "#333";} }
function OnExit( field ) { if( field.value == "" ) { field.value = field.defaultValue;field.style.color = "#999"; } }

/*四捨五入-------------*/
function round(value,num){
	return parseInt(value * Math.pow(10,num) + 0.5) / Math.pow(10,num);
}

/*imageAutoSize-------------*/
jQuery.fn.imageAutoSize = function(width,height){
	
	
    $(this).each(function(){
    	
    	$(this).css('width','auto'); 
    	$(this).css('height','auto'); 
    	$(this).css('max-width',width + 'px'); 
    	$(this).css('max-height',height + 'px');
    	
        var image = $(this);
        
        var isRun="true";
        
       
        if(isRun=="true"){
        	
        	//index_aboutus新式寫法<img alumb="true">
        	 $(this).attr("sv", "v.0812a");
        	 
        	 if (this.complete){ //if already loaded  
        		       
        		 $(this).attr("imageStatus", "loaded");
        		//移除目前設定的影像長寬
         		$(this).removeAttr('width');
         		$(this).removeAttr('height');
         		
         		//取得影像實際的長寬
        		var imgW = $(this).width();
        		var imgH = $(this).height();

        		//計算縮放比例
        		var w=round(width/imgW,2);
        		var h=round(height/imgH,2);
        		
        		var pre=1;
        		if(w>h){
        			pre=h;
        		}else{
        			pre=w;
        		}
        		//有可能小圖 if(pre>1){pre=1;}	

        		//設定目前的縮放比例
        		//舊版$(this).width(round(imgW*pre,0));
        		//舊版$(this).height(round(imgH*pre,0));			
        		//舊版$(this).attr("width", round(imgW*pre,0));       $(this).attr("height", round(imgH*pre,0));
        		
        		
        		//舊版$(this).attr("scale", pre);        		        $(this).attr("scaleW", w);
        		//舊版$(this).attr("scaleH", h);
        		
        		
        		var allTheSame = true;
        		if( imgW != width ){
        		    allTheSame = false;
        		}
        		if( allTheSame && (imgH != height) ){
        		    allTheSame = false;
        		}
        		if(allTheSame == false){
        			var xwidth=width;
        			var xheight=height;
        			$(this).attr("imageStatus", "ImageResize0");
        			$(this).attr("width", xwidth);  //先設預設寬高     
                	$(this).attr("height", xheight);
                	
                	$(this).attr("newWidth", width);        		$(this).attr("newHeight", height);
            		$(this).attr("oldWidth", imgW);        		    $(this).attr("oldHeight", imgH);
                	
        			$(this).aeImageResize({ height: xheight, width: xwidth });  //進行縮放 
        		}
             }else{     
            	 var xwidth=width;
            	 var xheight=height;
            	 $(this).attr("width", xwidth);  //先設預設寬高     
            	 $(this).attr("height", xheight);
            	 $(this).aeImageResize({ height: xheight, width: xwidth });  //進行縮放 
            	 
            	 var imgW = $(this).width();
         		 var imgH = $(this).height();         		 
            	 $(this).attr("imageStatus", "ImageResize1");
            	 
         		
            	 //$(this).attr("oldWidth", imgW);        		    $(this).attr("oldHeight", imgH);
              }

        	
		}else{
			//舊式寫法
			if(image.width()>width){
	            image.height(width/image.width()*image.height());
	            image.width(width);
	         }
	        if(image.height()>height){
	            image.width(height/image.height()*image.width());
	            image.height(height);
	        }
		}

    });
} 

$(function(){ $('.logo_thirdparty img').imageAutoSize(500,40);});
$(function(){ $('.side_company_logo').imageAutoSize(190,100);});

$(function(){ $('.big_img').imageAutoSize(360,360);});
$(function(){ $('.mid_img').imageAutoSize(150,150);});
$(function(){ $('.mid_img_200').imageAutoSize(200,200);});
$(function(){ $('.small_img').imageAutoSize(100,100);});
$(function(){ $('.p_related_slide img').imageAutoSize(90,90);});


/*Back to top link-------------------*/
 $(document).ready(function(){
	$('a[href=#top]').click(function(){
		$('html, body').animate({ scrollTop:0 }, '1000');
		 return false;
	});
 });
 
/*sidebar expand & contract -------------------*/

/*$(function(){  
    var $btn=$('ul.sub >li:gt(14)');
    $btn.hide();  
    $('.side_menu .btn_expand').click(function(){
	 if($btn.is(':visible')){  
	      $btn.hide();  
	      $('.side_menu .btn_expand').html('<span class="ico ico_ar_down"></span>All Categories');  
	  }else{  
	      $btn.show();  
	      $('.side_menu .btn_expand').html('<span class="ico ico_ar_up"></span>Contract');  
	  }   
      }) 
 })*/ 
/* expand & contract-------------------*/ 
$(function(){  
    var $cate=$('.category_list .over_hide >div:gt(5)');
    $cate.hide();  
    $('.category_list .btn_expand').click(function(){
	 if($cate.is(':visible')){		 
		  var allCategories = '<span class="ico ico_ar_down"></span>'+$('#showeAllCategories').html();
	      $cate.hide();  
	      $('.category_list .btn_expand').html(allCategories);  
	  }else{
		  var contract = '<span class="ico ico_ar_up"></span>'+$('#showeContract').html();
	      $cate.show();  
	      $('.category_list .btn_expand').html(contract);  
	  }   
      }) 
 })

/* image bubble --------------*/
*$(document).ready(function() {
	$('a[rel=tooltip]').mouseover(function(e) {
		var tip = $(this).attr('title');
		$(this).attr('title','');
		$('body').append('<div id="tooltip"><div class="tipBody">'+ tip + '</div></div>');		
		$('#tooltip').fadeIn('fast');
	}).mousemove(function(e) {
		$('#tooltip').css('top', e.pageY-110);
		$('#tooltip').css('left', e.pageX-90);
	}).mouseout(function() {
		$(this).attr('title',$('.tipBody').html());
		$('div#tooltip').remove();
	});
});

/* textarea.resizable */
(function($){var textarea,staticOffset;var iLastMousePos=0;var iMin=32;var grip;$.fn.TextAreaResizer=function(){return this.each(function(){textarea=$(this).addClass('processed'),staticOffset=null;$(this).wrap('<div class="resizable-textarea"><span></span></div>').parent().append($('<div class="grippie"></div>').bind("mousedown",{el:this},startDrag));var grippie=$('div.grippie',$(this).parent())[0];grippie.style.marginRight=(grippie.offsetWidth-$(this)[0].offsetWidth)+'px'})};function startDrag(e){textarea=$(e.data.el);textarea.blur();iLastMousePos=mousePosition(e).y;staticOffset=textarea.height()-iLastMousePos;textarea.css('opacity',0.25);$(document).mousemove(performDrag).mouseup(endDrag);return false}function performDrag(e){var iThisMousePos=mousePosition(e).y;var iMousePos=staticOffset+iThisMousePos;if(iLastMousePos>=(iThisMousePos)){iMousePos-=5}iLastMousePos=iThisMousePos;iMousePos=Math.max(iMin,iMousePos);textarea.height(iMousePos+'px');if(iMousePos<iMin){endDrag(e)}return false}function endDrag(e){$(document).unbind('mousemove',performDrag).unbind('mouseup',endDrag);textarea.css('opacity',1);textarea.focus();textarea=null;staticOffset=null;iLastMousePos=0}function mousePosition(e){return{x:e.clientX+document.documentElement.scrollLeft,y:e.clientY+document.documentElement.scrollTop}}})(jQuery);

